diff --git a/src/HTML5DOMDocument/Internal/QuerySelectors.php b/src/HTML5DOMDocument/Internal/QuerySelectors.php
index 60282b8..21e261f 100644
--- a/src/HTML5DOMDocument/Internal/QuerySelectors.php
+++ b/src/HTML5DOMDocument/Internal/QuerySelectors.php
@@ -143,11 +143,10 @@ 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']);
if (isset($attributeSelector['value'])) {
+ $attributeValue = $element->getAttribute($attributeSelector['name']);
$valueToMatch = $attributeSelector['value'];
switch ($attributeSelector['operator']) {
case '=':
@@ -187,11 +186,11 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
break;
}
} else {
- if ($attributeValue !== '') {
- $isMatch = true;
- } else {
- $found = array_search($attributeSelector['name'], array_keys($attributes));
- $isMatch = ($found !== FALSE);
+ foreach ($element->attributes as $elementAttributeName => $elementAttributeValue) {
+ if ($elementAttributeName === $attributeSelector['name']) {
+ $isMatch = true;
+ break;
+ }
}
}
if (!$isMatch) {
diff --git a/tests/Test.php b/tests/Test.php
index e63be25..c706f9f 100644
--- a/tests/Test.php
+++ b/tests/Test.php
@@ -366,6 +366,7 @@ public function testQuerySelector()
$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('[missing-attribute]')->length === 0);
$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);