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