Skip to content

Commit efe6472

Browse files
authored
Find empty attributes in querySelector (#58)
1 parent 2d0e3b6 commit efe6472

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/HTML5DOMDocument/Internal/QuerySelectors.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
143143
$tagName = strlen($match[1]) > 0 ? strtolower($match[1]) : null;
144144
$check = function ($element) use ($attributeSelectors) {
145145
if ($element->attributes->length > 0) {
146+
$attributes = $element->getAttributes();
146147
foreach ($attributeSelectors as $attributeSelector) {
147148
$isMatch = false;
148149
$attributeValue = $element->getAttribute($attributeSelector['name']);
@@ -188,6 +189,9 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
188189
} else {
189190
if ($attributeValue !== '') {
190191
$isMatch = true;
192+
} else {
193+
$found = array_search($attributeSelector['name'], array_keys($attributes));
194+
$isMatch = ($found !== FALSE);
191195
}
192196
}
193197
if (!$isMatch) {

tests/Test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function testQuerySelector()
344344
. '<div id="text1" class="class1">text1</div>'
345345
. '<div>text2</div>'
346346
. '<div>'
347-
. '<div class="text3 class1">text3</div>'
347+
. '<div empty-attribute class="text3 class1">text3</div>'
348348
. '</div>'
349349
. '<my-custom-element class="text5 class1">text5</my-custom-element>'
350350
. '<span id="text4" class="class1 class2">text4</div>'
@@ -365,6 +365,7 @@ public function testQuerySelector()
365365
$this->assertTrue($dom->querySelectorAll('span[id="text4"]')->item(0)->innerHTML === 'text4');
366366
$this->assertTrue($dom->querySelectorAll('[id]')->item(0)->innerHTML === 'text1');
367367
$this->assertTrue($dom->querySelectorAll('[id]')->length === 2);
368+
$this->assertTrue($dom->querySelectorAll('[empty-attribute]')->length === 1);
368369
$this->assertTrue($dom->querySelectorAll('span[id]')->item(0)->innerHTML === 'text4');
369370
$this->assertTrue($dom->querySelectorAll('span[data-other]')->length === 0);
370371
$this->assertTrue($dom->querySelectorAll('div#text4')->length === 0);

0 commit comments

Comments
 (0)