|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 "use strict"; |
|
4 |
|
5 const STACK_THICKNESS = 15; |
|
6 |
|
7 function init(callback) { |
|
8 let iframe = gBrowser.ownerDocument.createElement("iframe"); |
|
9 |
|
10 iframe.addEventListener("load", function onLoad() { |
|
11 iframe.removeEventListener("load", onLoad, true); |
|
12 callback(iframe); |
|
13 |
|
14 gBrowser.parentNode.removeChild(iframe); |
|
15 finish(); |
|
16 }, true); |
|
17 |
|
18 iframe.setAttribute("src", ["data:text/html,", |
|
19 "<!DOCTYPE html>", |
|
20 "<html>", |
|
21 "<body style='margin: 0;'>", |
|
22 "<div>", |
|
23 "<p>Foo</p>", |
|
24 "<div>", |
|
25 "<span>Bar</span>", |
|
26 "</div>", |
|
27 "<div></div>", |
|
28 "</div>", |
|
29 "</body>", |
|
30 "</html>" |
|
31 ].join("")); |
|
32 |
|
33 gBrowser.parentNode.appendChild(iframe); |
|
34 } |
|
35 |
|
36 function nodeCallback(aContentWindow, aNode, aParentPosition) { |
|
37 let coord = TiltUtils.DOM.getNodePosition(aContentWindow, aNode, aParentPosition); |
|
38 |
|
39 if (aNode.localName != "div") |
|
40 coord.thickness = 0; |
|
41 |
|
42 if (aNode.localName == "span") |
|
43 coord.depth += STACK_THICKNESS; |
|
44 |
|
45 return coord; |
|
46 } |
|
47 |
|
48 function test() { |
|
49 waitForExplicitFinish(); |
|
50 ok(TiltUtils, "The TiltUtils object doesn't exist."); |
|
51 |
|
52 let dom = TiltUtils.DOM; |
|
53 ok(dom, "The TiltUtils.DOM wasn't found."); |
|
54 |
|
55 init(function(iframe) { |
|
56 let store = dom.traverse(iframe.contentWindow, { |
|
57 nodeCallback: nodeCallback |
|
58 }); |
|
59 |
|
60 let expected = [ |
|
61 { name: "html", depth: 0 * STACK_THICKNESS, thickness: 0 }, |
|
62 { name: "head", depth: 0 * STACK_THICKNESS, thickness: 0 }, |
|
63 { name: "body", depth: 0 * STACK_THICKNESS, thickness: 0 }, |
|
64 { name: "div", depth: 0 * STACK_THICKNESS, thickness: STACK_THICKNESS }, |
|
65 { name: "p", depth: 1 * STACK_THICKNESS, thickness: 0 }, |
|
66 { name: "div", depth: 1 * STACK_THICKNESS, thickness: STACK_THICKNESS }, |
|
67 { name: "div", depth: 1 * STACK_THICKNESS, thickness: STACK_THICKNESS }, |
|
68 { name: "span", depth: 3 * STACK_THICKNESS, thickness: 0 }, |
|
69 ]; |
|
70 |
|
71 is(store.nodes.length, expected.length, |
|
72 "The traverse() function didn't walk the correct number of nodes."); |
|
73 is(store.info.length, expected.length, |
|
74 "The traverse() function didn't examine the correct number of nodes."); |
|
75 |
|
76 for (let i = 0; i < expected.length; i++) { |
|
77 is(store.info[i].name, expected[i].name, |
|
78 "traversed node " + (i + 1) + " isn't the expected one."); |
|
79 is(store.info[i].coord.depth, expected[i].depth, |
|
80 "traversed node " + (i + 1) + " doesn't have the expected depth."); |
|
81 is(store.info[i].coord.thickness, expected[i].thickness, |
|
82 "traversed node " + (i + 1) + " doesn't have the expected thickness."); |
|
83 } |
|
84 }); |
|
85 } |