Skip to content

Commit

Permalink
Performance optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Nov 24, 2023
1 parent efe6472 commit cbbe2d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/HTML5DOMDocument/Internal/QuerySelectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '=':
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit cbbe2d5

Please sign in to comment.