Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.dom");
12
dojo.dom.ELEMENT_NODE = 1;
13
dojo.dom.ATTRIBUTE_NODE = 2;
14
dojo.dom.TEXT_NODE = 3;
15
dojo.dom.CDATA_SECTION_NODE = 4;
16
dojo.dom.ENTITY_REFERENCE_NODE = 5;
17
dojo.dom.ENTITY_NODE = 6;
18
dojo.dom.PROCESSING_INSTRUCTION_NODE = 7;
19
dojo.dom.COMMENT_NODE = 8;
20
dojo.dom.DOCUMENT_NODE = 9;
21
dojo.dom.DOCUMENT_TYPE_NODE = 10;
22
dojo.dom.DOCUMENT_FRAGMENT_NODE = 11;
23
dojo.dom.NOTATION_NODE = 12;
24
dojo.dom.dojoml = "http://www.dojotoolkit.org/2004/dojoml";
25
dojo.dom.xmlns = {svg:"http://www.w3.org/2000/svg", smil:"http://www.w3.org/2001/SMIL20/", mml:"http://www.w3.org/1998/Math/MathML", cml:"http://www.xml-cml.org", xlink:"http://www.w3.org/1999/xlink", xhtml:"http://www.w3.org/1999/xhtml", xul:"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", xbl:"http://www.mozilla.org/xbl", fo:"http://www.w3.org/1999/XSL/Format", xsl:"http://www.w3.org/1999/XSL/Transform", xslt:"http://www.w3.org/1999/XSL/Transform", xi:"http://www.w3.org/2001/XInclude", xforms:"http://www.w3.org/2002/01/xforms", saxon:"http://icl.com/saxon", xalan:"http://xml.apache.org/xslt", xsd:"http://www.w3.org/2001/XMLSchema", dt:"http://www.w3.org/2001/XMLSchema-datatypes", xsi:"http://www.w3.org/2001/XMLSchema-instance", rdf:"http://www.w3.org/1999/02/22-rdf-syntax-ns#", rdfs:"http://www.w3.org/2000/01/rdf-schema#", dc:"http://purl.org/dc/elements/1.1/", dcq:"http://purl.org/dc/qualifiers/1.0", "soap-env":"http://schemas.xmlsoap.org/soap/envelope/", wsdl:"http://schemas.xmlsoap.org/wsdl/", AdobeExtensions:"http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"};
26
dojo.dom.isNode = function (wh) {
27
	if (typeof Element == "function") {
28
		try {
29
			return wh instanceof Element;
30
		}
31
		catch (e) {
32
		}
33
	} else {
34
		return wh && !isNaN(wh.nodeType);
35
	}
36
};
37
dojo.dom.getUniqueId = function () {
38
	var _document = dojo.doc();
39
	do {
40
		var id = "dj_unique_" + (++arguments.callee._idIncrement);
41
	} while (_document.getElementById(id));
42
	return id;
43
};
44
dojo.dom.getUniqueId._idIncrement = 0;
45
dojo.dom.firstElement = dojo.dom.getFirstChildElement = function (parentNode, tagName) {
46
	var node = parentNode.firstChild;
47
	while (node && node.nodeType != dojo.dom.ELEMENT_NODE) {
48
		node = node.nextSibling;
49
	}
50
	if (tagName && node && node.tagName && node.tagName.toLowerCase() != tagName.toLowerCase()) {
51
		node = dojo.dom.nextElement(node, tagName);
52
	}
53
	return node;
54
};
55
dojo.dom.lastElement = dojo.dom.getLastChildElement = function (parentNode, tagName) {
56
	var node = parentNode.lastChild;
57
	while (node && node.nodeType != dojo.dom.ELEMENT_NODE) {
58
		node = node.previousSibling;
59
	}
60
	if (tagName && node && node.tagName && node.tagName.toLowerCase() != tagName.toLowerCase()) {
61
		node = dojo.dom.prevElement(node, tagName);
62
	}
63
	return node;
64
};
65
dojo.dom.nextElement = dojo.dom.getNextSiblingElement = function (node, tagName) {
66
	if (!node) {
67
		return null;
68
	}
69
	do {
70
		node = node.nextSibling;
71
	} while (node && node.nodeType != dojo.dom.ELEMENT_NODE);
72
	if (node && tagName && tagName.toLowerCase() != node.tagName.toLowerCase()) {
73
		return dojo.dom.nextElement(node, tagName);
74
	}
75
	return node;
76
};
77
dojo.dom.prevElement = dojo.dom.getPreviousSiblingElement = function (node, tagName) {
78
	if (!node) {
79
		return null;
80
	}
81
	if (tagName) {
82
		tagName = tagName.toLowerCase();
83
	}
84
	do {
85
		node = node.previousSibling;
86
	} while (node && node.nodeType != dojo.dom.ELEMENT_NODE);
87
	if (node && tagName && tagName.toLowerCase() != node.tagName.toLowerCase()) {
88
		return dojo.dom.prevElement(node, tagName);
89
	}
90
	return node;
91
};
92
dojo.dom.moveChildren = function (srcNode, destNode, trim) {
93
	var count = 0;
94
	if (trim) {
95
		while (srcNode.hasChildNodes() && srcNode.firstChild.nodeType == dojo.dom.TEXT_NODE) {
96
			srcNode.removeChild(srcNode.firstChild);
97
		}
98
		while (srcNode.hasChildNodes() && srcNode.lastChild.nodeType == dojo.dom.TEXT_NODE) {
99
			srcNode.removeChild(srcNode.lastChild);
100
		}
101
	}
102
	while (srcNode.hasChildNodes()) {
103
		destNode.appendChild(srcNode.firstChild);
104
		count++;
105
	}
106
	return count;
107
};
108
dojo.dom.copyChildren = function (srcNode, destNode, trim) {
109
	var clonedNode = srcNode.cloneNode(true);
110
	return this.moveChildren(clonedNode, destNode, trim);
111
};
112
dojo.dom.replaceChildren = function (node, newChild) {
113
	var nodes = [];
114
	if (dojo.render.html.ie) {
115
		for (var i = 0; i < node.childNodes.length; i++) {
116
			nodes.push(node.childNodes[i]);
117
		}
118
	}
119
	dojo.dom.removeChildren(node);
120
	node.appendChild(newChild);
121
	for (var i = 0; i < nodes.length; i++) {
122
		dojo.dom.destroyNode(nodes[i]);
123
	}
124
};
125
dojo.dom.removeChildren = function (node) {
126
	var count = node.childNodes.length;
127
	while (node.hasChildNodes()) {
128
		dojo.dom.removeNode(node.firstChild);
129
	}
130
	return count;
131
};
132
dojo.dom.replaceNode = function (node, newNode) {
133
	return node.parentNode.replaceChild(newNode, node);
134
};
135
dojo.dom.destroyNode = function (node) {
136
	if (node.parentNode) {
137
		node = dojo.dom.removeNode(node);
138
	}
139
	if (node.nodeType != 3) {
140
		if (dojo.evalObjPath("dojo.event.browser.clean", false)) {
141
			dojo.event.browser.clean(node);
142
		}
143
		if (dojo.render.html.ie) {
144
			node.outerHTML = "";
145
		}
146
	}
147
};
148
dojo.dom.removeNode = function (node) {
149
	if (node && node.parentNode) {
150
		return node.parentNode.removeChild(node);
151
	}
152
};
153
dojo.dom.getAncestors = function (node, filterFunction, returnFirstHit) {
154
	var ancestors = [];
155
	var isFunction = (filterFunction && (filterFunction instanceof Function || typeof filterFunction == "function"));
156
	while (node) {
157
		if (!isFunction || filterFunction(node)) {
158
			ancestors.push(node);
159
		}
160
		if (returnFirstHit && ancestors.length > 0) {
161
			return ancestors[0];
162
		}
163
		node = node.parentNode;
164
	}
165
	if (returnFirstHit) {
166
		return null;
167
	}
168
	return ancestors;
169
};
170
dojo.dom.getAncestorsByTag = function (node, tag, returnFirstHit) {
171
	tag = tag.toLowerCase();
172
	return dojo.dom.getAncestors(node, function (el) {
173
		return ((el.tagName) && (el.tagName.toLowerCase() == tag));
174
	}, returnFirstHit);
175
};
176
dojo.dom.getFirstAncestorByTag = function (node, tag) {
177
	return dojo.dom.getAncestorsByTag(node, tag, true);
178
};
179
dojo.dom.isDescendantOf = function (node, ancestor, guaranteeDescendant) {
180
	if (guaranteeDescendant && node) {
181
		node = node.parentNode;
182
	}
183
	while (node) {
184
		if (node == ancestor) {
185
			return true;
186
		}
187
		node = node.parentNode;
188
	}
189
	return false;
190
};
191
dojo.dom.innerXML = function (node) {
192
	if (node.innerXML) {
193
		return node.innerXML;
194
	} else {
195
		if (node.xml) {
196
			return node.xml;
197
		} else {
198
			if (typeof XMLSerializer != "undefined") {
199
				return (new XMLSerializer()).serializeToString(node);
200
			}
201
		}
202
	}
203
};
204
dojo.dom.createDocument = function () {
205
	var doc = null;
206
	var _document = dojo.doc();
207
	if (!dj_undef("ActiveXObject")) {
208
		var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
209
		for (var i = 0; i < prefixes.length; i++) {
210
			try {
211
				doc = new ActiveXObject(prefixes[i] + ".XMLDOM");
212
			}
213
			catch (e) {
214
			}
215
			if (doc) {
216
				break;
217
			}
218
		}
219
	} else {
220
		if ((_document.implementation) && (_document.implementation.createDocument)) {
221
			doc = _document.implementation.createDocument("", "", null);
222
		}
223
	}
224
	return doc;
225
};
226
dojo.dom.createDocumentFromText = function (str, mimetype) {
227
	if (!mimetype) {
228
		mimetype = "text/xml";
229
	}
230
	if (!dj_undef("DOMParser")) {
231
		var parser = new DOMParser();
232
		return parser.parseFromString(str, mimetype);
233
	} else {
234
		if (!dj_undef("ActiveXObject")) {
235
			var domDoc = dojo.dom.createDocument();
236
			if (domDoc) {
237
				domDoc.async = false;
238
				domDoc.loadXML(str);
239
				return domDoc;
240
			} else {
241
				dojo.debug("toXml didn't work?");
242
			}
243
		} else {
244
			var _document = dojo.doc();
245
			if (_document.createElement) {
246
				var tmp = _document.createElement("xml");
247
				tmp.innerHTML = str;
248
				if (_document.implementation && _document.implementation.createDocument) {
249
					var xmlDoc = _document.implementation.createDocument("foo", "", null);
250
					for (var i = 0; i < tmp.childNodes.length; i++) {
251
						xmlDoc.importNode(tmp.childNodes.item(i), true);
252
					}
253
					return xmlDoc;
254
				}
255
				return ((tmp.document) && (tmp.document.firstChild ? tmp.document.firstChild : tmp));
256
			}
257
		}
258
	}
259
	return null;
260
};
261
dojo.dom.prependChild = function (node, parent) {
262
	if (parent.firstChild) {
263
		parent.insertBefore(node, parent.firstChild);
264
	} else {
265
		parent.appendChild(node);
266
	}
267
	return true;
268
};
269
dojo.dom.insertBefore = function (node, ref, force) {
270
	if ((force != true) && (node === ref || node.nextSibling === ref)) {
271
		return false;
272
	}
273
	var parent = ref.parentNode;
274
	parent.insertBefore(node, ref);
275
	return true;
276
};
277
dojo.dom.insertAfter = function (node, ref, force) {
278
	var pn = ref.parentNode;
279
	if (ref == pn.lastChild) {
280
		if ((force != true) && (node === ref)) {
281
			return false;
282
		}
283
		pn.appendChild(node);
284
	} else {
285
		return this.insertBefore(node, ref.nextSibling, force);
286
	}
287
	return true;
288
};
289
dojo.dom.insertAtPosition = function (node, ref, position) {
290
	if ((!node) || (!ref) || (!position)) {
291
		return false;
292
	}
293
	switch (position.toLowerCase()) {
294
	  case "before":
295
		return dojo.dom.insertBefore(node, ref);
296
	  case "after":
297
		return dojo.dom.insertAfter(node, ref);
298
	  case "first":
299
		if (ref.firstChild) {
300
			return dojo.dom.insertBefore(node, ref.firstChild);
301
		} else {
302
			ref.appendChild(node);
303
			return true;
304
		}
305
		break;
306
	  default:
307
		ref.appendChild(node);
308
		return true;
309
	}
310
};
311
dojo.dom.insertAtIndex = function (node, containingNode, insertionIndex) {
312
	var siblingNodes = containingNode.childNodes;
313
	if (!siblingNodes.length || siblingNodes.length == insertionIndex) {
314
		containingNode.appendChild(node);
315
		return true;
316
	}
317
	if (insertionIndex == 0) {
318
		return dojo.dom.prependChild(node, containingNode);
319
	}
320
	return dojo.dom.insertAfter(node, siblingNodes[insertionIndex - 1]);
321
};
322
dojo.dom.textContent = function (node, text) {
323
	if (arguments.length > 1) {
324
		var _document = dojo.doc();
325
		dojo.dom.replaceChildren(node, _document.createTextNode(text));
326
		return text;
327
	} else {
328
		if (node.textContent != undefined) {
329
			return node.textContent;
330
		}
331
		var _result = "";
332
		if (node == null) {
333
			return _result;
334
		}
335
		for (var i = 0; i < node.childNodes.length; i++) {
336
			switch (node.childNodes[i].nodeType) {
337
			  case 1:
338
			  case 5:
339
				_result += dojo.dom.textContent(node.childNodes[i]);
340
				break;
341
			  case 3:
342
			  case 2:
343
			  case 4:
344
				_result += node.childNodes[i].nodeValue;
345
				break;
346
			  default:
347
				break;
348
			}
349
		}
350
		return _result;
351
	}
352
};
353
dojo.dom.hasParent = function (node) {
354
	return Boolean(node && node.parentNode && dojo.dom.isNode(node.parentNode));
355
};
356
dojo.dom.isTag = function (node) {
357
	if (node && node.tagName) {
358
		for (var i = 1; i < arguments.length; i++) {
359
			if (node.tagName == String(arguments[i])) {
360
				return String(arguments[i]);
361
			}
362
		}
363
	}
364
	return "";
365
};
366
dojo.dom.setAttributeNS = function (elem, namespaceURI, attrName, attrValue) {
367
	if (elem == null || ((elem == undefined) && (typeof elem == "undefined"))) {
368
		dojo.raise("No element given to dojo.dom.setAttributeNS");
369
	}
370
	if (!((elem.setAttributeNS == undefined) && (typeof elem.setAttributeNS == "undefined"))) {
371
		elem.setAttributeNS(namespaceURI, attrName, attrValue);
372
	} else {
373
		var ownerDoc = elem.ownerDocument;
374
		var attribute = ownerDoc.createNode(2, attrName, namespaceURI);
375
		attribute.nodeValue = attrValue;
376
		elem.setAttributeNode(attribute);
377
	}
378
};
379