Skip to content

Commit

Permalink
Fix parsing complex queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Feb 11, 2019
1 parent d200b0f commit 11624fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/HTML5DOMDocument/Internal/QuerySelectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
};

// tagname[target] or [target] // Available values for targets: attr, attr="value", attr~="value", attr|="value", attr^="value", attr$="value", attr*="value"
$simpleSelectors['(?:[a-z0-9\-]*)(?:\[.+\])'] = function(string $mode, array $match, \DOMNode $context, callable $add = null) use ($walkChildren) {
$simpleSelectors['(?:[a-z0-9\-]*)(?:\[.+?\])'] = function(string $mode, array $match, \DOMNode $context, callable $add = null) use ($walkChildren) {
$attributeSelectors = explode('][', substr($match[2], 1, -1));
foreach ($attributeSelectors as $i => $attributeSelector) {
$attributeSelectorMatches = null;
if (preg_match('/^(.+?)(=|~=|\|=|\^=|\$=|\*=)\"(.+)\"$/', $attributeSelector, $attributeSelectorMatches) === 1) {
if (preg_match('/^(.+?)(=|~=|\|=|\^=|\$=|\*=)\"(.+?)\"$/', $attributeSelector, $attributeSelectorMatches) === 1) {
$attributeSelectors[$i] = [
'name' => $attributeSelectorMatches[1],
'value' => $attributeSelectorMatches[3],
Expand Down Expand Up @@ -200,7 +200,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
};

// tagname.classname, .classname, tagname.classname.classname2, .classname.classname2
$simpleSelectors['(?:[a-z0-9\-]*)\.(?:.+)'] = function(string $mode, array $match, \DOMNode $context, callable $add = null) use ($walkChildren) {
$simpleSelectors['(?:[a-z0-9\-]*)\.(?:.+?)'] = function(string $mode, array $match, \DOMNode $context, callable $add = null) use ($walkChildren) {
$tagName = strlen($match[1]) > 0 ? $match[1] : null;
$classesSelector = explode('.', $match[2]);
if (empty($classesSelector)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,21 @@ public function testComplexQuerySelectors()
$this->assertTrue($dom->querySelectorAll('span ~ div')->length === 3);
}

/**
* Tests multiple query selectors matching. If a query selector is not greedy problems may arise.
*/
public function testComplexQuerySelectors2()
{
$dom = new HTML5DOMDocument();
$dom->loadHTML('<body>'
. '<div class="a1">1</div>'
. '<div class="a2">2</div>'
. '<div class="a3">3</div>'
. '</body>');
$elements = $dom->querySelectorAll('.a1,.a2,.a3');
$this->assertEquals($elements->length, 3);
}

/**
*
*/
Expand Down

0 comments on commit 11624fe

Please sign in to comment.