b196f5313c2cd58d2314c3d37676e432b358c3d4
[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 };
272 /*
273 Copyright (C) 2007, 2008  Alina Friedrichsen <x-alina@gmx.net>
274
275 Redistribution and use in source and binary forms, with or without
276 modification, are permitted provided that the following conditions
277 are met:
278 1. Redistributions of source code must retain the above copyright
279    notice, this list of conditions and the following disclaimer.
280 2. Redistributions in binary form must reproduce the above copyright
281    notice, this list of conditions and the following disclaimer in the
282    documentation and/or other materials provided with the distribution.
283
284 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
285 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
286 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
287 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
288 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
289 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
290 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
292 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
293 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294 SUCH DAMAGE.
295 */
296
297 var XMLNS_XMLNS = "http://www.w3.org/2000/xmlns/";
298 var XMLNS_XML = "http://www.w3.org/XML/1998/namespace";
299 var XMLNS_XHTML = "http://www.w3.org/1999/xhtml";
300
301 function W3CDOM_Event(currentTarget) {
302         VarType.needObject(currentTarget);
303         this.currentTarget = currentTarget;
304         this.preventDefault = function() { window.event.returnValue = false; };
305         return this;
306 }
307
308 function XHTML1() {
309 }
310
311 XHTML1.isDOMSupported = function() {
312         if(!document.getElementById) return false;
313         if(!(window.addEventListener || window.attachEvent)) return false;
314         return true;
315 };
316
317 XHTML1.isXHTML = function() {
318         if(document.documentElement.nodeName == "HTML") return false;
319         return true;
320 };
321
322 XHTML1.addEventListener = function(target, type, listener) {
323         VarType.needObject(target);
324         type = VarType.toStr(type);
325         VarType.needFunction(listener);
326
327         if(target.addEventListener) {
328                 target.addEventListener(type, listener, false);
329         }
330         else if(target.attachEvent) {
331                 target.attachEvent("on" + type, function() { listener(new W3CDOM_Event(target)); } );
332         }
333 };
334
335 XHTML1.createElement = function(tagName) {
336         tagName = VarType.toStr(tagName);
337
338         if(XHTML1.isXHTML()) {
339                 return document.createElementNS(XMLNS_XHTML, tagName.toLowerCase());
340         }
341
342         return document.createElement(tagName.toUpperCase());
343 };
344
345 XHTML1.getElementsByTagName = function(tagName) {
346         tagName = VarType.toStr(tagName);
347
348         if(XHTML1.isXHTML()) {
349                 return document.getElementsByTagNameNS(XMLNS_XHTML, tagName.toLowerCase());
350         }
351
352         return document.getElementsByTagName(tagName.toUpperCase());
353 };
354
355 XHTML1.isElement = function(node, tagName) {
356         VarType.needNode(node);
357         tagName = VarType.toStr(tagName);
358
359         if(node.nodeType == 1) {
360                 if(XHTML1.isXHTML()) {
361                         if(node.namespaceURI == XMLNS_XHTML) {
362                                 if(node.localName == tagName.toLowerCase()) return true;
363                         }
364                 } else {
365                         if(node.nodeName == tagName.toUpperCase()) return true;
366                 }
367         }
368
369         return false;
370 };
371
372 XHTML1.getAttribute = function(element, name) {
373         VarType.needNode(element, 1);
374         name = VarType.toStr(name);
375
376         name = name.toLowerCase();
377
378         if(XHTML1.isXHTML()) {
379                 return element.getAttributeNS(null, name);
380         }
381
382         if(name == "class") {
383                 return element.className;
384         }
385
386         return element.getAttribute(name);
387 };
388
389 XHTML1.setAttribute = function(element, name, value) {
390         VarType.needNode(element, 1);
391         name = VarType.toStr(name);
392         value = VarType.toStr(value);
393
394         name = name.toLowerCase();
395
396         if(XHTML1.isXHTML()) {
397                 element.setAttributeNS(null, name, value);
398                 return;
399         }
400
401         if(name == "class") {
402                 element.className = value;
403                 return;
404         }
405
406         element.setAttribute(name, value);
407 };
408
409 XHTML1.removeAttribute = function(element, name) {
410         VarType.needNode(element, 1);
411         name = VarType.toStr(name);
412
413         name = name.toLowerCase();
414
415         if(XHTML1.isXHTML()) {
416                 element.removeAttributeNS(null, name);
417                 return;
418         }
419
420         if(name == "class") {
421                 element.className = "";
422                 return;
423         }
424
425         element.removeAttribute(name);
426 };
427
428 XHTML1.containsClass = function(element, className) {
429         VarType.needNode(element, 1);
430         className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
431
432         var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
433         var classArray = classString.split(" ");
434         for(var i = 0; i < classArray.length; i++) {
435                 if(classArray[i] == className) return true;
436         }
437
438         return false;
439 };
440
441 XHTML1.addClass = function(element, className) {
442         VarType.needNode(element, 1);
443         className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
444
445         var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
446         var classArray = classString.split(" ");
447         classString = "";
448         for(var i = 0; i < classArray.length; i++) {
449                 if(classArray[i] != className) {
450                         if(classString == "") classString = classArray[i];
451                         else classString += " " + classArray[i];
452                 }
453         }
454
455         if(classString == "") classString = className;
456         else classString += " " + className;
457
458         XHTML1.setAttribute(element, "class", classString);
459 };
460
461 XHTML1.removeClass = function(element, className) {
462         VarType.needNode(element, 1);
463         className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
464
465         var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
466         var classArray = classString.split(" ");
467         classString = "";
468         for(var i = 0; i < classArray.length; i++) {
469                 if(classArray[i] != className) {
470                         if(classString == "") classString = classArray[i];
471                         else classString += " " + classArray[i];
472                 }
473         }
474
475         XHTML1.setAttribute(element, "class", classString);
476 };
477
478 XHTML1.removeAllChildren = function(node) {
479         VarType.needNode(node);
480
481         while(node.lastChild) {
482                 node.removeChild(node.lastChild);
483         }
484 };
485
486 XHTML1.getTextContent = function(node) {
487         VarType.needNode(node);
488
489         if(typeof node.textContent != "undefined") {
490                 return node.textContent;
491         }
492
493         switch(node.nodeType) {
494                 case 1:
495                 case 2:
496                 case 5:
497                 case 6:
498                 case 11:
499                         var textContent = "";
500                         for(node = node.firstChild; node; node = node.nextSibling) {
501                                 if(node.nodeType == 7) continue;
502                                 if(node.nodeType == 8) continue;
503                                 textContent += VarType.toStr(XHTML1.getTextContent(node));
504                         }
505                         return textContent;
506                 case 3:
507                 case 4:
508                 case 7:
509                 case 8:
510                         return node.nodeValue;
511         }
512
513         return null;
514 };
515
516 XHTML1.setTextContent = function(node, value) {
517         VarType.needNode(node);
518         value = VarType.toStr(value);
519
520         if(typeof node.textContent != "undefined") {
521                 node.textContent = value;
522         }
523
524         switch(node.nodeType) {
525                 case 1:
526                 case 2:
527                 case 5:
528                 case 6:
529                 case 11:
530                         XHTML1.removeAllChildren(node);
531                         if(value != "") {
532                                 node.appendChild(document.createTextNode(value));
533                         }
534                         break;
535                 case 3:
536                 case 4:
537                 case 7:
538                 case 8:
539                         node.nodeValue = value;
540                         break;
541         }
542 };