diff --git a/src/HTML5DOMDocument/Internal/QuerySelectors.php b/src/HTML5DOMDocument/Internal/QuerySelectors.php index fafd13d..16dd54d 100644 --- a/src/HTML5DOMDocument/Internal/QuerySelectors.php +++ b/src/HTML5DOMDocument/Internal/QuerySelectors.php @@ -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], @@ -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)) { diff --git a/tests/Test.php b/tests/Test.php index 9936c9f..a5be9bc 100644 --- a/tests/Test.php +++ b/tests/Test.php @@ -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('
' + . '