Uh oh!
There was an error while loading. Please reload this page.
[1.10] Fix xpath php-string-to-javascript-string - #576
Merged
GrahamCampbell merged 3 commits intoMar 17, 2024
Conversation
TL;DR: addslashes() is not the correct way to convert a php-string to a javascript string. json_encode() is.
For example, addslashes will fail on the PHP string "foo".chr(10)."bar" , the old addslashes() will convert it into
"foo
bar"
which is a javascript syntax error.
Previously this code would fail:
$str = "foo".chr(10)."bar";
$xps = new XPathSelector("//span[contains(text(),'" . $str . "')]");
var_dump($xps->expressionCount());
it would generate a javascript syntax error:
string(134) "document.evaluate("//span[contains(text(),\'foo
bar\')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshot
Length"
now it generates legal javascript:
string(135) "document.evaluate("\/\/span[contains(text(),'foo\nbar')]", document
, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength"divinity76
commented
Dec 13, 2023
ContributorAuthor
I wonder if perhaps the original code was written to support PHP older than 5.2.0 (released 2006), where json_encode didn't exist yet |
GrahamCampbell
commented
Dec 13, 2023
Member
Ha, I doubt it. The original author probably just misunderstood how to prepare the string. |
GrahamCampbell
commented
Dec 13, 2023
Member
My mistake, really. I didn't catch it: #340. |
divinity76
commented
Feb 15, 2024
ContributorAuthor
i don't see any reason to postpone merging this |
divinity76 added a commit
to divinity76/chrome
that referenced
this pull request
May 23, 2025
CSS Selector expressions are not properly encoded, for example
input[type="password"]
is a perfectly valid CSS selector, and
$page->mouse()->find('input[type="password"]')
should have worked, but it generate a javascript syntax error at runtime:
```
document.querySelectorAll("input[name="email"]").length
```
resulting in
PHP Fatal error: Uncaught HeadlessChromium\Exception\ElementNotFoundException: The search for "document.querySelectorAll("input[name="email"]").length" returned no result. in /home/hans/projects/tryparrotai-unofficial-api/vendor/chrome-php/chrome/src/Input/Mouse.php:302
Stack trace:
#0 /home/hans/projects/tryparrotai-unofficial-api/vendor/chrome-php/chrome/src/Input/Mouse.php(269): HeadlessChromium\Input\Mouse->findElement()
Exactly the same issue as chrome-php#576
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR: addslashes() is not the correct way to convert a php-string to a javascript string. json_encode() is.
For example, addslashes will fail on the PHP string "foo".chr(10)."bar" , the old addslashes() code will convert it into "foo
bar"
which is a javascript syntax error.
Previously this code would fail:
it would generate a javascript syntax error:
now it generates valid javascript: