* luci/themes/openwrt.org:
[project/luci.git] / themes / openwrt.org / htdocs / luci-static / openwrt.org / XHTML1.js
1 /*
2 Copyright (C) 2007, 2008  Alina Friedrichsen <x-alina@gmx.net>
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 1. Redistributions of source code must retain the above copyright
8    notice, this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright
10    notice, this list of conditions and the following disclaimer in the
11    documentation and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 SUCH DAMAGE.
24 */
25
26 var XMLNS_XMLNS = "http://www.w3.org/2000/xmlns/";
27 var XMLNS_XML = "http://www.w3.org/XML/1998/namespace";
28 var XMLNS_XHTML = "http://www.w3.org/1999/xhtml";
29
30 function W3CDOM_Event(currentTarget) {
31         VarType.needObject(currentTarget);
32         this.currentTarget = currentTarget;
33         this.preventDefault = function() { window.event.returnValue = false; };
34         return this;
35 }
36
37 function XHTML1() {
38 }
39
40 XHTML1.isDOMSupported = function() {
41         if(!document.getElementById) return false;
42         if(!(window.addEventListener || window.attachEvent)) return false;
43         return true;
44 };
45
46 XHTML1.isXHTML = function() {
47         if(document.documentElement.nodeName == "HTML") return false;
48         return true;
49 };
50
51 XHTML1.addEventListener = function(target, type, listener) {
52         VarType.needObject(target);
53         type = VarType.toStr(type);
54         VarType.needFunction(listener);
55
56         if(target.addEventListener) {
57                 target.addEventListener(type, listener, false);
58         }
59         else if(target.attachEvent) {
60                 target.attachEvent("on" + type, function() { listener(new W3CDOM_Event(target)); } );
61         }
62 };
63
64 XHTML1.createElement = function(tagName) {
65         tagName = VarType.toStr(tagName);
66
67         if(XHTML1.isXHTML()) {
68                 return document.createElementNS(XMLNS_XHTML, tagName.toLowerCase());
69         }
70
71         return document.createElement(tagName.toUpperCase());
72 };
73
74 XHTML1.getElementsByTagName = function(tagName) {
75         tagName = VarType.toStr(tagName);
76
77         if(XHTML1.isXHTML()) {
78                 return document.getElementsByTagNameNS(XMLNS_XHTML, tagName.toLowerCase());
79         }
80
81         return document.getElementsByTagName(tagName.toUpperCase());
82 };
83
84 XHTML1.isElement = function(node, tagName) {
85         VarType.needNode(node);
86         tagName = VarType.toStr(tagName);
87
88         if(node.nodeType == 1) {
89                 if(XHTML1.isXHTML()) {
90                         if(node.namespaceURI == XMLNS_XHTML) {
91                                 if(node.localName == tagName.toLowerCase()) return true;
92                         }
93                 } else {
94                         if(node.nodeName == tagName.toUpperCase()) return true;
95                 }
96         }
97
98         return false;
99 };
100
101 XHTML1.getAttribute = function(element, name) {
102         VarType.needNode(element, 1);
103         name = VarType.toStr(name);
104
105         name = name.toLowerCase();
106
107         if(XHTML1.isXHTML()) {
108                 return element.getAttributeNS(null, name);
109         }
110
111         if(name == "class") {
112                 return element.className;
113         }
114
115         return element.getAttribute(name);
116 };
117
118 XHTML1.setAttribute = function(element, name, value) {
119         VarType.needNode(element, 1);
120         name = VarType.toStr(name);
121         value = VarType.toStr(value);
122
123         name = name.toLowerCase();
124
125         if(XHTML1.isXHTML()) {
126                 element.setAttributeNS(null, name, value);
127                 return;
128         }
129
130         if(name == "class") {
131                 element.className = value;
132                 return;
133         }
134
135         element.setAttribute(name, value);
136 };
137
138 XHTML1.removeAttribute = function(element, name) {
139         VarType.needNode(element, 1);
140         name = VarType.toStr(name);
141
142         name = name.toLowerCase();
143
144         if(XHTML1.isXHTML()) {
145                 element.removeAttributeNS(null, name);
146                 return;
147         }
148
149         if(name == "class") {
150                 element.className = "";
151                 return;
152         }
153
154         element.removeAttribute(name);
155 };
156
157 XHTML1.containsClass = function(element, className) {
158         VarType.needNode(element, 1);
159         className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
160
161         var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
162         var classArray = classString.split(" ");
163         for(var i = 0; i < classArray.length; i++) {
164                 if(classArray[i] == className) return true;
165         }
166
167         return false;
168 };
169
170 XHTML1.addClass = function(element, className) {
171         VarType.needNode(element, 1);
172         className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
173
174         var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
175         var classArray = classString.split(" ");
176         classString = "";
177         for(var i = 0; i < classArray.length; i++) {
178                 if(classArray[i] != className) {
179                         if(classString == "") classString = classArray[i];
180                         else classString += " " + classArray[i];
181                 }
182         }
183
184         if(classString == "") classString = className;
185         else classString += " " + className;
186
187         XHTML1.setAttribute(element, "class", classString);
188 };
189
190 XHTML1.removeClass = function(element, className) {
191         VarType.needNode(element, 1);
192         className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
193
194         var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
195         var classArray = classString.split(" ");
196         classString = "";
197         for(var i = 0; i < classArray.length; i++) {
198                 if(classArray[i] != className) {
199                         if(classString == "") classString = classArray[i];
200                         else classString += " " + classArray[i];
201                 }
202         }
203
204         XHTML1.setAttribute(element, "class", classString);
205 };
206
207 XHTML1.removeAllChildren = function(node) {
208         VarType.needNode(node);
209
210         while(node.lastChild) {
211                 node.removeChild(node.lastChild);
212         }
213 };
214
215 XHTML1.getTextContent = function(node) {
216         VarType.needNode(node);
217
218         if(typeof node.textContent != "undefined") {
219                 return node.textContent;
220         }
221
222         switch(node.nodeType) {
223                 case 1:
224                 case 2:
225                 case 5:
226                 case 6:
227                 case 11:
228                         var textContent = "";
229                         for(node = node.firstChild; node; node = node.nextSibling) {
230                                 if(node.nodeType == 7) continue;
231                                 if(node.nodeType == 8) continue;
232                                 textContent += VarType.toStr(XHTML1.getTextContent(node));
233                         }
234                         return textContent;
235                 case 3:
236                 case 4:
237                 case 7:
238                 case 8:
239                         return node.nodeValue;
240         }
241
242         return null;
243 };
244
245 XHTML1.setTextContent = function(node, value) {
246         VarType.needNode(node);
247         value = VarType.toStr(value);
248
249         if(typeof node.textContent != "undefined") {
250                 node.textContent = value;
251         }
252
253         switch(node.nodeType) {
254                 case 1:
255                 case 2:
256                 case 5:
257                 case 6:
258                 case 11:
259                         XHTML1.removeAllChildren(node);
260                         if(value != "") {
261                                 node.appendChild(document.createTextNode(value));
262                         }
263                         break;
264                 case 3:
265                 case 4:
266                 case 7:
267                 case 8:
268                         node.nodeValue = value;
269                         break;
270         }
271 };