Skip to content

Commit 48fea05

Browse files
committed
Improvements in query selectors
- Partial support to :nth-of-type - Support to :only-child - Fixed bugs
1 parent 9eb7403 commit 48fea05

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/Experimental/Dom/Selector.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,37 @@ class Selector extends \DOMXPath
1414
private $prevent;
1515
private $rules;
1616
private $qxs = array(
17-
array( '/^([^a-z*])/', '*\\1' ),
18-
array( '/([>\+~])([^a-z*])/', '\\1*\\2' ),
19-
array( '/\[(.*?)\]/', '[@\\1]' ),
20-
array( '/#(\w+)/', '[@id="\\1"]' ),
21-
array( '/\:empty/', '[count(*)=0]'),
22-
array( '/\:last-child/', '[last()]' ),
23-
array( '/^(.*?)\:first-child $/', 'descendant::\\1' ),
17+
array( '/^([^a-zA-Z*])/', '*\\1' ),
18+
array( '/\[([^@])(.*?)\]/', '[@\\1\\2]' ),
19+
array( '/([>+~]|^)\:nth-(last-)?(child|of-type)\(n\)/', '\\1*' ),
2420
array( '/\:nth-(last-)?(child|of-type)\(n\)/', '' ),
2521
array( '/\:nth-child\(odd\)/', ':nth-child(2n+1)' ),
2622
array( '/\:nth-child\(even\)/', ':nth-child(2n)' ),
27-
array( '/\:nth-child\((\d+)\)/', '[position() mod \\1 = 1]' ),
28-
array( '/\:nth-child\((\d+)n\)/', '[position() mod \\1 = 0]' ),
29-
array( '/\:nth-child\((\d+)n\+(\d+)\)/', '[position() mod \\1 = \\2]' ),
30-
array( '/\:nth-last-child\(odd\)/', ':nth-last-child(2n+1)' ),
31-
array( '/\:nth-last-child\(even\)/', ':nth-last-child(2n)' ),
32-
array( '/\:nth-last-child\((\d+)\)/', '[(count() - position()) mod \\1 = 1]' ),
33-
array( '/\:nth-last-child\((\d+)n\)/', '[(count() - position()) mod \\1 = 0]' ),
34-
array( '/\:nth-last-child\((\d+)n\+(\d+)\)/', '[(count() - position()) mod \\1 = \\2]' ),
23+
array( '/([>+~])?\*([^>+~]+)?\:nth-child\((\d+)n\)/', '\\1*[position() mod \\4=0]\\3' ),
24+
array( '/([>+~])?\*([^>+~]+)?\:nth-child\((\d+)n\+(\d+)\)/', '\\1*[position() mod \\4=\\5]\\3' ),
25+
array( '/([>+~])?(\w+)([^>+~]+)?\:nth-child\((\d+)n\)/', '\\1*[name()="\\2" and position() mod \\4=0]\\3' ),
26+
array( '/([>+~])?(\w+)([^>+~]+)?\:nth-child\((\d+)n\+(\d+)\)/', '\\1*[name()="\\2" and position() mod \\4=\\5]\\3' ),
27+
array( '/([>+~])?(\w+)([^>+~]+)?\:nth-of-type\((\d+)n\)/', '\\1\\2[position() mod \\4=0]\\3' ),
28+
array( '/([>+~])?(\w+)([^>+~]+)?\:nth-of-type\((\d+)n\+(\d+)\)/', '\\1\\2[position() mod \\4=\\5]\\3' ),
29+
array( '/([>+~])?\*([^>+~]+)?\:only-child/', '\\1*[last()=1]\\2'),
30+
array( '/([>+~])?\*([^>+~]+)?\:last-child/', '\\1*[position()=last()]\\2' ),
31+
array( '/([>+~])?(\w+)([^>+~]+)?\:only-child/', '\\1*[name()="\\2" and last()=1]\\3'),
32+
array( '/([>+~])?(\w+)([^>+~]+)?\:last-child/', '\\1*[name()="\\2" and position()=last()]\\3' ),
33+
array( '/([>+~])([^\w*\s])/', '\\1*\\2' ),
3534
array( '/\.(\w+)/', '[@class~="\\1"]' ),
35+
array( '/#(\w+)/', '[@id="\\1"]' ),
3636
array( '/\:lang\(([\w\-]+)\)/', '[@lang|="\\1"]' ),
37+
array( '/\:empty/', '[not(text())]'),
38+
array( '/^(.*?)\:first-child $/', 'descendant::\\1' ),
3739
array( '/\[(@\w+)\^=(["\'])(.*?)\\2\]/', '[starts-with(\\1,\\2\\3\\2)]' ),
3840
array( '/\[(@\w+)\*=(["\'])(.*?)\\2\]/', '[contains(\\1,\\2\\3\\2)]' ),
3941
array( '/\[(@\w+)\~=(["\'])(.*?)\\2\]/', '[contains(concat(" ",\\1," "),\\2 \\3 \\2)]' ),
4042
array( '/\[(@\w+)\|=(["\'])(.*?)\\2\]/', '[starts-with(concat(\\1,"-"),concat(\\2\\3\\2,"-"))]' ),
4143
array( '/\[(@\w+)\$=(["\'])(.*?)\\2\]/', '[substring(\\1,string-length(\\1)-2)=\\2\\3\\2]' ),
4244
array( '/\:contains\((.*?)\)/', '[contains(text(),\\1)]' ),
43-
array( '/[~]([*\w]+)/', '/following-sibling::*[count(\\1)]' ),
44-
array( '/\+/', '/following-sibling::' ),
45+
array( '/\+(\*)/', '/following-sibling::*[position()=1]' ),
46+
array( '/\+(\w+)/', '/following-sibling::*[name()="\\1" and position()=1]' ),
47+
array( '/[~](\*|\w+)/', '/following-sibling::\\1' ),
4548
array( '/[>]/', '/' )
4649
);
4750

@@ -127,8 +130,6 @@ private function toXPath($query)
127130

128131
$queries = explode(',', $query);
129132

130-
$restore = array_combine(array_values($this->prevent), array_keys($this->prevent));
131-
132133
foreach ($queries as &$descendants) {
133134

134135
$descendants = explode(' ', trim($descendants));
@@ -138,8 +139,6 @@ private function toXPath($query)
138139
$r = strtr($qx[1], $this->rules);
139140

140141
$descendant = trim(preg_replace($qx[0], $r, $descendant));
141-
142-
var_dump( strtr($descendant, $restore) . ' => ' . $qx[0] );
143142
}
144143
}
145144

0 commit comments

Comments
 (0)