Skip to content

Commit cbbe2d5

Browse files
committed
Performance optimizations.
1 parent efe6472 commit cbbe2d5

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/HTML5DOMDocument/Internal/QuerySelectors.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,10 @@ 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();
147146
foreach ($attributeSelectors as $attributeSelector) {
148147
$isMatch = false;
149-
$attributeValue = $element->getAttribute($attributeSelector['name']);
150148
if (isset($attributeSelector['value'])) {
149+
$attributeValue = $element->getAttribute($attributeSelector['name']);
151150
$valueToMatch = $attributeSelector['value'];
152151
switch ($attributeSelector['operator']) {
153152
case '=':
@@ -187,11 +186,11 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
187186
break;
188187
}
189188
} else {
190-
if ($attributeValue !== '') {
191-
$isMatch = true;
192-
} else {
193-
$found = array_search($attributeSelector['name'], array_keys($attributes));
194-
$isMatch = ($found !== FALSE);
189+
foreach ($element->attributes as $elementAttributeName => $elementAttributeValue) {
190+
if ($elementAttributeName === $attributeSelector['name']) {
191+
$isMatch = true;
192+
break;
193+
}
195194
}
196195
}
197196
if (!$isMatch) {

tests/Test.php

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ public function testQuerySelector()
366366
$this->assertTrue($dom->querySelectorAll('[id]')->item(0)->innerHTML === 'text1');
367367
$this->assertTrue($dom->querySelectorAll('[id]')->length === 2);
368368
$this->assertTrue($dom->querySelectorAll('[empty-attribute]')->length === 1);
369+
$this->assertTrue($dom->querySelectorAll('[missing-attribute]')->length === 0);
369370
$this->assertTrue($dom->querySelectorAll('span[id]')->item(0)->innerHTML === 'text4');
370371
$this->assertTrue($dom->querySelectorAll('span[data-other]')->length === 0);
371372
$this->assertTrue($dom->querySelectorAll('div#text4')->length === 0);

0 commit comments

Comments
 (0)