Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8.1k
DOMXPath::quote(string $str): string#13456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
1730af1bedc0d0779bfe19925f4a74404a6c17264eb85a7687ad4c91e3fb67862a6eed307a9be641f01f1d935fde9d4990f90a244a20e67bce9b737d9309797c478a1File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| --TEST-- | ||
| Test DOMXPath::quote with various inputs | ||
| --EXTENSIONS-- | ||
| dom | ||
| --SKIPIF-- | ||
| <?php if (!class_exists('DOMXPath')) die('skip DOMXPath not available.'); ?> | ||
| --FILE-- | ||
divinity76 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| <?php | ||
| $dom = new DOMDocument(); | ||
| $xpath = new DOMXPath($dom); | ||
| /** | ||
| * Quote a string for use in an XPath expression. | ||
| * | ||
| * Example: $xp->query("//span[contains(text()," . $xp->quote($string) . ")]") | ||
| * | ||
| * @param string $string string to quote. | ||
| * @return string quoted string. | ||
| */ | ||
| function UserlandDOMXPathQuote(string $string): string | ||
divinity76 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it makes sense trying to add this to the fuzzer to see if it finds differences in behaviour? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't know what fuzzer you're talking about, but sounds good because if there is any difference, it's probably a bug in the C version 👍 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could fuzz this, but it will be a bit cumbersome to write a specific driver for this. We could ask ourselves whether we want to do it generalised for all these kinds of quoting methods. | ||
| { | ||
| if (false === \strpos($string, '\'')) { | ||
| return '\'' . $string . '\''; | ||
| } | ||
| if (false === \strpos($string, '"')) { | ||
| return '"' . $string . '"'; | ||
| } | ||
| // if the string contains both single and double quotes, construct an | ||
| // expression that concatenates all non-double-quote substrings with | ||
| // the quotes, e.g.: | ||
| // 'foo'"bar => concat("'foo'", '"bar") | ||
| $sb = []; | ||
| while ($string !== '') { | ||
| $bytesUntilSingleQuote = \strcspn($string, '\''); | ||
| $bytesUntilDoubleQuote = \strcspn($string, '"'); | ||
| $quoteMethod = ($bytesUntilSingleQuote > $bytesUntilDoubleQuote) ? "'" : '"'; | ||
| $bytesUntilQuote = \max($bytesUntilSingleQuote, $bytesUntilDoubleQuote); | ||
| $sb[] = $quoteMethod . \substr($string, 0, $bytesUntilQuote) . $quoteMethod; | ||
| $string = \substr($string, $bytesUntilQuote); | ||
| } | ||
| $sb = \implode(',', $sb); | ||
| return 'concat(' . $sb . ')'; | ||
| } | ||
| $tests = [ | ||
| '' => "''", // empty string | ||
| 'foo' => "'foo'", // no quotes | ||
| '"foo' => '\'"foo\'', // double quotes only | ||
| '\'foo' => '"\'foo"', // single quotes only | ||
| '\'foo"bar' => 'concat("\'foo",\'"bar\')', // both; double quotes in mid-string | ||
| '\'foo"bar"baz' => 'concat("\'foo",\'"bar"baz\')', // multiple double quotes in mid-string | ||
| '\'foo"' => 'concat("\'foo",\'"\')', // string ends with double quotes | ||
| '\'foo""' => 'concat("\'foo",\'""\')', // string ends with run of double quotes | ||
| '"\'foo' => 'concat(\'"\',"\'foo")', // string begins with double quotes | ||
| '""\'foo' => 'concat(\'""\',"\'foo")', // string begins with run of double quotes | ||
| '\'foo""bar' => 'concat("\'foo",\'""bar\')', // run of double quotes in mid-string | ||
| ]; | ||
| foreach ($tests as $input => $expected) { | ||
| $result = $xpath->quote($input); | ||
| if ($result === $expected) { | ||
| echo "Pass: {$input} => {$result}\n"; | ||
| } else { | ||
| echo 'Fail: '; | ||
| var_dump([ | ||
| 'input' => $input, | ||
| 'expected' => $expected, | ||
| 'result' => $result, | ||
| 'userland_implementation_result' => UserlandDOMXPathQuote($input), | ||
| ]); | ||
| } | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| Pass: => '' | ||
| Pass: foo => 'foo' | ||
divinity76 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Pass: "foo => '"foo' | ||
| Pass: 'foo => "'foo" | ||
| Pass: 'foo"bar => concat("'foo",'"bar') | ||
| Pass: 'foo"bar"baz => concat("'foo",'"bar"baz') | ||
| Pass: 'foo" => concat("'foo",'"') | ||
| Pass: 'foo"" => concat("'foo",'""') | ||
| Pass: "'foo => concat('"',"'foo") | ||
| Pass: ""'foo => concat('""',"'foo") | ||
| Pass: 'foo""bar => concat("'foo",'""bar') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -446,6 +446,53 @@ PHP_METHOD(DOMXPath, registerPhpFunctionNS) | ||
| ); | ||
| } | ||
| /* {{{ */ | ||
| PHP_METHOD(DOMXPath, quote) { | ||
| const char *input; | ||
| size_t input_len; | ||
| if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &input, &input_len) == FAILURE) { | ||
| RETURN_THROWS(); | ||
| } | ||
| if (memchr(input, '\'', input_len) == NULL) { | ||
| zend_string *const output = zend_string_safe_alloc(1, input_len, 2, false); | ||
| output->val[0] = '\''; | ||
| memcpy(output->val + 1, input, input_len); | ||
| output->val[input_len + 1] = '\''; | ||
| output->val[input_len + 2] = '\0'; | ||
| RETURN_STR(output); | ||
divinity76 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } else if (memchr(input, '"', input_len) == NULL) { | ||
| zend_string *const output = zend_string_safe_alloc(1, input_len, 2, false); | ||
| output->val[0] = '"'; | ||
| memcpy(output->val + 1, input, input_len); | ||
| output->val[input_len + 1] = '"'; | ||
| output->val[input_len + 2] = '\0'; | ||
| RETURN_STR(output); | ||
| } else { | ||
| smart_str output = {0}; | ||
| // need to use the concat() trick published by Robert Rossney at https://stackoverflow.com/a/1352556/1067003 | ||
| smart_str_appendl(&output, "concat(", 7); | ||
| const char *ptr = input; | ||
| const char *const end = input + input_len; | ||
| while (ptr < end) { | ||
| const char *const single_quote_ptr = memchr(ptr, '\'', end - ptr); | ||
| const char *const double_quote_ptr = memchr(ptr, '"', end - ptr); | ||
| const size_t distance_to_single_quote = single_quote_ptr ? single_quote_ptr - ptr : end - ptr; | ||
| const size_t distance_to_double_quote = double_quote_ptr ? double_quote_ptr - ptr : end - ptr; | ||
| const size_t bytes_until_quote = MAX(distance_to_single_quote, distance_to_double_quote); | ||
| const char quote_method = (distance_to_single_quote > distance_to_double_quote) ? '\'' : '"'; | ||
| smart_str_appendc(&output, quote_method); | ||
| smart_str_appendl(&output, ptr, bytes_until_quote); | ||
| smart_str_appendc(&output, quote_method); | ||
| ptr += bytes_until_quote; | ||
| smart_str_appendc(&output, ','); | ||
| } | ||
| ZEND_ASSERT(ptr == end); | ||
| output.s->val[output.s->len - 1] = ')'; | ||
| RETURN_STR(smart_str_extract(&output)); | ||
| } | ||
| } | ||
| /* }}} */ | ||
| #endif /* LIBXML_XPATH_ENABLED */ | ||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.