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