-
Notifications
You must be signed in to change notification settings - Fork 2k
/
shady-events.html
274 lines (254 loc) · 7.43 KB
/
shady-events.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!doctype html>
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<script>
if (window.customElements) {
customElements.forcePolyfill = true;
}
window.ShadyDOM = {
force: true
};
</script>
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../polymer.html">
</head>
<body>
<dom-module id="x-event-scoped">
<template>
<div id="scoped" on-composed="childHandler" on-scoped="childHandler"></div>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-event-scoped',
properties: {
hostEvents: {
type: Array,
value: function() {
return [];
}
},
childEvents: {
type: Array,
value: function() {
return [];
}
}
},
listeners: {
'composed': 'hostHandler',
'scoped': 'hostHandler'
},
hostHandler: function(e) {
this.hostEvents.push({
target: e.target,
type: e.type,
path: e.composedPath()
});
},
childHandler: function(e) {
this.childEvents.push({
target: e.target,
type: e.type,
path: e.composedPath()
});
},
fireComposed: function() {
return this.fire('composed', null, {node: this.$.scoped});
},
fireScoped: function(){
return this.fire('scoped', null, {node: this.$.scoped, composed: false});
}
});
});
</script>
</dom-module>
<dom-module id="x-focus">
<template>
<style>
:host {
display: block;
}
</style>
<div id="child" on-focus="focusHandler"></div>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-focus',
properties: {
events: {
type: Array,
value: function() {
return [];
}
}
},
listeners: {
focus: 'focusHandler'
},
focusHandler: function(e) {
this.events.push(e.target);
},
fireComposed: function() {
var ev = new Event('focus', {composed: true});
this.$.child.dispatchEvent(ev);
},
fireScoped: function() {
var ev = new Event('focus');
this.$.child.dispatchEvent(ev);
}
});
});
</script>
</dom-module>
<dom-module id="x-a">
<template>
<div id="child" on-foo="childFooHandler"></div>
<div id="child2"></div>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-a',
listeners: {
'foo': 'fooHandler'
},
fooHandler: function(e) {
this.event = {target: e.target, relatedTarget: e.relatedTarget};
},
childFooHandler: function(e) {
this.childEvent = {target: e.target, relatedTarget: e.relatedTarget};
}
});
});
</script>
</dom-module>
<test-fixture id="scoped">
<template>
<x-event-scoped></x-event-scoped>
</template>
</test-fixture>
<test-fixture id="globalpatch">
<template>
<div></div>
</template>
</test-fixture>
<test-fixture id="focus">
<template>
<x-focus></x-focus>
</template>
</test-fixture>
<test-fixture id="relatedtarget">
<template>
<x-a id="one"></x-a>
<x-a id="two"></x-a>
</template>
</test-fixture>
<dom-module id="x-slot-inner">
</dom-module>
<test-fixture id="slot">
<template>
<x-slot>
<span class="target"></span>
</x-slot>
</template>
</test-fixture>
<script>
suite('ShadyDOM event patching', function() {
test('events retarget', function() {
var el = fixture('scoped');
el.fireComposed();
assert.equal(el.hostEvents[0].target, el);
assert.equal(el.childEvents[0].target, el.$.scoped);
});
test('event.composedPath is consistent', function() {
var el = fixture('scoped');
el.fireComposed();
assert.equal(el.hostEvents.length, el.childEvents.length);
});
test('event patching works on non Polymer elements', function() {
var el = fixture('globalpatch');
var path;
el.addEventListener('foo', function(e) {
path = e.composedPath();
});
var e = new Event('foo', {composed: true});
el.dispatchEvent(e);
assert.deepEqual([el, el.parentNode, document.body, document.documentElement, document, window], path);
});
test('`composed` flag controls event propagation through roots', function() {
var el = fixture('scoped');
el.fireScoped();
el.fireComposed();
assert.equal(el.hostEvents.length, 1);
assert.equal(el.hostEvents[0].type, 'composed');
assert.equal(el.childEvents.length, 2);
assert.equal(el.childEvents[0].type, 'scoped');
assert.equal(el.childEvents[1].type, 'composed');
});
test('composed focus and blur events retarget up tree', function() {
var el = fixture('focus');
el.fireComposed();
assert.equal(el.events.length, 2);
assert.equal(el.events[0], el.$.child);
assert.equal(el.events[1], el);
});
test('scoped focus and blur events do not retarget', function() {
var el = fixture('focus');
var e = new Event('focus');
if (e.isTrused !== false) {
// skip browser if we cannot distinguish
// native focus events from user created ones
this.skip();
}
el.fireScoped();
assert.equal(el.events.length, 1);
assert.equal(el.events[0], el.$.child);
});
test('composed relatedTarget retargets', function() {
var els = fixture('relatedtarget');
var a = els[0];
var b = els[1];
var ev = new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: b.$.child});
a.$.child.dispatchEvent(ev);
assert.property(a, 'childEvent');
assert.deepEqual(a.childEvent, {target: a.$.child, relatedTarget: b});
assert.property(a, 'event');
assert.deepEqual(a.event, {target: a, relatedTarget: b});
});
test('events do not fire if relatedtarget and target are the same node after retargeting', function() {
var els = fixture('relatedtarget');
var a = els[0];
var ev = new MouseEvent('foo', {bubbles: true, composed: true, relatedTarget: a.$.child2});
a.$.child.dispatchEvent(ev);
assert.property(a, 'childEvent');
assert.deepEqual(a.childEvent, {target: a.$.child, relatedTarget: a.$.child2});
assert.notProperty(a, 'event');
});
test('capturing event listeners fire correctly for focus and blur', function() {
var el = fixture('focus');
var timeStamp;
el.addEventListener('focus', function(e) {
timeStamp = e.timeStamp;
}, true);
el.fireComposed();
assert.ok(timeStamp);
assert.equal(el.events.length, 2);
assert.equal(el.events[0], el.$.child);
assert.equal(el.events[1], el);
});
});
</script>
</body>
</html>