Skip to content

Commit bd0fbe9

Browse files
authored
Merge pull request #8360 from kenjis/fix-DOMParser
fix: DOMParser cannot see element with `id="0"`
2 parents f06395c + c03d34a commit bd0fbe9

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,11 +3171,6 @@
31713171
'count' => 1,
31723172
'path' => __DIR__ . '/system/Test/DOMParser.php',
31733173
];
3174-
$ignoreErrors[] = [
3175-
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
3176-
'count' => 6,
3177-
'path' => __DIR__ . '/system/Test/DOMParser.php',
3178-
];
31793174
$ignoreErrors[] = [
31803175
'message' => '#^Access to an undefined property object\\:\\:\\$createdField\\.$#',
31813176
'count' => 1,

system/Test/DOMParser.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,23 @@ protected function doXPath(?string $search, string $element, array $paths = [])
189189
$path = '';
190190

191191
// By ID
192-
if (! empty($selector['id'])) {
193-
$path = empty($selector['tag'])
192+
if (isset($selector['id'])) {
193+
$path = ($selector['tag'] === '')
194194
? "id(\"{$selector['id']}\")"
195195
: "//{$selector['tag']}[@id=\"{$selector['id']}\"]";
196196
}
197197
// By Class
198-
elseif (! empty($selector['class'])) {
199-
$path = empty($selector['tag'])
198+
elseif (isset($selector['class'])) {
199+
$path = ($selector['tag'] === '')
200200
? "//*[@class=\"{$selector['class']}\"]"
201201
: "//{$selector['tag']}[@class=\"{$selector['class']}\"]";
202202
}
203203
// By tag only
204-
elseif (! empty($selector['tag'])) {
204+
elseif ($selector['tag'] !== '') {
205205
$path = "//{$selector['tag']}";
206206
}
207207

208-
if (! empty($selector['attr'])) {
208+
if (isset($selector['attr'])) {
209209
foreach ($selector['attr'] as $key => $value) {
210210
$path .= "[@{$key}=\"{$value}\"]";
211211
}
@@ -231,7 +231,7 @@ protected function doXPath(?string $search, string $element, array $paths = [])
231231
/**
232232
* Look for the a selector in the passed text.
233233
*
234-
* @return array
234+
* @return array{tag: string, id: string|null, class: string|null, attr: array<string, string>|null}
235235
*/
236236
public function parseSelector(string $selector)
237237
{

tests/system/Test/DOMParserTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function provideText(): iterable
9191
/**
9292
* @dataProvider provideText
9393
*
94-
* @param mixed $text
94+
* @param string $text
9595
*/
9696
public function testSeeText($text): void
9797
{
@@ -139,7 +139,7 @@ public function testSeeFail(): void
139139
/**
140140
* @dataProvider provideText
141141
*
142-
* @param mixed $text
142+
* @param string $text
143143
*/
144144
public function testSeeElement($text): void
145145
{
@@ -171,6 +171,16 @@ public function testSeeElementID(): void
171171
$this->assertTrue($dom->see('Hello World', '#heading'));
172172
}
173173

174+
public function testSeeElementIDZero(): void
175+
{
176+
$dom = new DOMParser();
177+
178+
$html = '<html><body><h1 id="0">Hello World Wide Web</h1></body></html>';
179+
$dom->withString($html);
180+
181+
$this->assertTrue($dom->see('Hello World', '#0'));
182+
}
183+
174184
public function testSeeElementIDFails(): void
175185
{
176186
$dom = new DOMParser();

0 commit comments

Comments
 (0)