forked from duzun/hQuery.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hquery.php
3287 lines (3030 loc) · 113 KB
/
hquery.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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// ------------------------------------------------------------------------
/**
* An extremely fast web scraper that parses megabytes of HTML in a blink of an eye.
* PHP5+, no dependencies.
*
* PHP 5+ classes:
* hQuery
* hQuery_Element
* hQuery_Node
*
* PHP 5.3+ classes:
* duzun\hQuery
* duzun\hQuery\Element
* duzun\hQuery\Node
*
*
* API Documentation at https://duzun.github.io/hQuery.php
*
* Copyright (C) 2014-2017 Dumitru Uzun
*
* @author Dumitru Uzun (DUzun.ME)
* @license MIT
* @version 1.7.1
*/
// ------------------------------------------------------------------------
/**
* Base class for HTML Elements and Documents.
*
* @internal
*/
abstract class hQuery_Node implements Iterator, Countable {
// ------------------------------------------------------------------------
const VERSION = '1.7.1';
// ------------------------------------------------------------------------
public static $last_http_result; // Response details of last request
// ------------------------------------------------------------------------
public static $selected_doc = NULL;
// ------------------------------------------------------------------------
protected $_prop = array(); // Properties
protected $doc; // Parent doc
protected $ids; // contained elements' IDs
protected $exc; // excluded elements' IDs
// ------------------------------------------------------------------------
public $tag_map; // map tag names (eg ['b' => 'strong', 'i' => 'em'])
// ------------------------------------------------------------------------
// Memory efficiency tricks ;-)
static $_ar_ = array() ;
static $_mi_ = PHP_INT_MAX ;
static $_nl_ = NULL ;
static $_fl_ = false ;
static $_tr_ = true ;
// ------------------------------------------------------------------------
protected function __construct($doc, $ids, $is_ctx=false) {
$this->doc = $doc;
if(is_int($ids)) $ids = array($ids => $doc->ids[$ids]);
$this->ids = $is_ctx ? $this->_ctx_ids($ids) : $ids;
if($doc === $this) { // documents have no $doc property
unset($this->doc);
self::$selected_doc = $this;
}
}
public function __destruct() {
if(self::$selected_doc === $this) self::$selected_doc = self::$_nl_;
$this->ids = self::$_nl_; // If any reference exists, destroy its contents! P.S. Might be buggy, but hey, I own this property. Sincerly yours, hQuery_Node class.
unset($this->doc, $this->ids);
}
// ------------------------------------------------------------------------
/**
* Get and attribute or all attributes of first element in the collection.
*
* @param string $attr attribute name, or NULL to get all
* @param boolean $to_str When $attr is NULL, if true, get the list of attributes as string
* @return array|string If no $attr, return a list of attributes, or attribute's value otherwise.
*/
public function attr($attr=NULL, $to_str=false) {
$k = key($this->ids);
if($k === NULL) {
reset($this->ids);
$k = key($this->ids);
}
return isset($k) ? $this->doc()->get_attr_byId($k, $attr, $to_str) : NULL;
}
// ------------------------------------------------------------------------
// Deprecated
public function is_empty() { return $this->isEmpty() ;}
public function isEmpty() {
return empty($this->ids);
}
public function isDoc() {
return !isset($this->doc) || $this === $this->doc;
}
/**
* Get parent doc of this node.
*
* @return hQuery
*/
public function doc() {
return isset($this->doc) ? $this->doc : $this;
}
/**
* Finds a collection of nodes inside current document/context (similar to jQuery.fn.find()).
*
* @param string $sel - A valid CSS selector (some pseudo-selectors supported).
* @param array|string $attr - OPTIONAL attributes as string or key-value pairs.
* @param hQuery_Node $ctx - OPTIONAL the context where to search. If omitted, $this is used.
*
* @return hQuery_Element collection of matched elements or NULL
*/
public function find($sel, $attr=NULL) {
return $this->doc()->find($sel, $attr, $this);
}
public function exclude($sel, $attr=NULL) {
$e = $this->find($sel, $attr, $this);
if($e) {
if(empty($this->exc)) {
$this->exc = $e->ids;
}
else {
// foreach($e->ids as $b => $e) $this->exc[$b] = $e;
$this->exc = $e->ids + $this->exc;
ksort($this->exc);
}
}
return $e;
}
public function __toString() {
// doc
if($this->isDoc()) return $this->html;
$ret = '';
$doc = $this->doc;
$ids = $this->ids;
if ( !empty($this->exc) ) {
$ids = array_diff_key($ids, $this->exc);
}
foreach($ids as $p => $q) {
// if(isset($this->exc, $this->exc[$p])) continue;
++$p;
if($p < $q) $ret .= substr($doc->html, $p, $q-$p);
}
return $ret;
}
// ------------------------------------------------------------------------
/**
* @return string .innerHTML
*/
public function html($id=NULL) {
if($this->isDoc()) return $this->html; // doc
$id = $this->_my_ids($id);
if($id === false) return self::$_fl_;
$doc = $this->doc;
if ( !empty($this->exc) ) {
$id = array_diff_key($id, $this->exc);
}
$ret = self::$_nl_;
foreach($id as $p => $q) {
// if(isset($this->exc, $this->exc[$p])) continue;
++$p;
if($p<$q) $ret .= substr($doc->html, $p, $q-$p);
}
return $ret;
}
/**
* @return string .outerHtml
*/
public function outerHtml($id=NULL) {
$dm = $this->isDoc() && !isset($id);
if($dm) return $this->html; // doc
$id = $this->_my_ids($id);
if($id === false) return self::$_fl_;
$doc = $this->doc();
$ret = self::$_nl_;
$map = isset($this->tag_map) ? $this->tag_map : (isset($doc->tag_map) ? $doc->tag_map : NULL);
foreach($id as $p => $q) {
$a = $doc->get_attr_byId($p, NULL, true);
$n = $doc->tags[$p];
if($map && isset($map[$_n=strtolower($n)])) $n = $map[$_n];
$h = $p++ == $q ? false : ($p<$q ? substr($doc->html, $p, $q-$p) : '');
$ret .= '<'.$n.($a?' '.$a:'') . ($h === false ? ' />' : '>' . $h . '</'.$n.'>');
}
return $ret;
}
/**
* @return string .innerText
*/
public function text($id=NULL) {
return html_entity_decode(strip_tags($this->html($id)), ENT_QUOTES);/* ??? */
}
/**
* @return string .nodeName
*/
public function nodeName($caseFolding = NULL, $id=NULL) {
if(!isset($caseFolding)) $caseFolding = hQuery_HTML_Parser::$case_folding;
$dm = $this->isDoc() && !isset($id);
if($dm) $ret = array_unique($this->tags); // doc
else {
$id = $this->_my_ids($id, true);
if($id === false) return self::$_fl_;
$ret = self::array_select($this->doc()->tags, $id);
}
if($caseFolding) {
foreach($ret as $i => $n) $ret[$i] = strtolower($n);
if($dm) $ret = array_unique($ret);
}
return count($ret) <= 1 ? reset($ret) : $ret;
}
// public function firstChild() {
// $doc = $this->doc();
// $q = reset($this->ids);
// $p = key($this->ids);
// return new hQuery_Element($doc, array($p=>$q));
// }
// public function lastChild() {
// $doc = $this->doc();
// $q = end($this->ids);
// $p = key($this->ids);
// return new hQuery_Element($doc, array($p=>$q));
// }
// ------------------------------------------------------------------------
/**
* Get string offset of the first/current element
* in the source HTML document.
*
* <div class="test"> Contents <span>of</span> #test </div>
* |
* pos()
*
* @param bool $restore - if true, restore internal pointer to previous position
*
* @return int
*/
public function pos($restore=true) {
$k = key($this->ids);
if($k === NULL) {
reset($this->ids);
$k = key($this->ids);
if($k !== NULL && $restore) {
end($this->ids);
next($this->ids);
}
}
return $k;
}
// ------------------------------------------------------------------------
/**
* Make a context array of ids:
* if x in $ids && exists y in $ids such that x in y then del x from $ids
*
* @return array ids
*/
protected function _ctx_ids($ids=NULL) {
$m = -1;
$exc = $this->exc;
if(!isset($ids)) $ids = $this->ids;
elseif(is_int($ids)) $ids = isset($this->ids[$ids]) ? array($ids => $this->ids[$ids]) : self::$_fl_;
else {
foreach($ids as $b => $e) {
if($b <= $m || $b+1 >= $e and empty($exc[$b])) unset($ids[$b]);
else $m = $e;
}
}
return $ids;
}
/**
* Get all ids from inside of this element
*
* @return array ids
*/
protected function _sub_ids($eq=false) {
$ret = array();
$ce = reset($this->ids);
$cb = key($this->ids);
$doc = $this->doc();
foreach($doc->ids as $b => $e) {
if($b < $cb || !$eq && $b == $cb) continue;
if($b < $ce) {
$ret[$b] = $e;
}
else {
$ce = next($this->ids);
if(!$ce) break; // end of context
$cb = key($this->ids);
}
}
return $ret;
}
/**
* Get and Normalize ids of $el
*
* @return array ids
*/
protected function _doc_ids($el, $force_array=true) {
if($el instanceof self) $el = $el->ids;
if($force_array) {
if(is_int($el)) $el = array($el=>$this->doc()->ids[$el]);
if(!is_array($el)) throw new Exception(__CLASS__ . '->' . __FUNCTION__ . ': not Array!');
}
return $el;
}
protected function _my_ids($id=NULL, $keys=false) {
if(!isset($id)) $id = $this->ids;
elseif(is_int($id)) {
if(!isset($this->ids[$id])) return self::$_fl_;
if($keys) return $id;
$id = array($id => $this->ids[$id]);
}
elseif(!$id) return self::$_fl_;
else ksort($id);
return $keys ? array_keys($id) : $id;
}
// ------------------------------------------------------------------------
protected function _parent($ids=NULL, $n=0) {
$ret = self::$_ar_;
$ids = $this->_my_ids($ids);
if(!$ids) return $ret;
$lb = $le = -1; // last parent
$ie = reset($ids); // current child
$ib = key($ids);
$dids = &$this->doc()->ids;
foreach($dids as $b => $e) { // $b < $ib < $e
// if current element is past current child and last parent is parent for current child
if($ib <= $b) {
if($ib < $le && $lb < $ib) {
$ret[$lb] = $le;
}
// while current element is past current child
do {
$ie = next($ids);
if($ie === false) { $ib = -1; break 2; }
$ib = key($ids);
} while($ib <= $b);
}
// here $b < $ib
if($ib < $e) { // [$b=>$e] is a parent of $ib
$lb = $b;
$le = $e;
}
}
if($ib <= $b && $ib < $le && $lb < $ib) { // while current element is past current child and last parent is parent for current child
$ret[$lb] = $le;
}
return $ret;
}
function _children($ids=NULL, $n=NULL) {
$ret = self::$_ar_;
$ids = $this->_my_ids($ids);
if(!$ids) return $ret;
$dids = &$this->doc()->ids;
$le = end($ids);
if(current($dids) === false) $ie = reset($dids);
$ib = key($dids);
foreach($ids as $b => $e) {
if($b+4 >= $e) continue; // empty tag; min 3 chars are required for a tag - eg. <b>
// child of prev element
if($b <= $le) {
while($b < $ib) if($ie = prev($dids)) $ib = key($dids); else { reset($dids); break; }
}
else {
if($ie === false && $ib < $b) break;
}
$le = $e;
while($ib <= $b) {
if($ie = next($dids)) $ib = key($dids);
else { end($dids); break; }
}
if($b < $ib) {
$i = 0;
while($ib < $e) {
if(!isset($n)) $ret[$ib] = $ie;
elseif($n == $i) { $ret[$ib] = $ie; break; }
else ++$i;
$lie = $ie < $e ? $ie : $e;
while($ib <= $lie) {
if($ie = next($dids)) $ib = key($dids);
else { end($dids); continue 3; }
}
}
}
}
return $ret;
}
function _next($ids=NULL, $n=0) {
$ret = self::$_ar_; // array()
$ids = $this->_my_ids($ids); // ids to search siblings for
if(!$ids) return $ret;
$dids = &$this->doc()->ids; // all elements in the doc
$kb = $le = self::$_mi_; // [$lb=>$le] (last parent) now is 100% parent for any element
$ke = $lb = -1; // last parent
$ie = reset($ids); // current child
$ib = key($ids);
$e = current($dids); // traverse starting from current position
if($e !== false) do { $b = key($dids); } while( ($ib <= $b || $e < $ib) && ($e = prev($dids)) ) ;
if(empty($e)) $e = reset($dids); // current element
$pt = $st = $ret; // stacks: $pt - parents, $st - siblings limits
while($e) {
$b = key($dids);
/* 4) */ if($ib <= $b) { // if current element is past our child, then its siblings context is found
if($kb < $ke) $st[$kb] = $ke;
$kb = $ie;
$ke = $le;
$ib = ($ie = next($ids)) ? key($ids) : self::$_mi_; // $ie < $ib === no more children
if($ie < $ib) break; // no more children, empty siblings context, search done!
// pop from stack, if not empty
while($le < $ib && $pt) { // if past current parent, pop another one from the stack
$le = end($pt);
unset($pt[$lb = key($pt)]); // there must be something in the stack anyway
}
}
/* 3) */ if($b < $ib && $ib < $e) { // push the parents to their stack
$pt[$lb] = $le;
$lb = $b;
$le = $e;
}
$e = next($dids);
} // while
if($ke < $kb) return $ret; // no siblings contexts found!
$st[$kb] = $ke;
ksort($st);
foreach($st as $kb => $ke) {
if($e !== false) do { $b = key($dids); } while( $kb < $b && ($e = prev($dids)) ) ;
if(empty($e)) $e = reset($dids); // current element
do {
$b = key($dids);
if($kb < $b) {
// iterate next siblings
$i = 0;
while($b < $ke) {
if($n == $i) { $ret[$b] = $e; break; } else ++$i;
$lie = $e < $ke ? $e : $ke;
while($b <= $lie && ($e = next($dids))) $b = key($dids);
if(!$e) { $e = end($dids); break; }
}
break;
}
} while($e = next($dids));
}
return $ret;
}
function _prev($ids=NULL, $n=0) {
$ret = self::$_ar_; // array()
$ids = $this->_my_ids($ids); // ids to search siblings for
if(!$ids) return $ret;
$dids = &$this->doc()->ids; // all elements in the doc
$kb = $le = self::$_mi_; // [$lb=>$le] (last parent) now is 100% parent for any element
$ke = $lb = -1; // last parent
$ie = reset($ids); // current child
$ib = key($ids);
$e = current($dids); // traverse starting from current position
if($e !== false) do {
$b = key($dids);
} while( ($ib <= $b || $e < $ib) && ($e = prev($dids)) ) ;
if(empty($e)) $e = reset($dids); // current element
$pt = $st = $ret; // stacks: $pt - parents, $st - siblings limits
while($e) {
$b = key($dids);
/* 4) */ if($ib <= $b) { // if current element is past our child, then its siblings context is found
if($kb < $ke) $st[$kb] = $ke;
$kb = $lb;
$ke = $ib;
$ib = ($ie = next($ids)) ? key($ids) : self::$_mi_; // $ie < $ib === no more children
if($ie < $ib) break; // no more children, empty siblings context, search done!
// pop from stack, if not empty
while($le < $ib && $pt) { // if past current parent, pop another one from the stack
$le = end($pt);
unset($pt[$lb = key($pt)]); // there must be something in the stack anyway
}
}
/* 3) */ if($b < $ib && $ib < $e) { // push the parents to their stack
$pt[$lb] = $le;
$lb = $b;
$le = $e;
}
$e = next($dids);
} // while
if($ke < $kb) return $ret; // no siblings contexts found!
if($e !== false) do { $b = key($dids); } while( $kb < $b && ($e = prev($dids)) ) ;
if(empty($e)) $e = reset($dids); // current element
$st[$kb] = $ke;
ksort($st);
$kb = reset($st);
$ke = key($st);
do {
$b = key($dids);
/* 1) */ if($kb < $b) {
// iterate next siblings
$pt = self::$_ar_;
$lie = -1;
while($b < $ke) {
$pt[$b] = $e;
$lie = $e < $ke ? $e : $ke;
while($b <= $lie && ($e = next($dids))) $b = key($dids);
if(!$e) { $e = end($dids); break; }
}
if($pt) {
$c = count($pt);
$i = $n < 0 ? 0 : $c;
$i -= $n + 1;
if(0 <= $i && $i < $c) {
$pt = array_slice($pt, $i, 1, true);
$ret[key($pt)] = reset($pt);
}
}
$pt = self::$_nl_;
if(empty($st)) break; // stack empty, no more children, search done!
// pop from stack, if not empty
if($kb = reset($st)) unset($st[$ke = key($st)]);
else $kb = self::$_mi_; // $ke < $kb === context empy
// rewind back
while($kb < $b && ($e = prev($dids))) $b = key($dids);
if(!$e) $e = reset($dids); // only for wrong context! - error
}
} while($e = next($dids));
return $ret;
}
function _all($ids=NULL) {
$ret = self::$_ar_;
$ids = $this->_my_ids($ids);
if(!$ids) return $ret;
return $this->doc()->_find('*', NULL, NULL, $ids);
}
/// $el < $this, with $eq == true -> $el <= $this
function _has($el, $eq=false) {
if(is_int($el)) {
$e = end($this->ids);
if($el >= $e) return self::$_fl_;
foreach($this->ids as $b => $e) {
if($el < $b) return self::$_fl_;
if($el == $b) return $eq;
if($el < $e) return self::$_tr_;
}
return self::$_fl_;
}
if($el instanceof self) { if($el === $this) return self::$_fl_; $el = $el->ids; } else
$el = $this->_ctx_ids($this->_doc_ids($el, true));
foreach($el as $b => $e) if(!$this->has($b)) return self::$_fl_;
return self::$_tr_;
}
/// filter all ids of $el that are contained in $this->ids
function _filter($el, $eq=false) {
if($el instanceof self) $o = $el;
$el = $this->_doc_ids($el);
$ret = self::$_ar_;
$lb = $le = -1; // last parent
$ie = reset($el); // current child
$ib = key($el);
foreach($this->ids as $b => $e) {
while($ib < $b || !$eq && $ib == $b) {
$ie = next($el);
if($ie === false) { $ib = -1; break 2; }
$ib = key($el);
}
// $b < $ib
while($ib < $e) {
$ret[$ib] = $ie;
$ie = next($el);
if($ie === false) { $ib = -1; break 2; }
$ib = key($el);
}
}
if(!empty($o)) {
$o = clone $o;
$o->ids = $ret;
$ret = $o;
}
return $ret;
}
// - Magic ------------------------------------------------
public function __get($name) {
if($this->_prop && array_key_exists($name, $this->_prop)) return $this->_prop[$name];
return $this->attr($name);
}
public function __set($name, $value) {
if(isset($value)) return $this->_prop[$name] = $value;
$this->__unset($name);
}
public function __isset($name) {
return isset($this->_prop[$name]);
}
public function __unset($name) {
unset($this->_prop[$name]);
}
// ------------------------------------------------------------------------
// Countable:
public function count() { return isset($this->ids) ? count($this->ids) : 0; }
// ------------------------------------------------------------------------
// Iterable:
public function current() {
$k = key($this->ids);
if($k === NULL) return false;
return array($k => $this->ids[$k]);
}
public function valid() { return current($this->ids) !== false; }
public function key() { return key($this->ids); }
public function next() { return next($this->ids) !== false ? $this->current() : false; }
public function prev() { return prev($this->ids) !== false ? $this->current() : false; }
public function rewind() { reset($this->ids); return $this->current(); }
// - Helpers ------------------------------------------------
/**
* Normalize a CSS selector pseudo-class string.
* ( int, string or array(name => value) )
*
* @return int|array
*
* @internal
*/
static function html_normal_pseudoClass($p) {
if(is_int($p)) return $p;
$i = (int)$p;
if((string)$i === $p) return $i;
static $map = array(
'lt' => '<',
'gt' => '>',
'prev' => '-',
'next' => '+',
'parent' => '|',
'children' => '*',
'*' => '*'
);
$p = explode('(', $p, 2);
$p[1] = isset($p[1]) ? trim(rtrim($p[1], ')')) : NULL;
switch($p[0]) {
case 'first' :
case 'first-child': return 0;
case 'last' :
case 'last-child' : return -1;
case 'eq' : return (int)$p[1];
default:
if(isset($map[$p[0]])) {
$p[0] = $map[$p[0]];
if(isset($p[1])) $p[1] = (int)$p[1];
}
else {
// ??? unknown ps
}
}
return array($p[0]=>$p[1]);
}
// ------------------------------------------------------------------------
/**
* Parse a selector string into an array structure.
*
* tn1#id1 .cl1.cl2:first tn2:5 , tn3.cl3 tn4#id2:eq(-1) > tn5:last-child > tn6:lt(3)
* -->
* [
* [
* [{ n: "tn1", i: "id1", c: [], p: [] }],
* [{ n: NULL, i: NULL, c: ["cl1","cl2"], p: [0] }],
* [{ n: "tn2", i: NULL, c: [], p: [5] }]
* ]
* , [
* [{ n: "tn3", i: NULL, c: ["cl3"], p: [] }],
* [
* { n: "tn4", i: "id2", c: [], p: [-1] },
* { n: "tn5", i: NULL , c: [], p: [-1] },
* { n: "tn6", i: NULL , c: [], p: ["<3"] }
* ]
* ]
* ]
*
* @return array
*
* @internal
*/
static function html_selector2struc($sel) {
$sc = '#.:';
$n = NULL;
$a = array();
$def = array('n'=>$n, 'i'=>$n, 'c'=>$a, 'p'=>$a);
$sel = rtrim(trim(preg_replace('/\\s*(>|,)\\s*/', '$1', $sel), " \t\n\r,>"), $sc);
$sel = explode(',', $sel);
foreach($sel as &$a) {
$a = preg_split('|\\s+|', $a);
foreach($a as &$b) {
$b = explode('>', $b);
foreach($b as &$c) {
$d = $def;
$l = strlen($c);
$j = strcspn($c, $sc, 0, $l);
if($j) $d['n'] = substr($c, 0, $j);
$i = $j;
while($i<$l) {
$k = $c[$i++];
$j = strcspn($c, $sc, $i, $l);
if($j) {
$e = substr($c, $i, $j);
switch($k) {
case '.': $d['c'][] = $e; break;
case '#': $d['i'] = $e; break;
case ':': $d['p'][] = self::html_normal_pseudoClass($e); break;
}
$i+=$j;
}
}
if(empty($d['c'])) $d['c'] = $n;
if(empty($d['p'])) $d['p'] = $n;
$c = $d;
}
}
}
return $sel;
}
// ------------------------------------------------------------------------
protected static function html_findTagClose($str, $p) {
$l = strlen($str);
while ( $i = $p < $l ? strpos($str, '>', $p) : $l ) {
$e = $p; // save pos
$p += strcspn($str, '"\'', $p, $i);
// If closest quote is after '>', return pos of '>'
if ( $p >= $i ) return $i;
// If there is any quote before '>', make sure '>' is outside an attribute string
$q = $str[$p]; // " | ' ?
++$p; // next char after the quote
$e += strcspn($str, '=', $e, $p); // is there a '=' before first quote?
// is this the attr_name (like in "attr_name"="attr_value") ?
if ( $e >= $p ) {
// Attribute name should not have '>'
$p += strcspn($str, '>' . $q, $p, $l);
// but if it has '>', it is the tag closing char
if ( $str[$p] == '>' ) return $p;
}
// else, its attr_value
else {
$p += strcspn($str, $q, $p, $l);
}
++$p; // next char after the quote
}
return $i;
}
// ------------------------------------------------------------------------
static function html_parseAttrStr($str, $case_folding = true, $extended = false) {
static $_attrName_firstLet = NULL;
if(!$_attrName_firstLet) $_attrName_firstLet = self::str_range('a-zA-Z_');
$ret = array();
for($i = strspn($str, " \t\n\r"), $len = strlen($str); $i < $len;) {
$i += strcspn($str, $_attrName_firstLet, $i);
if($i>=$len) break;
$b = $i;
$i += strcspn($str, " \t\n\r=\"\'", $i);
$attrName = rtrim(substr($str, $b, $i-$b));
if($case_folding) $attrName = strtolower($attrName);
$i += strspn($str, " \t\n\r", $i);
$attrValue = NULL;
if($i<$len && $str[$i]=='=') {
++$i;
$i += strspn($str, " \t\n\r", $i);
if($i < $len) {
$q = substr($str, $i, 1);
if($q=='"' || $q=="'") {
$b = ++$i;
$e = strpos($str, $q, $i);
if($e !== false) {
$attrValue = substr($str, $b, $e-$b);
$i = $e+1;
}
else {
/*??? no closing quote */
}
}
else {
$b = $i;
$i += strcspn($str, " \t\n\r\"\'", $i);
$attrValue = substr($str, $b, $i-$b);
}
}
}
if($extended && $attrValue) switch($case_folding ? $attrName : strtolower($attrName)) {
case 'class':
$attrValue = preg_split("|\\s+|", trim($attrValue));
if(count($attrValue) == 1) $attrValue = reset($attrValue);
else sort($attrValue);
break;
case 'style':
$attrValue = self::parseCSStr($attrValue, $case_folding);
break;
}
$ret[$attrName] = $attrValue;
}
return $ret;
}
// ------------------------------------------------------------------------
static function html_attr2str($attr, $quote='"') {
$sq = htmlspecialchars($quote);
if($sq == $quote) $sq = false;
ksort($attr);
if(isset($attr['class']) && is_array($attr['class'])) {
sort($attr['class']);
$attr['class'] = implode(' ', $attr['class']);
}
if(isset($attr['style']) && is_array($attr['style'])) {
$attr['style'] = self::CSSArr2Str($attr['style']);
}
$ret = array();
foreach($attr as $n => $v) {
$ret[] = $n . '=' . $quote . ($sq ? str_replace($quote, $sq, $v) : $v) . $quote;
}
return implode(' ', $ret);
}
// ------------------------------------------------------------------------
static function parseCSStr($str, $case_folding = true) {
$ret = array();
$a = explode(';', $str); // ??? what if ; in "" ?
foreach($a as $v) {
$v = explode(':', $v, 2);
$n = trim(reset($v));
if($case_folding) $n = strtolower($n);
$ret[$n] = count($v) == 2 ? trim(end($v)) : NULL;
}
unset($ret['']);
return $ret;
}
static function CSSArr2Str($css) {
if(is_array($css)) {
ksort($css);
$ret = array();
foreach($css as $n => $v) $ret[] = $n.':'.$v;
return implode(';', $ret);
}
return $css;
}
// ------------------------------------------------------------------------
static function str_range($comp, $pos=0, $len=NULL) {
$ret = array();
$b = strlen($comp);
if(!isset($len) || $len > $b) $len = $b;
$b = "\x0";
while($pos < $len) {
switch($c = $comp[$pos++]) {
case '\\': {
$b = substr($comp, $pos, 1);
$ret[$b] = $pos++;
} break;
case '-': {
$c_ = ord($c=substr($comp, $pos, 1));
$b = ord($b);
while($b++ < $c_) $ret[chr($b)] = $pos;
while($b-- > $c_) $ret[chr($b)] = $pos;
} break;
default: {
$ret[$b=$c] = $pos;
}
}
}
return implode('', array_keys($ret));
}
// ------------------------------------------------------------------------
static function array_select($arr, $keys, $force_null=false) {
$ret = array();
is_array($keys) or is_object($keys) or $keys = array($keys);
foreach($keys as $k) {
if(isset($arr[$k])) $ret[$k] = $arr[$k];
elseif($force_null) $ret[$k] = NULL;
}
return $ret;
}
// ------------------------------------------------------------------------
static function convert_encoding($a, $to, $from=NULL) {
static $meth = NULL;
isset($meth) or $meth = function_exists('mb_convert_encoding');
isset($from) or $from = $meth ? mb_internal_encoding() : iconv_get_encoding('internal_encoding');
if(is_array($a)) {
$ret = array();
foreach($a as $n => $v) {
$ret[is_string($n)?self::convert_encoding($n,$to,$from):$n] = is_string($v) || is_array($v) || $v instanceof stdClass
? self::convert_encoding($v, $to, $from)
: $v;
}
return $ret;
}
elseif($a instanceof stdClass) {
$ret = (object)array();
foreach($a as $n => $v) {
$ret->{is_string($n)?self::convert_encoding($n,$to,$from):$n} = is_string($v) || is_array($v) || $v instanceof stdClass
? self::convert_encoding($v, $to, $from)
: $v;
}
return $ret;
}
return is_string($a) ? $meth ? mb_convert_encoding($a, $to, $from) : iconv($from, $to, $a) : $a;
}
// ------------------------------------------------------------------------
}
// ------------------------------------------------------------------------
/**
* A context is a list of node addresses with reference to their document.
*
* @internal
*/
class hQuery_Context extends hQuery_Node {
public function __construct($doc=NULL, $el_arr=NULL) {
if($el_arr instanceof parent) {
if(!$doc) $doc = $el_arr->doc();
$el_arr = $el_arr->ids;
}
elseif(is_array($el_arr)) {
ksort($el_arr);
}
parent::__construct($doc, $el_arr, true);
}
/**
* ctx($el) * $this
*
* @return hQuery_Context ctx
*/
public function intersect($el, $eq=true) {
if($el instanceof self) {
if($el === $this) {
if($eq) return $this;
else $el = array();
}
else {
$el = $el->ids;