Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / dom / shadow / resources / shadow-dom.js
1 function createShadowRoot()
2 {
3     var children = Array.prototype.slice.call(arguments);
4     if ((children[0] instanceof Object) && !(children[0] instanceof Node))
5         return {'isShadowRoot': true,
6                 'attributes': children[0],
7                 'children': children.slice(1)};
8     return {'isShadowRoot': true,
9             'children': children};
10 }
11
12 // This function can take optional child elements, which might be a result of createShadowRoot(), as arguments[2:].
13 function createDOM(tagName, attributes)
14 {
15     var element = document.createElement(tagName);
16     for (var name in attributes)
17         element.setAttribute(name, attributes[name]);
18     var childElements = Array.prototype.slice.call(arguments, 2);
19     for (var i = 0; i < childElements.length; ++i) {
20         var child = childElements[i];
21         if (child.isShadowRoot) {
22             var shadowRoot = element.createShadowRoot();
23             if (child.attributes) {
24                 for (var attribute in child.attributes) {
25                     // Shadow Root does not have setAttribute.
26                     shadowRoot[attribute] = child.attributes[attribute];
27                 }
28             }
29             for (var j = 0; j < child.children.length; ++j)
30                 shadowRoot.appendChild(child.children[j]);
31         } else
32             element.appendChild(child);
33     }
34     return element;
35 }
36
37 function isShadowHost(node)
38 {
39     return window.internals.oldestShadowRoot(node);
40 }
41
42 function isShadowRoot(node)
43 {
44     return node instanceof window.ShadowRoot;
45 }
46
47 function isIframeElement(element)
48 {
49     return element && element.nodeName == 'IFRAME';
50 }
51
52 // You can spefify youngerShadowRoot by consecutive slashes.
53 // See LayoutTests/fast/dom/shadow/get-element-by-id-in-shadow-root.html for actual usages.
54 function getNodeInTreeOfTrees(path)
55 {
56     var ids = path.split('/');
57     var node = document.getElementById(ids[0]);
58     for (var i = 1; node != null && i < ids.length; ++i) {
59         if (isIframeElement(node)) {
60             node = node.contentDocument.getElementById(ids[i]);
61             continue;
62         }
63         if (isShadowRoot(node))
64             node = internals.youngerShadowRoot(node);
65         else if (internals.oldestShadowRoot(node))
66             node = internals.oldestShadowRoot(node);
67         else
68             return null;
69         if (ids[i] != '')
70             node = node.getElementById(ids[i]);
71     }
72     return node;
73 }
74
75 function dumpNode(node)
76 {
77     if (!node)
78       return 'null';
79     if (node.id)
80         return '#' + node.id;
81     return '' + node;
82 }
83
84 function dumpNodeList(nodeList) {
85     var result = "";
86     var length = nodeList.length;
87     for (var i = 0; i < length; i++)
88         result += dumpNode(nodeList[i]) + ", ";
89     result += "length: " + length;
90     return result;
91 }
92
93 function shouldBeEqualAsArray(nodeList, expectedNodes)
94 {
95     // FIXME: Avoid polluting the global namespace.
96     window.nodeList = nodeList;
97     window.expectedNodes = expectedNodes;
98     shouldBe("nodeList.length", "expectedNodes.length");
99     for (var i = 0; i < nodeList.length; ++i) {
100         shouldBe("nodeList.item(" + i + ")", "expectedNodes[" + i + "]");
101     }
102 }
103
104 function innermostActiveElement(element)
105 {
106     element = element || document.activeElement;
107     if (isIframeElement(element)) {
108         if (element.contentDocument.activeElement)
109             return innermostActiveElement(element.contentDocument.activeElement);
110         return element;
111     }
112     if (isShadowHost(element)) {
113         var shadowRoot = window.internals.oldestShadowRoot(element);
114         while (shadowRoot) {
115             if (shadowRoot.activeElement)
116                 return innermostActiveElement(shadowRoot.activeElement);
117             shadowRoot = window.internals.youngerShadowRoot(shadowRoot);
118         }
119     }
120     return element;
121 }
122
123 function isInnermostActiveElement(id)
124 {
125     var element = getNodeInTreeOfTrees(id);
126     if (!element) {
127         debug('FAIL: There is no such element with id: '+ from);
128         return false;
129     }
130     if (element == innermostActiveElement())
131         return true;
132     debug('Expected innermost activeElement is ' + id + ', but actual innermost activeElement is ' + dumpNode(innermostActiveElement()));
133     return false;
134 }
135
136 function shouldNavigateFocus(from, to, direction)
137 {
138     debug('Should move from ' + from + ' to ' + to + ' in ' + direction);
139     var fromElement = getNodeInTreeOfTrees(from);
140     if (!fromElement) {
141       debug('FAIL: There is no such element with id: '+ from);
142       return;
143     }
144     fromElement.focus();
145     if (!isInnermostActiveElement(from)) {
146         debug('FAIL: Can not be focused: '+ from);
147         return;
148     }
149     if (direction == 'forward')
150         navigateFocusForward();
151     else
152         navigateFocusBackward();
153     if (isInnermostActiveElement(to))
154         debug('PASS');
155     else
156         debug('FAIL');
157 }
158
159 function navigateFocusForward()
160 {
161     eventSender.keyDown('\t');
162 }
163
164 function navigateFocusBackward()
165 {
166     eventSender.keyDown('\t', ['shiftKey']);
167 }
168
169 function testFocusNavigationFowrad(elements)
170 {
171     for (var i = 0; i + 1 < elements.length; ++i)
172         shouldNavigateFocus(elements[i], elements[i + 1], 'forward');
173 }
174
175 function testFocusNavigationBackward(elements)
176 {
177     for (var i = 0; i + 1 < elements.length; ++i)
178         shouldNavigateFocus(elements[i], elements[i + 1], 'backward');
179 }
180
181 function dumpComposedShadowTree(node, indent)
182 {
183     indent = indent || "";
184     var output = indent + dumpNode(node) + "\n";
185     var child;
186     for (child = internals.firstChildByWalker(node); child; child = internals.nextSiblingByWalker(child))
187          output += dumpComposedShadowTree(child, indent + "\t");
188     return output;
189 }
190
191 function lastNodeByWalker(root)
192 {
193     var lastNode = root;
194     while (internals.lastChildByWalker(lastNode))
195         lastNode = internals.lastChildByWalker(lastNode);
196     return lastNode;
197 }
198
199 function showComposedShadowTreeByTraversingInForward(root)
200 {
201     var node = root;
202     var last = lastNodeByWalker(root);
203     while (node) {
204         debug(dumpNode(node));
205         if (node == last)
206             break;
207         node = internals.nextNodeByWalker(node);
208     }
209 }
210
211 function showComposedShadowTreeByTraversingInBackward(root)
212 {
213     var node = lastNodeByWalker(root);
214     while (node) {
215         debug(dumpNode(node));
216         if (node == root)
217             break;
218         node = internals.previousNodeByWalker(node);
219     }
220 }
221
222 function showComposedShadowTree(node)
223 {
224     debug('Composed Shadow Tree:');
225     debug(dumpComposedShadowTree(node));
226
227     debug('Traverse in forward.');
228     showComposedShadowTreeByTraversingInForward(node);
229
230     debug('Traverse in backward.');
231     showComposedShadowTreeByTraversingInBackward(node);
232
233     debug('');
234 }
235
236 function showNextNode(node) {
237     var next = internals.nextNodeByWalker(node);
238     debug('Next node of [' + dumpNode(node) + '] is [' + dumpNode(next) + ']');
239 }