Skip to content

Commit

Permalink
Find empty attributes in querySelector (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishow authored Nov 24, 2023
1 parent 2d0e3b6 commit efe6472
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/HTML5DOMDocument/Internal/QuerySelectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function testQuerySelector()
. '<div id="text1" class="class1">text1</div>'
. '<div>text2</div>'
. '<div>'
. '<div class="text3 class1">text3</div>'
. '<div empty-attribute class="text3 class1">text3</div>'
. '</div>'
. '<my-custom-element class="text5 class1">text5</my-custom-element>'
. '<span id="text4" class="class1 class2">text4</div>'
Expand All @@ -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);
Expand Down

0 comments on commit efe6472

Please sign in to comment.