add shorthand functions to set/add classes and set IDs on elements
[php-utility-classes.git] / classes / document.php-class
... / ...
CommitLineData
1<?php
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/. */
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 //
14 // static function initHTML5([$doc])
15 // initialize as an HTML5 document and return references to its basic elements.
16 // If a $doc is handed over (an ExtendedDocument or a derived class), load the content into that document.
17 // returns an associative array with the following elements: 'html', 'head', 'title', 'body'
18 //
19 // public function appendElement($name, [$value])
20 // appends a DOMDocument::createElement() as a child of this document (see there for params)
21 // returns the new child
22 //
23 // public function appendElementXML($name, $xmldata)
24 // appends a DOMDocument::createElement() with the given name as a child of this document,
25 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
26 // returns the new child
27 //
28 // public function appendLink($target, [$value])
29 // appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
30 // returns the new child
31 //
32 // public function appendImage($src, [$alt_text])
33 // appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
34 // returns the new child
35 //
36 // public function appendForm($action, $method, $name, [$id])
37 // appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
38 // returns the new child
39 //
40 // public function appendFormDiv($action, $method, $name, [$id])
41 // appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
42 // returns an HTML <div> that is a child of the new child
43 //
44 // public function appendInputHidden($name, $value)
45 // appends an ExtendedDocument::createElementInputHidden() as a child of this document (see there for params)
46 // returns the new child
47 //
48 // public function appendInputText($name, $maxlength, $size, [$id], [$value])
49 // appends an ExtendedDocument::createElementInputText() as a child of this document (see there for params)
50 // returns the new child
51 //
52 // public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
53 // appends an ExtendedDocument::createElementInputNumber() as a child of this document (see there for params)
54 // returns the new child
55 //
56 // public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
57 // appends an ExtendedDocument::createElementInputEmail() as a child of this document (see there for params)
58 // returns the new child
59 //
60 // public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
61 // appends an ExtendedDocument::createElementInputPassword() as a child of this document (see there for params)
62 // returns the new child
63 //
64 // public function appendInputRadio($name, $id, $value, $checked)
65 // appends an ExtendedDocument::createElementInputRadio() as a child of this document (see there for params)
66 // returns the new child
67 //
68 // public function appendInputCheckbox($name, $id, $value, $checked)
69 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this document (see there for params)
70 // returns the new child
71 //
72 // public function appendInputFile($name, $id, $accept)
73 // appends an ExtendedDocument::createElementInputFile() as a child of this document (see there for params)
74 // returns the new child
75 //
76 // public function appendInputSubmit($value)
77 // appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
78 // returns the new child
79 //
80 // public function appendButton($value, $onclick = null)
81 // appends an ExtendedDocument::createElementButton() as a child of this document (see there for params)
82 // returns the new child
83 //
84 // public function appendTextArea($name, $columns, $rows, [$id], [$value])
85 // appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
86 // returns the new child
87 //
88 // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
89 // appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
90 // returns the new child
91 //
92 // public function appendElementOption($key, $desc, [$selected])
93 // appends an ExtendedDocument::createElementOption() as a child of this document (see there for params)
94 // returns the new child
95 //
96 // public function appendLabel($for_id, $value)
97 // appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
98 // returns the new child
99 //
100 // public function appendText($text)
101 // appends a DOMDocument::createTextNode() as a child of this document (see there for params)
102 // returns the new child
103 //
104 // public function appendEntity($name)
105 // appends a DOMDocument::createEntityReference() as a child of this document (see there for params)
106 // returns the new child
107 //
108 // public function appendComment($comment_data)
109 // appends a DOMDocument::createComment() as a child of this document (see there for params)
110 // returns the new child
111 //
112 // public function appendHTMLMarkup($htmldata, [$parentNode])
113 // appends a representation of the HTML data as children of the given parent node, by default this document
114 // NO return value!
115 //
116 // public function appendXMLMarkup($xmldata, [$parentNode])
117 // appends a representation of the XML data as children of the given parent node, by default this document
118 // NO return value!
119 //
120 // public function appendJSElement($jsdata)
121 // appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
122 // NO return value!
123 //
124 // public function appendJSFile($jsURL, [$defer], [$async])
125 // appends an ExtendedDocument::createElementJSFile() as a child of this document (see there for params)
126 // returns the new child
127 //
128 // public function createElementLink($target, [$value])
129 // returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
130 //
131 // public function createElementImage($src, [$alt_text])
132 // returns an ExtendedElement that is an HTML <img> with the given (src) and alt attributes (set to '' by default)
133 //
134 // public function createElementForm($action, $method, $name)
135 // returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
136 // with the given action, method, and name
137 //
138 // public function createElementInputHidden($name, $value)
139 // returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
140 //
141 // public function createElementInputText($name, $maxlength, $size, [$id], [$value])
142 // returns an ExtendedElement that is an HTML <input> of type 'text' with the given name, maxlength, size,
143 // and optionally id and value
144 //
145 // public function createElementInputNumber($name, $maxlength, $size, [$id], [$value])
146 // returns an ExtendedElement that is an HTML <input> of type 'number' with the given name, maxlength, size,
147 // and optionally id and value
148 //
149 // public function createElementInputEmail($name, $maxlength, $size, [$id], [$value])
150 // returns an ExtendedElement that is an HTML <input> of type 'email' with the given name, maxlength, size,
151 // and optionally id and value
152 //
153 // public function createElementInputPassword($name, $maxlength, $size, [$id], [$value])
154 // returns an ExtendedElement that is an HTML <input> of type 'password' with the given name, maxlength, size,
155 // and optionally id and value
156 //
157 // public function createElementInputRadio($name, $id, $value, $checked)
158 // returns an ExtendedElement that is an HTML <input> of type 'radio' with the given name, id, value and
159 // checked state
160 //
161 // public function createElementInputCheckbox($name, $id, $value, $checked)
162 // returns an ExtendedElement that is an HTML <input> of type 'checkbox' with the given name, id, value and
163 // checked state
164 //
165 // public function createElementInputFile($name, $id, $accept)
166 // returns an ExtendedElement that is an HTML <input> of type 'file' with the given name, id and accept
167 //
168 // public function createElementInputSubmit($value)
169 // returns an ExtendedElement that is an HTML <input> of type 'submit' with the given value as label
170 //
171 // public function createElementButton($value, $onclick = null)
172 // returns an ExtendedElement that is an HTML button with the given value as label and optionally onclick attribute
173 //
174 // public function createElementTextArea($name, $columns, $rows, [$id], [$value])
175 // returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
176 // and optionally id and value
177 //
178 // public function createElementSelect($name, [$id], [$options], [$default], [$strictmatch])
179 // returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
180 // array of options (key => description) and key of the by-default selected entry (matched including type when strictmatch is true)
181 //
182 // public function createElementOption($key, $desc, [$selected])
183 // returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
184 // and optionally bool that tells if the entry is selected
185 //
186 // public function createElementLabel($for_id, $value)
187 // returns an ExtendedElement that is an HTML <label> with the given 'for' and value
188 //
189 // public function createElementJS($jsdata)
190 // returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
191 //
192 // public function createElementJSFile($jsURL, [$defer], [$async])
193 // returns an ExtendedElement that is an HTML <script> of JavaScript type linking to the file given by the URL
194 // $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.
195
196 function __construct($version = '1.0', $encoding = 'utf-8') {
197 // make sure the default DOMDocument constructor runs
198 parent::__construct($version, $encoding);
199 $this->registerNodeClass('DOMElement', 'ExtendedElement');
200 $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
201 }
202
203 static function initHTML5($doc = null) {
204 if (is_null($doc)) { $doc = new ExtendedDocument(); }
205 $doc->loadHTML('<!DOCTYPE html><html></html>'); // this seems to be the only way to get the DOCTYPE set properly.
206
207 // Created basic HTML document structure.
208 $root = $doc->getElementsByTagName('html')->item(0);
209 $head = $root->appendElement('head');
210 $title = $head->appendElement('title');
211 $body = $root->appendElement('body');
212
213 return array('document' => $doc,
214 'html' => $root,
215 'head' => $head,
216 'title' => $title,
217 'body' => $body);
218 }
219
220 public function appendElement($name, $value = '') {
221 return $this->appendChild($this->createElement($name, $value));
222 }
223 public function appendElementXML($name, $xmldata) {
224 $aelem = $this->appendChild($this->createElement($name));
225 $aelem->appendXMLMarkup($xmldata);
226 //$aelem->appendChild($this->createXMLFragment($xmldata));
227 return $aelem;
228 }
229 public function appendLink($target, $value = '') {
230 return $this->appendChild($this->createElementLink($target, $value));
231 }
232 public function appendImage($src, $alt_text = '') {
233 return $this->appendChild($this->createElementImage($src, $alt_text));
234 }
235 public function appendForm($action, $method, $name, $id = null) {
236 return $this->appendChild($this->createElementForm($action, $method, $name, $id));
237 }
238 public function appendFormDiv($action, $method, $name, $id = null) {
239 $formelem = $this->appendChild($this->createElementForm($action, $method, $name, $id));
240 return $formelem->appendElement('div');
241 }
242 public function appendInputHidden($name, $value) {
243 return $this->appendChild($this->createElementInputHidden($name, $value));
244 }
245 public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
246 return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
247 }
248 public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
249 return $this->appendChild($this->createElementInputNumber($name, $maxlength, $size, $id, $value));
250 }
251 public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
252 return $this->appendChild($this->createElementInputEmail($name, $maxlength, $size, $id, $value));
253 }
254 public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
255 return $this->appendChild($this->createElementInputPassword($name, $maxlength, $size, $id, $value));
256 }
257 public function appendInputRadio($name, $id, $value, $checked) {
258 return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
259 }
260 public function appendInputCheckbox($name, $id, $value, $checked) {
261 return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
262 }
263 public function appendInputFile($name, $id, $accept) {
264 return $this->appendChild($this->createElementInputFile($name, $id, $accept));
265 }
266 public function appendInputSubmit($value) {
267 return $this->appendChild($this->createElementInputSubmit($value));
268 }
269 public function appendButton($value, $onclick = null) {
270 return $this->appendChild($this->createElementButton($value, $onclick));
271 }
272 public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
273 return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
274 }
275 public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
276 return $this->appendChild($this->createElementSelect($name, $id, $options, $default, $strictmatch));
277 }
278 public function appendElementOption($key, $desc, $selected = false) {
279 return $this->appendChild($this->createElementOption($key, $desc, $selected));
280 }
281 public function appendLabel($for_id, $value) {
282 return $this->appendChild($this->createElementLabel($for_id, $value));
283 }
284 public function appendText($text) {
285 return $this->appendChild($this->createTextNode($text));
286 }
287 public function appendEntity($name) {
288 return $this->appendChild($this->createEntityReference($name));
289 }
290 public function appendComment($comment_data) {
291 return $this->appendChild($this->createComment($comment_data));
292 }
293 public function appendJSElement($jsdata) {
294 $this->appendChild($this->createElementJS($jsdata));
295 }
296 public function appendJSFile($jsdata, $defer = false, $async = false) {
297 return $this->appendChild($this->createElementJSFile($jsdata, $defer, $async));
298 }
299
300 public function appendHTMLMarkup($htmldata, $parentNode = null) {
301 if (is_null($parentNode)) { $parentNode =& $this; }
302 // Use loadHTML() to parse and turn the markup into proper HTML.
303 $tmpdoc = new ExtendedDocument;
304 // The XML line is needed to tell the parser that we need UTF-8 parsing.
305 $tmpdoc->loadHTML('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html><body>'.$htmldata.'</body></html>');
306 foreach ($tmpdoc->getElementsByTagName('body')->item(0)->childNodes as $child) {
307 $parentNode->appendChild($this->importNode($child, true));
308 }
309 }
310
311 public function appendXMLMarkup($xmldata, $parentNode = null) {
312 if (is_null($parentNode)) { $parentNode =& $this; }
313 $tmpdoc = new ExtendedDocument;
314 $tmpxml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
315 $tmpxml .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
316 $tmpxml .= '<root>'.$xmldata.'</root>';
317 $tmpdoc->loadXML($tmpxml);
318 foreach ($tmpdoc->getElementsByTagName('root')->item(0)->childNodes as $child) {
319 $parentNode->appendChild($this->importNode($child, true));
320 }
321 }
322
323 public function createElement($name, $value = '') {
324 // Adding the $value in DOMDocument's createElement does NOT escape it, so override it and use appendText to support that.
325 $aelem = parent::createElement($name);
326 $aelem->appendText($value);
327 return $aelem;
328 }
329
330 public function createElementLink($target, $value = '') {
331 $link = $this->createElement('a', $value);
332 $link->setAttribute('href', $target); // XXX: take care of & etc. in links
333 return $link;
334 }
335
336 public function createElementImage($src, $alt_text = '') {
337 $img = $this->createElement('img');
338 $img->setAttribute('src', $src);
339 $img->setAttribute('alt', $alt_text);
340 return $img;
341 }
342
343 public function createElementForm($action, $method, $name, $id = null) {
344 $formelem = $this->createElement('form');
345 $formelem->setAttribute('action', $action);
346 $formelem->setAttribute('method', $method);
347 $formelem->setAttribute('name', $name);
348 $formelem->setAttribute('id', $id);
349 return $formelem;
350 }
351
352 public function createElementInputHidden($name, $value) {
353 $hidden = $this->createElement('input');
354 $hidden->setAttribute('type', 'hidden');
355 $hidden->setAttribute('name', $name);
356 $hidden->setAttribute('value', $value);
357 return $hidden;
358 }
359
360 public function createElementInputText($name, $maxlength, $size, $id = null, $value = null) {
361 $txfield = $this->createElement('input');
362 $txfield->setAttribute('type', 'text');
363 if (!is_null($id)) { $txfield->setAttribute('id', $id); }
364 $txfield->setAttribute('name', $name);
365 $txfield->setAttribute('maxlength', $maxlength);
366 $txfield->setAttribute('size', $size);
367 if (!is_null($value)) { $txfield->setAttribute('value', $value); }
368 return $txfield;
369 }
370
371 public function createElementInputNumber($name, $maxlength, $size, $id = null, $value = null) {
372 $txfield = $this->createElement('input');
373 $txfield->setAttribute('type', 'number');
374 if (!is_null($id)) { $txfield->setAttribute('id', $id); }
375 $txfield->setAttribute('name', $name);
376 $txfield->setAttribute('maxlength', $maxlength);
377 $txfield->setAttribute('size', $size);
378 if (!is_null($value)) { $txfield->setAttribute('value', $value); }
379 return $txfield;
380 }
381
382 public function createElementInputEmail($name, $maxlength, $size, $id = null, $value = null) {
383 $txfield = $this->createElement('input');
384 $txfield->setAttribute('type', 'email');
385 if (!is_null($id)) { $txfield->setAttribute('id', $id); }
386 $txfield->setAttribute('name', $name);
387 $txfield->setAttribute('maxlength', $maxlength);
388 $txfield->setAttribute('size', $size);
389 if (!is_null($value)) { $txfield->setAttribute('value', $value); }
390 return $txfield;
391 }
392
393 public function createElementInputPassword($name, $maxlength, $size, $id = null, $value = null) {
394 $pwfield = $this->createElement('input');
395 $pwfield->setAttribute('type', 'password');
396 if (!is_null($id)) { $pwfield->setAttribute('id', $id); }
397 $pwfield->setAttribute('name', $name);
398 $pwfield->setAttribute('maxlength', $maxlength);
399 $pwfield->setAttribute('size', $size);
400 if (!is_null($value)) { $pwfield->setAttribute('value', $value); }
401 return $pwfield;
402 }
403
404 public function createElementInputRadio($name, $id, $value, $checked) {
405 $radio = $this->createElement('input');
406 $radio->setAttribute('type', 'radio');
407 $radio->setAttribute('name', $name);
408 if (!is_null($id)) { $radio->setAttribute('id', $id); }
409 $radio->setAttribute('value', $value);
410 if ($checked) { $radio->setAttribute('checked', ''); }
411 return $radio;
412 }
413
414 public function createElementInputCheckbox($name, $id, $value, $checked) {
415 $cbox = $this->createElement('input');
416 $cbox->setAttribute('type', 'checkbox');
417 $cbox->setAttribute('name', $name);
418 if (!is_null($id)) { $cbox->setAttribute('id', $id); }
419 $cbox->setAttribute('value', $value);
420 if ($checked) { $cbox->setAttribute('checked', ''); }
421 return $cbox;
422 }
423
424 public function createElementInputFile($name, $id, $accept) {
425 $fileinput = $this->createElement('input');
426 $fileinput->setAttribute('type', 'file');
427 $fileinput->setAttribute('name', $name);
428 if (!is_null($id)) { $fileinput->setAttribute('id', $id); }
429 $fileinput->setAttribute('accept', $accept);
430 return $fileinput;
431 }
432
433 public function createElementInputSubmit($value) {
434 $submitbtn = $this->createElement('input');
435 $submitbtn->setAttribute('type', 'submit');
436 $submitbtn->setAttribute('value', $value);
437 return $submitbtn;
438 }
439
440 public function createElementButton($value, $onclick = null) {
441 $btn = $this->createElement('input');
442 $btn->setAttribute('type', 'button');
443 $btn->setAttribute('value', $value);
444 if (!is_null($onclick)) { $btn->setAttribute('onclick', $onclick); }
445 return $btn;
446 }
447
448 public function createElementTextArea($name, $columns, $rows, $id = null, $value = null) {
449 $txtarea = $this->createElement('textarea', $value);
450 $txtarea->setAttribute('name', $name);
451 $txtarea->setAttribute('cols', $columns);
452 $txtarea->setAttribute('rows', $rows);
453 if (!is_null($id)) { $txtarea->setAttribute('id', $id); }
454 return $txtarea;
455 }
456
457 public function createElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
458 $select = $this->createElement('select');
459 $select->setAttribute('name', $name);
460 if (!is_null($id)) { $select->setAttribute('id', $id); }
461 foreach ($options as $key => $desc) {
462 $select->appendElementOption($key, $desc, $strictmatch ? ($key === $default) : ($key == $default));
463 }
464 return $select;
465 }
466
467 public function createElementOption($key, $desc, $selected = false) {
468 $option = $this->createElement('option', $desc);
469 $option->setAttribute('value', $key);
470 if ($selected) { $option->setAttribute('selected', ''); }
471 return $option;
472 }
473
474 public function createElementLabel($for_id, $value) {
475 $label = $this->createElement('label', $value);
476 $label->setAttribute('for', $for_id);
477 return $label;
478 }
479
480 public function createElementJS($jsdata) {
481 $jselem = $this->createElement('script');
482 // Note: type can/should be left out for HTML5.
483 $jselem->setAttribute('type', 'text/javascript');
484 $jselem->appendChild($this->createCDATASection($jsdata));
485 return $jselem;
486 }
487
488 public function createElementJSFile($jsURL, $defer = false, $async = false) {
489 $jselem = $this->createElement('script');
490 // Note: type can/should be left out for HTML5.
491 $jselem->setAttribute('type', 'text/javascript');
492 if ($defer) {
493 $jselem->setAttribute('defer', '');
494 }
495 if ($async) {
496 $jselem->setAttribute('async', '');
497 }
498 $jselem->setAttribute('src', $jsURL);
499 return $jselem;
500 }
501}
502
503class ExtendedElement extends DOMElement {
504 // ExtendedElement PHP class
505 // this extends the general PHP DOM Element class to simplify some usual constructs
506 //
507 // public function appendElement($name, [$value])
508 // appends a DOMDocument::createElement() as a child of this element (see there for params)
509 // returns the new child
510 //
511 // public function appendElementXML($name, $xmldata)
512 // appends a DOMDocument::createElement() with the given name as a child of this element,
513 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
514 // returns the new child
515 //
516 // public function appendLink($target, [$value])
517 // appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
518 // returns the new child
519 //
520 // public function appendImage($src, [$alt_text])
521 // appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
522 // returns the new child
523 //
524 // public function appendForm($action, $method, $name, [$id])
525 // appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
526 // returns the new child
527 //
528 // public function appendFormDiv($action, $method, $name, [$id])
529 // appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
530 // returns an HTML <div> that is a child of the new child
531 //
532 // public function appendInputHidden($name, $value)
533 // appends an ExtendedDocument::createElementInputHidden() as a child of this element (see there for params)
534 // returns the new child
535 //
536 // public function appendInputText($name, $maxlength, $size, [$id], [$value])
537 // appends an ExtendedDocument::createElementInputText() as a child of this element (see there for params)
538 // returns the new child
539 //
540 // public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
541 // appends an ExtendedDocument::createElementInputNumber() as a child of this element (see there for params)
542 // returns the new child
543 //
544 // public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
545 // appends an ExtendedDocument::createElementInputEmail() as a child of this element (see there for params)
546 // returns the new child
547 //
548 // public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
549 // appends an ExtendedDocument::createElementInputPassword() as a child of this element (see there for params)
550 // returns the new child
551 //
552 // public function appendInputRadio($name, $id, $value, $checked)
553 // appends an ExtendedDocument::createElementInputRadio() as a child of this element (see there for params)
554 // returns the new child
555 //
556 // public function appendInputCheckbox($name, $id, $value, $checked)
557 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this element (see there for params)
558 // returns the new child
559 //
560 // public function appendInputFile($name, $id, $accept)
561 // appends an ExtendedDocument::createElementInputFile() as a child of this element (see there for params)
562 // returns the new child
563 //
564 // public function appendInputSubmit($value)
565 // appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
566 // returns the new child
567 //
568 // public function appendButton($value, $onclick = null)
569 // appends an ExtendedDocument::createElementButton() as a child of this element (see there for params)
570 // returns the new child
571 //
572 // public function appendTextArea($name, $columns, $rows, [$id], [$value])
573 // appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
574 // returns the new child
575 //
576 // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
577 // appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
578 // returns the new child
579 //
580 // public function appendElementOption($key, $desc, [$selected])
581 // appends an ExtendedDocument::createElementOption() as a child of this element (see there for params)
582 // returns the new child
583 //
584 // public function appendLabel($for_id, $value)
585 // appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
586 // returns the new child
587 //
588 // public function appendText($text)
589 // appends a DOMDocument::createTextNode() as a child of this element (see there for params)
590 // returns the new child
591 //
592 // public function appendEntity($name)
593 // appends a DOMDocument::createEntityReference() as a child of this element (see there for params)
594 // returns the new child
595 //
596 // public function appendComment($comment_data)
597 // appends a DOMDocument::createComment() as a child of this element (see there for params)
598 // returns the new child
599 //
600 // public function appendHTMLMarkup($htmldata)
601 // appends a representation of the HTML data as children of this element
602 // NO return value!
603 //
604 // public function appendXMLMarkup($xmldata)
605 // appends a representation of the XML data as children of this element
606 // NO return value!
607 //
608 // public function appendJSElement($jsdata)
609 // appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
610 // NO return value!
611 //
612 // public function appendJSFile($jsURL, [$defer], [$async])
613 // appends an ExtendedDocument::createElementJSFile() as a child of this element (see there for params)
614 // returns the new child
615 //
616 // public function setClass($classname)
617 // sets the 'class' attribute of the element to the given classname value
618 //
619 // public function addClass($classname)
620 // adds the given classname value to the space-separated list in the 'class' attribute
621 // returns the value of the 'class' attribute
622 //
623 // public function setID($elem_id)
624 // sets the 'id' attribute of the element to the given elem_id value
625
626 public function appendElement($name, $value = '') {
627 return $this->appendChild($this->ownerDocument->createElement($name, $value));
628 }
629 public function appendElementXML($name, $xmldata) {
630 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
631 $aelem->appendXMLMarkup($xmldata);
632 return $aelem;
633 }
634 public function appendLink($target, $value = '') {
635 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
636 }
637 public function appendImage($src, $alt_text = '') {
638 return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
639 }
640 public function appendForm($action, $method, $name, $id = null) {
641 return $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
642 }
643 public function appendFormDiv($action, $method, $name, $id = null) {
644 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
645 return $formelem->appendElement('div');
646 }
647 public function appendInputHidden($name, $value) {
648 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
649 }
650 public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
651 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
652 }
653 public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
654 return $this->appendChild($this->ownerDocument->createElementInputNumber($name, $maxlength, $size, $id, $value));
655 }
656 public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
657 return $this->appendChild($this->ownerDocument->createElementInputEmail($name, $maxlength, $size, $id, $value));
658 }
659 public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
660 return $this->appendChild($this->ownerDocument->createElementInputPassword($name, $maxlength, $size, $id, $value));
661 }
662 public function appendInputRadio($name, $id, $value, $checked) {
663 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
664 }
665 public function appendInputCheckbox($name, $id, $value, $checked) {
666 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
667 }
668 public function appendInputFile($name, $id, $accept) {
669 return $this->appendChild($this->ownerDocument->createElementInputFile($name, $id, $accept));
670 }
671 public function appendInputSubmit($value) {
672 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
673 }
674 public function appendButton($value, $onclick = null) {
675 return $this->appendChild($this->ownerDocument->createElementButton($value, $onclick));
676 }
677 public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
678 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
679 }
680 public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
681 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
682 }
683 public function appendElementOption($key, $desc, $selected = false) {
684 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
685 }
686 public function appendLabel($for_id, $value) {
687 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
688 }
689 public function appendText($text) {
690 return $this->appendChild($this->ownerDocument->createTextNode($text));
691 }
692 public function appendEntity($name) {
693 return $this->appendChild($this->ownerDocument->createEntityReference($name));
694 }
695 public function appendComment($comment_data) {
696 return $this->appendChild($this->ownerDocument->createComment($comment_data));
697 }
698 public function appendHTMLMarkup($htmldata) {
699 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
700 }
701 public function appendXMLMarkup($xmldata) {
702 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
703 }
704 public function appendJSElement($jsdata) {
705 $this->appendChild($this->ownerDocument->createElementJS($jsdata));
706 }
707 public function appendJSFile($jsdata, $defer = false, $async = false) {
708 return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
709 }
710 public function setClass($classname) {
711 $this->setAttribute('class', $classname);
712 }
713 public function addClass($classname) {
714 $classval = $this->getAttribute('class');
715 if (strlen($classval)) { $classval .= ' '; }
716 $classval .= $classname;
717 $this->setClass($classval);
718 return $classval;
719 }
720 public function setID($elem_id) {
721 $this->setAttribute('id', $elem_id);
722 }
723}
724
725class ExtendedDocumentFragment extends DOMDocumentFragment {
726 // ExtendedDocumentFragment PHP class
727 // this extends the general PHP DOM Document Fragment class to simplify some usual constructs
728 //
729 // public function appendElement($name, [$value])
730 // appends a DOMDocument::createElement() as a child of this fragment (see there for params)
731 // returns the new child
732 //
733 // public function appendElementXML($name, $xmldata)
734 // appends a DOMDocument::createElement() with the given name as a child of this fragment,
735 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
736 // returns the new child
737 //
738 // public function appendLink($target, [$value])
739 // appends an ExtendedDocument::createElementLink() as a child of this fragment (see there for params)
740 // returns the new child
741 //
742 // public function appendImage($src, [$alt_text])
743 // appends an ExtendedDocument::createElementImage() as a child of this document (see there for params)
744 // returns the new child
745 //
746 // public function appendForm($action, $method, $name, [$id])
747 // appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
748 // returns the new child
749 //
750 // public function appendFormDiv($action, $method, $name, [$id])
751 // appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
752 // returns an HTML <div> that is a child of the new child
753 //
754 // public function appendInputHidden($name, $value)
755 // appends an ExtendedDocument::createElementInputHidden() as a child of this fragment (see there for params)
756 // returns the new child
757 //
758 // public function appendInputText($name, $maxlength, $size, [$id], [$value])
759 // appends an ExtendedDocument::createElementInputText() as a child of this fragment (see there for params)
760 // returns the new child
761 //
762 // public function appendInputNumber($name, $maxlength, $size, [$id], [$value])
763 // appends an ExtendedDocument::createElementInputNumber() as a child of this fragment (see there for params)
764 // returns the new child
765 //
766 // public function appendInputEmail($name, $maxlength, $size, [$id], [$value])
767 // appends an ExtendedDocument::createElementInputEmail() as a child of this fragment (see there for params)
768 // returns the new child
769 //
770 // public function appendInputPassword($name, $maxlength, $size, [$id], [$value])
771 // appends an ExtendedDocument::createElementInputPassword() as a child of this fragment (see there for params)
772 // returns the new child
773 //
774 // public function appendInputRadio($name, $id, $value, $checked)
775 // appends an ExtendedDocument::createElementInputRadio() as a child of this fragment (see there for params)
776 // returns the new child
777 //
778 // public function appendInputCheckbox($name, $id, $value, $checked)
779 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this fragment (see there for params)
780 // returns the new child
781 //
782 // public function appendInputFile($name, $id, $accept)
783 // appends an ExtendedDocument::createElementInputFile() as a child of this fragment (see there for params)
784 // returns the new child
785 //
786 // public function appendInputSubmit($value)
787 // appends an ExtendedDocument::createElementInputSubmit() as a child of this fragment (see there for params)
788 // returns the new child
789 //
790 // public function appendButton($value, $onclick = null)
791 // appends an ExtendedDocument::createElementButton() as a child of this fragment (see there for params)
792 // returns the new child
793 //
794 // public function appendTextArea($name, $columns, $rows, [$id], [$value])
795 // appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
796 // returns the new child
797 //
798 // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
799 // appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
800 // returns the new child
801 //
802 // public function appendElementOption($key, $desc, [$selected])
803 // appends an ExtendedDocument::createElementOption() as a child of this fragment (see there for params)
804 // returns the new child
805 //
806 // public function appendLabel($for_id, $value)
807 // appends an ExtendedDocument::createElementLabel() as a child of this fragment (see there for params)
808 // returns the new child
809 //
810 // public function appendText($text)
811 // appends a DOMDocument::createTextNode() as a child of this fragment (see there for params)
812 // returns the new child
813 //
814 // public function appendEntity($name)
815 // appends a DOMDocument::createEntityReference() as a child of this fragment (see there for params)
816 // returns the new child
817 //
818 // public function appendComment($comment_data)
819 // appends a DOMDocument::createComment() as a child of this fragment (see there for params)
820 // returns the new child
821 //
822 // public function appendHTMLMarkup($htmldata)
823 // appends a representation of the HTML data as children of this fragment
824 // NO return value!
825 //
826 // public function appendXMLMarkup($xmldata)
827 // appends a representation of the XML data as children of this fragment
828 // NO return value!
829 //
830 // public function appendJSElement($jsdata)
831 // appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
832 // NO return value!
833 //
834 // public function appendJSFile($jsURL, [$defer], [$async])
835 // appends an ExtendedDocument::createElementJSFile() as a child of this fragment (see there for params)
836 // returns the new child
837
838 public function appendElement($name, $value = '') {
839 return $this->appendChild($this->ownerDocument->createElement($name, $value));
840 }
841 public function appendElementXML($name, $xmldata) {
842 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
843 $aelem->appendXMLMarkup($xmldata);
844 return $aelem;
845 }
846 public function appendLink($target, $value = '') {
847 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
848 }
849 public function appendImage($src, $alt_text = '') {
850 return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
851 }
852 public function appendForm($action, $method, $name, $id = null) {
853 return $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
854 }
855 public function appendFormDiv($action, $method, $name, $id = null) {
856 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name, $id));
857 return $formelem->appendElement('div');
858 }
859 public function appendInputHidden($name, $value) {
860 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
861 }
862 public function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
863 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
864 }
865 public function appendInputNumber($name, $maxlength, $size, $id = null, $value = null) {
866 return $this->appendChild($this->ownerDocument->createElementInputNumber($name, $maxlength, $size, $id, $value));
867 }
868 public function appendInputEmail($name, $maxlength, $size, $id = null, $value = null) {
869 return $this->appendChild($this->ownerDocument->createElementInputEmail($name, $maxlength, $size, $id, $value));
870 }
871 public function appendInputPassword($name, $maxlength, $size, $id = null, $value = null) {
872 return $this->appendChild($this->ownerDocument->createElementInputPassword($name, $maxlength, $size, $id, $value));
873 }
874 public function appendInputRadio($name, $id, $value, $checked) {
875 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
876 }
877 public function appendInputCheckbox($name, $id, $value, $checked) {
878 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
879 }
880 public function appendInputFile($name, $id, $accept) {
881 return $this->appendChild($this->ownerDocument->createElementInputFile($name, $id, $accept));
882 }
883 public function appendInputSubmit($value) {
884 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
885 }
886 public function appendButton($value, $onclick = null) {
887 return $this->appendChild($this->ownerDocument->createElementButton($value, $onclick));
888 }
889 public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
890 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
891 }
892 public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
893 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
894 }
895 public function appendElementOption($key, $desc, $selected = false) {
896 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
897 }
898 public function appendLabel($for_id, $value) {
899 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
900 }
901 public function appendText($text) {
902 return $this->appendChild($this->ownerDocument->createTextNode($text));
903 }
904 public function appendEntity($name) {
905 return $this->appendChild($this->ownerDocument->createEntityReference($name));
906 }
907 public function appendComment($comment_data) {
908 return $this->appendChild($this->ownerDocument->createComment($comment_data));
909 }
910 public function appendHTMLMarkup($htmldata) {
911 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
912 }
913 public function appendXMLMarkup($xmldata) {
914 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
915 }
916 public function appendJSElement($jsdata) {
917 $this->appendChild($this->ownerDocument->createElementJS($jsdata));
918 }
919 public function appendJSFile($jsdata, $defer = false, $async = false) {
920 return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
921 }
922}
923?>