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
;