6
6
7
7
use PHPHtmlParser \Contracts \Selector \SeekerInterface ;
8
8
use PHPHtmlParser \Dom \Node \AbstractNode ;
9
+ use PHPHtmlParser \Dom \Node \HtmlNode ;
9
10
use PHPHtmlParser \Dom \Node \InnerNode ;
10
11
use PHPHtmlParser \Dom \Node \LeafNode ;
11
12
use PHPHtmlParser \DTO \Selector \RuleDTO ;
@@ -23,24 +24,6 @@ class Seeker implements SeekerInterface
23
24
*/
24
25
public function seek (array $ nodes , RuleDTO $ rule , array $ options ): array
25
26
{
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
- }
44
27
45
28
$ options = $ this ->flattenOptions ($ options );
46
29
@@ -62,16 +45,34 @@ public function seek(array $nodes, RuleDTO $rule, array $options): array
62
45
continue ;
63
46
}
64
47
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 ;
68
51
}
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
+ }
75
76
}
76
77
77
78
if ($ pass ) {
0 commit comments