2 /* ***** BEGIN LICENSE BLOCK *****
4 * The contents of this file are subject to Austrian copyright reegulations
5 * ("Urheberrecht"); you may not use this file except in compliance with
7 * This contents and any derived work, if it gets distributed in any way,
8 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
9 * either express or implied.
11 * The Original Code is KaiRo's extended DOM document classes.
13 * The Initial Developer of the Original Code is
14 * KaiRo - Robert Kaiser.
15 * Portions created by the Initial Developer are Copyright (C) 2010
16 * the Initial Developer. All Rights Reserved.
18 * Contributor(s): Robert Kaiser <kairo@kairo.at>
20 * ***** END LICENSE BLOCK ***** */
22 class ExtendedDocument extends DOMDocument {
23 // ExtendedDocument PHP class
24 // this extends the general PHP DOM Document class to simplify some usual constructs
26 // function __construct([$version], [$encoding])
28 // construct a new DOM Document that uses our element definitions
31 // the default XML header
33 // private $xhtmldtype
34 // the XHTML doctype to use by default
36 // function appendElement($name, [$value])
37 // appends a DOMDocument::createElement() as a child of this document (see there for params)
38 // returns the new child
40 // function appendElementXML($name, $xmldata)
41 // appends a DOMDocument::createElement() with the given name as a child of this document,
42 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
43 // returns the new child
45 // function appendLink($target, [$value])
46 // appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
47 // returns the new child
49 // function appendFormDiv($action, $method, $name)
50 // appends an ExtendedDocument::createElementForm() as a child of this document (see there for params)
51 // returns an HTML <div> that is a child of the new child
53 // function appendInputHidden($name, $value)
54 // appends an ExtendedDocument::createElementInputHidden() as a child of this document (see there for params)
55 // returns the new child
57 // function appendInputText($name, $maxlength, $size, [$id], [$value])
58 // appends an ExtendedDocument::createElementInputText() as a child of this document (see there for params)
59 // returns the new child
61 // function appendInputRadio($name, $id, $value, $checked)
62 // appends an ExtendedDocument::createElementInputRadio() as a child of this document (see there for params)
63 // returns the new child
65 // function appendInputCheckbox($name, $id, $value, $checked)
66 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this document (see there for params)
67 // returns the new child
69 // function appendInputSubmit($value)
70 // appends an ExtendedDocument::createElementInputSubmit() as a child of this document (see there for params)
71 // returns the new child
73 // function appendTextArea($name, $columns, $rows, [$id], [$value])
74 // appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
75 // returns the new child
77 // function appendElementSelect($name, [$id], [$options], [$default])
78 // appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
79 // returns the new child
81 // function appendElementOption($key, $desc, [$selected])
82 // appends an ExtendedDocument::createElementOption() as a child of this document (see there for params)
83 // returns the new child
85 // function appendLabel($for_id, $value)
86 // appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
87 // returns the new child
89 // function appendText($text)
90 // appends a DOMDocument::createTextNode() as a child of this document (see there for params)
91 // returns the new child
93 // function appendHTMLMarkup($htmldata, [$parentNode])
94 // appends a representation of the HTML data as children of the given parent node, by default this document
97 // function appendXMLMarkup($xmldata, [$parentNode])
98 // appends a representation of the XML data as children of the given parent node, by default this document
101 // function appendJSElement($jsdata)
102 // appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
105 // function appendCOMElement($module, $attributes)
106 // appends an ExtendedDocument::createCOMElement() as a child of this document (see there for params)
107 // returns the new child
109 // function createElementLink($target, [$value])
110 // returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
112 // function createElementForm($action, $method, $name)
113 // returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
114 // with the given action, method, and name
116 // function createElementInputHidden($name, $value)
117 // returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
119 // function createElementInputText($name, $maxlength, $size, [$id], [$value])
120 // returns an ExtendedElement that is an HTML <input> of type 'text' with the given name, maxlength, size,
121 // and optionally id and value
123 // function createElementInputRadio($name, $id, $value, $checked)
124 // returns an ExtendedElement that is an HTML <input> of type 'radio' with the given name, id, value and
127 // function createElementInputCheckbox($name, $id, $value, $checked)
128 // returns an ExtendedElement that is an HTML <input> of type 'checkbox' with the given name, id, value and
131 // function createElementInputSubmit($value)
132 // returns an ExtendedElement that is an HTML <input> of type 'submit' with the given name and value
134 // function createElementTextArea($name, $columns, $rows, [$id], [$value])
135 // returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
136 // and optionally id and value
138 // function createElementSelect($name, [$id], [$options], [$default])
139 // returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
140 // array of options (key => description) and key of the by-default selected entry
142 // function createElementOption($key, $desc, [$selected])
143 // returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
144 // and optionally bool that tells if the entry is selected
146 // function createElementLabel($for_id, $value)
147 // returns an ExtendedElement that is an HTML <label> with the given 'for' and value
149 // function createElementJS($jsdata)
150 // returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
152 // function createCOMElement($module, $attributes)
153 // returns an ExtendedElement that is in COM_NS namespace, with the given module as name and the
154 // given name=>value array as attributes
156 function __construct($version = '1.0', $encoding = 'utf-8') {
157 // make sure the default DOMDocument constructor runs
158 parent::__construct($version, $encoding);
159 $this->registerNodeClass('DOMElement', 'ExtendedElement');
160 $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
163 function appendElement($name, $value = '') {
164 return $this->appendChild($this->createElement($name, $value));
166 function appendElementXML($name, $xmldata) {
167 $aelem = $this->appendChild($this->createElement($name));
168 $aelem->appendXMLMarkup($xmldata);
169 //$aelem->appendChild($this->createXMLFragment($xmldata));
172 function appendLink($target, $value = '') {
173 return $this->appendChild($this->createElementLink($target, $value));
175 function appendFormDiv($action, $method, $name) {
176 $formelem = $this->appendChild($this->createElementForm($action, $method, $name));
177 return $formelem->appendElement('div');
179 function appendInputHidden($name, $value) {
180 return $this->appendChild($this->createElementInputHidden($name, $value));
182 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
183 return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
185 function appendInputRadio($name, $id, $value, $checked) {
186 return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
188 function appendInputCheckbox($name, $id, $value, $checked) {
189 return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
191 function appendInputSubmit($value) {
192 return $this->appendChild($this->createElementInputSubmit($value));
194 function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
195 return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
197 function appendElementSelect($name, $id = null, $options = array(), $default = null) {
198 return $this->appendChild($this->createElementSelect($name, $id, $options, $default));
200 function appendElementOption($key, $desc, $selected = false) {
201 return $this->appendChild($this->createElementOption($key, $desc, $selected));
203 function appendLabel($for_id, $value) {
204 return $this->appendChild($this->createElementLabel($for_id, $value));
206 function appendText($text) {
207 return $this->appendChild($this->createTextNode($text));
209 function appendJSElement($jsdata) {
210 $this->appendChild($this->createElementJS($jsdata));
212 function appendCOMElement($module, $attributes) {
213 $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
216 function appendHTMLMarkup($htmldata, $parentNode = null) {
217 if (is_null($parentNode)) { $parentNode =& $this; }
218 // XXX: just a workaround for now!
219 $parentNode->appendChild($this->createCDATASection($htmldata));
222 function appendXMLMarkup($xmldata, $parentNode = null) {
223 if (is_null($parentNode)) { $parentNode =& $this; }
224 $tmpdoc = new ExtendedDocument;
225 $tmpxml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
226 $tmpxml .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
227 $tmpxml .= '<root>'.$xmldata.'</root>';
228 $tmpdoc->loadXML($tmpxml);
229 foreach ($tmpdoc->getElementsByTagName('root')->item(0)->childNodes as $child) {
230 $parentNode->appendChild($this->importNode($child, true));
234 function createElementLink($target, $value = '') {
235 $link = $this->createElement('a', $value);
236 $link->setAttribute('href', $target); // XXX: take care of & etc. in links
240 function createElementForm($action, $method, $name) {
241 $formelem = $this->createElement('form');
242 $formelem->setAttribute('action', $action);
243 $formelem->setAttribute('method', $method);
244 $formelem->setAttribute('name', $name);
248 function createElementInputHidden($name, $value) {
249 $hidden = $this->createElement('input');
250 $hidden->setAttribute('type', 'hidden');
251 $hidden->setAttribute('name', $name);
252 $hidden->setAttribute('value', $value);
256 function createElementInputText($name, $maxlength, $size, $id = null, $value = null) {
257 $txfield = $this->createElement('input');
258 $txfield->setAttribute('type', 'text');
259 if (!is_null($id)) { $txfield->setAttribute('id', $id); }
260 $txfield->setAttribute('name', $name);
261 $txfield->setAttribute('maxlength', $maxlength);
262 $txfield->setAttribute('size', $size);
263 if (!is_null($value)) { $txfield->setAttribute('value', $value); }
267 function createElementInputRadio($name, $id, $value, $checked) {
268 $radio = $this->createElement('input');
269 $radio->setAttribute('type', 'radio');
270 $radio->setAttribute('name', $name);
271 $radio->setAttribute('id', $id);
272 $radio->setAttribute('value', $value);
273 if ($checked) { $radio->setAttribute('checked', ''); }
277 function createElementInputCheckbox($name, $id, $value, $checked) {
278 $cbox = $this->createElement('input');
279 $cbox->setAttribute('type', 'checkbox');
280 $cbox->setAttribute('name', $name);
281 $cbox->setAttribute('id', $id);
282 $cbox->setAttribute('value', $value);
283 if ($checked) { $cbox->setAttribute('checked', ''); }
287 function createElementInputSubmit($value) {
288 $submitbtn = $this->createElement('input');
289 $submitbtn->setAttribute('type', 'submit');
290 $submitbtn->setAttribute('value', $value);
294 function createElementTextArea($name, $columns, $rows, $id = null, $value = null) {
295 $txtarea = $this->createElement('textarea', $value);
296 $txtarea->setAttribute('name', $name);
297 $txtarea->setAttribute('cols', $columns);
298 $txtarea->setAttribute('rows', $rows);
299 if (!is_null($id)) { $txtarea->setAttribute('id', $id); }
303 function createElementSelect($name, $id = null, $options = array(), $default = null) {
304 $select = $this->createElement('select');
305 $select->setAttribute('name', $name);
306 if (!is_null($id)) { $select->setAttribute('id', $id); }
307 foreach ($options as $key => $desc) {
308 $select->appendElementOption($key, $desc, ($key == $default));
313 function createElementOption($key, $desc, $selected = false) {
314 $option = $this->createElement('option', $desc);
315 $option->setAttribute('value', $key);
316 if ($selected) { $option->setAttribute('selected', ''); }
320 function createElementLabel($for_id, $value) {
321 $label = $this->createElement('label', $value);
322 $label->setAttribute('for', $for_id);
326 function createElementJS($jsdata) {
327 $jselem = $this->createElement('script');
328 $jselem->setAttribute('type', 'text/javascript');
329 $jselem->appendChild($this->createCDATASection($jsdata));
333 function createCOMElement($module, $attributes) {
334 $com_elem = $this->createElementNS(COM_NS, $module);
335 if (is_array($attributes) && count($attributes)) {
336 foreach ($attributes as $name=>$value) {
337 $com_elem->setAttribute($name, $value);
344 class ExtendedElement extends DOMElement {
345 // ExtendedElement PHP class
346 // this extends the general PHP DOM Element class to simplify some usual constructs
348 // function appendElement($name, [$value])
349 // appends a DOMDocument::createElement() as a child of this element (see there for params)
350 // returns the new child
352 // function appendElementXML($name, $xmldata)
353 // appends a DOMDocument::createElement() with the given name as a child of this element,
354 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
355 // returns the new child
357 // function appendLink($target, [$value])
358 // appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
359 // returns the new child
361 // function appendFormDiv($action, $method, $name)
362 // appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
363 // returns an HTML <div> that is a child of the new child
365 // function appendInputHidden($name, $value)
366 // appends an ExtendedDocument::createElementInputHidden() as a child of this element (see there for params)
367 // returns the new child
369 // function appendInputText($name, $maxlength, $size, [$id], [$value])
370 // appends an ExtendedDocument::createElementInputText() as a child of this element (see there for params)
371 // returns the new child
373 // function appendInputRadio($name, $id, $value, $checked)
374 // appends an ExtendedDocument::createElementInputRadio() as a child of this element (see there for params)
375 // returns the new child
377 // function appendInputCheckbox($name, $id, $value, $checked)
378 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this element (see there for params)
379 // returns the new child
381 // function appendInputSubmit($value)
382 // appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
383 // returns the new child
385 // function appendTextArea($name, $columns, $rows, [$id], [$value])
386 // appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
387 // returns the new child
389 // function appendElementSelect($name, [$id], [$options], [$default])
390 // appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
391 // returns the new child
393 // function appendElementOption($key, $desc, [$selected])
394 // appends an ExtendedDocument::createElementOption() as a child of this element (see there for params)
395 // returns the new child
397 // function appendLabel($for_id, $value)
398 // appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
399 // returns the new child
401 // function appendText($text)
402 // appends a DOMDocument::createTextNode() as a child of this element (see there for params)
403 // returns the new child
405 // function appendHTMLMarkup($htmldata)
406 // appends a representation of the HTML data as children of this element
409 // function appendXMLMarkup($xmldata)
410 // appends a representation of the XML data as children of this element
413 // function appendJSElement($jsdata)
414 // appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
417 // function appendCOMElement($module, $attributes)
418 // appends an ExtendedDocument::createCOMElement() as a child of this element (see there for params)
419 // returns the new child
421 function appendElement($name, $value = '') {
422 return $this->appendChild($this->ownerDocument->createElement($name, $value));
424 function appendElementXML($name, $xmldata) {
425 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
426 $aelem->appendXMLMarkup($xmldata);
429 function appendLink($target, $value = '') {
430 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
432 function appendFormDiv($action, $method, $name) {
433 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
434 return $formelem->appendElement('div');
436 function appendInputHidden($name, $value) {
437 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
439 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
440 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
442 function appendInputRadio($name, $id, $value, $checked) {
443 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
445 function appendInputCheckbox($name, $id, $value, $checked) {
446 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
448 function appendInputSubmit($value) {
449 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
451 function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
452 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
454 function appendElementSelect($name, $id = null, $options = array(), $default = null) {
455 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
457 function appendElementOption($key, $desc, $selected = false) {
458 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
460 function appendLabel($for_id, $value) {
461 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
463 function appendText($text) {
464 return $this->appendChild($this->ownerDocument->createTextNode($text));
466 function appendHTMLMarkup($htmldata) {
467 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
469 function appendXMLMarkup($xmldata) {
470 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
472 function appendJSElement($jsdata) {
473 $this->appendChild($this->ownerDocument->createElementJS($jsdata));
475 function appendCOMElement($module, $attributes) {
476 $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
480 class ExtendedDocumentFragment extends DOMDocumentFragment {
481 // ExtendedDocumentFragment PHP class
482 // this extends the general PHP DOM Document Fragment class to simplify some usual constructs
484 // function appendElement($name, [$value])
485 // appends a DOMDocument::createElement() as a child of this fragment (see there for params)
486 // returns the new child
488 // function appendElementXML($name, $xmldata)
489 // appends a DOMDocument::createElement() with the given name as a child of this fragment,
490 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
491 // returns the new child
493 // function appendLink($target, [$value])
494 // appends an ExtendedDocument::createElementLink() as a child of this fragment (see there for params)
495 // returns the new child
497 // function appendFormDiv($action, $method, $name)
498 // appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
499 // returns an HTML <div> that is a child of the new child
501 // function appendInputHidden($name, $value)
502 // appends an ExtendedDocument::createElementInputHidden() as a child of this fragment (see there for params)
503 // returns the new child
505 // function appendInputText($name, $maxlength, $size, [$id], [$value])
506 // appends an ExtendedDocument::createElementInputText() as a child of this fragment (see there for params)
507 // returns the new child
509 // function appendInputRadio($name, $id, $value, $checked)
510 // appends an ExtendedDocument::createElementInputRadio() as a child of this fragment (see there for params)
511 // returns the new child
513 // function appendInputCheckbox($name, $id, $value, $checked)
514 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this fragment (see there for params)
515 // returns the new child
517 // function appendInputSubmit($value)
518 // appends an ExtendedDocument::createElementInputSubmit() as a child of this fragment (see there for params)
519 // returns the new child
521 // function appendTextArea($name, $columns, $rows, [$id], [$value])
522 // appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
523 // returns the new child
525 // function appendElementSelect($name, [$id], [$options], [$default])
526 // appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
527 // returns the new child
529 // function appendElementOption($key, $desc, [$selected])
530 // appends an ExtendedDocument::createElementOption() as a child of this fragment (see there for params)
531 // returns the new child
533 // function appendLabel($for_id, $value)
534 // appends an ExtendedDocument::createElementLabel() as a child of this fragment (see there for params)
535 // returns the new child
537 // function appendText($text)
538 // appends a DOMDocument::createTextNode() as a child of this fragment (see there for params)
539 // returns the new child
541 // function appendHTMLMarkup($htmldata)
542 // appends a representation of the HTML data as children of this fragment
545 // function appendXMLMarkup($xmldata)
546 // appends a representation of the XML data as children of this fragment
549 // function appendJSElement($jsdata)
550 // appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
553 // function appendCOMElement($module, $attributes)
554 // appends an ExtendedDocument::createCOMElement() as a child of this fragment (see there for params)
555 // returns the new child
557 function appendElement($name, $value = '') {
558 return $this->appendChild($this->ownerDocument->createElement($name, $value));
560 function appendElementXML($name, $xmldata) {
561 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
562 $aelem->appendXMLMarkup($xmldata);
565 function appendLink($target, $value = '') {
566 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
568 function appendFormDiv($action, $method, $name) {
569 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
570 return $formelem->appendElement('div');
572 function appendInputHidden($name, $value) {
573 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
575 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
576 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
578 function appendInputRadio($name, $id, $value, $checked) {
579 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
581 function appendInputCheckbox($name, $id, $value, $checked) {
582 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
584 function appendInputSubmit($value) {
585 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
587 function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
588 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
590 function appendElementSelect($name, $id = null, $options = array(), $default = null) {
591 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
593 function appendElementOption($key, $desc, $selected = false) {
594 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
596 function appendLabel($for_id, $value) {
597 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
599 function appendText($text) {
600 return $this->appendChild($this->ownerDocument->createTextNode($text));
602 function appendHTMLMarkup($htmldata) {
603 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
605 function appendXMLMarkup($xmldata) {
606 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
608 function appendJSElement($jsdata) {
609 $this->appendChild($this->ownerDocument->createElementJS($jsdata));
611 function appendCOMElement($module, $attributes) {
612 $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));