Subversion Repositories Applications.papyrus

Rev

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