-
Notifications
You must be signed in to change notification settings - Fork 25
/
gan_node_html.php
2855 lines (2580 loc) · 74 KB
/
gan_node_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
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
/**
* @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;
/**
* Holds (x)html/xml tag information like tag name, attributes,
* parent, children, self close, etc.
*
*/
class DomNode implements IQuery {
/**
* Element Node, used for regular elements
*/
const NODE_ELEMENT = 0;
/**
* Text Node
*/
const NODE_TEXT = 1;
/**
* Comment Node
*/
const NODE_COMMENT = 2;
/**
* Conditional Node (<![if]> <![endif])
*/
const NODE_CONDITIONAL = 3;
/**
* CDATA Node (<![CDATA[]]>
*/
const NODE_CDATA = 4;
/**
* Doctype Node
*/
const NODE_DOCTYPE = 5;
/**
* XML Node, used for tags that start with ?, like <?xml and <?php
*/
const NODE_XML = 6;
/**
* ASP Node
*/
const NODE_ASP = 7;
#php4 Compatibility with PHP4, this gets changed to a regular var in release tool
#static $NODE_TYPE = self::NODE_ELEMENT;
#php4e
#php5
/**
* Node type of class
*/
const NODE_TYPE = self::NODE_ELEMENT;
#php5e
/**
* Name of the selector class
* @var string
* @see select()
*/
var $selectClass = 'pQuery\\HtmlSelector';
/**
* Name of the parser class
* @var string
* @see setOuterText()
* @see setInnerText()
*/
var $parserClass = 'pQuery\\Html5Parser';
/**
* Name of the class used for {@link addChild()}
* @var string
*/
var $childClass = __CLASS__;
/**
* Name of the class used for {@link addText()}
* @var string
*/
var $childClass_Text = 'pQuery\\TextNode';
/**
* Name of the class used for {@link addComment()}
* @var string
*/
var $childClass_Comment = 'pQuery\\CommentNode';
/**
* Name of the class used for {@link addContional()}
* @var string
*/
var $childClass_Conditional = 'pQuery\\ConditionalTagNode';
/**
* Name of the class used for {@link addCDATA()}
* @var string
*/
var $childClass_CDATA = 'pQuery\\CdataNode';
/**
* Name of the class used for {@link addDoctype()}
* @var string
*/
var $childClass_Doctype = 'pQuery\\DoctypeNode';
/**
* Name of the class used for {@link addXML()}
* @var string
*/
var $childClass_XML = 'pQuery\\XmlNode';
/**
* Name of the class used for {@link addASP()}
* @var string
*/
var $childClass_ASP = 'pQuery\\AspEmbeddedNode';
/**
* Parent node, null if none
* @var DomNode
* @see changeParent()
*/
var $parent = null;
/**
* Attributes of node
* @var array
* @internal array('attribute' => 'value')
* @internal Public for faster access!
* @see getAttribute()
* @see setAttribute()
* @access private
*/
var $attributes = array();
/**
* Namespace info for attributes
* @var array
* @internal array('tag' => array(array('ns', 'tag', 'ns:tag', index)))
* @internal Public for easy outside modifications!
* @see findAttribute()
* @access private
*/
var $attributes_ns = null;
/**
* Array of child nodes
* @var array
* @internal Public for faster access!
* @see childCount()
* @see getChild()
* @see addChild()
* @see deleteChild()
* @access private
*/
var $children = array();
/**
* Full tag name (including namespace)
* @var string
* @see getTagName()
* @see getNamespace()
*/
var $tag = '';
/**
* Namespace info for tag
* @var array
* @internal array('namespace', 'tag')
* @internal Public for easy outside modifications!
* @access private
*/
var $tag_ns = null;
/**
* Is node a self closing node? No closing tag if true.
* @var bool
*/
var $self_close = false;
/**
* If self close, then this will be used to close the tag
* @var string
* @see $self_close
*/
var $self_close_str = ' /';
/**
* Use short tags for attributes? If true, then attributes
* with values equal to the attribute name will not output
* the value, e.g. selected="selected" will be selected.
* @var bool
*/
var $attribute_shorttag = true;
/**
* Function map used for the selector filter
* @var array
* @internal array('root' => 'filter_root') will cause the
* selector to call $this->filter_root at :root
* @access private
*/
var $filter_map = array(
'root' => 'filter_root',
'nth-child' => 'filter_nchild',
'eq' => 'filter_nchild', //jquery (naming) compatibility
'gt' => 'filter_gt',
'lt' => 'filter_lt',
'nth-last-child' => 'filter_nlastchild',
'nth-of-type' => 'filter_ntype',
'nth-last-of-type' => 'filter_nlastype',
'odd' => 'filter_odd',
'even' => 'filter_even',
'every' => 'filter_every',
'first-child' => 'filter_first',
'last-child' => 'filter_last',
'first-of-type' => 'filter_firsttype',
'last-of-type' => 'filter_lasttype',
'only-child' => 'filter_onlychild',
'only-of-type' => 'filter_onlytype',
'empty' => 'filter_empty',
'not-empty' => 'filter_notempty',
'has-text' => 'filter_hastext',
'no-text' => 'filter_notext',
'lang' => 'filter_lang',
'contains' => 'filter_contains',
'has' => 'filter_has',
'not' => 'filter_not',
'element' => 'filter_element',
'text' => 'filter_text',
'comment' => 'filter_comment',
'checked' => 'filter_checked',
'selected' => 'filter_selected',
);
/**
* Class constructor
* @param string|array $tag Name of the tag, or array with taginfo (array(
* 'tag_name' => 'tag',
* 'self_close' => false,
* 'attributes' => array('attribute' => 'value')))
* @param DomNode $parent Parent of node, null if none
*/
function __construct($tag, $parent) {
$this->parent = $parent;
if (is_string($tag)) {
$this->tag = $tag;
} else {
$this->tag = $tag['tag_name'];
$this->self_close = $tag['self_close'];
$this->attributes = $tag['attributes'];
}
}
#php4 PHP4 class constructor compatibility
#function DomNode($tag, $parent) {return $this->__construct($tag, $parent);}
#php4e
/**
* Class destructor
* @access private
*/
function __destruct() {
$this->delete();
}
/**
* Class toString, outputs {@link $tag}
* @return string
* @access private
*/
function __toString() {
return (($this->tag === '~root~') ? $this->toString(true, true, 1) : $this->tag);
}
/**
* Class magic get method, outputs {@link getAttribute()}
* @return string
* @access private
*/
function __get($attribute) {
return $this->getAttribute($attribute);
}
/**
* Class magic set method, performs {@link setAttribute()}
* @access private
*/
function __set($attribute, $value) {
$this->setAttribute($attribute, $value);
}
/**
* Class magic isset method, returns {@link hasAttribute()}
* @return bool
* @access private
*/
function __isset($attribute) {
return $this->hasAttribute($attribute);
}
/**
* Class magic unset method, performs {@link deleteAttribute()}
* @access private
*/
function __unset($attribute) {
return $this->deleteAttribute($attribute);
}
/**
* Class magic invoke method, performs {@link query()}.
* @param string $query The css query to run on the nodes.
* @return \pQuery
*/
function __invoke($query = '*') {
return $this->query($query);
}
/**
* Returns place in document
* @return string
*/
function dumpLocation() {
return (($this->parent) ? (($p = $this->parent->dumpLocation()) ? $p.' > ' : '').$this->tag.'('.$this->typeIndex().')' : '');
}
/**
* Returns all the attributes and their values
* @return string
* @access private
*/
protected function toString_attributes() {
$s = '';
foreach($this->attributes as $a => $v) {
$s .= ' '.$a;
if ((!$this->attribute_shorttag) || ($v !== $a)) {
$quote = (strpos($v, '"') === false) ? '"' : "'";
$s .= '='.$quote.$v.$quote;
}
}
return $s;
}
/**
* Returns the content of the node (child tags and text)
* @param bool $attributes Print attributes of child tags
* @param bool|int $recursive How many sublevels of childtags to print. True for all.
* @param bool $content_only Only print text, false will print tags too.
* @return string
* @access private
*/
protected function toString_content($attributes = true, $recursive = true, $content_only = false) {
$s = '';
foreach($this->children as $c) {
$s .= $c->toString($attributes, $recursive, $content_only);
}
return $s;
}
/**
* Returns the node as string
* @param bool $attributes Print attributes (of child tags)
* @param bool|int $recursive How many sub-levels of child tags to print. True for all.
* @param bool|int $content_only Only print text, false will print tags too.
* @return string
*/
function toString($attributes = true, $recursive = true, $content_only = false) {
if ($content_only) {
if (is_int($content_only)) {
--$content_only;
}
return $this->toString_content($attributes, $recursive, $content_only);
}
$s = '<'.$this->tag;
if ($attributes) {
$s .= $this->toString_attributes();
}
if ($this->self_close) {
$s .= $this->self_close_str.'>';
} else {
$s .= '>';
if($recursive) {
$s .= $this->toString_content($attributes);
}
$s .= '</'.$this->tag.'>';
}
return $s;
}
/**
* Similar to JavaScript outerText, will return full (html formatted) node
* @return string
*/
function getOuterText() {
return html_entity_decode($this->toString(), ENT_QUOTES);
}
/**
* Similar to JavaScript outerText, will replace node (and child nodes) with new text
* @param string $text
* @param HtmlParserBase $parser Null to auto create instance
* @return bool|array True on succeed, array with errors on failure
*/
function setOuterText($text, $parser = null) {
if (trim($text)) {
$index = $this->index();
if ($parser === null) {
$parser = new $this->parserClass();
}
$parser->setDoc($text);
$parser->parse_all();
$parser->root->moveChildren($this->parent, $index);
}
$this->delete();
return (($parser && $parser->errors) ? $parser->errors : true);
}
/**
* Return html code of node
* @internal jquery (naming) compatibility
* @param string|null $value The value to set or null to get the value.
* @see toString()
* @return string
*/
function html($value = null) {
if ($value !== null) {
$this->setInnerText($value);
}
return $this->getInnerText();
}
/**
* Similar to JavaScript innerText, will return (html formatted) content
* @return string
*/
function getInnerText() {
return html_entity_decode($this->toString(true, true, 1), ENT_QUOTES);
}
/**
* Similar to JavaScript innerText, will replace child nodes with new text
* @param string $text
* @param HtmlParserBase $parser Null to auto create instance
* @return bool|array True on succeed, array with errors on failure
*/
function setInnerText($text, $parser = null) {
$this->clear();
if (trim($text)) {
if ($parser === null) {
$parser = new $this->parserClass();
}
$parser->root =& $this;
$parser->setDoc($text);
$parser->parse_all();
}
return (($parser && $parser->errors) ? $parser->errors : true);
}
/**
* Similar to JavaScript plainText, will return text in node (and subnodes)
* @return string
*/
function getPlainText() {
return preg_replace('`\s+`', ' ', html_entity_decode($this->toString(true, true, true), ENT_QUOTES));
}
/**
* Return plaintext taking document encoding into account
* @return string
*/
function getPlainTextUTF8() {
$txt = $this->toString(true, true, true);
$enc = $this->getEncoding();
if ($enc !== false) {
$txt = mb_convert_encoding($txt, 'UTF-8', $enc);
}
return preg_replace('`\s+`', ' ', html_entity_decode($txt, ENT_QUOTES, 'UTF-8'));
}
/**
* Similar to JavaScript plainText, will replace child nodes with new text (literal)
* @param string $text
*/
function setPlainText($text) {
$this->clear();
if (trim($text)) {
$this->addText(htmlentities($text, ENT_QUOTES));
}
}
/**
* Delete node from parent and clear node
*/
function delete() {
if (($p = $this->parent) !== null) {
$this->parent = null;
$p->deleteChild($this);
} else {
$this->clear();
}
}
/**
* Detach node from parent
* @param bool $move_children_up Only detach current node and replace it with child nodes
* @internal jquery (naming) compatibility
* @see delete()
*/
function detach($move_children_up = false) {
if (($p = $this->parent) !== null) {
$index = $this->index();
$this->parent = null;
if ($move_children_up) {
$this->moveChildren($p, $index);
}
$p->deleteChild($this, true);
}
}
/**
* Deletes all child nodes from node
*/
function clear() {
foreach($this->children as $c) {
$c->parent = null;
$c->delete();
}
$this->children = array();
}
/**
* Get top parent
* @return DomNode Root, null if node has no parent
*/
function getRoot() {
$r = $this->parent;
$n = ($r === null) ? null : $r->parent;
while ($n !== null) {
$r = $n;
$n = $r->parent;
}
return $r;
}
/**
* Change parent
* @param null|DomNode $to New parent, null if none
* @param false|int $index Add child to parent if not present at index, false to not add, negative to count from end, null to append
*/
#php4
#function changeParent($to, &$index) {
#php4e
#php5
function changeParent($to, &$index = null) {
#php5e
if ($this->parent !== null) {
$this->parent->deleteChild($this, true);
}
$this->parent = $to;
if ($index !== false) {
$new_index = $this->index();
if (!(is_int($new_index) && ($new_index >= 0))) {
$this->parent->addChild($this, $index);
}
}
}
/**
* Find out if node has (a certain) parent
* @param DomNode|string $tag Match against parent, string to match tag, object to fully match node, null to return if node has parent
* @param bool $recursive
* @return bool
*/
function hasParent($tag = null, $recursive = false) {
if ($this->parent !== null) {
if ($tag === null) {
return true;
} elseif (is_string($tag)) {
return (($this->parent->tag === $tag) || ($recursive && $this->parent->hasParent($tag)));
} elseif (is_object($tag)) {
return (($this->parent === $tag) || ($recursive && $this->parent->hasParent($tag)));
}
}
return false;
}
/**
* Find out if node is parent of a certain tag
* @param DomNode|string $tag Match against parent, string to match tag, object to fully match node
* @param bool $recursive
* @return bool
* @see hasParent()
*/
function isParent($tag, $recursive = false) {
return ($this->hasParent($tag, $recursive) === ($tag !== null));
}
/**
* Find out if node is text
* @return bool
*/
function isText() {
return false;
}
/**
* Find out if node is comment
* @return bool
*/
function isComment() {
return false;
}
/**
* Find out if node is text or comment node
* @return bool
*/
function isTextOrComment() {
return false;
}
/**
* Move node to other node
* @param DomNode $to New parent, null if none
* @param int $new_index Add child to parent at index if not present, null to not add, negative to count from end
* @internal Performs {@link changeParent()}
*/
#php4
#function move($to, &$new_index) {
#php4e
#php5
function move($to, &$new_index = -1) {
#php5e
$this->changeParent($to, $new_index);
}
/**
* Move child nodes to other node
* @param DomNode $to New parent, null if none
* @param int $new_index Add child to new node at index if not present, null to not add, negative to count from end
* @param int $start Index from child node where to start wrapping, 0 for first element
* @param int $end Index from child node where to end wrapping, -1 for last element
*/
#php4
#function moveChildren($to, &$new_index, $start = 0, $end = -1) {
#php4e
#php5
function moveChildren($to, &$new_index = -1, $start = 0, $end = -1) {
#php5e
if ($end < 0) {
$end += count($this->children);
}
for ($i = $start; $i <= $end; $i++) {
$this->children[$start]->changeParent($to, $new_index);
}
}
/**
* Index of node in parent
* @param bool $count_all True to count all tags, false to ignore text and comments
* @return int -1 if not found
*/
function index($count_all = true) {
if (!$this->parent) {
return -1;
} elseif ($count_all) {
return $this->parent->findChild($this);
} else{
$index = -1;
//foreach($this->parent->children as &$c) {
// if (!$c->isTextOrComment()) {
// ++$index;
// }
// if ($c === $this) {
// return $index;
// }
//}
foreach(array_keys($this->parent->children) as $k) {
if (!$this->parent->children[$k]->isTextOrComment()) {
++$index;
}
if ($this->parent->children[$k] === $this) {
return $index;
}
}
return -1;
}
}
/**
* Change index of node in parent
* @param int $index New index
*/
function setIndex($index) {
if ($this->parent) {
if ($index > $this->index()) {
--$index;
}
$this->delete();
$this->parent->addChild($this, $index);
}
}
/**
* Index of all similar nodes in parent
* @return int -1 if not found
*/
function typeIndex() {
if (!$this->parent) {
return -1;
} else {
$index = -1;
//foreach($this->parent->children as &$c) {
// if (strcasecmp($this->tag, $c->tag) === 0) {
// ++$index;
// }
// if ($c === $this) {
// return $index;
// }
//}
foreach(array_keys($this->parent->children) as $k) {
if (strcasecmp($this->tag, $this->parent->children[$k]->tag) === 0) {
++$index;
}
if ($this->parent->children[$k] === $this) {
return $index;
}
}
return -1;
}
}
/**
* Calculate indent of node (number of parent tags - 1)
* @return int
*/
function indent() {
return (($this->parent) ? $this->parent->indent() + 1 : -1);
}
/**
* Get sibling node
* @param int $offset Offset from current node
* @return DomNode Null if not found
*/
function getSibling($offset = 1) {
$index = $this->index() + $offset;
if (($index >= 0) && ($index < $this->parent->childCount())) {
return $this->parent->getChild($index);
} else {
return null;
}
}
/**
* Get node next to current
* @param bool $skip_text_comments
* @return DomNode Null if not found
* @see getSibling()
* @see getPreviousSibling()
*/
function getNextSibling($skip_text_comments = true) {
$offset = 1;
while (($n = $this->getSibling($offset)) !== null) {
if ($skip_text_comments && ($n->tag[0] === '~')) {
++$offset;
} else {
break;
}
}
return $n;
}
/**
* Get node previous to current
* @param bool $skip_text_comments
* @return DomNode Null if not found
* @see getSibling()
* @see getNextSibling()
*/
function getPreviousSibling($skip_text_comments = true) {
$offset = -1;
while (($n = $this->getSibling($offset)) !== null) {
if ($skip_text_comments && ($n->tag[0] === '~')) {
--$offset;
} else {
break;
}
}
return $n;
}
/**
* Get namespace of node
* @return string
* @see setNamespace()
*/
function getNamespace() {
if ($this->tag_ns === null) {
$a = explode(':', $this->tag, 2);
if (empty($a[1])) {
$this->tag_ns = array('', $a[0]);
} else {
$this->tag_ns = array($a[0], $a[1]);
}
}
return $this->tag_ns[0];
}
/**
* Set namespace of node
* @param string $ns
* @see getNamespace()
*/
function setNamespace($ns) {
if ($this->getNamespace() !== $ns) {
$this->tag_ns[0] = $ns;
$this->tag = $ns.':'.$this->tag_ns[1];
}
}
/**
* Get tagname of node (without namespace)
* @return string
* @see setTag()
*/
function getTag() {
if ($this->tag_ns === null) {
$this->getNamespace();
}
return $this->tag_ns[1];
}
/**
* Set tag (with or without namespace)
* @param string $tag
* @param bool $with_ns Does $tag include namespace?
* @see getTag()
*/
function setTag($tag, $with_ns = false) {
$with_ns = $with_ns || (strpos($tag, ':') !== false);
if ($with_ns) {
$this->tag = $tag;
$this->tag_ns = null;
} elseif ($this->getTag() !== $tag) {
$this->tag_ns[1] = $tag;
$this->tag = (($this->tag_ns[0]) ? $this->tag_ns[0].':' : '').$tag;
}
}
/**
* Try to determine the encoding of the current tag
* @return string|bool False if encoding could not be found
*/
function getEncoding() {
$root = $this->getRoot();
if ($root !== null) {
if ($enc = $root->select('meta[charset]', 0, true, true)) {
return $enc->getAttribute("charset");
} elseif ($enc = $root->select('"?xml"[encoding]', 0, true, true)) {
return $enc->getAttribute("encoding");
} elseif ($enc = $root->select('meta[content*="charset="]', 0, true, true)) {
$enc = $enc->getAttribute("content");
return substr($enc, strpos($enc, "charset=")+8);
}
}
return false;
}
/**
* Number of children in node
* @param bool $ignore_text_comments Ignore text/comments with calculation
* @return int
*/
function childCount($ignore_text_comments = false) {
if (!$ignore_text_comments) {
return count($this->children);
} else{
$count = 0;
//foreach($this->children as &$c) {
// if (!$c->isTextOrComment()) {
// ++$count;
// }
//}
foreach(array_keys($this->children) as $k) {
if (!$this->children[$k]->isTextOrComment()) {
++$count;
}
}
return $count;
}
}
/**
* Find node in children
* @param DomNode $child
* @return int False if not found
*/
function findChild($child) {
return array_search($child, $this->children, true);
}
/**
* Checks if node has another node as child
* @param DomNode $child
* @return bool
*/
function hasChild($child) {
return ((bool) findChild($child));
}
/**
* Get childnode
* @param int|DomNode $child Index, negative to count from end
* @param bool $ignore_text_comments Ignore text/comments with index calculation
* @return DomNode
*/
function &getChild($child, $ignore_text_comments = false) {
if (!is_int($child)) {
$child = $this->findChild($child);
} elseif ($child < 0) {
$child += $this->childCount($ignore_text_comments);
}
if ($ignore_text_comments) {
$count = 0;
$last = null;
//foreach($this->children as &$c) {
// if (!$c->isTextOrComment()) {
// if ($count++ === $child) {
// return $c;
// }
// $last = $c;
// }
//}
foreach(array_keys($this->children) as $k) {
if (!$this->children[$k]->isTextOrComment()) {
if ($count++ === $child) {
return $this->children[$k];
}
$last = $this->children[$k];
}
}
return (($child > $count) ? $last : null);
} else {
return $this->children[$child];
}
}
/**
* Add child node
* @param string|DomNode $tag Tag name or object
* @param int $offset Position to insert node, negative to count from end, null to append
* @return DomNode Added node
*/
#php4
#function &addChild($tag, &$offset) {
#php4e
#php5
function &addChild($tag, &$offset = null) {
#php5e
if (is_array($tag)) {
$tag = new $this->childClass($tag, $this);
} elseif (is_string($tag)) {
$nodes = $this->createNodes($tag);
$tag = array_shift($nodes);
if ($tag && $tag->parent !== $this) {
$index = false;
$tag->changeParent($this, $index);
}
} elseif (is_object($tag) && $tag->parent !== $this) {
$index = false; //Needs to be passed by ref
$tag->changeParent($this, $index);
}
if (is_int($offset) && ($offset < count($this->children)) && ($offset !== -1)) {
if ($offset < 0) {
$offset += count($this->children);
}
array_splice($this->children, $offset++, 0, array(&$tag));
} else {
$this->children[] =& $tag;
}
return $tag;