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 createElementLink($target, [$value])
106 // returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
108 // function createElementForm($action, $method, $name)
109 // returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
110 // with the given action, method, and name
112 // function createElementInputHidden($name, $value)
113 // returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
115 // function createElementInputText($name, $maxlength, $size, [$id], [$value])
116 // returns an ExtendedElement that is an HTML <input> of type 'text' with the given name, maxlength, size,
117 // and optionally id and value
119 // function createElementInputRadio($name, $id, $value, $checked)
120 // returns an ExtendedElement that is an HTML <input> of type 'radio' with the given name, id, value and
123 // function createElementInputCheckbox($name, $id, $value, $checked)
124 // returns an ExtendedElement that is an HTML <input> of type 'checkbox' with the given name, id, value and
127 // function createElementInputSubmit($value)
128 // returns an ExtendedElement that is an HTML <input> of type 'submit' with the given name and value
130 // function createElementTextArea($name, $columns, $rows, [$id], [$value])
131 // returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
132 // and optionally id and value
134 // function createElementSelect($name, [$id], [$options], [$default])
135 // returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
136 // array of options (key => description) and key of the by-default selected entry
138 // function createElementOption($key, $desc, [$selected])
139 // returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
140 // and optionally bool that tells if the entry is selected
142 // function createElementLabel($for_id, $value)
143 // returns an ExtendedElement that is an HTML <label> with the given 'for' and value
145 // function createElementJS($jsdata)
146 // returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
148 function __construct($version = '1.0', $encoding = 'utf-8') {
149 // make sure the default DOMDocument constructor runs
150 parent::__construct($version, $encoding);
151 $this->registerNodeClass('DOMElement', 'ExtendedElement');
152 $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
155 function appendElement($name, $value = '') {
156 return $this->appendChild($this->createElement($name, $value));
158 function appendElementXML($name, $xmldata) {
159 $aelem = $this->appendChild($this->createElement($name));
160 $aelem->appendXMLMarkup($xmldata);
161 //$aelem->appendChild($this->createXMLFragment($xmldata));
164 function appendLink($target, $value = '') {
165 return $this->appendChild($this->createElementLink($target, $value));
167 function appendFormDiv($action, $method, $name) {
168 $formelem = $this->appendChild($this->createElementForm($action, $method, $name));
169 return $formelem->appendElement('div');
171 function appendInputHidden($name, $value) {
172 return $this->appendChild($this->createElementInputHidden($name, $value));
174 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
175 return $this->appendChild($this->createElementInputText($name, $maxlength, $size, $id, $value));
177 function appendInputRadio($name, $id, $value, $checked) {
178 return $this->appendChild($this->createElementInputRadio($name, $id, $value, $checked));
180 function appendInputCheckbox($name, $id, $value, $checked) {
181 return $this->appendChild($this->createElementInputCheckbox($name, $id, $value, $checked));
183 function appendInputSubmit($value) {
184 return $this->appendChild($this->createElementInputSubmit($value));
186 function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
187 return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
189 function appendElementSelect($name, $id = null, $options = array(), $default = null) {
190 return $this->appendChild($this->createElementSelect($name, $id, $options, $default));
192 function appendElementOption($key, $desc, $selected = false) {
193 return $this->appendChild($this->createElementOption($key, $desc, $selected));
195 function appendLabel($for_id, $value) {
196 return $this->appendChild($this->createElementLabel($for_id, $value));
198 function appendText($text) {
199 return $this->appendChild($this->createTextNode($text));
201 function appendJSElement($jsdata) {
202 $this->appendChild($this->createElementJS($jsdata));
205 function appendHTMLMarkup($htmldata, $parentNode = null) {
206 if (is_null($parentNode)) { $parentNode =& $this; }
207 // XXX: just a workaround for now!
208 $parentNode->appendChild($this->createCDATASection($htmldata));
211 function appendXMLMarkup($xmldata, $parentNode = null) {
212 if (is_null($parentNode)) { $parentNode =& $this; }
213 $tmpdoc = new ExtendedDocument;
214 $tmpxml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
215 $tmpxml .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
216 $tmpxml .= '<root>'.$xmldata.'</root>';
217 $tmpdoc->loadXML($tmpxml);
218 foreach ($tmpdoc->getElementsByTagName('root')->item(0)->childNodes as $child) {
219 $parentNode->appendChild($this->importNode($child, true));
223 function createElementLink($target, $value = '') {
224 $link = $this->createElement('a', $value);
225 $link->setAttribute('href', $target); // XXX: take care of & etc. in links
229 function createElementForm($action, $method, $name) {
230 $formelem = $this->createElement('form');
231 $formelem->setAttribute('action', $action);
232 $formelem->setAttribute('method', $method);
233 $formelem->setAttribute('name', $name);
237 function createElementInputHidden($name, $value) {
238 $hidden = $this->createElement('input');
239 $hidden->setAttribute('type', 'hidden');
240 $hidden->setAttribute('name', $name);
241 $hidden->setAttribute('value', $value);
245 function createElementInputText($name, $maxlength, $size, $id = null, $value = null) {
246 $txfield = $this->createElement('input');
247 $txfield->setAttribute('type', 'text');
248 if (!is_null($id)) { $txfield->setAttribute('id', $id); }
249 $txfield->setAttribute('name', $name);
250 $txfield->setAttribute('maxlength', $maxlength);
251 $txfield->setAttribute('size', $size);
252 if (!is_null($value)) { $txfield->setAttribute('value', $value); }
256 function createElementInputRadio($name, $id, $value, $checked) {
257 $radio = $this->createElement('input');
258 $radio->setAttribute('type', 'radio');
259 $radio->setAttribute('name', $name);
260 $radio->setAttribute('id', $id);
261 $radio->setAttribute('value', $value);
262 if ($checked) { $radio->setAttribute('checked', ''); }
266 function createElementInputCheckbox($name, $id, $value, $checked) {
267 $cbox = $this->createElement('input');
268 $cbox->setAttribute('type', 'checkbox');
269 $cbox->setAttribute('name', $name);
270 $cbox->setAttribute('id', $id);
271 $cbox->setAttribute('value', $value);
272 if ($checked) { $cbox->setAttribute('checked', ''); }
276 function createElementInputSubmit($value) {
277 $submitbtn = $this->createElement('input');
278 $submitbtn->setAttribute('type', 'submit');
279 $submitbtn->setAttribute('value', $value);
283 function createElementTextArea($name, $columns, $rows, $id = null, $value = null) {
284 $txtarea = $this->createElement('textarea', $value);
285 $txtarea->setAttribute('name', $name);
286 $txtarea->setAttribute('cols', $columns);
287 $txtarea->setAttribute('rows', $rows);
288 if (!is_null($id)) { $txtarea->setAttribute('id', $id); }
292 function createElementSelect($name, $id = null, $options = array(), $default = null) {
293 $select = $this->createElement('select');
294 $select->setAttribute('name', $name);
295 if (!is_null($id)) { $select->setAttribute('id', $id); }
296 foreach ($options as $key => $desc) {
297 $select->appendElementOption($key, $desc, ($key == $default));
302 function createElementOption($key, $desc, $selected = false) {
303 $option = $this->createElement('option', $desc);
304 $option->setAttribute('value', $key);
305 if ($selected) { $option->setAttribute('selected', ''); }
309 function createElementLabel($for_id, $value) {
310 $label = $this->createElement('label', $value);
311 $label->setAttribute('for', $for_id);
315 function createElementJS($jsdata) {
316 $jselem = $this->createElement('script');
317 $jselem->setAttribute('type', 'text/javascript');
318 $jselem->appendChild($this->createCDATASection($jsdata));
323 class ExtendedElement extends DOMElement {
324 // ExtendedElement PHP class
325 // this extends the general PHP DOM Element class to simplify some usual constructs
327 // function appendElement($name, [$value])
328 // appends a DOMDocument::createElement() as a child of this element (see there for params)
329 // returns the new child
331 // function appendElementXML($name, $xmldata)
332 // appends a DOMDocument::createElement() with the given name as a child of this element,
333 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
334 // returns the new child
336 // function appendLink($target, [$value])
337 // appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
338 // returns the new child
340 // function appendFormDiv($action, $method, $name)
341 // appends an ExtendedDocument::createElementForm() as a child of this element (see there for params)
342 // returns an HTML <div> that is a child of the new child
344 // function appendInputHidden($name, $value)
345 // appends an ExtendedDocument::createElementInputHidden() as a child of this element (see there for params)
346 // returns the new child
348 // function appendInputText($name, $maxlength, $size, [$id], [$value])
349 // appends an ExtendedDocument::createElementInputText() as a child of this element (see there for params)
350 // returns the new child
352 // function appendInputRadio($name, $id, $value, $checked)
353 // appends an ExtendedDocument::createElementInputRadio() as a child of this element (see there for params)
354 // returns the new child
356 // function appendInputCheckbox($name, $id, $value, $checked)
357 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this element (see there for params)
358 // returns the new child
360 // function appendInputSubmit($value)
361 // appends an ExtendedDocument::createElementInputSubmit() as a child of this element (see there for params)
362 // returns the new child
364 // function appendTextArea($name, $columns, $rows, [$id], [$value])
365 // appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
366 // returns the new child
368 // function appendElementSelect($name, [$id], [$options], [$default])
369 // appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
370 // returns the new child
372 // function appendElementOption($key, $desc, [$selected])
373 // appends an ExtendedDocument::createElementOption() as a child of this element (see there for params)
374 // returns the new child
376 // function appendLabel($for_id, $value)
377 // appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
378 // returns the new child
380 // function appendText($text)
381 // appends a DOMDocument::createTextNode() as a child of this element (see there for params)
382 // returns the new child
384 // function appendHTMLMarkup($htmldata)
385 // appends a representation of the HTML data as children of this element
388 // function appendXMLMarkup($xmldata)
389 // appends a representation of the XML data as children of this element
392 // function appendJSElement($jsdata)
393 // appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
396 function appendElement($name, $value = '') {
397 return $this->appendChild($this->ownerDocument->createElement($name, $value));
399 function appendElementXML($name, $xmldata) {
400 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
401 $aelem->appendXMLMarkup($xmldata);
404 function appendLink($target, $value = '') {
405 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
407 function appendFormDiv($action, $method, $name) {
408 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
409 return $formelem->appendElement('div');
411 function appendInputHidden($name, $value) {
412 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
414 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
415 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
417 function appendInputRadio($name, $id, $value, $checked) {
418 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
420 function appendInputCheckbox($name, $id, $value, $checked) {
421 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
423 function appendInputSubmit($value) {
424 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
426 function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
427 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
429 function appendElementSelect($name, $id = null, $options = array(), $default = null) {
430 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
432 function appendElementOption($key, $desc, $selected = false) {
433 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
435 function appendLabel($for_id, $value) {
436 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
438 function appendText($text) {
439 return $this->appendChild($this->ownerDocument->createTextNode($text));
441 function appendHTMLMarkup($htmldata) {
442 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
444 function appendXMLMarkup($xmldata) {
445 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
447 function appendJSElement($jsdata) {
448 $this->appendChild($this->ownerDocument->createElementJS($jsdata));
452 class ExtendedDocumentFragment extends DOMDocumentFragment {
453 // ExtendedDocumentFragment PHP class
454 // this extends the general PHP DOM Document Fragment class to simplify some usual constructs
456 // function appendElement($name, [$value])
457 // appends a DOMDocument::createElement() as a child of this fragment (see there for params)
458 // returns the new child
460 // function appendElementXML($name, $xmldata)
461 // appends a DOMDocument::createElement() with the given name as a child of this fragment,
462 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
463 // returns the new child
465 // function appendLink($target, [$value])
466 // appends an ExtendedDocument::createElementLink() as a child of this fragment (see there for params)
467 // returns the new child
469 // function appendFormDiv($action, $method, $name)
470 // appends an ExtendedDocument::createElementForm() as a child of this fragment (see there for params)
471 // returns an HTML <div> that is a child of the new child
473 // function appendInputHidden($name, $value)
474 // appends an ExtendedDocument::createElementInputHidden() as a child of this fragment (see there for params)
475 // returns the new child
477 // function appendInputText($name, $maxlength, $size, [$id], [$value])
478 // appends an ExtendedDocument::createElementInputText() as a child of this fragment (see there for params)
479 // returns the new child
481 // function appendInputRadio($name, $id, $value, $checked)
482 // appends an ExtendedDocument::createElementInputRadio() as a child of this fragment (see there for params)
483 // returns the new child
485 // function appendInputCheckbox($name, $id, $value, $checked)
486 // appends an ExtendedDocument::createElementInputCheckbox() as a child of this fragment (see there for params)
487 // returns the new child
489 // function appendInputSubmit($value)
490 // appends an ExtendedDocument::createElementInputSubmit() as a child of this fragment (see there for params)
491 // returns the new child
493 // function appendTextArea($name, $columns, $rows, [$id], [$value])
494 // appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
495 // returns the new child
497 // function appendElementSelect($name, [$id], [$options], [$default])
498 // appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
499 // returns the new child
501 // function appendElementOption($key, $desc, [$selected])
502 // appends an ExtendedDocument::createElementOption() as a child of this fragment (see there for params)
503 // returns the new child
505 // function appendLabel($for_id, $value)
506 // appends an ExtendedDocument::createElementLabel() as a child of this fragment (see there for params)
507 // returns the new child
509 // function appendText($text)
510 // appends a DOMDocument::createTextNode() as a child of this fragment (see there for params)
511 // returns the new child
513 // function appendHTMLMarkup($htmldata)
514 // appends a representation of the HTML data as children of this fragment
517 // function appendXMLMarkup($xmldata)
518 // appends a representation of the XML data as children of this fragment
521 // function appendJSElement($jsdata)
522 // appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
525 function appendElement($name, $value = '') {
526 return $this->appendChild($this->ownerDocument->createElement($name, $value));
528 function appendElementXML($name, $xmldata) {
529 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
530 $aelem->appendXMLMarkup($xmldata);
533 function appendLink($target, $value = '') {
534 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
536 function appendFormDiv($action, $method, $name) {
537 $formelem = $this->appendChild($this->ownerDocument->createElementForm($action, $method, $name));
538 return $formelem->appendElement('div');
540 function appendInputHidden($name, $value) {
541 return $this->appendChild($this->ownerDocument->createElementInputHidden($name, $value));
543 function appendInputText($name, $maxlength, $size, $id = null, $value = null) {
544 return $this->appendChild($this->ownerDocument->createElementInputText($name, $maxlength, $size, $id, $value));
546 function appendInputRadio($name, $id, $value, $checked) {
547 return $this->appendChild($this->ownerDocument->createElementInputRadio($name, $id, $value, $checked));
549 function appendInputCheckbox($name, $id, $value, $checked) {
550 return $this->appendChild($this->ownerDocument->createElementInputCheckbox($name, $id, $value, $checked));
552 function appendInputSubmit($value) {
553 return $this->appendChild($this->ownerDocument->createElementInputSubmit($value));
555 function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
556 return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
558 function appendElementSelect($name, $id = null, $options = array(), $default = null) {
559 return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
561 function appendElementOption($key, $desc, $selected = false) {
562 return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
564 function appendLabel($for_id, $value) {
565 return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
567 function appendText($text) {
568 return $this->appendChild($this->ownerDocument->createTextNode($text));
570 function appendHTMLMarkup($htmldata) {
571 $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
573 function appendXMLMarkup($xmldata) {
574 $this->ownerDocument->appendXMLMarkup($xmldata, $this);
576 function appendJSElement($jsdata) {
577 $this->appendChild($this->ownerDocument->createElementJS($jsdata));