git-svn-id: svn://euphorik.ch/pompage@45 02bbb61a-6d21-0410-aba0-cb053bdfd66a
[pompage.git] / doc / webdeveloper / common / dom.js
1 // Returns the current content document
2 function webdeveloper_getContentDocument()
3 {
4 return webdeveloper_getSelectedBrowser().contentDocument;
5 }
6
7 // Returns the current content window
8 function webdeveloper_getContentWindow()
9 {
10 return webdeveloper_getSelectedBrowser().contentWindow;
11 }
12
13 // Returns the document body element
14 function webdeveloper_getDocumentBodyElement(contentDocument)
15 {
16 // If there is a body element
17 if(contentDocument.body)
18 {
19 return contentDocument.body;
20 }
21 else
22 {
23 var bodyElementList = contentDocument.getElementsByTagName("body");
24
25 // If there is a body element
26 if(bodyElementList.length > 0)
27 {
28 return bodyElementList[0];
29 }
30 }
31
32 return contentDocument.documentElement;
33 }
34
35 // Returns the document head element
36 function webdeveloper_getDocumentHeadElement(contentDocument)
37 {
38 var headElementList = contentDocument.getElementsByTagName("head");
39
40 // If there is a head element
41 if(headElementList.length > 0)
42 {
43 return headElementList[0];
44 }
45
46 return contentDocument.documentElement;
47 }
48
49 // Gets all the documents from the current page
50 function webdeveloper_getDocuments(frame)
51 {
52 var documents = new Array();
53
54 // If the frame is set
55 if(frame)
56 {
57 var frames = frame.frames;
58 var framesLength = frames.length;
59
60 // If the frame document exists
61 if(frame.document)
62 {
63 documents.push(frame.document);
64 }
65
66 // Loop through the frames
67 for(var i = 0; i < framesLength; i++)
68 {
69 documents = documents.concat(webdeveloper_getDocuments(frames[i]));
70 }
71 }
72
73 return documents;
74 }
75
76 // Get the ancestors of the element
77 function webdeveloper_getElementAncestors(element)
78 {
79 var ancestors = webdeveloper_getElementAncestorsInternal(element);
80
81 // Reverse the list and remove the last element which is the original element
82 ancestors.reverse();
83 ancestors.pop();
84
85 return ancestors;
86 }
87
88 // Recursively gets the ancestors of an element
89 function webdeveloper_getElementAncestorsInternal(element)
90 {
91 var ancestors = new Array();
92
93 // If the element is set
94 if(element)
95 {
96 var parentElement = element.parentNode;
97
98 // If the element has a tag name
99 if(element.tagName)
100 {
101 ancestors.push(element);
102 }
103
104 // If there is a parent element
105 if(parentElement)
106 {
107 ancestors = ancestors.concat(webdeveloper_getElementAncestorsInternal(parentElement));
108 }
109 }
110
111 return ancestors;
112 }
113
114 // Get the children of the element
115 function webdeveloper_getElementChildren(element)
116 {
117 var children = new Array();
118
119 // If the element is set
120 if(element)
121 {
122 var child = null;
123 var childNodes = element.childNodes;
124 var childLength = childNodes.length;
125
126 // Loop through the children
127 for(var i = 0; i < childLength; i++)
128 {
129 child = childNodes[i];
130
131 // If the child and tag name are set
132 if(child && child.tagName)
133 {
134 children.push(child);
135 }
136 }
137 }
138
139 return children;
140 }
141
142 // Get the position of the element
143 function webdeveloper_getElementPosition(element, xPosition)
144 {
145 var position = 0;
146
147 // If the element is set
148 if(element)
149 {
150 var elementOffsetParent = element.offsetParent;
151
152 // If the element has an offset parent
153 if(elementOffsetParent)
154 {
155 // While there is an offset parent
156 while((elementOffsetParent = element.offsetParent) != null)
157 {
158 // If getting the x position
159 if(xPosition)
160 {
161 position += element.offsetLeft;
162 }
163 else
164 {
165 position += element.offsetTop;
166 }
167
168 element = elementOffsetParent;
169 }
170 }
171 else
172 {
173 // If getting the x position
174 if(xPosition)
175 {
176 position = element.offsetLeft;
177 }
178 else
179 {
180 position = element.offsetTop;
181 }
182 }
183 }
184
185 return position;
186 }
187
188 // Get the x position of the element
189 function webdeveloper_getElementPositionX(element)
190 {
191 return webdeveloper_getElementPosition(element, true);
192 }
193
194 // Get the y position of the element
195 function webdeveloper_getElementPositionY(element)
196 {
197 return webdeveloper_getElementPosition(element, false);
198 }
199
200 // Returns the text from an element
201 function webdeveloper_getElementText(element)
202 {
203 var elementText = "";
204
205 // If the element is set
206 if(element)
207 {
208 var childNode = null;
209 var childNodeList = element.childNodes;
210 var childNodeLength = childNodeList.length;
211 var childNodeType = null;
212
213 // Loop through the child nodes
214 for(var i = 0; i < childNodeLength; i++)
215 {
216 childNode = childNodeList[i];
217 childNodeType = childNode.nodeType;
218
219 // If the child node type is an element
220 if(childNodeType == Node.ELEMENT_NODE)
221 {
222 elementText += webdeveloper_getElementText(childNode);
223 }
224 else if(childNodeType == Node.TEXT_NODE)
225 {
226 elementText += childNode.nodeValue + " ";
227 }
228 }
229 }
230
231 return elementText;
232 }
233
234 // Returns the list of the images for the specified document
235 function webdeveloper_getImagesForDocument(contentDocument, includeBackgroundImages, includeIcons)
236 {
237 var images = new Array();
238
239 // If the content document is set
240 if(contentDocument)
241 {
242 var backgroundImage = null;
243 var computedStyle = null;
244 var cssURI = CSSPrimitiveValue.CSS_URI;
245 var documentURL = contentDocument.documentURI;
246 var element = null;
247 var image = null;
248 var imageInterface = Components.interfaces.nsIDOMHTMLImageElement;
249 var inputInterface = Components.interfaces.nsIDOMHTMLInputElement;
250 var linkInterface = Components.interfaces.nsIDOMHTMLLinkElement;
251 var treeWalker = contentDocument.createTreeWalker(contentDocument, NodeFilter.SHOW_ELEMENT, null, false);
252 var url = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURL);
253
254 // While the tree walker has more nodes
255 while((element = treeWalker.nextNode()) != null)
256 {
257 // If this is an image element
258 if(element instanceof imageInterface)
259 {
260 images.push(element);
261 }
262 else if(element instanceof inputInterface && element.src && element.type && element.type.toLowerCase() == "image")
263 {
264 image = new Image();
265 url.spec = documentURL;
266 image.src = url.resolve(element.src);
267
268 // If this is not a chrome image
269 if(image.src.indexOf("chrome://") != 0)
270 {
271 images.push(image);
272 }
273 }
274 else if(includeIcons && element instanceof linkInterface && element.href && element.href.indexOf("chrome://") != 0 && element.rel && element.rel.indexOf("icon") != -1)
275 {
276 image = new Image();
277 url.spec = documentURL;
278 image.src = url.resolve(element.href);
279
280 images.push(image);
281 }
282 else if(includeBackgroundImages)
283 {
284 computedStyle = element.ownerDocument.defaultView.getComputedStyle(element, null);
285
286 // If the computed style is set
287 if(computedStyle)
288 {
289 backgroundImage = computedStyle.getPropertyCSSValue("background-image");
290
291 // If this element has a background image and it is a URI
292 if(backgroundImage && backgroundImage.primitiveType == cssURI)
293 {
294 image = new Image();
295 image.src = backgroundImage.getStringValue();
296
297 // If this is not a chrome image
298 if(image.src.indexOf("chrome://") != 0)
299 {
300 images.push(image);
301 }
302 }
303 }
304 }
305 }
306 }
307
308 return images;
309 }
310
311 // Returns the list of the objects for the specified document
312 function webdeveloper_getObjectsForDocument(contentDocument)
313 {
314 var objects = new Array();
315
316 // If the content document is set
317 if(contentDocument)
318 {
319 var documentObjects = contentDocument.embeds;
320 var documentObjectsLength = documentObjects.length;
321
322 // Loop through the document objects
323 for(var i = 0; i < documentObjectsLength; i++)
324 {
325 objects.push(documentObjects[i]);
326 }
327 }
328
329 return objects;
330 }
331
332 // Returns the selected browser
333 function webdeveloper_getSelectedBrowser()
334 {
335 return window.top.getBrowser().selectedBrowser;
336 }
337
338 // Returns the list of the scripts for the specified document
339 function webdeveloper_getScriptsForDocument(contentDocument, includeInline)
340 {
341 var scripts = new Array();
342
343 // If the content document is set
344 if(contentDocument)
345 {
346 var documentScript = null;
347 var documentScripts = contentDocument.getElementsByTagName("script");
348 var documentScriptsLength = documentScripts.length;
349 var documentURL = contentDocument.documentURI;
350
351 // Loop through the document scripts
352 for(var i = 0; i < documentScriptsLength; i++)
353 {
354 documentScript = documentScripts[i];
355
356 // If including inline scripts or this is not inline
357 if(includeInline || documentScript.src != documentURL)
358 {
359 scripts.push(documentScript);
360 }
361 }
362 }
363
364 return scripts;
365 }
366
367 // Inserts the given child after the element
368 function webdeveloper_insertAfter(child, after)
369 {
370 // If the child and after are set
371 if(child && after)
372 {
373 var nextSibling = after.nextSibling;
374 var parent = after.parentNode;
375
376 // If the element has a next sibling
377 if(nextSibling)
378 {
379 parent.insertBefore(child, nextSibling);
380 }
381 else
382 {
383 parent.appendChild(child);
384 }
385 }
386 }
387
388 // Inserts the given child as the first child of the element
389 function webdeveloper_insertAsFirstChild(element, child)
390 {
391 // If the element and child are set
392 if(element && child)
393 {
394 // If the element has child nodes
395 if(element.hasChildNodes())
396 {
397 element.insertBefore(child, element.firstChild);
398 }
399 else
400 {
401 element.appendChild(child);
402 }
403 }
404 }
405
406 // Returns true if the ancestor element is an ancestor of the element
407 function webdeveloper_isAncestor(element, ancestorElement)
408 {
409 // If the element and ancestor element are set
410 if(element && ancestorElement)
411 {
412 var parentElement = null;
413
414 // Loop through the parent elements
415 while((parentElement = element.parentNode) != null)
416 {
417 // If the parent element is the ancestor element
418 if(parentElement == ancestorElement)
419 {
420 return true;
421 }
422 else
423 {
424 element = parentElement;
425 }
426 }
427 }
428
429 return false;
430 }
431
432 // Returns true if the page has frames
433 function webdeveloper_pageHasFrames()
434 {
435 // If the content document has a frame element
436 if(webdeveloper_getContentDocument().getElementsByTagName("frame").length > 0)
437 {
438 return true;
439 }
440
441 return false;
442 }
443
444 // Removes all child elements from an element
445 function webdeveloper_removeAllChildElements(element)
446 {
447 // If the element is set
448 if(element)
449 {
450 var childElements = element.childNodes;
451
452 // Loop through the child elements
453 for(var i = 0; i < childElements.length; i++)
454 {
455 element.removeChild(childElements[i]);
456 }
457
458 childElements = element.childNodes;
459
460 // Loop through the child elements
461 while(childElements.length > 0)
462 {
463 element.removeChild(childElements[0]);
464 }
465 }
466 }
467
468 // Removes all elements matching the XPath
469 function webdeveloper_removeAllElementsByXPath(contentDocument, xPath)
470 {
471 var elementList = webdeveloper_evaluateXPath(contentDocument, xPath);
472 var elementsLength = elementList.length;
473
474 // Loop through all the elements
475 for(var i = 0; i < elementsLength; i++)
476 {
477 webdeveloper_removeElement(elementList[i]);
478 }
479
480 }
481
482 // Removes an element
483 function webdeveloper_removeElement(element)
484 {
485 // If the element and it's parent node are set
486 if(element && element.parentNode)
487 {
488 element.parentNode.removeChild(element);
489 }
490 }