diff --git a/CHANGELOG.md b/CHANGELOG.md index 45724acc..6266c9df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ * Add PHP 8.3 support +## 1.10.1 (2024-03-17) + +* Fixed encoding bugs in `XPathSelector` + + ## 1.10.0 (2023-12-10) * Add `Add Node::setHTML` method diff --git a/LICENSE b/LICENSE index 269b2393..664d299c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,8 +1,8 @@ The MIT License (MIT) Copyright (c) 2017-2020 Soufiane Ghzal -Copyright (c) 2020-2023 Graham Campbell -Copyright (c) 2020-2023 Enrico Dias +Copyright (c) 2020-2024 Graham Campbell +Copyright (c) 2020-2024 Enrico Dias Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 5e08f939..1cacdd9d 100644 --- a/README.md +++ b/README.md @@ -799,6 +799,8 @@ selectors, you can pass an instance of the required `Selector`. Wait for element by XPath selector: ```php +use HeadlessChromium\Dom\Selector\XPathSelector; + $page = $browser->createPage(); $page->navigate('http://example.com')->waitForNavigation(); diff --git a/src/Dom/Selector/XPathSelector.php b/src/Dom/Selector/XPathSelector.php index 5d984def..71981ee2 100644 --- a/src/Dom/Selector/XPathSelector.php +++ b/src/Dom/Selector/XPathSelector.php @@ -19,18 +19,11 @@ public function __construct(string $expression) public function expressionCount(): string { - return \sprintf( - 'document.evaluate("%s", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength', - \addslashes($this->expression) - ); + return 'document.evaluate('.\json_encode($this->expression, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE).', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength'; } public function expressionFindOne(int $position): string { - return \sprintf( - 'document.evaluate("%s[%d]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue', - \addslashes($this->expression), - $position - ); + return 'document.evaluate('.\json_encode($this->expression."[{$position}]", \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE).', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue'; } }