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