Skip to content
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

fix: DOMParser cannot see element with id="0" #8360

Merged
merged 5 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -3171,11 +3171,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Test/DOMParser.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 6,
'path' => __DIR__ . '/system/Test/DOMParser.php',
];
$ignoreErrors[] = [
'message' => '#^Access to an undefined property object\\:\\:\\$createdField\\.$#',
'count' => 1,
Expand Down
14 changes: 7 additions & 7 deletions system/Test/DOMParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,23 @@ protected function doXPath(?string $search, string $element, array $paths = [])
$path = '';

// By ID
if (! empty($selector['id'])) {
$path = empty($selector['tag'])
if (isset($selector['id'])) {
$path = ($selector['tag'] === '')
? "id(\"{$selector['id']}\")"
: "//{$selector['tag']}[@id=\"{$selector['id']}\"]";
}
// By Class
elseif (! empty($selector['class'])) {
$path = empty($selector['tag'])
elseif (isset($selector['class'])) {
$path = ($selector['tag'] === '')
? "//*[@class=\"{$selector['class']}\"]"
: "//{$selector['tag']}[@class=\"{$selector['class']}\"]";
}
// By tag only
elseif (! empty($selector['tag'])) {
elseif ($selector['tag'] !== '') {
$path = "//{$selector['tag']}";
}

if (! empty($selector['attr'])) {
if (isset($selector['attr'])) {
foreach ($selector['attr'] as $key => $value) {
$path .= "[@{$key}=\"{$value}\"]";
}
Expand All @@ -231,7 +231,7 @@ protected function doXPath(?string $search, string $element, array $paths = [])
/**
* Look for the a selector in the passed text.
*
* @return array
* @return array{tag: string, id: string|null, class: string|null, attr: array<string, string>|null}
*/
public function parseSelector(string $selector)
{
Expand Down
14 changes: 12 additions & 2 deletions tests/system/Test/DOMParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function provideText(): iterable
/**
* @dataProvider provideText
*
* @param mixed $text
* @param string $text
*/
public function testSeeText($text): void
{
Expand Down Expand Up @@ -139,7 +139,7 @@ public function testSeeFail(): void
/**
* @dataProvider provideText
*
* @param mixed $text
* @param string $text
*/
public function testSeeElement($text): void
{
Expand Down Expand Up @@ -171,6 +171,16 @@ public function testSeeElementID(): void
$this->assertTrue($dom->see('Hello World', '#heading'));
}

public function testSeeElementIDZero(): void
{
$dom = new DOMParser();

$html = '<html><body><h1 id="0">Hello World Wide Web</h1></body></html>';
$dom->withString($html);

$this->assertTrue($dom->see('Hello World', '#0'));
}

public function testSeeElementIDFails(): void
{
$dom = new DOMParser();
Expand Down
Loading