a1c494ad34e162f713bbf0e9162db836c06a2b80
[WebKit-https.git] / LayoutTests / fast / shadow-dom / resources / event-path-test-helpers.js
1
2 function dispatchEventWithLog(shadow, target, event) {
3     var eventPath = [];
4     var relatedTargets = [];
5     var pathAtTargets = [];
6
7     var attachedNodes = [];
8     for (var nodeKey in shadow) {
9         var startingNode = shadow[nodeKey];
10         for (var node = startingNode; node; node = node.parentNode) {
11             if (attachedNodes.indexOf(node) >= 0)
12                 continue;
13             attachedNodes.push(node);
14             node.addEventListener(event.type, (function (event) {
15                 eventPath.push(this.label);
16                 relatedTargets.push(event.relatedTarget ? event.relatedTarget.label : null);
17
18                 if (!event.deepPath) // Don't fail all tests just for the lack of deepPath.
19                     return;
20
21                 pathAtTargets.push(event.deepPath().map(function (node) { return node.label; }));
22             }).bind(node));
23         }
24     }
25
26     target.dispatchEvent(event);
27
28     return {eventPath: eventPath, relatedTargets: relatedTargets, pathAtTargets: pathAtTargets};
29 }
30
31 /*
32 -SR: ShadowRoot  -S: Slot
33 A ------------------------------- A-SR
34 + B ------------ B-SR             + A1 --- A1-SR
35   + C            + B1 --- B1-SR   + A2-S   + A1a
36   + D --- D-SR     + B1a  + B1b --- B1b-SR
37           + D1            + B1c-S   + B1b1
38                                     + B1b2
39 */
40 function createTestTree(mode) {
41     var namedNodes = {};
42
43     function element(name) {
44         var element = document.createElement(name.indexOf('-S') > 0 ? 'slot' : 'div');
45         element.label = name;
46         namedNodes[name] = element;
47         for (var i = 1; i < arguments.length; i++) {
48             var item = arguments[i];
49             if (typeof(item) == 'function')
50                 item(element);
51             else
52                 element.appendChild(item);
53         }
54         return element;
55     }
56
57     function shadow(name) {
58         var children = [];
59         for (var i = 1; i < arguments.length; i++)
60             children.push(arguments[i]);
61         return function (element) {
62             var shadowRoot = element.attachShadow({mode: mode});
63             shadowRoot.label = name;
64             namedNodes[name] = shadowRoot;
65             for (var child of children)
66                 shadowRoot.appendChild(child);
67         }
68     }
69
70     var host = element('A',
71         shadow('A-SR',
72             element('A1',
73                 shadow('A1-SR',
74                     element('A1a'))),
75             element('A2-S')
76         ),
77         element('B',
78             shadow('B-SR',
79                 element('B1',
80                     shadow('B1-SR',
81                         element('B1b',
82                             shadow('B1b-SR',
83                                 element('B1b1'),
84                                 element('B1b2'))),
85                         element('B1c-S')),
86                     element('B1a'))),
87             element('C'),
88             element('D',
89                 shadow('D-SR',
90                     element('D1')))));
91
92     return namedNodes;
93 }