-
Notifications
You must be signed in to change notification settings - Fork 25
/
gan_selector_html.php
949 lines (855 loc) · 23.9 KB
/
gan_selector_html.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
<?php
/**
* @author Niels A.D.
* @author Todd Burry <[email protected]>
* @copyright 2010 Niels A.D., 2014 Todd Burry
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
* @package pQuery
*/
namespace pQuery;
/**
* Tokenizes a css selector query
*/
class CSSQueryTokenizer extends TokenizerBase {
/**
* Opening bracket token, used for "["
*/
const TOK_BRACKET_OPEN = 100;
/**
* Closing bracket token, used for "]"
*/
const TOK_BRACKET_CLOSE = 101;
/**
* Opening brace token, used for "("
*/
const TOK_BRACE_OPEN = 102;
/**
* Closing brace token, used for ")"
*/
const TOK_BRACE_CLOSE = 103;
/**
* String token
*/
const TOK_STRING = 104;
/**
* Colon token, used for ":"
*/
const TOK_COLON = 105;
/**
* Comma token, used for ","
*/
const TOK_COMMA = 106;
/**
* "Not" token, used for "!"
*/
const TOK_NOT = 107;
/**
* "All" token, used for "*" in query
*/
const TOK_ALL = 108;
/**
* Pipe token, used for "|"
*/
const TOK_PIPE = 109;
/**
* Plus token, used for "+"
*/
const TOK_PLUS = 110;
/**
* "Sibling" token, used for "~" in query
*/
const TOK_SIBLING = 111;
/**
* Class token, used for "." in query
*/
const TOK_CLASS = 112;
/**
* ID token, used for "#" in query
*/
const TOK_ID = 113;
/**
* Child token, used for ">" in query
*/
const TOK_CHILD = 114;
/**
* Attribute compare prefix token, used for "|="
*/
const TOK_COMPARE_PREFIX = 115;
/**
* Attribute contains token, used for "*="
*/
const TOK_COMPARE_CONTAINS = 116;
/**
* Attribute contains word token, used for "~="
*/
const TOK_COMPARE_CONTAINS_WORD = 117;
/**
* Attribute compare end token, used for "$="
*/
const TOK_COMPARE_ENDS = 118;
/**
* Attribute equals token, used for "="
*/
const TOK_COMPARE_EQUALS = 119;
/**
* Attribute not equal token, used for "!="
*/
const TOK_COMPARE_NOT_EQUAL = 120;
/**
* Attribute compare bigger than token, used for ">="
*/
const TOK_COMPARE_BIGGER_THAN = 121;
/**
* Attribute compare smaller than token, used for "<="
*/
const TOK_COMPARE_SMALLER_THAN = 122;
/**
* Attribute compare with regex, used for "%="
*/
const TOK_COMPARE_REGEX = 123;
/**
* Attribute compare start token, used for "^="
*/
const TOK_COMPARE_STARTS = 124;
/**
* Sets query identifiers
* @see TokenizerBase::$identifiers
* @access private
*/
var $identifiers = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_-?';
/**
* Map characters to match their tokens
* @see TokenizerBase::$custom_char_map
* @access private
*/
var $custom_char_map = array(
'.' => self::TOK_CLASS,
'#' => self::TOK_ID,
',' => self::TOK_COMMA,
'>' => 'parse_gt',//self::TOK_CHILD,
'+' => self::TOK_PLUS,
'~' => 'parse_sibling',
'|' => 'parse_pipe',
'*' => 'parse_star',
'$' => 'parse_compare',
'=' => self::TOK_COMPARE_EQUALS,
'!' => 'parse_not',
'%' => 'parse_compare',
'^' => 'parse_compare',
'<' => 'parse_compare',
'"' => 'parse_string',
"'" => 'parse_string',
'(' => self::TOK_BRACE_OPEN,
')' => self::TOK_BRACE_CLOSE,
'[' => self::TOK_BRACKET_OPEN,
']' => self::TOK_BRACKET_CLOSE,
':' => self::TOK_COLON
);
/**
* Parse ">" character
* @internal Could be {@link TOK_CHILD} or {@link TOK_COMPARE_BIGGER_THAN}
* @return int
*/
protected function parse_gt() {
if ((($this->pos + 1) < $this->size) && ($this->doc[$this->pos + 1] === '=')) {
++$this->pos;
return ($this->token = self::TOK_COMPARE_BIGGER_THAN);
} else {
return ($this->token = self::TOK_CHILD);
}
}
/**
* Parse "~" character
* @internal Could be {@link TOK_SIBLING} or {@link TOK_COMPARE_CONTAINS_WORD}
* @return int
*/
protected function parse_sibling() {
if ((($this->pos + 1) < $this->size) && ($this->doc[$this->pos + 1] === '=')) {
++$this->pos;
return ($this->token = self::TOK_COMPARE_CONTAINS_WORD);
} else {
return ($this->token = self::TOK_SIBLING);
}
}
/**
* Parse "|" character
* @internal Could be {@link TOK_PIPE} or {@link TOK_COMPARE_PREFIX}
* @return int
*/
protected function parse_pipe() {
if ((($this->pos + 1) < $this->size) && ($this->doc[$this->pos + 1] === '=')) {
++$this->pos;
return ($this->token = self::TOK_COMPARE_PREFIX);
} else {
return ($this->token = self::TOK_PIPE);
}
}
/**
* Parse "*" character
* @internal Could be {@link TOK_ALL} or {@link TOK_COMPARE_CONTAINS}
* @return int
*/
protected function parse_star() {
if ((($this->pos + 1) < $this->size) && ($this->doc[$this->pos + 1] === '=')) {
++$this->pos;
return ($this->token = self::TOK_COMPARE_CONTAINS);
} else {
return ($this->token = self::TOK_ALL);
}
}
/**
* Parse "!" character
* @internal Could be {@link TOK_NOT} or {@link TOK_COMPARE_NOT_EQUAL}
* @return int
*/
protected function parse_not() {
if ((($this->pos + 1) < $this->size) && ($this->doc[$this->pos + 1] === '=')) {
++$this->pos;
return ($this->token = self::TOK_COMPARE_NOT_EQUAL);
} else {
return ($this->token = self::TOK_NOT);
}
}
/**
* Parse several compare characters
* @return int
*/
protected function parse_compare() {
if ((($this->pos + 1) < $this->size) && ($this->doc[$this->pos + 1] === '=')) {
switch($this->doc[$this->pos++]) {
case '$':
return ($this->token = self::TOK_COMPARE_ENDS);
case '%':
return ($this->token = self::TOK_COMPARE_REGEX);
case '^':
return ($this->token = self::TOK_COMPARE_STARTS);
case '<':
return ($this->token = self::TOK_COMPARE_SMALLER_THAN);
}
}
return false;
}
/**
* Parse strings (" and ')
* @return int
*/
protected function parse_string() {
$char = $this->doc[$this->pos];
while (true) {
if ($this->next_search($char.'\\', false) !== self::TOK_NULL) {
if($this->doc[$this->pos] === $char) {
break;
} else {
++$this->pos;
}
} else {
$this->pos = $this->size - 1;
break;
}
}
return ($this->token = self::TOK_STRING);
}
}
/**
* Performs a css select query on HTML nodes
*/
class HtmlSelector {
/**
* Parser object
* @internal If string, then it will create a new instance as parser
* @var CSSQueryTokenizer
*/
var $parser = 'pQuery\\CSSQueryTokenizer';
/**
* Target of queries
* @var DomNode
*/
var $root = null;
/**
* Last performed query, result in {@link $result}
* @var string
*/
var $query = '';
/**
* Array of matching nodes
* @var array
*/
var $result = array();
/**
* Include root in search, if false the only child nodes are evaluated
* @var bool
*/
var $search_root = false;
/**
* Search recursively
* @var bool
*/
var $search_recursive = true;
/**
* Extra function map for custom filters
* @var array
* @internal array('root' => 'filter_root') will cause the
* selector to call $this->filter_root at :root
* @see DomNode::$filter_map
*/
var $custom_filter_map = array();
/**
* Class constructor
* @param DomNode $root {@link $root}
* @param string $query
* @param bool $search_root {@link $search_root}
* @param bool $search_recursive {@link $search_recursive}
* @param CSSQueryTokenizer $parser If null, then default class will be used
*/
function __construct($root, $query = '*', $search_root = false, $search_recursive = true, $parser = null) {
if ($parser === null) {
$parser = new $this->parser();
}
$this->parser = $parser;
$this->root =& $root;
$this->search_root = $search_root;
$this->search_recursive = $search_recursive;
$this->select($query);
}
#php4 PHP4 class constructor compatibility
#function HtmlSelector($root, $query = '*', $search_root = false, $search_recursive = true, $parser = null) {return $this->__construct($root, $query, $search_root, $search_recursive, $parser);}
#php4e
/**
* toString method, returns {@link $query}
* @return string
* @access private
*/
function __toString() {
return $this->query;
}
/**
* Class magic invoke method, performs {@link select()}
* @return array
* @access private
*/
function __invoke($query = '*') {
return $this->select($query);
}
/**
* Perform query
* @param string $query
* @return array False on failure
*/
function select($query = '*') {
$this->parser->setDoc($query);
$this->query = $query;
return (($this->parse()) ? $this->result : false);
}
/**
* Trigger error
* @param string $error
* @internal %pos% and %tok% will be replace in string with position and token(string)
* @access private
*/
protected function error($error) {
$error = htmlentities(str_replace(
array('%tok%', '%pos%'),
array($this->parser->getTokenString(), (int) $this->parser->getPos()),
$error
));
trigger_error($error);
}
/**
* Get identifier (parse identifier or string)
* @param bool $do_error Error on failure
* @return string False on failure
* @access private
*/
protected function parse_getIdentifier($do_error = true) {
$p =& $this->parser;
$tok = $p->token;
if ($tok === CSSQueryTokenizer::TOK_IDENTIFIER) {
return $p->getTokenString();
} elseif($tok === CSSQueryTokenizer::TOK_STRING) {
return str_replace(array('\\\'', '\\"', '\\\\'), array('\'', '"', '\\'), $p->getTokenString(1, -1));
} elseif ($do_error) {
$this->error('Expected identifier at %pos%!');
}
return false;
}
/**
* Get query conditions (tag, attribute and filter conditions)
* @return array False on failure
* @see DomNode::match()
* @access private
*/
protected function parse_conditions() {
$p =& $this->parser;
$tok = $p->token;
if ($tok === CSSQueryTokenizer::TOK_NULL) {
$this->error('Invalid search pattern(1): Empty string!');
return false;
}
$conditions_all = array();
//Tags
while ($tok !== CSSQueryTokenizer::TOK_NULL) {
$conditions = array('tags' => array(), 'attributes' => array());
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$tok = $p->next();
if (($tok === CSSQueryTokenizer::TOK_PIPE) && ($tok = $p->next()) && ($tok !== CSSQueryTokenizer::TOK_ALL)) {
if (($tag = $this->parse_getIdentifier()) === false) {
return false;
}
$conditions['tags'][] = array(
'tag' => $tag,
'compare' => 'name'
);
$tok = $p->next_no_whitespace();
} else {
$conditions['tags'][''] = array(
'tag' => '',
'match' => false
);
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$tok = $p->next_no_whitespace();
}
}
} elseif ($tok === CSSQueryTokenizer::TOK_PIPE) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$conditions['tags'][] = array(
'tag' => '',
'compare' => 'namespace',
);
} elseif (($tag = $this->parse_getIdentifier()) !== false) {
$conditions['tags'][] = array(
'tag' => $tag,
'compare' => 'total',
);
} else {
return false;
}
$tok = $p->next_no_whitespace();
} elseif ($tok === CSSQueryTokenizer::TOK_BRACE_OPEN) {
$tok = $p->next_no_whitespace();
$last_mode = 'or';
while (true) {
$match = true;
$compare = 'total';
if ($tok === CSSQueryTokenizer::TOK_NOT) {
$match = false;
$tok = $p->next_no_whitespace();
}
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_PIPE) {
$this->next();
$compare = 'name';
if (($tag = $this->parse_getIdentifier()) === false) {
return false;
}
}
} elseif ($tok === CSSQueryTokenizer::TOK_PIPE) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$tag = '';
$compare = 'namespace';
} elseif (($tag = $this->parse_getIdentifier()) === false) {
return false;
}
$tok = $p->next_no_whitespace();
} else {
if (($tag = $this->parse_getIdentifier()) === false) {
return false;
}
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_PIPE) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$compare = 'namespace';
} elseif (($tag_name = $this->parse_getIdentifier()) !== false) {
$tag = $tag.':'.$tag_name;
} else {
return false;
}
$tok = $p->next_no_whitespace();
}
}
if ($tok === CSSQueryTokenizer::TOK_WHITESPACE) {
$tok = $p->next_no_whitespace();
}
$conditions['tags'][] = array(
'tag' => $tag,
'match' => $match,
'operator' => $last_mode,
'compare' => $compare
);
switch($tok) {
case CSSQueryTokenizer::TOK_COMMA:
$tok = $p->next_no_whitespace();
$last_mode = 'or';
continue 2;
case CSSQueryTokenizer::TOK_PLUS:
$tok = $p->next_no_whitespace();
$last_mode = 'and';
continue 2;
case CSSQueryTokenizer::TOK_BRACE_CLOSE:
$tok = $p->next();
break 2;
default:
$this->error('Expected closing brace or comma at pos %pos%!');
return false;
}
}
} elseif (($tag = $this->parse_getIdentifier(false)) !== false) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_PIPE) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$conditions['tags'][] = array(
'tag' => $tag,
'compare' => 'namespace'
);
} elseif (($tag_name = $this->parse_getIdentifier()) !== false) {
$tag = $tag.':'.$tag_name;
$conditions['tags'][] = array(
'tag' => $tag,
'match' => true
);
} else {
return false;
}
$tok = $p->next();
} elseif ($tag === 'text' && $tok === CSSQueryTokenizer::TOK_BRACE_OPEN) {
$pos = $p->getPos();
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_BRACE_CLOSE) {
$conditions['tags'][] = array(
'tag' => '~text~',
'match' => true
);
$p->next();
} else {
$p->setPos($pos);
}
} else {
$conditions['tags'][] = array(
'tag' => $tag,
'match' => true
);
}
} else {
unset($conditions['tags']);
}
//Class
$last_mode = 'or';
if ($tok === CSSQueryTokenizer::TOK_CLASS) {
$p->next();
if (($class = $this->parse_getIdentifier()) === false) {
return false;
}
$conditions['attributes'][] = array(
'attribute' => 'class',
'operator_value' => 'contains_word',
'value' => $class,
'operator_result' => $last_mode
);
$last_mode = 'and';
$tok = $p->next();
}
//ID
if ($tok === CSSQueryTokenizer::TOK_ID) {
$p->next();
if (($id = $this->parse_getIdentifier()) === false) {
return false;
}
$conditions['attributes'][] = array(
'attribute' => 'id',
'operator_value' => 'equals',
'value' => $id,
'operator_result' => $last_mode
);
$last_mode = 'and';
$tok = $p->next();
}
//Attributes
if ($tok === CSSQueryTokenizer::TOK_BRACKET_OPEN) {
$tok = $p->next_no_whitespace();
while (true) {
$match = true;
$compare = 'total';
if ($tok === CSSQueryTokenizer::TOK_NOT) {
$match = false;
$tok = $p->next_no_whitespace();
}
if ($tok === CSSQueryTokenizer::TOK_ALL) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_PIPE) {
$tok = $p->next();
if (($attribute = $this->parse_getIdentifier()) === false) {
return false;
}
$compare = 'name';
$tok = $p->next();
} else {
$this->error('Expected pipe at pos %pos%!');
return false;
}
} elseif ($tok === CSSQueryTokenizer::TOK_PIPE) {
$tok = $p->next();
if (($tag = $this->parse_getIdentifier()) === false) {
return false;
}
$tok = $p->next_no_whitespace();
} elseif (($attribute = $this->parse_getIdentifier()) !== false) {
$tok = $p->next();
if ($tok === CSSQueryTokenizer::TOK_PIPE) {
$tok = $p->next();
if (($attribute_name = $this->parse_getIdentifier()) !== false) {
$attribute = $attribute.':'.$attribute_name;
} else {
return false;
}
$tok = $p->next();
}
} else {
return false;
}
if ($tok === CSSQueryTokenizer::TOK_WHITESPACE) {
$tok = $p->next_no_whitespace();
}
$operator_value = '';
$val = '';
switch($tok) {
case CSSQueryTokenizer::TOK_COMPARE_PREFIX:
case CSSQueryTokenizer::TOK_COMPARE_CONTAINS:
case CSSQueryTokenizer::TOK_COMPARE_CONTAINS_WORD:
case CSSQueryTokenizer::TOK_COMPARE_ENDS:
case CSSQueryTokenizer::TOK_COMPARE_EQUALS:
case CSSQueryTokenizer::TOK_COMPARE_NOT_EQUAL:
case CSSQueryTokenizer::TOK_COMPARE_REGEX:
case CSSQueryTokenizer::TOK_COMPARE_STARTS:
case CSSQueryTokenizer::TOK_COMPARE_BIGGER_THAN:
case CSSQueryTokenizer::TOK_COMPARE_SMALLER_THAN:
$operator_value = $p->getTokenString(($tok === CSSQueryTokenizer::TOK_COMPARE_EQUALS) ? 0 : -1);
$p->next_no_whitespace();
if (($val = $this->parse_getIdentifier()) === false) {
return false;
}
$tok = $p->next_no_whitespace();
break;
}
if ($operator_value && $val) {
$conditions['attributes'][] = array(
'attribute' => $attribute,
'operator_value' => $operator_value,
'value' => $val,
'match' => $match,
'operator_result' => $last_mode,
'compare' => $compare
);
} else {
$conditions['attributes'][] = array(
'attribute' => $attribute,
'value' => $match,
'operator_result' => $last_mode,
'compare' => $compare
);
}
switch($tok) {
case CSSQueryTokenizer::TOK_COMMA:
$tok = $p->next_no_whitespace();
$last_mode = 'or';
continue 2;
case CSSQueryTokenizer::TOK_PLUS:
$tok = $p->next_no_whitespace();
$last_mode = 'and';
continue 2;
case CSSQueryTokenizer::TOK_BRACKET_CLOSE:
$tok = $p->next();
break 2;
default:
$this->error('Expected closing bracket or comma at pos %pos%!');
return false;
}
}
}
if (count($conditions['attributes']) < 1) {
unset($conditions['attributes']);
}
while($tok === CSSQueryTokenizer::TOK_COLON) {
if (count($conditions) < 1) {
$conditions['tags'] = array(array(
'tag' => '',
'match' => false
));
}
$tok = $p->next();
if (($filter = $this->parse_getIdentifier()) === false) {
return false;
}
if (($tok = $p->next()) === CSSQueryTokenizer::TOK_BRACE_OPEN) {
$start = $p->pos;
$count = 1;
while ((($tok = $p->next()) !== CSSQueryTokenizer::TOK_NULL) && !(($tok === CSSQueryTokenizer::TOK_BRACE_CLOSE) && (--$count === 0))) {
if ($tok === CSSQueryTokenizer::TOK_BRACE_OPEN) {
++$count;
}
}
if ($tok !== CSSQueryTokenizer::TOK_BRACE_CLOSE) {
$this->error('Expected closing brace at pos %pos%!');
return false;
}
$len = $p->pos - 1 - $start;
$params = (($len > 0) ? substr($p->doc, $start + 1, $len) : '');
$tok = $p->next();
} else {
$params = '';
}
$conditions['filters'][] = array('filter' => $filter, 'params' => $params);
}
if (count($conditions) < 1) {
$this->error('Invalid search pattern(2): No conditions found!');
return false;
}
$conditions_all[] = $conditions;
if ($tok === CSSQueryTokenizer::TOK_WHITESPACE) {
$tok = $p->next_no_whitespace();
}
if ($tok === CSSQueryTokenizer::TOK_COMMA) {
$tok = $p->next_no_whitespace();
continue;
} else {
break;
}
}
return $conditions_all;
}
/**
* Evaluate root node using custom callback
* @param array $conditions {@link parse_conditions()}
* @param bool|int $recursive
* @param bool $check_root
* @return array
* @access private
*/
protected function parse_callback($conditions, $recursive = true, $check_root = false) {
return ($this->result = $this->root->getChildrenByMatch(
$conditions,
$recursive,
$check_root,
$this->custom_filter_map
));
}
/**
* Parse first bit of query, only root node has to be evaluated now
* @param bool|int $recursive
* @return bool
* @internal Result of query is set in {@link $result}
* @access private
*/
protected function parse_single($recursive = true) {
if (($c = $this->parse_conditions()) === false) {
return false;
}
$this->parse_callback($c, $recursive, $this->search_root);
return true;
}
/**
* Evaluate sibling nodes
* @return bool
* @internal Result of query is set in {@link $result}
* @access private
*/
protected function parse_adjacent() {
$tmp = $this->result;
$this->result = array();
if (($c = $this->parse_conditions()) === false) {
return false;
}
foreach($tmp as $t) {
if (($sibling = $t->getNextSibling()) !== false) {
if ($sibling->match($c, true, $this->custom_filter_map)) {
$this->result[] = $sibling;
}
}
}
return true;
}
/**
* Evaluate {@link $result}
* @param bool $parent Evaluate parent nodes
* @param bool|int $recursive
* @return bool
* @internal Result of query is set in {@link $result}
* @access private
*/
protected function parse_result($parent = false, $recursive = true) {
$tmp = $this->result;
$tmp_res = array();
if (($c = $this->parse_conditions()) === false) {
return false;
}
foreach(array_keys($tmp) as $t) {
$this->root = (($parent) ? $tmp[$t]->parent : $tmp[$t]);
$this->parse_callback($c, $recursive);
foreach(array_keys($this->result) as $r) {
if (!in_array($this->result[$r], $tmp_res, true)) {
$tmp_res[] = $this->result[$r];
}
}
}
$this->result = $tmp_res;
return true;
}
/**
* Parse full query
* @return bool
* @internal Result of query is set in {@link $result}
* @access private
*/
protected function parse() {
$p =& $this->parser;
$p->setPos(0);
$this->result = array();
if (!$this->parse_single()) {
return false;
}
while (count($this->result) > 0) {
switch($p->token) {
case CSSQueryTokenizer::TOK_CHILD:
$this->parser->next_no_whitespace();
if (!$this->parse_result(false, 1)) {
return false;
}
break;
case CSSQueryTokenizer::TOK_SIBLING:
$this->parser->next_no_whitespace();
if (!$this->parse_result(true, 1)) {
return false;
}
break;
case CSSQueryTokenizer::TOK_PLUS:
$this->parser->next_no_whitespace();
if (!$this->parse_adjacent()) {
return false;
}
break;
case CSSQueryTokenizer::TOK_ALL:
case CSSQueryTokenizer::TOK_IDENTIFIER:
case CSSQueryTokenizer::TOK_STRING:
case CSSQueryTokenizer::TOK_BRACE_OPEN:
case CSSQueryTokenizer::TOK_BRACKET_OPEN:
case CSSQueryTokenizer::TOK_ID:
case CSSQueryTokenizer::TOK_CLASS:
case CSSQueryTokenizer::TOK_COLON:
if (!$this->parse_result()) {
return false;
}
break;
case CSSQueryTokenizer::TOK_NULL:
break 2;
default:
$this->error('Invalid search pattern(3): No result modifier found!');
return false;
}
}
return true;
}
}
?>