2 Copyright (C) 2007, 2008 Alina Friedrichsen <x-alina@gmx.net>
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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.
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
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";
30 function W3CDOM_Event(currentTarget) {
31 VarType.needObject(currentTarget);
32 this.currentTarget = currentTarget;
33 this.preventDefault = function() { window.event.returnValue = false; };
40 XHTML1.isDOMSupported = function() {
41 if(!document.getElementById) return false;
42 if(!(window.addEventListener || window.attachEvent)) return false;
46 XHTML1.isXHTML = function() {
47 if(document.documentElement.nodeName == "HTML") return false;
51 XHTML1.addEventListener = function(target, type, listener) {
52 VarType.needObject(target);
53 type = VarType.toStr(type);
54 VarType.needFunction(listener);
56 if(target.addEventListener) {
57 target.addEventListener(type, listener, false);
59 else if(target.attachEvent) {
60 target.attachEvent("on" + type, function() { listener(new W3CDOM_Event(target)); } );
64 XHTML1.createElement = function(tagName) {
65 tagName = VarType.toStr(tagName);
67 if(XHTML1.isXHTML()) {
68 return document.createElementNS(XMLNS_XHTML, tagName.toLowerCase());
71 return document.createElement(tagName.toUpperCase());
74 XHTML1.getElementsByTagName = function(tagName) {
75 tagName = VarType.toStr(tagName);
77 if(XHTML1.isXHTML()) {
78 return document.getElementsByTagNameNS(XMLNS_XHTML, tagName.toLowerCase());
81 return document.getElementsByTagName(tagName.toUpperCase());
84 XHTML1.isElement = function(node, tagName) {
85 VarType.needNode(node);
86 tagName = VarType.toStr(tagName);
88 if(node.nodeType == 1) {
89 if(XHTML1.isXHTML()) {
90 if(node.namespaceURI == XMLNS_XHTML) {
91 if(node.localName == tagName.toLowerCase()) return true;
94 if(node.nodeName == tagName.toUpperCase()) return true;
101 XHTML1.getAttribute = function(element, name) {
102 VarType.needNode(element, 1);
103 name = VarType.toStr(name);
105 name = name.toLowerCase();
107 if(XHTML1.isXHTML()) {
108 return element.getAttributeNS(null, name);
111 if(name == "class") {
112 return element.className;
115 return element.getAttribute(name);
118 XHTML1.setAttribute = function(element, name, value) {
119 VarType.needNode(element, 1);
120 name = VarType.toStr(name);
121 value = VarType.toStr(value);
123 name = name.toLowerCase();
125 if(XHTML1.isXHTML()) {
126 element.setAttributeNS(null, name, value);
130 if(name == "class") {
131 element.className = value;
135 element.setAttribute(name, value);
138 XHTML1.removeAttribute = function(element, name) {
139 VarType.needNode(element, 1);
140 name = VarType.toStr(name);
142 name = name.toLowerCase();
144 if(XHTML1.isXHTML()) {
145 element.removeAttributeNS(null, name);
149 if(name == "class") {
150 element.className = "";
154 element.removeAttribute(name);
157 XHTML1.containsClass = function(element, className) {
158 VarType.needNode(element, 1);
159 className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
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;
170 XHTML1.addClass = function(element, className) {
171 VarType.needNode(element, 1);
172 className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
174 var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
175 var classArray = classString.split(" ");
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];
184 if(classString == "") classString = className;
185 else classString += " " + className;
187 XHTML1.setAttribute(element, "class", classString);
190 XHTML1.removeClass = function(element, className) {
191 VarType.needNode(element, 1);
192 className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
194 var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
195 var classArray = classString.split(" ");
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];
204 XHTML1.setAttribute(element, "class", classString);
207 XHTML1.removeAllChildren = function(node) {
208 VarType.needNode(node);
210 while(node.lastChild) {
211 node.removeChild(node.lastChild);
215 XHTML1.getTextContent = function(node) {
216 VarType.needNode(node);
218 if(typeof node.textContent != "undefined") {
219 return node.textContent;
222 switch(node.nodeType) {
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));
239 return node.nodeValue;
245 XHTML1.setTextContent = function(node, value) {
246 VarType.needNode(node);
247 value = VarType.toStr(value);
249 if(typeof node.textContent != "undefined") {
250 node.textContent = value;
253 switch(node.nodeType) {
259 XHTML1.removeAllChildren(node);
261 node.appendChild(document.createTextNode(value));
268 node.nodeValue = value;
273 Copyright (C) 2007, 2008 Alina Friedrichsen <x-alina@gmx.net>
275 Redistribution and use in source and binary forms, with or without
276 modification, are permitted provided that the following conditions
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.
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
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";
301 function W3CDOM_Event(currentTarget) {
302 VarType.needObject(currentTarget);
303 this.currentTarget = currentTarget;
304 this.preventDefault = function() { window.event.returnValue = false; };
311 XHTML1.isDOMSupported = function() {
312 if(!document.getElementById) return false;
313 if(!(window.addEventListener || window.attachEvent)) return false;
317 XHTML1.isXHTML = function() {
318 if(document.documentElement.nodeName == "HTML") return false;
322 XHTML1.addEventListener = function(target, type, listener) {
323 VarType.needObject(target);
324 type = VarType.toStr(type);
325 VarType.needFunction(listener);
327 if(target.addEventListener) {
328 target.addEventListener(type, listener, false);
330 else if(target.attachEvent) {
331 target.attachEvent("on" + type, function() { listener(new W3CDOM_Event(target)); } );
335 XHTML1.createElement = function(tagName) {
336 tagName = VarType.toStr(tagName);
338 if(XHTML1.isXHTML()) {
339 return document.createElementNS(XMLNS_XHTML, tagName.toLowerCase());
342 return document.createElement(tagName.toUpperCase());
345 XHTML1.getElementsByTagName = function(tagName) {
346 tagName = VarType.toStr(tagName);
348 if(XHTML1.isXHTML()) {
349 return document.getElementsByTagNameNS(XMLNS_XHTML, tagName.toLowerCase());
352 return document.getElementsByTagName(tagName.toUpperCase());
355 XHTML1.isElement = function(node, tagName) {
356 VarType.needNode(node);
357 tagName = VarType.toStr(tagName);
359 if(node.nodeType == 1) {
360 if(XHTML1.isXHTML()) {
361 if(node.namespaceURI == XMLNS_XHTML) {
362 if(node.localName == tagName.toLowerCase()) return true;
365 if(node.nodeName == tagName.toUpperCase()) return true;
372 XHTML1.getAttribute = function(element, name) {
373 VarType.needNode(element, 1);
374 name = VarType.toStr(name);
376 name = name.toLowerCase();
378 if(XHTML1.isXHTML()) {
379 return element.getAttributeNS(null, name);
382 if(name == "class") {
383 return element.className;
386 return element.getAttribute(name);
389 XHTML1.setAttribute = function(element, name, value) {
390 VarType.needNode(element, 1);
391 name = VarType.toStr(name);
392 value = VarType.toStr(value);
394 name = name.toLowerCase();
396 if(XHTML1.isXHTML()) {
397 element.setAttributeNS(null, name, value);
401 if(name == "class") {
402 element.className = value;
406 element.setAttribute(name, value);
409 XHTML1.removeAttribute = function(element, name) {
410 VarType.needNode(element, 1);
411 name = VarType.toStr(name);
413 name = name.toLowerCase();
415 if(XHTML1.isXHTML()) {
416 element.removeAttributeNS(null, name);
420 if(name == "class") {
421 element.className = "";
425 element.removeAttribute(name);
428 XHTML1.containsClass = function(element, className) {
429 VarType.needNode(element, 1);
430 className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
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;
441 XHTML1.addClass = function(element, className) {
442 VarType.needNode(element, 1);
443 className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
445 var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
446 var classArray = classString.split(" ");
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];
455 if(classString == "") classString = className;
456 else classString += " " + className;
458 XHTML1.setAttribute(element, "class", classString);
461 XHTML1.removeClass = function(element, className) {
462 VarType.needNode(element, 1);
463 className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");
465 var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
466 var classArray = classString.split(" ");
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];
475 XHTML1.setAttribute(element, "class", classString);
478 XHTML1.removeAllChildren = function(node) {
479 VarType.needNode(node);
481 while(node.lastChild) {
482 node.removeChild(node.lastChild);
486 XHTML1.getTextContent = function(node) {
487 VarType.needNode(node);
489 if(typeof node.textContent != "undefined") {
490 return node.textContent;
493 switch(node.nodeType) {
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));
510 return node.nodeValue;
516 XHTML1.setTextContent = function(node, value) {
517 VarType.needNode(node);
518 value = VarType.toStr(value);
520 if(typeof node.textContent != "undefined") {
521 node.textContent = value;
524 switch(node.nodeType) {
530 XHTML1.removeAllChildren(node);
532 node.appendChild(document.createTextNode(value));
539 node.nodeValue = value;