create new extended DOM classes and start using those, making working with DOM more...
[php-utility-classes.git] / include / cbsm / util / document.php-class
CommitLineData
cea5b93a
RK
1<?php
2/* ***** BEGIN LICENSE BLOCK *****
3 *
4 * The contents of this file are subject to Austrian copyright reegulations
5 * ("Urheberrecht"); you may not use this file except in compliance with
6 * those laws.
7 * This contents and any derived work, if it gets distributed in any way,
8 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
9 * either express or implied.
10 *
11 * The Original Code is KaiRo's extended DOM document classes.
12 *
13 * The Initial Developer of the Original Code is
14 * KaiRo - Robert Kaiser.
15 * Portions created by the Initial Developer are Copyright (C) 2010
16 * the Initial Developer. All Rights Reserved.
17 *
18 * Contributor(s): Robert Kaiser <kairo@kairo.at>
19 *
20 * ***** END LICENSE BLOCK ***** */
21
22class ExtendedDocument extends DOMDocument {
23 // ExtendedDocument PHP class
24 // this extends the general PHP DOM Document class to simplify some usual constructs
25 //
26 // function __construct([$version], [$encoding])
27 // CONSTRUCTOR
28 // construct a new DOM Document that uses our element definitions
29 //
30 // function appendElement($name, [$value])
31 // appends a DOMDocument::createElement() as a child of this document (see there for params)
32 // returns the new child
33 //
34 // function appendElementXML($name, $xmldata)
35 // appends a DOMDocument::createElement() with the given name as a child of this document,
36 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
37 // returns the new child
38 //
39 // function appendLink($target, [$value])
40 // appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
41 // returns the new child
42 //
43 // function appendText($text)
44 // appends a DOMDocument::createTextNode() as a child of this document (see there for params)
45 // returns the new child
46 //
47 // function appendRawMarkup($markupdata)
48 // appends an ExtendedDocument::createElementRawMarkup() as a child of this document (see there for params)
49 // NO return value!
50 //
51 // function appendJSElement($jsdata)
52 // appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
53 // NO return value!
54 //
55 // function createXMLFragment($xmldata)
56 // returns a DOMDocumentFragment containing the DOM of the given XML data
57 //
58 // function createElementLink($target, [$value])
59 // returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
60 //
61 // function createElementRawMarkup($markupdata)
62 // returns a DOMNode containing a representation of the given raw markup data
63 //
64 // function createElementJS($jsdata)
65 // returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
66
67 function __construct($version = '1.0', $encoding = 'utf-8') {
68 // make sure the default DOMDocument constructor runs
69 parent::__construct($version, $encoding);
70 $this->registerNodeClass('DOMElement', 'ExtendedElement');
71 }
72
73 function appendElement($name, $value = '') {
74 return $this->appendChild($this->createElement($name, $value));
75 }
76 function appendElementXML($name, $xmldata) {
77 $aelem = $this->appendChild($this->createElement($name));
78 $aelem->appendChild($this->createXMLFragment($xmldata));
79 return $aelem;
80 }
81 function appendLink($target, $value = '') {
82 return $this->appendChild($this->createElementLink($target, $value));
83 }
84 function appendText($text) {
85 return $this->appendChild($this->createTextNode($text));
86 }
87 function appendRawMarkup($markupdata) {
88 $this->appendChild($this->createElementRawMarkup($markupdata));
89 }
90 function appendJSElement($jsdata) {
91 $this->appendChild($this->createElementJS($jsdata));
92 }
93
94 function createXMLFragment($xmldata) {
95 $xmlfragment = $this->createDocumentFragment();
96 $xmlfragment->appendXML($xmldata);
97 return $xmlfragment;
98 }
99
100 function createElementLink($target, $value = '') {
101 $link = $this->createElement('a', $value);
102 $link->setAttribute('href', $target);
103 return $link;
104 }
105
106 function createElementRawMarkup($markupdata) {
107 // XXX: just a workaround for now!
108 return $this->createCDATASection($markupdata);
109 }
110
111 function createElementJS($jsdata) {
112 $jselem = $this->createElement('script');
113 $jselem->setAttribute('type', 'text/javascript');
114 $jselem->appendChild($this->createCDATASection($jsdata));
115 return $jselem;
116 }
117}
118
119class ExtendedElement extends DOMElement {
120 // ExtendedElement PHP class
121 // this extends the general PHP DOM Element class to simplify some usual constructs
122 //
123 // function appendElement($name, [$value])
124 // appends a DOMDocument::createElement() as a child of this element (see there for params)
125 // returns the new child
126 //
127 // function appendElementXML($name, $xmldata)
128 // appends a DOMDocument::createElement() with the given name as a child of this element,
129 // with an ExtendedDocument::createXMLFragment() of the given XML data inside
130 // returns the new child
131 //
132 // function appendLink($target, [$value])
133 // appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
134 // returns the new child
135 //
136 // function appendText($text)
137 // appends a DOMDocument::createTextNode() as a child of this element (see there for params)
138 // returns the new child
139 //
140 // function appendRawMarkup($markupdata)
141 // appends an ExtendedDocument::createElementRawMarkup() as a child of this element (see there for params)
142 // NO return value!
143 //
144 // function appendJSElement($jsdata)
145 // appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
146 // NO return value!
147
148 function appendElement($name, $value = '') {
149 return $this->appendChild($this->ownerDocument->createElement($name, $value));
150 }
151 function appendElementXML($name, $xmldata) {
152 $aelem = $this->appendChild($this->ownerDocument->createElement($name));
153 $aelem->appendChild($this->ownerDocument->createXMLFragment($xmldata));
154 return $aelem;
155 }
156 function appendLink($target, $value = '') {
157 return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
158 }
159 function appendText($text) {
160 return $this->appendChild($this->ownerDocument->createTextNode($text));
161 }
162 function appendRawMarkup($markupdata) {
163 $this->appendChild($this->ownerDocument->createElementRawMarkup($markupdata));
164 }
165 function appendJSElement($jsdata) {
166 $this->appendChild($this->ownerDocument->createElementJS($jsdata));
167 }
168}
169?>