Skip to content

Commit 51fef3b

Browse files
authored
fix missing encoding in CssSelector.php
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 #576
1 parent b10c8c1 commit 51fef3b

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Dom/Selector/CssSelector.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,32 @@ public function __construct(string $expression)
1919

2020
public function expressionCount(): string
2121
{
22-
return \sprintf('document.querySelectorAll("%s").length', $this->expression);
22+
$encodedExpr = \json_encode(
23+
$this->expression,
24+
\JSON_UNESCAPED_SLASHES
25+
| \JSON_UNESCAPED_UNICODE
26+
| \JSON_THROW_ON_ERROR
27+
);
28+
29+
return \sprintf(
30+
'document.querySelectorAll(%s).length',
31+
$encodedExpr
32+
);
2333
}
2434

2535
public function expressionFindOne(int $position): string
2636
{
27-
return \sprintf('document.querySelectorAll("%s")[%d]', $this->expression, $position - 1);
37+
$encodedExpr = \json_encode(
38+
$this->expression,
39+
\JSON_UNESCAPED_SLASHES
40+
| \JSON_UNESCAPED_UNICODE
41+
| \JSON_THROW_ON_ERROR
42+
);
43+
44+
return \sprintf(
45+
'document.querySelectorAll(%s)[%d]',
46+
$encodedExpr,
47+
$position - 1
48+
);
2849
}
2950
}

0 commit comments

Comments
 (0)