66
77use PHPHtmlParser \Contracts \Selector \SeekerInterface ;
88use PHPHtmlParser \Dom \Node \AbstractNode ;
9+ use PHPHtmlParser \Dom \Node \HtmlNode ;
910use PHPHtmlParser \Dom \Node \InnerNode ;
1011use PHPHtmlParser \Dom \Node \LeafNode ;
1112use PHPHtmlParser \DTO \Selector \RuleDTO ;
@@ -23,24 +24,6 @@ class Seeker implements SeekerInterface
2324 */
2425 public function seek (array $ nodes , RuleDTO $ rule , array $ options ): array
2526 {
26- // XPath index
27- if ($ rule ->getTag () !== null && \is_numeric ($ rule ->getKey ())) {
28- $ count = 0 ;
29- foreach ($ nodes as $ node ) {
30- if ($ rule ->getTag () == '* '
31- || $ rule ->getTag () == $ node ->getTag ()
32- ->name ()
33- ) {
34- ++$ count ;
35- if ($ count == $ rule ->getKey ()) {
36- // found the node we wanted
37- return [$ node ];
38- }
39- }
40- }
41-
42- return [];
43- }
4427
4528 $ options = $ this ->flattenOptions ($ options );
4629
@@ -62,16 +45,34 @@ public function seek(array $nodes, RuleDTO $rule, array $options): array
6245 continue ;
6346 }
6447
65- $ pass = $ this -> checkTag ( $ rule , $ child);
66- if ( $ pass && $ rule -> getKey () !== null ) {
67- $ pass = $ this -> checkKey ( $ rule , $ child ) ;
48+ if (! $ child instanceof HtmlNode) {
49+ $ child = $ this -> getNextChild ( $ node , $ child );
50+ continue ;
6851 }
69- if ($ pass &&
70- $ rule ->getKey () !== null &&
71- $ rule ->getValue () !== null &&
72- $ rule ->getValue () != '* '
73- ) {
74- $ pass = $ this ->checkComparison ($ rule , $ child );
52+
53+ $ pass = true ;
54+
55+ if ($ rule ->getTag () !== null && \is_numeric ($ rule ->getKey ()) && $ node instanceof HtmlNode) {
56+ if (strpos ($ rule ->getValue (), 'nth- ' ) === 0 ) {
57+ $ children = $ node ->childElements ();
58+ $ n = $ rule ->getKey () < 0 ? count ($ children ) + $ rule ->getKey () : $ rule ->getKey ()-1 ;
59+ $ pass = $ n >= 0 && $ n < count ($ children ) && $ child == $ children [$ n ];
60+ }
61+ }
62+
63+ if ($ pass ) {
64+ $ pass = $ this ->checkTag ($ rule , $ child );
65+ if ($ pass && $ rule ->getKey () !== null && !\is_numeric ($ rule ->getKey ())) {
66+ $ pass = $ this ->checkKey ($ rule , $ child );
67+ }
68+ if ($ pass &&
69+ $ rule ->getKey () !== null &&
70+ $ rule ->getValue () !== null &&
71+ $ rule ->getValue () != '* ' &&
72+ !\is_numeric ($ rule ->getKey ())
73+ ) {
74+ $ pass = $ this ->checkComparison ($ rule , $ child );
75+ }
7576 }
7677
7778 if ($ pass ) {
0 commit comments