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