improve loading of documents into HTML5
[php-utility-classes.git] / classes / document.php-class
CommitLineData
cea5b93a 1<?php
880bcb60
RK
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
cea5b93a
RK
5
6class ExtendedDocument extends DOMDocument {
7 // ExtendedDocument PHP class
8 // this extends the general PHP DOM Document class to simplify some usual constructs
9 //
10 // function __construct([$version], [$encoding])
11 // CONSTRUCTOR
12 // construct a new DOM Document that uses our element definitions
13 //
b841a92b 14 // static function initHTML5([$doc])
14014b8f 15 // initialize as an HTML5 document and return references to its basic elements.
b841a92b 16 // If a $doc is handed over (an ExtendedDocument or a derived class), load the content into that document.
14014b8f 17 // returns an associative array with the following elements: 'html', 'head', 'title', 'body'
ae41c41b 18 //
d7403a92
RK
19 // public function loadHTML5($source) {
20 // A version of loadHTML() - see DOMDocument documentation - that is made for loading HTML5 and not emitting warnings/errors for unknown elements.
21 // returns true on success, false otherwise, just like loadHTML5.
22 //
61cc8aa9 23 // public function appendElement($name, [$value])
cea5b93a
RK
24 // appends a DOMDocument::createElement() as a child of this document (see there for params)
25 // returns the new child
26 //
61cc8aa9 27 // public function appendElementXML($name, $xmldata)
cea5b93a
RK
28 // appends a DOMDocument::createElement() with the given name as a child of this document,
29 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
30 // returns the new child
31 //
b3cc0fef 32 // public function appendLink($target, [$value], [$title])
cea5b93a
RK
33 // appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
34 // returns the new child
35 //
61cc8aa9 36 // public function appendImage($src, [$alt_text])
53510e9e
RK
37 // appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
38 // returns the new child
39 //
61cc8aa9 40 // public function appendForm($action, $method, $name, [$id])
b8d16837
RK
41 // appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
42 // returns the new child
43 //
61cc8aa9 44 // public function appendFormDiv($action, $method, $name, [$id])
ae41c41b
RK
45 // appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
46 // returns an HTML <div> that is a child of the new child
47 //
61cc8aa9 48 // public function appendInputHidden($name, $value)
ae41c41b
RK
49 // appends an ExtendedDocument::createElementInputHidden() as a child of this document (see there for params)
50 // returns the new child
51 //
61cc8aa9 52 // public function appendInputText($name, $maxlength, $size, [$id], [$value])
ae41c41b
RK
53 // appends an ExtendedDocument::createElementInputText() as a child of this document (see there for params)
54 // returns the new child
55 //
f925e4a1
RK
56 // public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
57 // appends an ExtendedDocument::createElementInputPassword() as a child of this document (see there for params)
58 // returns the new child
59 //
61cc8aa9 60 // public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
262e0bbb
RK
61 // appends an ExtendedDocument::createElementInputNumber() as a child of this document (see there for params)
62 // returns the new child
63 //
f925e4a1
RK
64 // public function appendInputRange($name, $id, $min, $max, [$step], [$value])
65 // appends an ExtendedDocument::createElementInputRange() as a child of this document (see there for params)
66 // returns the new child
67 //
6f794328
RK
68 // public function appendInputSearch($name, $maxlength, $size, [$id], [$value])
69 // appends an ExtendedDocument::createElementInputSearch() as a child of this document (see there for params)
70 // returns the new child
71 //
f925e4a1
RK
72 // public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
73 // appends an ExtendedDocument::createElementInputUrl() as a child of this document (see there for params)
74 // returns the new child
75 //
ea32f7e5
RK
76 // public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
77 // appends an ExtendedDocument::createElementInputEmail() as a child of this document (see there for params)
78 // returns the new child
79 //
f925e4a1
RK
80 // public function appendInputTel($name, $maxlength, $size, [$id], [$value])
81 // appends an ExtendedDocument::createElementInputTel() as a child of this document (see there for params)
82 // returns the new child
83 //
84 // public function appendInputDate($name, [$id], [$min], [$max], [$value])
85 // appends an ExtendedDocument::createElementInputDate() as a child of this document (see there for params)
86 // returns the new child
87 //
88 // public function appendInputTime($name, [$id], [$min], [$max], [$value])
89 // appends an ExtendedDocument::createElementInputTime() as a child of this document (see there for params)
90 // returns the new child
91 //
92 // public function appendInputColor($name, [$id], [$value])
93 // appends an ExtendedDocument::createElementInputColor() as a child of this document (see there for params)
645a7eb6
RK
94 // returns the new child
95 //
61cc8aa9 96 // public function appendInputRadio($name, $id, $value, $checked)
ae41c41b
RK
97 // appends an ExtendedDocument::createElementInputRadio() as a child of this document (see there for params)
98 // returns the new child
99 //
61cc8aa9 100 // public function appendInputCheckbox($name, $id, $value, $checked)
ae41c41b
RK
101 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this document (see there for params)
102 // returns the new child
103 //
61cc8aa9 104 // public function appendInputFile($name, $id, $accept)
4bb9d784
RK
105 // appends an ExtendedDocument::createElementInputFile() as a child of this document (see there for params)
106 // returns the new child
107 //
61cc8aa9 108 // public function appendInputSubmit($value)
ae41c41b
RK
109 // appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
110 // returns the new child
111 //
61cc8aa9 112 // public function appendButton($value, $onclick = null)
1109f526
RK
113 // appends an ExtendedDocument::createElementButton() as a child of this document (see there for params)
114 // returns the new child
115 //
61cc8aa9 116 // public function appendTextArea($name, $columns, $rows, [$id], [$value])
bf91764c
RK
117 // appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
118 // returns the new child
119 //
69409ddb 120 // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
bf91764c
RK
121 // appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
122 // returns the new child
123 //
61cc8aa9 124 // public function appendElementOption($key, $desc, [$selected])
bf91764c
RK
125 // appends an ExtendedDocument::createElementOption() as a child of this document (see there for params)
126 // returns the new child
127 //
61cc8aa9 128 // public function appendLabel($for_id, $value)
ae41c41b
RK
129 // appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
130 // returns the new child
131 //
c79be4c2
RK
132 // public function appendElementDatalist([$id], [$options])
133 // appends an ExtendedDocument::createElementDatalist() as a child of this document (see there for params)
134 // returns the new child
135 //
61cc8aa9 136 // public function appendText($text)
cea5b93a
RK
137 // appends a DOMDocument::createTextNode() as a child of this document (see there for params)
138 // returns the new child
139 //
71771b0c
RK
140 // public function appendLinebreak()
141 // appends a <br> as a child of this document
142 // returns the new child
143 //
6638efd5
RK
144 // public function appendEntity($name)
145 // appends a DOMDocument::createEntityReference() as a child of this document (see there for params)
146 // returns the new child
147 //
61cc8aa9 148 // public function appendComment($comment_data)
cadc5980
RK
149 // appends a DOMDocument::createComment() as a child of this document (see there for params)
150 // returns the new child
151 //
3d4db013
RK
152 // public function appendClonedElement($dom_element, [$deep])
153 // appends a clone of the given DOMElement as a child of this document
154 // the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
155 // returns the new child
156 //
61cc8aa9 157 // public function appendHTMLMarkup($htmldata, [$parentNode])
ae41c41b
RK
158 // appends a representation of the HTML data as children of the given parent node, by default this document
159 // NO return value!
160 //
61cc8aa9 161 // public function appendXMLMarkup($xmldata, [$parentNode])
ae41c41b 162 // appends a representation of the XML data as children of the given parent node, by default this document
cea5b93a
RK
163 // NO return value!
164 //
acaa2daa
RK
165 // public function appendStyleElement($styledata)
166 // appends an ExtendedDocument::createElementStyle() as a child of this document (see there for params)
167 // returns the new child
168 //
61cc8aa9 169 // public function appendJSElement($jsdata)
cea5b93a 170 // appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
acaa2daa 171 // returns the new child
cea5b93a 172 //
8afa2e61 173 // public function appendJSFile($jsURL, [$defer], [$async])
ea32f7e5
RK
174 // appends an ExtendedDocument::createElementJSFile() as a child of this document (see there for params)
175 // returns the new child
176 //
b3cc0fef
RK
177 // public function createElementLink($target, [$value], [$title])
178 // returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value as well as (optional) title attribute
cea5b93a 179 //
61cc8aa9 180 // public function createElementImage($src, [$alt_text])
b3cc0fef 181 // returns an ExtendedElement that is an HTML <img> with the given src and alt attributes (set to '' by default)
53510e9e 182 //
61cc8aa9 183 // public function createElementForm($action, $method, $name)
cb2420c6 184 // returns an ExtendedElement that is an HTML <form> with the given action, method, and name
ae41c41b 185 //
61cc8aa9 186 // public function createElementInputHidden($name, $value)
ae41c41b
RK
187 // returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
188 //
61cc8aa9 189 // public function createElementInputText($name, $maxlength, $size, [$id], [$value])
ae41c41b
RK
190 // returns an ExtendedElement that is an HTML <input> of type 'text' with the given name, maxlength, size,
191 // and optionally id and value
192 //
f925e4a1
RK
193 // public function createElementInputPassword($name, $maxlength, $size, [$id], [$value])
194 // returns an ExtendedElement that is an HTML <input> of type 'password' with the given name, maxlength, size,
195 // and optionally id and value
196 //
61cc8aa9 197 // public function createElementInputNumber($name, $maxlength, $size, [$id], [$value])
262e0bbb
RK
198 // returns an ExtendedElement that is an HTML <input> of type 'number' with the given name, maxlength, size,
199 // and optionally id and value
200 //
f925e4a1
RK
201 // public function createElementInputRange($name, $id, $min, $max, [$step], [$value])
202 // returns an ExtendedElement that is an HTML <input> of type 'range' with the given name, id, min, max,
203 // and optionally step and value
204 //
205 // public function createElementInputUrl($name, $maxlength, $size, [$id], [$value])
206 // returns an ExtendedElement that is an HTML <input> of type 'url' with the given name, maxlength, size,
207 // and optionally id and value
208 //
6f794328
RK
209 // public function createElementInputSearch($name, $maxlength, $size, [$id], [$value])
210 // returns an ExtendedElement that is an HTML <input> of type 'search' with the given name, maxlength, size,
211 // and optionally id and value
212 //
ea32f7e5
RK
213 // public function createElementInputEmail($name, $maxlength, $size, [$id], [$value])
214 // returns an ExtendedElement that is an HTML <input> of type 'email' with the given name, maxlength, size,
215 // and optionally id and value
216 //
f925e4a1
RK
217 // public function createElementInputTel($name, $maxlength, $size, [$id], [$value])
218 // returns an ExtendedElement that is an HTML <input> of type 'tel' with the given name, maxlength, size,
219 // and optionally id and value
220 //
221 // public function createElementInputDate($name, [$id], [$min], [$max], [$value])
222 // returns an ExtendedElement that is an HTML <input> of type 'date' with the given name,
223 // and optionally id, min, max, and value
224 //
225 // public function createElementInputTime($name, [$id], [$min], [$max], [$value])
226 // returns an ExtendedElement that is an HTML <input> of type 'time' with the given name,
227 // and optionally id, min, max, and value
228 //
229 // public function createElementInputColor($name, [$id], [$value])
230 // returns an ExtendedElement that is an HTML <input> of type 'color' with the given name,
645a7eb6
RK
231 // and optionally id and value
232 //
61cc8aa9 233 // public function createElementInputRadio($name, $id, $value, $checked)
ae41c41b
RK
234 // returns an ExtendedElement that is an HTML <input> of type 'radio' with the given name, id, value and
235 // checked state
236 //
61cc8aa9 237 // public function createElementInputCheckbox($name, $id, $value, $checked)
ae41c41b
RK
238 // returns an ExtendedElement that is an HTML <input> of type 'checkbox' with the given name, id, value and
239 // checked state
240 //
61cc8aa9 241 // public function createElementInputFile($name, $id, $accept)
4bb9d784
RK
242 // returns an ExtendedElement that is an HTML <input> of type 'file' with the given name, id and accept
243 //
61cc8aa9 244 // public function createElementInputSubmit($value)
1109f526
RK
245 // returns an ExtendedElement that is an HTML <input> of type 'submit' with the given value as label
246 //
61cc8aa9 247 // public function createElementButton($value, $onclick = null)
1109f526 248 // returns an ExtendedElement that is an HTML button with the given value as label and optionally onclick attribute
ae41c41b 249 //
61cc8aa9 250 // public function createElementTextArea($name, $columns, $rows, [$id], [$value])
bf91764c
RK
251 // returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
252 // and optionally id and value
253 //
69409ddb 254 // public function createElementSelect($name, [$id], [$options], [$default], [$strictmatch])
bf91764c 255 // returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
69409ddb 256 // array of options (key => description) and key of the by-default selected entry (matched including type when strictmatch is true)
bf91764c 257 //
61cc8aa9 258 // public function createElementOption($key, $desc, [$selected])
bf91764c
RK
259 // returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
260 // and optionally bool that tells if the entry is selected
261 //
61cc8aa9 262 // public function createElementLabel($for_id, $value)
ae41c41b 263 // returns an ExtendedElement that is an HTML <label> with the given 'for' and value
cea5b93a 264 //
c79be4c2
RK
265 // public function createElementDatalist([$id], [$options])
266 // returns an ExtendedElement that is an HTML <datalist> with optionally the given id and array of options (key => description)
267 //
acaa2daa
RK
268 // public function createElementStyle($styledata)
269 // returns an ExtendedElement that is an HTML <style> of CSS type with the style data inside
270 //
61cc8aa9 271 // public function createElementJS($jsdata)
cea5b93a 272 // returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
c1666e91 273 //
8afa2e61 274 // public function createElementJSFile($jsURL, [$defer], [$async])
ea32f7e5 275 // returns an ExtendedElement that is an HTML <script> of JavaScript type linking to the file given by the URL
8afa2e61 276 // $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.
cea5b93a
RK
277
278 function __construct($version = '1.0', $encoding = 'utf-8') {
279 // make sure the default DOMDocument constructor runs
280 parent::__construct($version, $encoding);
281 $this->registerNodeClass('DOMElement', 'ExtendedElement');
bf91764c 282 $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
cea5b93a
RK
283 }
284
b841a92b
RK
285 static function initHTML5($doc = null) {
286 if (is_null($doc)) { $doc = new ExtendedDocument(); }
0fe05664 287 $doc->loadHTML5('<?xml version="1.0" encoding="utf-8"?>'."\n".'<!DOCTYPE html>'."\n".'<html></html>'); // this seems to be the only way to get the DOCTYPE set properly.
14014b8f
RK
288
289 // Created basic HTML document structure.
290 $root = $doc->getElementsByTagName('html')->item(0);
291 $head = $root->appendElement('head');
292 $title = $head->appendElement('title');
293 $body = $root->appendElement('body');
294
295 return array('document' => $doc,
296 'html' => $root,
297 'head' => $head,
298 'title' => $title,
299 'body' => $body);
300 }
301
d7403a92
RK
302 public function loadHTML5($source) {
303 // Do our own handling of DOMDocument error reporting so we can ignore "unknown tags" which are usually fine in HTML5.
304 libxml_use_internal_errors(true);
0fe05664 305 if (!preg_match('/^\s*<\?xml /', $source)) {
d7403a92
RK
306 // Add an XML declaration to force DOMDocument into UTF-8 mode.
307 $source = '<?xml version="1.0" encoding="utf-8"?>'."\n".$source;
308 }
309 $result = $this->loadHTML($source);
0fe05664
RK
310 // Set encoding directly a,d remove any processing node that isn't the first node
311 $this->encoding = 'utf-8';
312 foreach ($this->childNodes as $i => $child) {
313 if ($i && $child->nodeType == XML_PI_NODE) {
314 $this->removeChild($child);
315 }
316 }
d7403a92
RK
317 // Handle DOMDocument loading errors, throw away warnings on unknown tags as HTML5 allows all kinds.
318 $errseverity = array(LIBXML_ERR_WARNING => 'Warning', LIBXML_ERR_ERROR => 'Error', LIBXML_ERR_FATAL => 'Fatal');
319 foreach (libxml_get_errors() as $error) {
320 // $error is a libXMLError, see https://www.php.net/manual/en/class.libxmlerror.php
321 // See http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors for error numbers
322 if ($error->code != 801) { // XML_HTML_UNKNOWN_TAG gets no output, should not exist for HTML5.
323 trigger_error($errseverity[$error->level].' loading HTML5: '.$error->message.' (code '.$error->code.'), line: '.$error->line, E_USER_WARNING);
324 }
325 }
326 libxml_clear_errors();
327 libxml_use_internal_errors(false);
328 return $result;
329 }
330
61cc8aa9 331 public function appendElement($name, $value = '') {
a8816f43 332 return $this->appendChild($this->createElement($name, $value));
cea5b93a 333 }
61cc8aa9 334 public function appendElementXML($name, $xmldata) {
cea5b93a 335 $aelem = $this->appendChild($this->createElement($name));
ae41c41b
RK
336 $aelem->appendXMLMarkup($xmldata);
337 //$aelem->appendChild($this->createXMLFragment($xmldata));
cea5b93a
RK
338 return $aelem;
339 }
b3cc0fef
RK
340 public function appendLink($target, $value = '', $title = null) {
341 return $this->appendChild($this->createElementLink($target, $value, $title));
cea5b93a 342 }
61cc8aa9 343 public function appendImage($src, $alt_text = '') {
4aa31782
RK
344 return $this->appendChild($this->createElementImage($src, $alt_text));
345 }
61cc8aa9 346 public function appendForm($action, $method, $name, $id = null) {
b8d16837
RK
347 return $this->appendChild($this->createElementForm($action, $method, $name, $id));
348 }
61cc8aa9 349 public function appendFormDiv($action, $method, $name, $id = null) {
b8d16837 350 $formelem = $this->appendChild($this->createElementForm($action, $method, $name, $id));
ae41c41b
RK
351 return $formelem->appendElement('div');
352 }
61cc8aa9 353 public function appendInputHidden($name, $value) {
ae41c41b
RK
354 return $this->appendChild($this->createElementInputHidden($name, $value));
355 }
61cc8aa9 356 public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
ae41c41b
RK
357 return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
358 }
f925e4a1
RK
359 public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
360 return $this->appendChild($this->createElementInputPassword($name, $maxlength, $size, $id, $value));
361 }
61cc8aa9 362 public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
262e0bbb
RK
363 return $this->appendChild($this->createElementInputNumber($name, $maxlength, $size, $id, $value));
364 }
f925e4a1
RK
365 public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
366 return $this->appendChild($this->createElementInputRange($name, $id, $min, $max, $step, $value));
367 }
6f794328
RK
368 public function appendInputSearch($name, $maxlength, $size, $id = null, $value = null) {
369 return $this->appendChild($this->createElementInputSearch($name, $maxlength, $size, $id, $value));
370 }
f925e4a1
RK
371 public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
372 return $this->appendChild($this->createElementInputUrl($name, $maxlength, $size, $id, $value));
373 }
ea32f7e5
RK
374 public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
375 return $this->appendChild($this->createElementInputEmail($name, $maxlength, $size, $id, $value));
376 }
f925e4a1
RK
377 public function appendInputTel($name, $maxlength, $size, $id = null, $value = null) {
378 return $this->appendChild($this->createElementInputTel($name, $maxlength, $size, $id, $value));
379 }
380 public function appendInputDate($name, $id = null, $min = null, $max = null, $value = null) {
381 return $this->appendChild($this->createElementInputDate($name, $id, $min, $max, $value));
382 }
383 public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
384 return $this->appendChild($this->createElementInputTime($name, $id, $min, $max, $value));
385 }
386 public function appendInputColor($name, $id = null, $value = null) {
387 return $this->appendChild($this->createElementInputColor($name, $id, $value));
645a7eb6 388 }
61cc8aa9 389 public function appendInputRadio($name, $id, $value, $checked) {
ae41c41b
RK
390 return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
391 }
61cc8aa9 392 public function appendInputCheckbox($name, $id, $value, $checked) {
ae41c41b
RK
393 return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
394 }
61cc8aa9 395 public function appendInputFile($name, $id, $accept) {
4bb9d784
RK
396 return $this->appendChild($this->createElementInputFile($name, $id, $accept));
397 }
61cc8aa9 398 public function appendInputSubmit($value) {
ae41c41b
RK
399 return $this->appendChild($this->createElementInputSubmit($value));
400 }
61cc8aa9 401 public function appendButton($value, $onclick = null) {
1109f526
RK
402 return $this->appendChild($this->createElementButton($value, $onclick));
403 }
61cc8aa9 404 public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
bf91764c
RK
405 return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
406 }
69409ddb
RK
407 public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
408 return $this->appendChild($this->createElementSelect($name, $id, $options, $default, $strictmatch));
bf91764c 409 }
61cc8aa9 410 public function appendElementOption($key, $desc, $selected = false) {
bf91764c
RK
411 return $this->appendChild($this->createElementOption($key, $desc, $selected));
412 }
61cc8aa9 413 public function appendLabel($for_id, $value) {
ae41c41b
RK
414 return $this->appendChild($this->createElementLabel($for_id, $value));
415 }
c79be4c2
RK
416 public function appendElementDatalist($id = null, $options = array()) {
417 return $this->appendChild($this->createElementDatalist($id, $options));
418 }
61cc8aa9 419 public function appendText($text) {
cea5b93a
RK
420 return $this->appendChild($this->createTextNode($text));
421 }
71771b0c
RK
422 public function appendLinebreak() {
423 return $this->appendChild($this->createElement('br'));
424 }
6638efd5
RK
425 public function appendEntity($name) {
426 return $this->appendChild($this->createEntityReference($name));
427 }
61cc8aa9 428 public function appendComment($comment_data) {
cadc5980
RK
429 return $this->appendChild($this->createComment($comment_data));
430 }
3d4db013
RK
431 public function appendClonedElement($dom_element, $deep = true) {
432 return $this->appendChild($dom_element->cloneNode($deep));
433 }
acaa2daa
RK
434 public function appendStyleElement($styledata) {
435 return $this->appendChild($this->createElementStyle($styledata));
436 }
61cc8aa9 437 public function appendJSElement($jsdata) {
3d4db013 438 return $this->appendChild($this->createElementJS($jsdata));
cea5b93a 439 }
1a4ee0f0
RK
440 public function appendJSFile($jsURL, $defer = false, $async = false) {
441 return $this->appendChild($this->createElementJSFile($jsURL, $defer, $async));
ea32f7e5 442 }
cea5b93a 443
61cc8aa9 444 public function appendHTMLMarkup($htmldata, $parentNode = null) {
ae41c41b 445 if (is_null($parentNode)) { $parentNode =& $this; }
d7403a92 446 // Use loadHTML5() to parse and turn the markup into proper HTML.
a9a5a68a
RK
447 $tmpdoc = new ExtendedDocument;
448 // The XML line is needed to tell the parser that we need UTF-8 parsing.
0fe05664 449 $tmpdoc->loadHTML5('<?xml version="1.0" encoding="utf-8"?>'."\n".'<!DOCTYPE html>'."\n".'<html><body>'.$htmldata.'</body></html>');
a9a5a68a
RK
450 foreach ($tmpdoc->getElementsByTagName('body')->item(0)->childNodes as $child) {
451 $parentNode->appendChild($this->importNode($child, true));
452 }
ae41c41b
RK
453 }
454
61cc8aa9 455 public function appendXMLMarkup($xmldata, $parentNode = null) {
ae41c41b
RK
456 if (is_null($parentNode)) { $parentNode =& $this; }
457 $tmpdoc = new ExtendedDocument;
458 $tmpxml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
459 $tmpxml .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
460 $tmpxml .= '<root>'.$xmldata.'</root>';
461 $tmpdoc->loadXML($tmpxml);
462 foreach ($tmpdoc->getElementsByTagName('root')->item(0)->childNodes as $child) {
463 $parentNode->appendChild($this->importNode($child, true));
464 }
cea5b93a
RK
465 }
466
61cc8aa9 467 public function createElement($name, $value = '') {
a8816f43
RK
468 // Adding the $value in DOMDocument's createElement does NOT escape it, so override it and use appendText to support that.
469 $aelem = parent::createElement($name);
5ae00086 470 if (strlen($value ?? '')) { $aelem->appendText($value); }
a8816f43
RK
471 return $aelem;
472 }
473
b3cc0fef 474 public function createElementLink($target, $value = '', $title = null) {
a8816f43 475 $link = $this->createElement('a', $value);
bf91764c 476 $link->setAttribute('href', $target); // XXX: take care of & etc. in links
b3cc0fef 477 if (!is_null($title)) { $link->setAttribute('title', $title); }
cea5b93a
RK
478 return $link;
479 }
480
61cc8aa9 481 public function createElementImage($src, $alt_text = '') {
319cfe8d
RK
482 $img = $this->createElement('img');
483 $img->setAttribute('src', $src);
484 $img->setAttribute('alt', $alt_text);
485 return $img;
4aa31782
RK
486 }
487
61cc8aa9 488 public function createElementForm($action, $method, $name, $id = null) {
ae41c41b
RK
489 $formelem = $this->createElement('form');
490 $formelem->setAttribute('action', $action);
491 $formelem->setAttribute('method', $method);
492 $formelem->setAttribute('name', $name);
b3cc0fef 493 if (!is_null($id)) { $formelem->setAttribute('id', $id); }
ae41c41b
RK
494 return $formelem;
495 }
496
61cc8aa9 497 public function createElementInputHidden($name, $value) {
ae41c41b
RK
498 $hidden = $this->createElement('input');
499 $hidden->setAttribute('type', 'hidden');
500 $hidden->setAttribute('name', $name);
501 $hidden->setAttribute('value', $value);
502 return $hidden;
503 }
504
61cc8aa9 505 public function createElementInputText($name, $maxlength, $size, $id = null, $value = null) {
ae41c41b
RK
506 $txfield = $this->createElement('input');
507 $txfield->setAttribute('type', 'text');
508 if (!is_null($id)) { $txfield->setAttribute('id', $id); }
509 $txfield->setAttribute('name', $name);
510 $txfield->setAttribute('maxlength', $maxlength);
511 $txfield->setAttribute('size', $size);
512 if (!is_null($value)) { $txfield->setAttribute('value', $value); }
513 return $txfield;
514 }
515
61cc8aa9 516 public function createElementInputPassword($name, $maxlength, $size, $id = null, $value = null) {
319cfe8d
RK
517 $pwfield = $this->createElement('input');
518 $pwfield->setAttribute('type', 'password');
519 if (!is_null($id)) { $pwfield->setAttribute('id', $id); }
520 $pwfield->setAttribute('name', $name);
521 $pwfield->setAttribute('maxlength', $maxlength);
522 $pwfield->setAttribute('size', $size);
523 if (!is_null($value)) { $pwfield->setAttribute('value', $value); }
524 return $pwfield;
645a7eb6
RK
525 }
526
f925e4a1
RK
527 public function createElementInputNumber($name, $maxlength, $size, $id = null, $value = null) {
528 $numfield = $this->createElement('input');
529 $numfield->setAttribute('type', 'number');
530 if (!is_null($id)) { $numfield->setAttribute('id', $id); }
531 $numfield->setAttribute('name', $name);
532 $numfield->setAttribute('maxlength', $maxlength);
533 $numfield->setAttribute('size', $size);
534 if (!is_null($value)) { $numfield->setAttribute('value', $value); }
535 return $numfield;
536 }
537
538 public function createElementInputRange($name, $id, $min, $max, $step = null, $value = null) {
539 $rgfield = $this->createElement('input');
540 $rgfield->setAttribute('type', 'range');
541 if (!is_null($id)) { $rgfield->setAttribute('id', $id); }
542 $rgfield->setAttribute('name', $name);
543 if (!is_null($min)) { $rgfield->setAttribute('min', $min); }
544 if (!is_null($max)) { $rgfield->setAttribute('max', $max); }
545 if (!is_null($step)) { $rgfield->setAttribute('step', $step); }
546 if (!is_null($value)) { $rgfield->setAttribute('value', $value); }
547 return $rgfield;
548 }
549
6f794328
RK
550 public function createElementInputSearch($name, $maxlength, $size, $id = null, $value = null) {
551 $urlfield = $this->createElement('input');
552 $urlfield->setAttribute('type', 'search');
553 if (!is_null($id)) { $urlfield->setAttribute('id', $id); }
554 $urlfield->setAttribute('name', $name);
555 $urlfield->setAttribute('maxlength', $maxlength);
556 $urlfield->setAttribute('size', $size);
557 if (!is_null($value)) { $urlfield->setAttribute('value', $value); }
558 return $urlfield;
559 }
560
f925e4a1
RK
561 public function createElementInputUrl($name, $maxlength, $size, $id = null, $value = null) {
562 $urlfield = $this->createElement('input');
563 $urlfield->setAttribute('type', 'url');
564 if (!is_null($id)) { $urlfield->setAttribute('id', $id); }
565 $urlfield->setAttribute('name', $name);
566 $urlfield->setAttribute('maxlength', $maxlength);
567 $urlfield->setAttribute('size', $size);
568 if (!is_null($value)) { $urlfield->setAttribute('value', $value); }
569 return $urlfield;
570 }
571
572 public function createElementInputEmail($name, $maxlength, $size, $id = null, $value = null) {
573 $mailfield = $this->createElement('input');
574 $mailfield->setAttribute('type', 'email');
575 if (!is_null($id)) { $mailfield->setAttribute('id', $id); }
576 $mailfield->setAttribute('name', $name);
577 $mailfield->setAttribute('maxlength', $maxlength);
578 $mailfield->setAttribute('size', $size);
579 if (!is_null($value)) { $mailfield->setAttribute('value', $value); }
580 return $mailfield;
581 }
582
583 public function createElementInputTel($name, $maxlength, $size, $id = null, $value = null) {
584 $telfield = $this->createElement('input');
585 $telfield->setAttribute('type', 'tel');
586 if (!is_null($id)) { $telfield->setAttribute('id', $id); }
587 $telfield->setAttribute('name', $name);
588 $telfield->setAttribute('maxlength', $maxlength);
589 $telfield->setAttribute('size', $size);
590 if (!is_null($value)) { $telfield->setAttribute('value', $value); }
591 return $telfield;
592 }
593
594 public function createElementInputDate($name, $id = null, $min = null, $max = null, $value = null) {
b5897591
RK
595 $dtfield = $this->createElement('input');
596 $dtfield->setAttribute('type', 'date');
597 if (!is_null($id)) { $dtfield->setAttribute('id', $id); }
598 $dtfield->setAttribute('name', $name);
599 if (!is_null($min)) { $dtfield->setAttribute('min', $min); }
600 if (!is_null($max)) { $dtfield->setAttribute('max', $max); }
601 if (!is_null($value)) { $dtfield->setAttribute('value', $value); }
602 return $dtfield;
f925e4a1
RK
603 }
604
605 public function createElementInputTime($name, $id = null, $min = null, $max = null, $value = null) {
b5897591
RK
606 $timefield = $this->createElement('input');
607 $timefield->setAttribute('type', 'time');
608 if (!is_null($id)) { $timefield->setAttribute('id', $id); }
609 $timefield->setAttribute('name', $name);
610 if (!is_null($min)) { $timefield->setAttribute('min', $min); }
611 if (!is_null($max)) { $timefield->setAttribute('max', $max); }
612 if (!is_null($value)) { $timefield->setAttribute('value', $value); }
613 return $timefield;
f925e4a1
RK
614 }
615
616 public function createElementInputColor($name, $id = null, $value = null) {
617 $colfield = $this->createElement('input');
618 $colfield->setAttribute('type', 'color');
619 if (!is_null($id)) { $colfield->setAttribute('id', $id); }
620 $colfield->setAttribute('name', $name);
621 if (!is_null($value)) { $colfield->setAttribute('value', $value); }
622 return $colfield;
623 }
624
61cc8aa9 625 public function createElementInputRadio($name, $id, $value, $checked) {
ae41c41b
RK
626 $radio = $this->createElement('input');
627 $radio->setAttribute('type', 'radio');
628 $radio->setAttribute('name', $name);
4bb9d784 629 if (!is_null($id)) { $radio->setAttribute('id', $id); }
ae41c41b
RK
630 $radio->setAttribute('value', $value);
631 if ($checked) { $radio->setAttribute('checked', ''); }
632 return $radio;
633 }
634
61cc8aa9 635 public function createElementInputCheckbox($name, $id, $value, $checked) {
ae41c41b
RK
636 $cbox = $this->createElement('input');
637 $cbox->setAttribute('type', 'checkbox');
638 $cbox->setAttribute('name', $name);
4bb9d784 639 if (!is_null($id)) { $cbox->setAttribute('id', $id); }
ae41c41b
RK
640 $cbox->setAttribute('value', $value);
641 if ($checked) { $cbox->setAttribute('checked', ''); }
642 return $cbox;
643 }
644
61cc8aa9 645 public function createElementInputFile($name, $id, $accept) {
4bb9d784
RK
646 $fileinput = $this->createElement('input');
647 $fileinput->setAttribute('type', 'file');
648 $fileinput->setAttribute('name', $name);
649 if (!is_null($id)) { $fileinput->setAttribute('id', $id); }
650 $fileinput->setAttribute('accept', $accept);
651 return $fileinput;
652 }
653
61cc8aa9 654 public function createElementInputSubmit($value) {
ae41c41b
RK
655 $submitbtn = $this->createElement('input');
656 $submitbtn->setAttribute('type', 'submit');
657 $submitbtn->setAttribute('value', $value);
658 return $submitbtn;
659 }
660
61cc8aa9 661 public function createElementButton($value, $onclick = null) {
1109f526
RK
662 $btn = $this->createElement('input');
663 $btn->setAttribute('type', 'button');
664 $btn->setAttribute('value', $value);
665 if (!is_null($onclick)) { $btn->setAttribute('onclick', $onclick); }
666 return $btn;
667 }
668
61cc8aa9 669 public function createElementTextArea($name, $columns, $rows, $id = null, $value = null) {
bf91764c
RK
670 $txtarea = $this->createElement('textarea', $value);
671 $txtarea->setAttribute('name', $name);
672 $txtarea->setAttribute('cols', $columns);
673 $txtarea->setAttribute('rows', $rows);
674 if (!is_null($id)) { $txtarea->setAttribute('id', $id); }
675 return $txtarea;
676 }
677
69409ddb 678 public function createElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
bf91764c
RK
679 $select = $this->createElement('select');
680 $select->setAttribute('name', $name);
681 if (!is_null($id)) { $select->setAttribute('id', $id); }
682 foreach ($options as $key => $desc) {
69409ddb 683 $select->appendElementOption($key, $desc, $strictmatch ? ($key === $default) : ($key == $default));
bf91764c
RK
684 }
685 return $select;
686 }
687
61cc8aa9 688 public function createElementOption($key, $desc, $selected = false) {
bf91764c 689 $option = $this->createElement('option', $desc);
c79be4c2
RK
690 if ($key) {
691 $option->setAttribute('value', $key);
692 }
bf91764c
RK
693 if ($selected) { $option->setAttribute('selected', ''); }
694 return $option;
695 }
696
61cc8aa9 697 public function createElementLabel($for_id, $value) {
ae41c41b
RK
698 $label = $this->createElement('label', $value);
699 $label->setAttribute('for', $for_id);
700 return $label;
cea5b93a
RK
701 }
702
c79be4c2
RK
703 public function createElementDatalist($id = null, $options = array()) {
704 $select = $this->createElement('datalist');
705 if (!is_null($id)) { $select->setAttribute('id', $id); }
706 foreach ($options as $key => $desc) {
707 $select->appendElementOption($key, $desc);
708 }
709 return $select;
710 }
711
acaa2daa
RK
712 public function createElementStyle($styledata) {
713 $style_elem = $this->createElement('style');
714 // Note: type can/should be left out for HTML5.
715 $style_elem->setAttribute('type', 'text/css');
716 $style_elem->appendChild($this->createCDATASection($styledata));
717 return $style_elem;
718 }
719
61cc8aa9 720 public function createElementJS($jsdata) {
cea5b93a 721 $jselem = $this->createElement('script');
ea32f7e5 722 // Note: type can/should be left out for HTML5.
cea5b93a
RK
723 $jselem->setAttribute('type', 'text/javascript');
724 $jselem->appendChild($this->createCDATASection($jsdata));
725 return $jselem;
726 }
c1666e91 727
8afa2e61 728 public function createElementJSFile($jsURL, $defer = false, $async = false) {
ea32f7e5
RK
729 $jselem = $this->createElement('script');
730 // Note: type can/should be left out for HTML5.
731 $jselem->setAttribute('type', 'text/javascript');
8afa2e61
RK
732 if ($defer) {
733 $jselem->setAttribute('defer', '');
734 }
735 if ($async) {
736 $jselem->setAttribute('async', '');
737 }
ea32f7e5
RK
738 $jselem->setAttribute('src', $jsURL);
739 return $jselem;
740 }
cea5b93a
RK
741}
742
743class ExtendedElement extends DOMElement {
744 // ExtendedElement PHP class
745 // this extends the general PHP DOM Element class to simplify some usual constructs
746 //
61cc8aa9 747 // public function appendElement($name, [$value])
cea5b93a
RK
748 // appends a DOMDocument::createElement() as a child of this element (see there for params)
749 // returns the new child
750 //
61cc8aa9 751 // public function appendElementXML($name, $xmldata)
cea5b93a
RK
752 // appends a DOMDocument::createElement() with the given name as a child of this element,
753 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
754 // returns the new child
755 //
b3cc0fef 756 // public function appendLink($target, [$value], [$title])
cea5b93a
RK
757 // appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
758 // returns the new child
759 //
61cc8aa9 760 // public function appendImage($src, [$alt_text])
53510e9e
RK
761 // appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
762 // returns the new child
763 //
61cc8aa9 764 // public function appendForm($action, $method, $name, [$id])
b8d16837
RK
765 // appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
766 // returns the new child
767 //
61cc8aa9 768 // public function appendFormDiv($action, $method, $name, [$id])
ae41c41b
RK
769 // appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
770 // returns an HTML <div> that is a child of the new child
771 //
61cc8aa9 772 // public function appendInputHidden($name, $value)
ae41c41b
RK
773 // appends an ExtendedDocument::createElementInputHidden() as a child of this element (see there for params)
774 // returns the new child
775 //
61cc8aa9 776 // public function appendInputText($name, $maxlength, $size, [$id], [$value])
ae41c41b
RK
777 // appends an ExtendedDocument::createElementInputText() as a child of this element (see there for params)
778 // returns the new child
779 //
f925e4a1
RK
780 // public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
781 // appends an ExtendedDocument::createElementInputPassword() as a child of this element (see there for params)
782 // returns the new child
783 //
61cc8aa9 784 // public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
262e0bbb
RK
785 // appends an ExtendedDocument::createElementInputNumber() as a child of this element (see there for params)
786 // returns the new child
787 //
f925e4a1
RK
788 // public function appendInputRange($name, $id, $min, $max, [$step], [$value])
789 // appends an ExtendedDocument::createElementInputRange() as a child of this element (see there for params)
790 // returns the new child
791 //
6f794328
RK
792 // public function appendInputSearch($name, $maxlength, $size, [$id], [$value])
793 // appends an ExtendedDocument::createElementInputSearch() as a child of this element (see there for params)
794 // returns the new child
795 //
f925e4a1
RK
796 // public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
797 // appends an ExtendedDocument::createElementInputUrl() as a child of this element (see there for params)
798 // returns the new child
799 //
ea32f7e5
RK
800 // public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
801 // appends an ExtendedDocument::createElementInputEmail() as a child of this element (see there for params)
802 // returns the new child
803 //
f925e4a1
RK
804 // public function appendInputTel($name, $maxlength, $size, [$id], [$value])
805 // appends an ExtendedDocument::createElementInputTel() as a child of this element (see there for params)
806 // returns the new child
807 //
808 // public function appendInputDate($name, [$id], [$min], [$max], [$value])
809 // appends an ExtendedDocument::createElementInputDate() as a child of this element (see there for params)
810 // returns the new child
811 //
812 // public function appendInputTime($name, [$id], [$min], [$max], [$value])
813 // appends an ExtendedDocument::createElementInputTime() as a child of this element (see there for params)
645a7eb6
RK
814 // returns the new child
815 //
f925e4a1
RK
816 // public function appendInputColor($name, [$id], [$value])
817 // appends an ExtendedDocument::createElementInputColor() as a child of this element (see there for params)
818 //
61cc8aa9 819 // public function appendInputRadio($name, $id, $value, $checked)
ae41c41b
RK
820 // appends an ExtendedDocument::createElementInputRadio() as a child of this element (see there for params)
821 // returns the new child
822 //
61cc8aa9 823 // public function appendInputCheckbox($name, $id, $value, $checked)
ae41c41b
RK
824 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this element (see there for params)
825 // returns the new child
826 //
61cc8aa9 827 // public function appendInputFile($name, $id, $accept)
4bb9d784
RK
828 // appends an ExtendedDocument::createElementInputFile() as a child of this element (see there for params)
829 // returns the new child
830 //
61cc8aa9 831 // public function appendInputSubmit($value)
ae41c41b
RK
832 // appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
833 // returns the new child
834 //
61cc8aa9 835 // public function appendButton($value, $onclick = null)
1109f526
RK
836 // appends an ExtendedDocument::createElementButton() as a child of this element (see there for params)
837 // returns the new child
838 //
61cc8aa9 839 // public function appendTextArea($name, $columns, $rows, [$id], [$value])
bf91764c
RK
840 // appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
841 // returns the new child
842 //
69409ddb 843 // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
bf91764c
RK
844 // appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
845 // returns the new child
846 //
61cc8aa9 847 // public function appendElementOption($key, $desc, [$selected])
bf91764c
RK
848 // appends an ExtendedDocument::createElementOption() as a child of this element (see there for params)
849 // returns the new child
850 //
61cc8aa9 851 // public function appendLabel($for_id, $value)
ae41c41b
RK
852 // appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
853 // returns the new child
854 //
c79be4c2
RK
855 // public function appendElementDatalist([$id], [$options])
856 // appends an ExtendedDocument::createElementDatalist() as a child of this element (see there for params)
857 // returns the new child
858 //
61cc8aa9 859 // public function appendText($text)
cea5b93a
RK
860 // appends a DOMDocument::createTextNode() as a child of this element (see there for params)
861 // returns the new child
862 //
71771b0c
RK
863 // public function appendLinebreak()
864 // appends a <br> as a child of this element
865 // returns the new child
866 //
6638efd5
RK
867 // public function appendEntity($name)
868 // appends a DOMDocument::createEntityReference() as a child of this element (see there for params)
869 // returns the new child
870 //
61cc8aa9 871 // public function appendComment($comment_data)
cadc5980
RK
872 // appends a DOMDocument::createComment() as a child of this element (see there for params)
873 // returns the new child
874 //
3d4db013
RK
875 // public function appendClonedElement($dom_element, [$deep])
876 // appends a clone of the given DOMElement as a child of this element
877 // the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
878 // returns the new child
879 //
61cc8aa9 880 // public function appendHTMLMarkup($htmldata)
ae41c41b
RK
881 // appends a representation of the HTML data as children of this element
882 // NO return value!
883 //
61cc8aa9 884 // public function appendXMLMarkup($xmldata)
ae41c41b 885 // appends a representation of the XML data as children of this element
cea5b93a
RK
886 // NO return value!
887 //
acaa2daa
RK
888 // public function appendStyleElement($styledata)
889 // appends an ExtendedDocument::createElementStyle() as a child of this element (see there for params)
890 // returns the new child
891 //
61cc8aa9 892 // public function appendJSElement($jsdata)
cea5b93a 893 // appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
3d4db013 894 // returns the new child
c1666e91 895 //
8afa2e61 896 // public function appendJSFile($jsURL, [$defer], [$async])
0bbc241e 897 // appends an ExtendedDocument::createElementJSFile() as a child of this element (see there for params)
ea32f7e5 898 // returns the new child
f887cdcb
RK
899 //
900 // public function setClass($classname)
901 // sets the 'class' attribute of the element to the given classname value
902 //
903 // public function addClass($classname)
904 // adds the given classname value to the space-separated list in the 'class' attribute
905 // returns the value of the 'class' attribute
906 //
907 // public function setID($elem_id)
908 // sets the 'id' attribute of the element to the given elem_id value
cea5b93a 909
61cc8aa9 910 public function appendElement($name, $value = '') {
a8816f43 911 return $this->appendChild($this->ownerDocument->createElement($name, $value));
cea5b93a 912 }
61cc8aa9 913 public function appendElementXML($name, $xmldata) {
cea5b93a 914 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
ae41c41b 915 $aelem->appendXMLMarkup($xmldata);
cea5b93a
RK
916 return $aelem;
917 }
b3cc0fef
RK
918 public function appendLink($target, $value = '', $title = null) {
919 return $this->appendChild($this->ownerDocument->createElementLink($target, $value, $title));
cea5b93a 920 }
61cc8aa9 921 public function appendImage($src, $alt_text = '') {
53510e9e
RK
922 return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
923 }
61cc8aa9 924 public function appendForm($action, $method, $name, $id = null) {
b8d16837
RK
925 return $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
926 }
61cc8aa9 927 public function appendFormDiv($action, $method, $name, $id = null) {
b8d16837 928 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
ae41c41b
RK
929 return $formelem->appendElement('div');
930 }
61cc8aa9 931 public function appendInputHidden($name, $value) {
ae41c41b
RK
932 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
933 }
61cc8aa9 934 public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
ae41c41b
RK
935 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
936 }
f925e4a1
RK
937 public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
938 return $this->appendChild($this->ownerDocument->createElementInputPassword($name, $maxlength, $size, $id, $value));
939 }
61cc8aa9 940 public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
262e0bbb
RK
941 return $this->appendChild($this->ownerDocument->createElementInputNumber($name, $maxlength, $size, $id, $value));
942 }
f925e4a1
RK
943 public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
944 return $this->appendChild($this->ownerDocument->createElementInputRange($name, $id, $min, $max, $step, $value));
945 }
6f794328
RK
946 public function appendInputSearch($name, $maxlength, $size, $id = null, $value = null) {
947 return $this->appendChild($this->ownerDocument->createElementInputSearch($name, $maxlength, $size, $id, $value));
948 }
f925e4a1
RK
949 public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
950 return $this->appendChild($this->ownerDocument->createElementInputUrl($name, $maxlength, $size, $id, $value));
951 }
ea32f7e5
RK
952 public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
953 return $this->appendChild($this->ownerDocument->createElementInputEmail($name, $maxlength, $size, $id, $value));
954 }
f925e4a1
RK
955 public function appendInputTel($name, $maxlength, $size, $id = null, $value = null) {
956 return $this->appendChild($this->ownerDocument->createElementInputTel($name, $maxlength, $size, $id, $value));
957 }
958 public function appendInputDate($name, $id = null, $min = null, $max = null, $value = null) {
959 return $this->appendChild($this->ownerDocument->createElementInputDate($name, $id, $min, $max, $value));
960 }
961 public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
962 return $this->appendChild($this->ownerDocument->createElementInputTime($name, $id, $min, $max, $value));
963 }
964 public function appendInputColor($name, $id = null, $value = null) {
965 return $this->appendChild($this->ownerDocument->createElementInputColor($name, $id, $value));
645a7eb6 966 }
61cc8aa9 967 public function appendInputRadio($name, $id, $value, $checked) {
ae41c41b
RK
968 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
969 }
61cc8aa9 970 public function appendInputCheckbox($name, $id, $value, $checked) {
ae41c41b
RK
971 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
972 }
61cc8aa9 973 public function appendInputFile($name, $id, $accept) {
4bb9d784
RK
974 return $this->appendChild($this->ownerDocument->createElementInputFile($name, $id, $accept));
975 }
61cc8aa9 976 public function appendInputSubmit($value) {
ae41c41b
RK
977 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
978 }
61cc8aa9 979 public function appendButton($value, $onclick = null) {
1109f526
RK
980 return $this->appendChild($this->ownerDocument->createElementButton($value, $onclick));
981 }
61cc8aa9 982 public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
bf91764c
RK
983 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
984 }
69409ddb
RK
985 public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
986 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
bf91764c 987 }
61cc8aa9 988 public function appendElementOption($key, $desc, $selected = false) {
bf91764c
RK
989 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
990 }
61cc8aa9 991 public function appendLabel($for_id, $value) {
bf91764c
RK
992 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
993 }
c79be4c2
RK
994 public function appendElementDatalist($id = null, $options = array()) {
995 return $this->appendChild($this->ownerDocument->createElementDatalist($id, $options));
996 }
61cc8aa9 997 public function appendText($text) {
c79be4c2 998 return $this->appendChild($this->ownerDocument->createTextNode($text ?? ''));
bf91764c 999 }
71771b0c
RK
1000 public function appendLinebreak() {
1001 return $this->appendChild($this->ownerDocument->createElement('br'));
1002 }
6638efd5
RK
1003 public function appendEntity($name) {
1004 return $this->appendChild($this->ownerDocument->createEntityReference($name));
1005 }
61cc8aa9 1006 public function appendComment($comment_data) {
cadc5980
RK
1007 return $this->appendChild($this->ownerDocument->createComment($comment_data));
1008 }
3d4db013
RK
1009 public function appendClonedElement($dom_element, $deep = true) {
1010 return $this->appendChild($dom_element->cloneNode($deep));
1011 }
61cc8aa9 1012 public function appendHTMLMarkup($htmldata) {
bf91764c
RK
1013 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
1014 }
61cc8aa9 1015 public function appendXMLMarkup($xmldata) {
bf91764c
RK
1016 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
1017 }
acaa2daa
RK
1018 public function appendStyleElement($styledata) {
1019 return $this->appendChild($this->ownerDocument->createElementStyle($styledata));
1020 }
61cc8aa9 1021 public function appendJSElement($jsdata) {
3d4db013 1022 return $this->appendChild($this->ownerDocument->createElementJS($jsdata));
bf91764c 1023 }
1a4ee0f0
RK
1024 public function appendJSFile($jsURL, $defer = false, $async = false) {
1025 return $this->appendChild($this->ownerDocument->createElementJSFile($jsURL, $defer, $async));
ea32f7e5 1026 }
f887cdcb
RK
1027 public function setClass($classname) {
1028 $this->setAttribute('class', $classname);
1029 }
1030 public function addClass($classname) {
1031 $classval = $this->getAttribute('class');
1032 if (strlen($classval)) { $classval .= ' '; }
1033 $classval .= $classname;
1034 $this->setClass($classval);
1035 return $classval;
1036 }
1037 public function setID($elem_id) {
1038 $this->setAttribute('id', $elem_id);
1039 }
bf91764c
RK
1040}
1041
1042class ExtendedDocumentFragment extends DOMDocumentFragment {
1043 // ExtendedDocumentFragment PHP class
1044 // this extends the general PHP DOM Document Fragment class to simplify some usual constructs
1045 //
61cc8aa9 1046 // public function appendElement($name, [$value])
bf91764c
RK
1047 // appends a DOMDocument::createElement() as a child of this fragment (see there for params)
1048 // returns the new child
1049 //
61cc8aa9 1050 // public function appendElementXML($name, $xmldata)
bf91764c
RK
1051 // appends a DOMDocument::createElement() with the given name as a child of this fragment,
1052 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
1053 // returns the new child
1054 //
b3cc0fef 1055 // public function appendLink($target, [$value], [$title])
bf91764c
RK
1056 // appends an ExtendedDocument::createElementLink() as a child of this fragment (see there for params)
1057 // returns the new child
1058 //
61cc8aa9 1059 // public function appendImage($src, [$alt_text])
53510e9e
RK
1060 // appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
1061 // returns the new child
1062 //
61cc8aa9 1063 // public function appendForm($action, $method, $name, [$id])
b8d16837
RK
1064 // appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
1065 // returns the new child
1066 //
61cc8aa9 1067 // public function appendFormDiv($action, $method, $name, [$id])
bf91764c
RK
1068 // appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
1069 // returns an HTML <div> that is a child of the new child
1070 //
61cc8aa9 1071 // public function appendInputHidden($name, $value)
bf91764c
RK
1072 // appends an ExtendedDocument::createElementInputHidden() as a child of this fragment (see there for params)
1073 // returns the new child
1074 //
61cc8aa9 1075 // public function appendInputText($name, $maxlength, $size, [$id], [$value])
bf91764c
RK
1076 // appends an ExtendedDocument::createElementInputText() as a child of this fragment (see there for params)
1077 // returns the new child
1078 //
f925e4a1
RK
1079 // public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
1080 // appends an ExtendedDocument::createElementInputPassword() as a child of this fragment (see there for params)
1081 // returns the new child
1082 //
61cc8aa9 1083 // public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
262e0bbb
RK
1084 // appends an ExtendedDocument::createElementInputNumber() as a child of this fragment (see there for params)
1085 // returns the new child
1086 //
f925e4a1
RK
1087 // public function appendInputRange($name, $id, $min, $max, [$step], [$value])
1088 // appends an ExtendedDocument::createElementInputRange() as a child of this fragment (see there for params)
1089 // returns the new child
1090 //
6f794328
RK
1091 // public function appendInputSearch($name, $maxlength, $size, [$id], [$value])
1092 // appends an ExtendedDocument::createElementInputSearch() as a child of this fragment (see there for params)
1093 // returns the new child
1094 //
f925e4a1
RK
1095 // public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
1096 // appends an ExtendedDocument::createElementInputUrl() as a child of this fragment (see there for params)
1097 // returns the new child
1098 //
ea32f7e5
RK
1099 // public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
1100 // appends an ExtendedDocument::createElementInputEmail() as a child of this fragment (see there for params)
1101 // returns the new child
1102 //
f925e4a1
RK
1103 // public function appendInputTel($name, $maxlength, $size, [$id], [$value])
1104 // appends an ExtendedDocument::createElementInputTel() as a child of this fragment (see there for params)
645a7eb6
RK
1105 // returns the new child
1106 //
f925e4a1
RK
1107 // public function appendInputDate($name, [$id], [$min], [$max], [$value])
1108 // appends an ExtendedDocument::createElementInputDate() as a child of this fragment (see there for params)
1109 // returns the new child
1110 //
1111 // public function appendInputTime($name, [$id], [$min], [$max], [$value])
1112 // appends an ExtendedDocument::createElementInputTime() as a child of this fragment (see there for params)
1113 // returns the new child
1114 //
1115 // public function appendInputColor($name, [$id], [$value])
1116 // appends an ExtendedDocument::createElementInputColor() as a child of this fragment (see there for params)
1117 //
61cc8aa9 1118 // public function appendInputRadio($name, $id, $value, $checked)
bf91764c
RK
1119 // appends an ExtendedDocument::createElementInputRadio() as a child of this fragment (see there for params)
1120 // returns the new child
1121 //
61cc8aa9 1122 // public function appendInputCheckbox($name, $id, $value, $checked)
bf91764c
RK
1123 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this fragment (see there for params)
1124 // returns the new child
1125 //
61cc8aa9 1126 // public function appendInputFile($name, $id, $accept)
4bb9d784
RK
1127 // appends an ExtendedDocument::createElementInputFile() as a child of this fragment (see there for params)
1128 // returns the new child
1129 //
61cc8aa9 1130 // public function appendInputSubmit($value)
bf91764c
RK
1131 // appends an ExtendedDocument::createElementInputSubmit() as a child of this fragment (see there for params)
1132 // returns the new child
1133 //
61cc8aa9 1134 // public function appendButton($value, $onclick = null)
1109f526
RK
1135 // appends an ExtendedDocument::createElementButton() as a child of this fragment (see there for params)
1136 // returns the new child
1137 //
61cc8aa9 1138 // public function appendTextArea($name, $columns, $rows, [$id], [$value])
bf91764c
RK
1139 // appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
1140 // returns the new child
1141 //
69409ddb 1142 // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
bf91764c
RK
1143 // appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
1144 // returns the new child
1145 //
61cc8aa9 1146 // public function appendElementOption($key, $desc, [$selected])
bf91764c
RK
1147 // appends an ExtendedDocument::createElementOption() as a child of this fragment (see there for params)
1148 // returns the new child
1149 //
61cc8aa9 1150 // public function appendLabel($for_id, $value)
bf91764c
RK
1151 // appends an ExtendedDocument::createElementLabel() as a child of this fragment (see there for params)
1152 // returns the new child
1153 //
c79be4c2
RK
1154 // public function appendElementDatalist([$id], [$options])
1155 // appends an ExtendedDocument::createElementDatalist() as a child of this fragment (see there for params)
1156 // returns the new child
1157 //
61cc8aa9 1158 // public function appendText($text)
bf91764c
RK
1159 // appends a DOMDocument::createTextNode() as a child of this fragment (see there for params)
1160 // returns the new child
1161 //
71771b0c
RK
1162 // public function appendLinebreak()
1163 // appends a <br> as a child of this fragment
1164 // returns the new child
1165 //
6638efd5
RK
1166 // public function appendEntity($name)
1167 // appends a DOMDocument::createEntityReference() as a child of this fragment (see there for params)
1168 // returns the new child
1169 //
61cc8aa9 1170 // public function appendComment($comment_data)
cadc5980
RK
1171 // appends a DOMDocument::createComment() as a child of this fragment (see there for params)
1172 // returns the new child
1173 //
3d4db013
RK
1174 // public function appendClonedElement($dom_element, [$deep])
1175 // appends a clone of the given DOMElement as a child of this fragment
1176 // the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
1177 // returns the new child
1178 //
61cc8aa9 1179 // public function appendHTMLMarkup($htmldata)
bf91764c
RK
1180 // appends a representation of the HTML data as children of this fragment
1181 // NO return value!
1182 //
61cc8aa9 1183 // public function appendXMLMarkup($xmldata)
bf91764c
RK
1184 // appends a representation of the XML data as children of this fragment
1185 // NO return value!
1186 //
acaa2daa
RK
1187 // public function appendStyleElement($styledata)
1188 // appends an ExtendedDocument::createElementStyle() as a child of this element (see there for params)
1189 // returns the new child
1190 //
61cc8aa9 1191 // public function appendJSElement($jsdata)
bf91764c 1192 // appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
3d4db013 1193 // returns the new child
c1666e91 1194 //
8afa2e61 1195 // public function appendJSFile($jsURL, [$defer], [$async])
0bbc241e 1196 // appends an ExtendedDocument::createElementJSFile() as a child of this fragment (see there for params)
ea32f7e5 1197 // returns the new child
bf91764c 1198
61cc8aa9 1199 public function appendElement($name, $value = '') {
a8816f43 1200 return $this->appendChild($this->ownerDocument->createElement($name, $value));
bf91764c 1201 }
61cc8aa9 1202 public function appendElementXML($name, $xmldata) {
bf91764c
RK
1203 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
1204 $aelem->appendXMLMarkup($xmldata);
1205 return $aelem;
1206 }
b3cc0fef
RK
1207 public function appendLink($target, $value = '', $title = null) {
1208 return $this->appendChild($this->ownerDocument->createElementLink($target, $value, $title));
bf91764c 1209 }
61cc8aa9 1210 public function appendImage($src, $alt_text = '') {
53510e9e
RK
1211 return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
1212 }
61cc8aa9 1213 public function appendForm($action, $method, $name, $id = null) {
b8d16837
RK
1214 return $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
1215 }
61cc8aa9 1216 public function appendFormDiv($action, $method, $name, $id = null) {
b8d16837 1217 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
bf91764c
RK
1218 return $formelem->appendElement('div');
1219 }
61cc8aa9 1220 public function appendInputHidden($name, $value) {
bf91764c
RK
1221 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
1222 }
61cc8aa9 1223 public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
bf91764c 1224 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
645a7eb6 1225 }
f925e4a1
RK
1226 public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
1227 return $this->appendChild($this->ownerDocument->createElementInputPassword($name, $maxlength, $size, $id, $value));
1228 }
61cc8aa9 1229 public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
262e0bbb
RK
1230 return $this->appendChild($this->ownerDocument->createElementInputNumber($name, $maxlength, $size, $id, $value));
1231 }
f925e4a1
RK
1232 public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
1233 return $this->appendChild($this->ownerDocument->createElementInputRange($name, $id, $min, $max, $step, $value));
1234 }
6f794328
RK
1235 public function appendInputSearch($name, $maxlength, $size, $id = null, $value = null) {
1236 return $this->appendChild($this->ownerDocument->createElementInputSearch($name, $maxlength, $size, $id, $value));
1237 }
f925e4a1
RK
1238 public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
1239 return $this->appendChild($this->ownerDocument->createElementInputUrl($name, $maxlength, $size, $id, $value));
1240 }
ea32f7e5
RK
1241 public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
1242 return $this->appendChild($this->ownerDocument->createElementInputEmail($name, $maxlength, $size, $id, $value));
1243 }
f925e4a1
RK
1244 public function appendInputTel($name, $maxlength, $size, $id = null, $value = null) {
1245 return $this->appendChild($this->ownerDocument->createElementInputTel($name, $maxlength, $size, $id, $value));
1246 }
1247 public function appendInputDate($name, $id = null, $min = null, $max = null, $value = null) {
1248 return $this->appendChild($this->ownerDocument->createElementInputDate($name, $id, $min, $max, $value));
1249 }
1250 public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
1251 return $this->appendChild($this->ownerDocument->createElementInputTime($name, $id, $min, $max, $value));
1252 }
1253 public function appendInputColor($name, $id = null, $value = null) {
1254 return $this->appendChild($this->ownerDocument->createElementInputColor($name, $id, $value));
bf91764c 1255 }
61cc8aa9 1256 public function appendInputRadio($name, $id, $value, $checked) {
bf91764c
RK
1257 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
1258 }
61cc8aa9 1259 public function appendInputCheckbox($name, $id, $value, $checked) {
bf91764c
RK
1260 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
1261 }
61cc8aa9 1262 public function appendInputFile($name, $id, $accept) {
4bb9d784
RK
1263 return $this->appendChild($this->ownerDocument->createElementInputFile($name, $id, $accept));
1264 }
61cc8aa9 1265 public function appendInputSubmit($value) {
bf91764c
RK
1266 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
1267 }
61cc8aa9 1268 public function appendButton($value, $onclick = null) {
1109f526
RK
1269 return $this->appendChild($this->ownerDocument->createElementButton($value, $onclick));
1270 }
61cc8aa9 1271 public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
bf91764c
RK
1272 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
1273 }
69409ddb
RK
1274 public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
1275 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
bf91764c 1276 }
61cc8aa9 1277 public function appendElementOption($key, $desc, $selected = false) {
bf91764c
RK
1278 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
1279 }
61cc8aa9 1280 public function appendLabel($for_id, $value) {
ae41c41b
RK
1281 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
1282 }
c79be4c2
RK
1283 public function appendElementDatalist($id = null, $options = array()) {
1284 return $this->appendChild($this->ownerDocument->createElementDatalist($id, $options));
1285 }
61cc8aa9 1286 public function appendText($text) {
cea5b93a
RK
1287 return $this->appendChild($this->ownerDocument->createTextNode($text));
1288 }
71771b0c
RK
1289 public function appendLinebreak() {
1290 return $this->appendChild($this->ownerDocument->createElement('br'));
1291 }
6638efd5
RK
1292 public function appendEntity($name) {
1293 return $this->appendChild($this->ownerDocument->createEntityReference($name));
1294 }
61cc8aa9 1295 public function appendComment($comment_data) {
cadc5980
RK
1296 return $this->appendChild($this->ownerDocument->createComment($comment_data));
1297 }
3d4db013
RK
1298 public function appendClonedElement($dom_element, $deep = true) {
1299 return $this->appendChild($dom_element->cloneNode($deep));
1300 }
61cc8aa9 1301 public function appendHTMLMarkup($htmldata) {
ae41c41b
RK
1302 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
1303 }
61cc8aa9 1304 public function appendXMLMarkup($xmldata) {
ae41c41b 1305 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
cea5b93a 1306 }
acaa2daa
RK
1307 public function appendStyleElement($styledata) {
1308 return $this->appendChild($this->ownerDocument->createElementStyle($styledata));
1309 }
61cc8aa9 1310 public function appendJSElement($jsdata) {
3d4db013 1311 return $this->appendChild($this->ownerDocument->createElementJS($jsdata));
cea5b93a 1312 }
1a4ee0f0
RK
1313 public function appendJSFile($jsURL, $defer = false, $async = false) {
1314 return $this->appendChild($this->ownerDocument->createElementJSFile($jsURL, $defer, $async));
ea32f7e5 1315 }
cea5b93a
RK
1316}
1317?>