Skip to content

Commit

Permalink
Merge pull request #8360 from kenjis/fix-DOMParser
Browse files Browse the repository at this point in the history
fix: DOMParser cannot see element with `id="0"`
  • Loading branch information
kenjis authored Dec 25, 2023
2 parents f06395c + c03d34a commit bd0fbe9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
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

0 comments on commit bd0fbe9

Please sign in to comment.