Repository navigation
Expand file tree
/
Copy pathdocument.php-class
More file actions
executable file
·1239 lines (1199 loc) · 60.6 KB
/
Copy pathdocument.php-class
File metadata and controls
executable file
·1239 lines (1199 loc) · 60.6 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
class ExtendedDocument extends DOMDocument {
// ExtendedDocument PHP class
// this extends the general PHP DOM Document class to simplify some usual constructs
//
// function __construct([$version], [$encoding])
// CONSTRUCTOR
// construct a new DOM Document that uses our element definitions
//
// static function initHTML5([$doc])
// initialize as an HTML5 document and return references to its basic elements.
// If a $doc is handed over (an ExtendedDocument or a derived class), load the content into that document.
// returns an associative array with the following elements: 'html', 'head', 'title', 'body'
//
// public function loadHTML5($source) {
// A version of loadHTML() - see DOMDocument documentation - that is made for loading HTML5 and not emitting warnings/errors for unknown elements.
// returns true on success, false otherwise, just like loadHTML5.
//
// public function appendElement($name, [$value])
// appends a DOMDocument::createElement() as a child of this document (see there for params)
// returns the new child
//
// public function appendElementXML($name, $xmldata)
// appends a DOMDocument::createElement() with the given name as a child of this document,
// with an ExtendedDocument::createXMLFragment() of the given XML data inside
// returns the new child
//
// public function appendLink($target, [$value], [$title])
// appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
// returns the new child
//
// public function appendImage($src, [$alt_text])
// appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
// returns the new child
//
// public function appendForm($action, $method, $name, [$id])
// appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
// returns the new child
//
// public function appendFormDiv($action, $method, $name, [$id])
// appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
// returns an HTML <div> that is a child of the new child
//
// public function appendInputHidden($name, $value)
// appends an ExtendedDocument::createElementInputHidden() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputText($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputText() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputPassword() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputNumber() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputRange($name, $id, $min, $max, [$step], [$value])
// appends an ExtendedDocument::createElementInputRange() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputUrl() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputEmail() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputTel($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputTel() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputDate($name, [$id], [$min], [$max], [$value])
// appends an ExtendedDocument::createElementInputDate() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputTime($name, [$id], [$min], [$max], [$value])
// appends an ExtendedDocument::createElementInputTime() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputColor($name, [$id], [$value])
// appends an ExtendedDocument::createElementInputColor() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputRadio($name, $id, $value, $checked)
// appends an ExtendedDocument::createElementInputRadio() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputCheckbox($name, $id, $value, $checked)
// appends an ExtendedDocument::createElementInputCheckbox() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputFile($name, $id, $accept)
// appends an ExtendedDocument::createElementInputFile() as a child of this document (see there for params)
// returns the new child
//
// public function appendInputSubmit($value)
// appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
// returns the new child
//
// public function appendButton($value, $onclick = null)
// appends an ExtendedDocument::createElementButton() as a child of this document (see there for params)
// returns the new child
//
// public function appendTextArea($name, $columns, $rows, [$id], [$value])
// appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
// returns the new child
//
// public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
// appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
// returns the new child
//
// public function appendElementOption($key, $desc, [$selected])
// appends an ExtendedDocument::createElementOption() as a child of this document (see there for params)
// returns the new child
//
// public function appendLabel($for_id, $value)
// appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
// returns the new child
//
// public function appendText($text)
// appends a DOMDocument::createTextNode() as a child of this document (see there for params)
// returns the new child
//
// public function appendLinebreak()
// appends a <br> as a child of this document
// returns the new child
//
// public function appendEntity($name)
// appends a DOMDocument::createEntityReference() as a child of this document (see there for params)
// returns the new child
//
// public function appendComment($comment_data)
// appends a DOMDocument::createComment() as a child of this document (see there for params)
// returns the new child
//
// public function appendClonedElement($dom_element, [$deep])
// appends a clone of the given DOMElement as a child of this document
// the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
// returns the new child
//
// public function appendHTMLMarkup($htmldata, [$parentNode])
// appends a representation of the HTML data as children of the given parent node, by default this document
// NO return value!
//
// public function appendXMLMarkup($xmldata, [$parentNode])
// appends a representation of the XML data as children of the given parent node, by default this document
// NO return value!
//
// public function appendStyleElement($styledata)
// appends an ExtendedDocument::createElementStyle() as a child of this document (see there for params)
// returns the new child
//
// public function appendJSElement($jsdata)
// appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
// returns the new child
//
// public function appendJSFile($jsURL, [$defer], [$async])
// appends an ExtendedDocument::createElementJSFile() as a child of this document (see there for params)
// returns the new child
//
// public function createElementLink($target, [$value], [$title])
// returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value as well as (optional) title attribute
//
// public function createElementImage($src, [$alt_text])
// returns an ExtendedElement that is an HTML <img> with the given src and alt attributes (set to '' by default)
//
// public function createElementForm($action, $method, $name)
// returns an ExtendedElement that is an HTML <form> with the given action, method, and name
//
// public function createElementInputHidden($name, $value)
// returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
//
// public function createElementInputText($name, $maxlength, $size, [$id], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'text' with the given name, maxlength, size,
// and optionally id and value
//
// public function createElementInputPassword($name, $maxlength, $size, [$id], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'password' with the given name, maxlength, size,
// and optionally id and value
//
// public function createElementInputNumber($name, $maxlength, $size, [$id], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'number' with the given name, maxlength, size,
// and optionally id and value
//
// public function createElementInputRange($name, $id, $min, $max, [$step], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'range' with the given name, id, min, max,
// and optionally step and value
//
// public function createElementInputUrl($name, $maxlength, $size, [$id], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'url' with the given name, maxlength, size,
// and optionally id and value
//
// public function createElementInputEmail($name, $maxlength, $size, [$id], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'email' with the given name, maxlength, size,
// and optionally id and value
//
// public function createElementInputTel($name, $maxlength, $size, [$id], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'tel' with the given name, maxlength, size,
// and optionally id and value
//
// public function createElementInputDate($name, [$id], [$min], [$max], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'date' with the given name,
// and optionally id, min, max, and value
//
// public function createElementInputTime($name, [$id], [$min], [$max], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'time' with the given name,
// and optionally id, min, max, and value
//
// public function createElementInputColor($name, [$id], [$value])
// returns an ExtendedElement that is an HTML <input> of type 'color' with the given name,
// and optionally id and value
//
// public function createElementInputRadio($name, $id, $value, $checked)
// returns an ExtendedElement that is an HTML <input> of type 'radio' with the given name, id, value and
// checked state
//
// public function createElementInputCheckbox($name, $id, $value, $checked)
// returns an ExtendedElement that is an HTML <input> of type 'checkbox' with the given name, id, value and
// checked state
//
// public function createElementInputFile($name, $id, $accept)
// returns an ExtendedElement that is an HTML <input> of type 'file' with the given name, id and accept
//
// public function createElementInputSubmit($value)
// returns an ExtendedElement that is an HTML <input> of type 'submit' with the given value as label
//
// public function createElementButton($value, $onclick = null)
// returns an ExtendedElement that is an HTML button with the given value as label and optionally onclick attribute
//
// public function createElementTextArea($name, $columns, $rows, [$id], [$value])
// returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
// and optionally id and value
//
// public function createElementSelect($name, [$id], [$options], [$default], [$strictmatch])
// returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
// array of options (key => description) and key of the by-default selected entry (matched including type when strictmatch is true)
//
// public function createElementOption($key, $desc, [$selected])
// returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
// and optionally bool that tells if the entry is selected
//
// public function createElementLabel($for_id, $value)
// returns an ExtendedElement that is an HTML <label> with the given 'for' and value
//
// public function createElementStyle($styledata)
// returns an ExtendedElement that is an HTML <style> of CSS type with the style data inside
//
// public function createElementJS($jsdata)
// returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
//
// public function createElementJSFile($jsURL, [$defer], [$async])
// returns an ExtendedElement that is an HTML <script> of JavaScript type linking to the file given by the URL
// $defer and $async are boolean attributes that set if the script should be executed after parsing but before onload, and if it should be loaded asynchronously.
function __construct($version = '1.0', $encoding = 'utf-8') {
// make sure the default DOMDocument constructor runs
parent::__construct($version, $encoding);
$this->registerNodeClass('DOMElement', 'ExtendedElement');
$this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
}
static function initHTML5($doc = null) {
if (is_null($doc)) { $doc = new ExtendedDocument(); }
$doc->loadHTML5('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html></html>'); // this seems to be the only way to get the DOCTYPE set properly.
// Created basic HTML document structure.
$root = $doc->getElementsByTagName('html')->item(0);
$head = $root->appendElement('head');
$title = $head->appendElement('title');
$body = $root->appendElement('body');
return array('document' => $doc,
'html' => $root,
'head' => $head,
'title' => $title,
'body' => $body);
}
public function loadHTML5($source) {
// Do our own handling of DOMDocument error reporting so we can ignore "unknown tags" which are usually fine in HTML5.
libxml_use_internal_errors(true);
if (!preg_match('/^\s*<?xml /', $source)) {
// Add an XML declaration to force DOMDocument into UTF-8 mode.
$source = '<?xml version="1.0" encoding="utf-8"?>'."\n".$source;
}
$result = $this->loadHTML($source);
// Handle DOMDocument loading errors, throw away warnings on unknown tags as HTML5 allows all kinds.
$errseverity = array(LIBXML_ERR_WARNING => 'Warning', LIBXML_ERR_ERROR => 'Error', LIBXML_ERR_FATAL => 'Fatal');
foreach (libxml_get_errors() as $error) {
// $error is a libXMLError, see https://www.php.net/manual/en/class.libxmlerror.php
// See http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors for error numbers
if ($error->code != 801) { // XML_HTML_UNKNOWN_TAG gets no output, should not exist for HTML5.
trigger_error($errseverity[$error->level].' loading HTML5: '.$error->message.' (code '.$error->code.'), line: '.$error->line, E_USER_WARNING);
}
}
libxml_clear_errors();
libxml_use_internal_errors(false);
return $result;
}
public function appendElement($name, $value = '') {
return $this->appendChild($this->createElement($name, $value));
}
public function appendElementXML($name, $xmldata) {
$aelem = $this->appendChild($this->createElement($name));
$aelem->appendXMLMarkup($xmldata);
//$aelem->appendChild($this->createXMLFragment($xmldata));
return $aelem;
}
public function appendLink($target, $value = '', $title = null) {
return $this->appendChild($this->createElementLink($target, $value, $title));
}
public function appendImage($src, $alt_text = '') {
return $this->appendChild($this->createElementImage($src, $alt_text));
}
public function appendForm($action, $method, $name, $id = null) {
return $this->appendChild($this->createElementForm($action, $method, $name, $id));
}
public function appendFormDiv($action, $method, $name, $id = null) {
$formelem = $this->appendChild($this->createElementForm($action, $method, $name, $id));
return $formelem->appendElement('div');
}
public function appendInputHidden($name, $value) {
return $this->appendChild($this->createElementInputHidden($name, $value));
}
public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
}
public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->createElementInputPassword($name, $maxlength, $size, $id, $value));
}
public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->createElementInputNumber($name, $maxlength, $size, $id, $value));
}
public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
return $this->appendChild($this->createElementInputRange($name, $id, $min, $max, $step, $value));
}
public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->createElementInputUrl($name, $maxlength, $size, $id, $value));
}
public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->createElementInputEmail($name, $maxlength, $size, $id, $value));
}
public function appendInputTel($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->createElementInputTel($name, $maxlength, $size, $id, $value));
}
public function appendInputDate($name, $id = null, $min = null, $max = null, $value = null) {
return $this->appendChild($this->createElementInputDate($name, $id, $min, $max, $value));
}
public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
return $this->appendChild($this->createElementInputTime($name, $id, $min, $max, $value));
}
public function appendInputColor($name, $id = null, $value = null) {
return $this->appendChild($this->createElementInputColor($name, $id, $value));
}
public function appendInputRadio($name, $id, $value, $checked) {
return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
}
public function appendInputCheckbox($name, $id, $value, $checked) {
return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
}
public function appendInputFile($name, $id, $accept) {
return $this->appendChild($this->createElementInputFile($name, $id, $accept));
}
public function appendInputSubmit($value) {
return $this->appendChild($this->createElementInputSubmit($value));
}
public function appendButton($value, $onclick = null) {
return $this->appendChild($this->createElementButton($value, $onclick));
}
public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
}
public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
return $this->appendChild($this->createElementSelect($name, $id, $options, $default, $strictmatch));
}
public function appendElementOption($key, $desc, $selected = false) {
return $this->appendChild($this->createElementOption($key, $desc, $selected));
}
public function appendLabel($for_id, $value) {
return $this->appendChild($this->createElementLabel($for_id, $value));
}
public function appendText($text) {
return $this->appendChild($this->createTextNode($text));
}
public function appendLinebreak() {
return $this->appendChild($this->createElement('br'));
}
public function appendEntity($name) {
return $this->appendChild($this->createEntityReference($name));
}
public function appendComment($comment_data) {
return $this->appendChild($this->createComment($comment_data));
}
public function appendClonedElement($dom_element, $deep = true) {
return $this->appendChild($dom_element->cloneNode($deep));
}
public function appendStyleElement($styledata) {
return $this->appendChild($this->createElementStyle($styledata));
}
public function appendJSElement($jsdata) {
return $this->appendChild($this->createElementJS($jsdata));
}
public function appendJSFile($jsURL, $defer = false, $async = false) {
return $this->appendChild($this->createElementJSFile($jsURL, $defer, $async));
}
public function appendHTMLMarkup($htmldata, $parentNode = null) {
if (is_null($parentNode)) { $parentNode =& $this; }
// Use loadHTML5() to parse and turn the markup into proper HTML.
$tmpdoc = new ExtendedDocument;
// The XML line is needed to tell the parser that we need UTF-8 parsing.
$tmpdoc->loadHTML5('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html><body>'.$htmldata.'</body></html>');
foreach ($tmpdoc->getElementsByTagName('body')->item(0)->childNodes as $child) {
$parentNode->appendChild($this->importNode($child, true));
}
}
public function appendXMLMarkup($xmldata, $parentNode = null) {
if (is_null($parentNode)) { $parentNode =& $this; }
$tmpdoc = new ExtendedDocument;
$tmpxml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
$tmpxml .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
$tmpxml .= '<root>'.$xmldata.'</root>';
$tmpdoc->loadXML($tmpxml);
foreach ($tmpdoc->getElementsByTagName('root')->item(0)->childNodes as $child) {
$parentNode->appendChild($this->importNode($child, true));
}
}
public function createElement($name, $value = '') {
// Adding the $value in DOMDocument's createElement does NOT escape it, so override it and use appendText to support that.
$aelem = parent::createElement($name);
if (strlen($value ?? '')) { $aelem->appendText($value); }
return $aelem;
}
public function createElementLink($target, $value = '', $title = null) {
$link = $this->createElement('a', $value);
$link->setAttribute('href', $target); // XXX: take care of & etc. in links
if (!is_null($title)) { $link->setAttribute('title', $title); }
return $link;
}
public function createElementImage($src, $alt_text = '') {
$img = $this->createElement('img');
$img->setAttribute('src', $src);
$img->setAttribute('alt', $alt_text);
return $img;
}
public function createElementForm($action, $method, $name, $id = null) {
$formelem = $this->createElement('form');
$formelem->setAttribute('action', $action);
$formelem->setAttribute('method', $method);
$formelem->setAttribute('name', $name);
if (!is_null($id)) { $formelem->setAttribute('id', $id); }
return $formelem;
}
public function createElementInputHidden($name, $value) {
$hidden = $this->createElement('input');
$hidden->setAttribute('type', 'hidden');
$hidden->setAttribute('name', $name);
$hidden->setAttribute('value', $value);
return $hidden;
}
public function createElementInputText($name, $maxlength, $size, $id = null, $value = null) {
$txfield = $this->createElement('input');
$txfield->setAttribute('type', 'text');
if (!is_null($id)) { $txfield->setAttribute('id', $id); }
$txfield->setAttribute('name', $name);
$txfield->setAttribute('maxlength', $maxlength);
$txfield->setAttribute('size', $size);
if (!is_null($value)) { $txfield->setAttribute('value', $value); }
return $txfield;
}
public function createElementInputPassword($name, $maxlength, $size, $id = null, $value = null) {
$pwfield = $this->createElement('input');
$pwfield->setAttribute('type', 'password');
if (!is_null($id)) { $pwfield->setAttribute('id', $id); }
$pwfield->setAttribute('name', $name);
$pwfield->setAttribute('maxlength', $maxlength);
$pwfield->setAttribute('size', $size);
if (!is_null($value)) { $pwfield->setAttribute('value', $value); }
return $pwfield;
}
public function createElementInputNumber($name, $maxlength, $size, $id = null, $value = null) {
$numfield = $this->createElement('input');
$numfield->setAttribute('type', 'number');
if (!is_null($id)) { $numfield->setAttribute('id', $id); }
$numfield->setAttribute('name', $name);
$numfield->setAttribute('maxlength', $maxlength);
$numfield->setAttribute('size', $size);
if (!is_null($value)) { $numfield->setAttribute('value', $value); }
return $numfield;
}
public function createElementInputRange($name, $id, $min, $max, $step = null, $value = null) {
$rgfield = $this->createElement('input');
$rgfield->setAttribute('type', 'range');
if (!is_null($id)) { $rgfield->setAttribute('id', $id); }
$rgfield->setAttribute('name', $name);
if (!is_null($min)) { $rgfield->setAttribute('min', $min); }
if (!is_null($max)) { $rgfield->setAttribute('max', $max); }
if (!is_null($step)) { $rgfield->setAttribute('step', $step); }
if (!is_null($value)) { $rgfield->setAttribute('value', $value); }
return $rgfield;
}
public function createElementInputUrl($name, $maxlength, $size, $id = null, $value = null) {
$urlfield = $this->createElement('input');
$urlfield->setAttribute('type', 'url');
if (!is_null($id)) { $urlfield->setAttribute('id', $id); }
$urlfield->setAttribute('name', $name);
$urlfield->setAttribute('maxlength', $maxlength);
$urlfield->setAttribute('size', $size);
if (!is_null($value)) { $urlfield->setAttribute('value', $value); }
return $urlfield;
}
public function createElementInputEmail($name, $maxlength, $size, $id = null, $value = null) {
$mailfield = $this->createElement('input');
$mailfield->setAttribute('type', 'email');
if (!is_null($id)) { $mailfield->setAttribute('id', $id); }
$mailfield->setAttribute('name', $name);
$mailfield->setAttribute('maxlength', $maxlength);
$mailfield->setAttribute('size', $size);
if (!is_null($value)) { $mailfield->setAttribute('value', $value); }
return $mailfield;
}
public function createElementInputTel($name, $maxlength, $size, $id = null, $value = null) {
$telfield = $this->createElement('input');
$telfield->setAttribute('type', 'tel');
if (!is_null($id)) { $telfield->setAttribute('id', $id); }
$telfield->setAttribute('name', $name);
$telfield->setAttribute('maxlength', $maxlength);
$telfield->setAttribute('size', $size);
if (!is_null($value)) { $telfield->setAttribute('value', $value); }
return $telfield;
}
public function createElementInputDate($name, $id = null, $min = null, $max = null, $value = null) {
$dtfield = $this->createElement('input');
$dtfield->setAttribute('type', 'date');
if (!is_null($id)) { $dtfield->setAttribute('id', $id); }
$dtfield->setAttribute('name', $name);
if (!is_null($min)) { $dtfield->setAttribute('min', $min); }
if (!is_null($max)) { $dtfield->setAttribute('max', $max); }
if (!is_null($value)) { $dtfield->setAttribute('value', $value); }
return $dtfield;
}
public function createElementInputTime($name, $id = null, $min = null, $max = null, $value = null) {
$timefield = $this->createElement('input');
$timefield->setAttribute('type', 'time');
if (!is_null($id)) { $timefield->setAttribute('id', $id); }
$timefield->setAttribute('name', $name);
if (!is_null($min)) { $timefield->setAttribute('min', $min); }
if (!is_null($max)) { $timefield->setAttribute('max', $max); }
if (!is_null($value)) { $timefield->setAttribute('value', $value); }
return $timefield;
}
public function createElementInputColor($name, $id = null, $value = null) {
$colfield = $this->createElement('input');
$colfield->setAttribute('type', 'color');
if (!is_null($id)) { $colfield->setAttribute('id', $id); }
$colfield->setAttribute('name', $name);
if (!is_null($value)) { $colfield->setAttribute('value', $value); }
return $colfield;
}
public function createElementInputRadio($name, $id, $value, $checked) {
$radio = $this->createElement('input');
$radio->setAttribute('type', 'radio');
$radio->setAttribute('name', $name);
if (!is_null($id)) { $radio->setAttribute('id', $id); }
$radio->setAttribute('value', $value);
if ($checked) { $radio->setAttribute('checked', ''); }
return $radio;
}
public function createElementInputCheckbox($name, $id, $value, $checked) {
$cbox = $this->createElement('input');
$cbox->setAttribute('type', 'checkbox');
$cbox->setAttribute('name', $name);
if (!is_null($id)) { $cbox->setAttribute('id', $id); }
$cbox->setAttribute('value', $value);
if ($checked) { $cbox->setAttribute('checked', ''); }
return $cbox;
}
public function createElementInputFile($name, $id, $accept) {
$fileinput = $this->createElement('input');
$fileinput->setAttribute('type', 'file');
$fileinput->setAttribute('name', $name);
if (!is_null($id)) { $fileinput->setAttribute('id', $id); }
$fileinput->setAttribute('accept', $accept);
return $fileinput;
}
public function createElementInputSubmit($value) {
$submitbtn = $this->createElement('input');
$submitbtn->setAttribute('type', 'submit');
$submitbtn->setAttribute('value', $value);
return $submitbtn;
}
public function createElementButton($value, $onclick = null) {
$btn = $this->createElement('input');
$btn->setAttribute('type', 'button');
$btn->setAttribute('value', $value);
if (!is_null($onclick)) { $btn->setAttribute('onclick', $onclick); }
return $btn;
}
public function createElementTextArea($name, $columns, $rows, $id = null, $value = null) {
$txtarea = $this->createElement('textarea', $value);
$txtarea->setAttribute('name', $name);
$txtarea->setAttribute('cols', $columns);
$txtarea->setAttribute('rows', $rows);
if (!is_null($id)) { $txtarea->setAttribute('id', $id); }
return $txtarea;
}
public function createElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
$select = $this->createElement('select');
$select->setAttribute('name', $name);
if (!is_null($id)) { $select->setAttribute('id', $id); }
foreach ($options as $key => $desc) {
$select->appendElementOption($key, $desc, $strictmatch ? ($key === $default) : ($key == $default));
}
return $select;
}
public function createElementOption($key, $desc, $selected = false) {
$option = $this->createElement('option', $desc);
$option->setAttribute('value', $key);
if ($selected) { $option->setAttribute('selected', ''); }
return $option;
}
public function createElementLabel($for_id, $value) {
$label = $this->createElement('label', $value);
$label->setAttribute('for', $for_id);
return $label;
}
public function createElementStyle($styledata) {
$style_elem = $this->createElement('style');
// Note: type can/should be left out for HTML5.
$style_elem->setAttribute('type', 'text/css');
$style_elem->appendChild($this->createCDATASection($styledata));
return $style_elem;
}
public function createElementJS($jsdata) {
$jselem = $this->createElement('script');
// Note: type can/should be left out for HTML5.
$jselem->setAttribute('type', 'text/javascript');
$jselem->appendChild($this->createCDATASection($jsdata));
return $jselem;
}
public function createElementJSFile($jsURL, $defer = false, $async = false) {
$jselem = $this->createElement('script');
// Note: type can/should be left out for HTML5.
$jselem->setAttribute('type', 'text/javascript');
if ($defer) {
$jselem->setAttribute('defer', '');
}
if ($async) {
$jselem->setAttribute('async', '');
}
$jselem->setAttribute('src', $jsURL);
return $jselem;
}
}
class ExtendedElement extends DOMElement {
// ExtendedElement PHP class
// this extends the general PHP DOM Element class to simplify some usual constructs
//
// public function appendElement($name, [$value])
// appends a DOMDocument::createElement() as a child of this element (see there for params)
// returns the new child
//
// public function appendElementXML($name, $xmldata)
// appends a DOMDocument::createElement() with the given name as a child of this element,
// with an ExtendedDocument::createXMLFragment() of the given XML data inside
// returns the new child
//
// public function appendLink($target, [$value], [$title])
// appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
// returns the new child
//
// public function appendImage($src, [$alt_text])
// appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
// returns the new child
//
// public function appendForm($action, $method, $name, [$id])
// appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
// returns the new child
//
// public function appendFormDiv($action, $method, $name, [$id])
// appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
// returns an HTML <div> that is a child of the new child
//
// public function appendInputHidden($name, $value)
// appends an ExtendedDocument::createElementInputHidden() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputText($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputText() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputPassword() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputNumber() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputRange($name, $id, $min, $max, [$step], [$value])
// appends an ExtendedDocument::createElementInputRange() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputUrl() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputEmail() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputTel($name, $maxlength, $size, [$id], [$value])
// appends an ExtendedDocument::createElementInputTel() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputDate($name, [$id], [$min], [$max], [$value])
// appends an ExtendedDocument::createElementInputDate() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputTime($name, [$id], [$min], [$max], [$value])
// appends an ExtendedDocument::createElementInputTime() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputColor($name, [$id], [$value])
// appends an ExtendedDocument::createElementInputColor() as a child of this element (see there for params)
//
// public function appendInputRadio($name, $id, $value, $checked)
// appends an ExtendedDocument::createElementInputRadio() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputCheckbox($name, $id, $value, $checked)
// appends an ExtendedDocument::createElementInputCheckbox() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputFile($name, $id, $accept)
// appends an ExtendedDocument::createElementInputFile() as a child of this element (see there for params)
// returns the new child
//
// public function appendInputSubmit($value)
// appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
// returns the new child
//
// public function appendButton($value, $onclick = null)
// appends an ExtendedDocument::createElementButton() as a child of this element (see there for params)
// returns the new child
//
// public function appendTextArea($name, $columns, $rows, [$id], [$value])
// appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
// returns the new child
//
// public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
// appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
// returns the new child
//
// public function appendElementOption($key, $desc, [$selected])
// appends an ExtendedDocument::createElementOption() as a child of this element (see there for params)
// returns the new child
//
// public function appendLabel($for_id, $value)
// appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
// returns the new child
//
// public function appendText($text)
// appends a DOMDocument::createTextNode() as a child of this element (see there for params)
// returns the new child
//
// public function appendLinebreak()
// appends a <br> as a child of this element
// returns the new child
//
// public function appendEntity($name)
// appends a DOMDocument::createEntityReference() as a child of this element (see there for params)
// returns the new child
//
// public function appendComment($comment_data)
// appends a DOMDocument::createComment() as a child of this element (see there for params)
// returns the new child
//
// public function appendClonedElement($dom_element, [$deep])
// appends a clone of the given DOMElement as a child of this element
// the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
// returns the new child
//
// public function appendHTMLMarkup($htmldata)
// appends a representation of the HTML data as children of this element
// NO return value!
//
// public function appendXMLMarkup($xmldata)
// appends a representation of the XML data as children of this element
// NO return value!
//
// public function appendStyleElement($styledata)
// appends an ExtendedDocument::createElementStyle() as a child of this element (see there for params)
// returns the new child
//
// public function appendJSElement($jsdata)
// appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
// returns the new child
//
// public function appendJSFile($jsURL, [$defer], [$async])
// appends an ExtendedDocument::createElementJSFile() as a child of this element (see there for params)
// returns the new child
//
// public function setClass($classname)
// sets the 'class' attribute of the element to the given classname value
//
// public function addClass($classname)
// adds the given classname value to the space-separated list in the 'class' attribute
// returns the value of the 'class' attribute
//
// public function setID($elem_id)
// sets the 'id' attribute of the element to the given elem_id value
public function appendElement($name, $value = '') {
return $this->appendChild($this->ownerDocument->createElement($name, $value));
}
public function appendElementXML($name, $xmldata) {
$aelem = $this->appendChild($this->ownerDocument->createElement($name));
$aelem->appendXMLMarkup($xmldata);
return $aelem;
}
public function appendLink($target, $value = '', $title = null) {
return $this->appendChild($this->ownerDocument->createElementLink($target, $value, $title));
}
public function appendImage($src, $alt_text = '') {
return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
}
public function appendForm($action, $method, $name, $id = null) {
return $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
}
public function appendFormDiv($action, $method, $name, $id = null) {
$formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
return $formelem->appendElement('div');
}
public function appendInputHidden($name, $value) {
return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
}
public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
}
public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputPassword($name, $maxlength, $size, $id, $value));
}
public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputNumber($name, $maxlength, $size, $id, $value));
}
public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputRange($name, $id, $min, $max, $step, $value));
}
public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputUrl($name, $maxlength, $size, $id, $value));
}
public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputEmail($name, $maxlength, $size, $id, $value));
}
public function appendInputTel($name, $maxlength, $size, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputTel($name, $maxlength, $size, $id, $value));
}
public function appendInputDate($name, $id = null, $min = null, $max = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputDate($name, $id, $min, $max, $value));
}
public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputTime($name, $id, $min, $max, $value));
}
public function appendInputColor($name, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementInputColor($name, $id, $value));
}
public function appendInputRadio($name, $id, $value, $checked) {
return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
}
public function appendInputCheckbox($name, $id, $value, $checked) {
return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
}
public function appendInputFile($name, $id, $accept) {
return $this->appendChild($this->ownerDocument->createElementInputFile($name, $id, $accept));
}
public function appendInputSubmit($value) {
return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
}
public function appendButton($value, $onclick = null) {
return $this->appendChild($this->ownerDocument->createElementButton($value, $onclick));
}
public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
}
public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
}
public function appendElementOption($key, $desc, $selected = false) {
return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
}
public function appendLabel($for_id, $value) {
return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
}
public function appendText($text) {
return $this->appendChild($this->ownerDocument->createTextNode($text));
}
public function appendLinebreak() {
return $this->appendChild($this->ownerDocument->createElement('br'));
}
public function appendEntity($name) {
return $this->appendChild($this->ownerDocument->createEntityReference($name));
}
public function appendComment($comment_data) {
return $this->appendChild($this->ownerDocument->createComment($comment_data));
}
public function appendClonedElement($dom_element, $deep = true) {
return $this->appendChild($dom_element->cloneNode($deep));
}
public function appendHTMLMarkup($htmldata) {
$this->ownerDocument->appendHTMLMarkup($htmldata, $this);
}
public function appendXMLMarkup($xmldata) {
$this->ownerDocument->appendXMLMarkup($xmldata, $this);
}
public function appendStyleElement($styledata) {
return $this->appendChild($this->ownerDocument->createElementStyle($styledata));
}
public function appendJSElement($jsdata) {
return $this->appendChild($this->ownerDocument->createElementJS($jsdata));
}
public function appendJSFile($jsURL, $defer = false, $async = false) {
return $this->appendChild($this->ownerDocument->createElementJSFile($jsURL, $defer, $async));
}
public function setClass($classname) {
$this->setAttribute('class', $classname);
}
public function addClass($classname) {
$classval = $this->getAttribute('class');
if (strlen($classval)) { $classval .= ' '; }
$classval .= $classname;
$this->setClass($classval);
return $classval;
}
public function setID($elem_id) {
$this->setAttribute('id', $elem_id);
}
}
class ExtendedDocumentFragment extends DOMDocumentFragment {
// ExtendedDocumentFragment PHP class
// this extends the general PHP DOM Document Fragment class to simplify some usual constructs
//
// public function appendElement($name, [$value])
// appends a DOMDocument::createElement() as a child of this fragment (see there for params)
// returns the new child
//
// public function appendElementXML($name, $xmldata)
// appends a DOMDocument::createElement() with the given name as a child of this fragment,
// with an ExtendedDocument::createXMLFragment() of the given XML data inside
// returns the new child
//
// public function appendLink($target, [$value], [$title])
// appends an ExtendedDocument::createElementLink() as a child of this fragment (see there for params)
// returns the new child
//
// public function appendImage($src, [$alt_text])
// appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
// returns the new child
//
// public function appendForm($action, $method, $name, [$id])
// appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)