From efe6472d000e82c9fcb54076dc10d69351776fbb Mon Sep 17 00:00:00 2001 From: Chris How Date: Fri, 24 Nov 2023 17:08:18 +0000 Subject: [PATCH] Find empty attributes in querySelector (#58) --- src/HTML5DOMDocument/Internal/QuerySelectors.php | 4 ++++ tests/Test.php | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/HTML5DOMDocument/Internal/QuerySelectors.php b/src/HTML5DOMDocument/Internal/QuerySelectors.php index b99ffd0..60282b8 100644 --- a/src/HTML5DOMDocument/Internal/QuerySelectors.php +++ b/src/HTML5DOMDocument/Internal/QuerySelectors.php @@ -143,6 +143,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu $tagName = strlen($match[1]) > 0 ? strtolower($match[1]) : null; $check = function ($element) use ($attributeSelectors) { if ($element->attributes->length > 0) { + $attributes = $element->getAttributes(); foreach ($attributeSelectors as $attributeSelector) { $isMatch = false; $attributeValue = $element->getAttribute($attributeSelector['name']); @@ -188,6 +189,9 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu } else { if ($attributeValue !== '') { $isMatch = true; + } else { + $found = array_search($attributeSelector['name'], array_keys($attributes)); + $isMatch = ($found !== FALSE); } } if (!$isMatch) { diff --git a/tests/Test.php b/tests/Test.php index 4273b60..e63be25 100644 --- a/tests/Test.php +++ b/tests/Test.php @@ -344,7 +344,7 @@ public function testQuerySelector() . '
text1
' . '
text2
' . '
' - . '
text3
' + . '
text3
' . '
' . 'text5' . 'text4' @@ -365,6 +365,7 @@ public function testQuerySelector() $this->assertTrue($dom->querySelectorAll('span[id="text4"]')->item(0)->innerHTML === 'text4'); $this->assertTrue($dom->querySelectorAll('[id]')->item(0)->innerHTML === 'text1'); $this->assertTrue($dom->querySelectorAll('[id]')->length === 2); + $this->assertTrue($dom->querySelectorAll('[empty-attribute]')->length === 1); $this->assertTrue($dom->querySelectorAll('span[id]')->item(0)->innerHTML === 'text4'); $this->assertTrue($dom->querySelectorAll('span[data-other]')->length === 0); $this->assertTrue($dom->querySelectorAll('div#text4')->length === 0);