summaryrefslogtreecommitdiffhomepage
path: root/themes/base/htdocs/luci-static/resources/XHTML1.js
blob: a7d4f7ddd6281be44533f5eaca5e5268e4484a5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Copyright (C) 2007, 2008  Alina Friedrichsen <x-alina@gmx.net>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/

var XMLNS_XMLNS = "http://www.w3.org/2000/xmlns/";
var XMLNS_XML = "http://www.w3.org/XML/1998/namespace";
var XMLNS_XHTML = "http://www.w3.org/1999/xhtml";

function W3CDOM_Event(currentTarget) {
	VarType.needObject(currentTarget);
	this.currentTarget = currentTarget;
	this.preventDefault = function() { window.event.returnValue = false; };
	return this;
}

function XHTML1() {
}

XHTML1.isDOMSupported = function() {
	if(!document.getElementById) return false;
	if(!(window.addEventListener || window.attachEvent)) return false;
	return true;
};

XHTML1.isXHTML = function() {
	if(document.documentElement.nodeName == "HTML") return false;
	return true;
};

XHTML1.addEventListener = function(target, type, listener) {
	VarType.needObject(target);
	type = VarType.toStr(type);
	VarType.needFunction(listener);

	if(target.addEventListener) {
		target.addEventListener(type, listener, false);
	}
	else if(target.attachEvent) {
		target.attachEvent("on" + type, function() { listener(new W3CDOM_Event(target)); } );
	}
};

XHTML1.createElement = function(tagName) {
	tagName = VarType.toStr(tagName);

	if(XHTML1.isXHTML()) {
		return document.createElementNS(XMLNS_XHTML, tagName.toLowerCase());
	}

	return document.createElement(tagName.toUpperCase());
};

XHTML1.getElementsByTagName = function(tagName) {
	tagName = VarType.toStr(tagName);

	if(XHTML1.isXHTML()) {
		return document.getElementsByTagNameNS(XMLNS_XHTML, tagName.toLowerCase());
	}

	return document.getElementsByTagName(tagName.toUpperCase());
};

XHTML1.isElement = function(node, tagName) {
	VarType.needNode(node);
	tagName = VarType.toStr(tagName);

	if(node.nodeType == 1) {
		if(XHTML1.isXHTML()) {
			if(node.namespaceURI == XMLNS_XHTML) {
				if(node.localName == tagName.toLowerCase()) return true;
			}
		} else {
			if(node.nodeName == tagName.toUpperCase()) return true;
		}
	}

	return false;
};

XHTML1.getAttribute = function(element, name) {
	VarType.needNode(element, 1);
	name = VarType.toStr(name);

	name = name.toLowerCase();

	if(XHTML1.isXHTML()) {
		return element.getAttributeNS(null, name);
	}

	if(name == "class") {
		return element.className;
	}

	return element.getAttribute(name);
};

XHTML1.setAttribute = function(element, name, value) {
	VarType.needNode(element, 1);
	name = VarType.toStr(name);
	value = VarType.toStr(value);

	name = name.toLowerCase();

	if(XHTML1.isXHTML()) {
		element.setAttributeNS(null, name, value);
		return;
	}

	if(name == "class") {
		element.className = value;
		return;
	}

	element.setAttribute(name, value);
};

XHTML1.removeAttribute = function(element, name) {
	VarType.needNode(element, 1);
	name = VarType.toStr(name);

	name = name.toLowerCase();

	if(XHTML1.isXHTML()) {
		element.removeAttributeNS(null, name);
		return;
	}

	if(name == "class") {
		element.className = "";
		return;
	}

	element.removeAttribute(name);
};

XHTML1.containsClass = function(element, className) {
	VarType.needNode(element, 1);
	className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");

	var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
	var classArray = classString.split(" ");
	for(var i = 0; i < classArray.length; i++) {
		if(classArray[i] == className) return true;
	}

	return false;
};

XHTML1.addClass = function(element, className) {
	VarType.needNode(element, 1);
	className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");

	var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
	var classArray = classString.split(" ");
	classString = "";
	for(var i = 0; i < classArray.length; i++) {
		if(classArray[i] != className) {
			if(classString == "") classString = classArray[i];
			else classString += " " + classArray[i];
		}
	}

	if(classString == "") classString = className;
	else classString += " " + className;

	XHTML1.setAttribute(element, "class", classString);
};

XHTML1.removeClass = function(element, className) {
	VarType.needNode(element, 1);
	className = VarType.toStr(className).replace(/^\s+/g, "").replace(/\s+$/g, "");

	var classString = XHTML1.getAttribute(element, "class").replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");
	var classArray = classString.split(" ");
	classString = "";
	for(var i = 0; i < classArray.length; i++) {
		if(classArray[i] != className) {
			if(classString == "") classString = classArray[i];
			else classString += " " + classArray[i];
		}
	}

	XHTML1.setAttribute(element, "class", classString);
};

XHTML1.removeAllChildren = function(node) {
	VarType.needNode(node);

	while(node.lastChild) {
		node.removeChild(node.lastChild);
	}
};

XHTML1.getTextContent = function(node) {
	VarType.needNode(node);

	if(typeof node.textContent != "undefined") {
		return node.textContent;
	}

	switch(node.nodeType) {
		case 1:
		case 2:
		case 5:
		case 6:
		case 11:
			var textContent = "";
			for(node = node.firstChild; node; node = node.nextSibling) {
				if(node.nodeType == 7) continue;
				if(node.nodeType == 8) continue;
				textContent += VarType.toStr(XHTML1.getTextContent(node));
			}
			return textContent;
		case 3:
		case 4:
		case 7:
		case 8:
			return node.nodeValue;
	}

	return null;
};

XHTML1.setTextContent = function(node, value) {
	VarType.needNode(node);
	value = VarType.toStr(value);

	if(typeof node.textContent != "undefined") {
		node.textContent = value;
	}

	switch(node.nodeType) {
		case 1:
		case 2:
		case 5:
		case 6:
		case 11:
			XHTML1.removeAllChildren(node);
			if(value != "") {
				node.appendChild(document.createTextNode(value));
			}
			break;
		case 3:
		case 4:
		case 7:
		case 8:
			node.nodeValue = value;
			break;
	}
};