Skip to content

Commit

Permalink
Handle nested pseudo-classes with expression correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Imangazaliev committed Feb 9, 2021
1 parent cf9200b commit 2465282
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/DiDom/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,11 @@ public static function getSegments($selector)
$classes = '(?P<classes>\.[\w|\-|\.]+)*';
$attrs = '(?P<attrs>(?:\[.+?\])*)?';
$name = '(?P<pseudo>[\w\-]+)';
$expr = '(?:\((?P<expr>[^\)]+)\))';
$pseudo = '(?::'.$name.$expr.'?)?';
$expr = '(?:\((?P<expr>.+)\))';
$pseudo = '(?::' . $name . $expr . '?)?';
$rel = '\s*(?P<rel>>)?';

$regexp = '/'.$tag.$id.$classes.$attrs.$pseudo.$rel.'/is';
$regexp = '/' . $tag . $id . $classes . $attrs . $pseudo . $rel . '/is';

if (preg_match($regexp, $selector, $segments)) {
if ($segments[0] === '') {
Expand Down
2 changes: 2 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ public function compileCssTests()

$compiled = array_merge($compiled, [
['a[title="foo, bar::baz"]', '//a[@title="foo, bar::baz"]'],
// nested pseudo-class with expression
[':not(:contains(foo))', '//*[not(self::*[contains(text(), "foo")])]'],
]);

return $compiled;
Expand Down

1 comment on commit 2465282

@lokanaft
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it cannot be solved so easily, I have already tried

td:nth-child(1) a:not(:contains(" 50"))

Fatal error: Uncaught DiDom\Exceptions\InvalidSelectorException: Invalid nth-child expression "1) a:not(:contains(" 50")"

Please sign in to comment.