| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2024 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
import {suite} from 'uvu'; |
| 8 |
// eslint-disable-next-line import/extensions |
| 9 |
import * as assert from 'uvu/assert'; |
| 10 |
import { |
| 11 |
HTMLElement, |
| 12 |
EventTargetShimMeta, |
| 13 |
document, |
| 14 |
} from '@lit-labs/ssr-dom-shim'; |
| 15 |
|
| 16 |
const test = suite('Event Target Shim'); |
| 17 |
|
| 18 |
const eventName = 'test'; |
| 19 |
const eventPhases = ['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE']; |
| 20 |
// Pattern: element-name:id/capture/eventPhase/target:id |
| 21 |
let eventPath: string[] = []; |
| 22 |
let nextId = 0; |
| 23 |
const createElementWithListener = (localName: string) => { |
| 24 |
const el = new HTMLElement() as HTMLElement & EventTargetShimMeta; |
| 25 |
Object.defineProperty(el, 'localName', { |
| 26 |
get() { |
| 27 |
return localName; |
| 28 |
}, |
| 29 |
}); |
| 30 |
const id = (el.id = `${nextId++}`); |
| 31 |
for (const capture of [true, false]) { |
| 32 |
el.addEventListener( |
| 33 |
eventName, |
| 34 |
({target, eventPhase, currentTarget}) => { |
| 35 |
const {localName, id: targetId} = target as HTMLElement; |
| 36 |
eventPath.push( |
| 37 |
`${el.localName}:${id}/${capture ? 'capture' : 'non-capture'}/${eventPhases[eventPhase]}/${localName}:${targetId}` |
| 38 |
); |
| 39 |
assert.equal( |
| 40 |
currentTarget, |
| 41 |
el, |
| 42 |
'Expected currentTarget to equal the event listener element' |
| 43 |
); |
| 44 |
}, |
| 45 |
{capture} |
| 46 |
); |
| 47 |
} |
| 48 |
return el; |
| 49 |
}; |
| 50 |
const createElementsWithListener = (...localNames: string[]) => |
| 51 |
localNames.map((n) => createElementWithListener(n)); |
| 52 |
|
| 53 |
test.before.each(() => { |
| 54 |
nextId = 0; |
| 55 |
eventPath = []; |
| 56 |
}); |
| 57 |
|
| 58 |
test('should handle dispatched event', () => { |
| 59 |
const el = createElementWithListener('div'); |
| 60 |
const result = el.dispatchEvent(new Event(eventName)); |
| 61 |
assert.ok(result); |
| 62 |
assert.equal(eventPath, [ |
| 63 |
'div:0/capture/AT_TARGET/div:0', |
| 64 |
'div:0/non-capture/AT_TARGET/div:0', |
| 65 |
]); |
| 66 |
}); |
| 67 |
|
| 68 |
test('should ignore preventDefault without cancelable', () => { |
| 69 |
const el = createElementWithListener('div'); |
| 70 |
el.addEventListener(eventName, (e) => e.preventDefault()); |
| 71 |
const result = el.dispatchEvent(new Event(eventName)); |
| 72 |
assert.ok(result); |
| 73 |
}); |
| 74 |
|
| 75 |
test('should respect preventDefault with cancelable', () => { |
| 76 |
const el = createElementWithListener('div'); |
| 77 |
el.addEventListener(eventName, (e) => e.preventDefault()); |
| 78 |
const result = el.dispatchEvent(new Event(eventName, {cancelable: true})); |
| 79 |
assert.not.ok(result); |
| 80 |
}); |
| 81 |
|
| 82 |
test('should match event and target', () => { |
| 83 |
const el = new HTMLElement(); |
| 84 |
const event = new Event(eventName); |
| 85 |
let eventCounter = 0; |
| 86 |
el.addEventListener(eventName, (e) => { |
| 87 |
eventCounter++; |
| 88 |
assert.equal(e, event, 'Expected event to equal to the dispatched event'); |
| 89 |
assert.equal( |
| 90 |
e.target, |
| 91 |
el, |
| 92 |
'Expected event target to be the dispatching element' |
| 93 |
); |
| 94 |
}); |
| 95 |
el.dispatchEvent(event); |
| 96 |
assert.equal( |
| 97 |
eventCounter, |
| 98 |
1, |
| 99 |
'Expected the event handler to have been called exactly once' |
| 100 |
); |
| 101 |
}); |
| 102 |
|
| 103 |
test('should handle dispatched non-bubbling event with parent', () => { |
| 104 |
const [parent, child] = createElementsWithListener('parent', 'child'); |
| 105 |
child.__eventTargetParent = parent; |
| 106 |
child.dispatchEvent(new Event(eventName)); |
| 107 |
assert.equal(eventPath, [ |
| 108 |
'parent:0/capture/CAPTURING_PHASE/child:1', |
| 109 |
'child:1/capture/AT_TARGET/child:1', |
| 110 |
'child:1/non-capture/AT_TARGET/child:1', |
| 111 |
]); |
| 112 |
}); |
| 113 |
|
| 114 |
test('should handle dispatched bubbling event with parent', () => { |
| 115 |
const [parent, child] = createElementsWithListener('parent', 'child'); |
| 116 |
child.__eventTargetParent = parent; |
| 117 |
child.dispatchEvent(new Event(eventName, {bubbles: true})); |
| 118 |
assert.equal(eventPath, [ |
| 119 |
'parent:0/capture/CAPTURING_PHASE/child:1', |
| 120 |
'child:1/capture/AT_TARGET/child:1', |
| 121 |
'child:1/non-capture/AT_TARGET/child:1', |
| 122 |
'parent:0/non-capture/BUBBLING_PHASE/child:1', |
| 123 |
]); |
| 124 |
}); |
| 125 |
|
| 126 |
test('should handle stopPropagation during capture phase', () => { |
| 127 |
const [parent, child] = createElementsWithListener('parent', 'child'); |
| 128 |
child.__eventTargetParent = parent; |
| 129 |
let called = false; |
| 130 |
parent.addEventListener(eventName, (e) => e.stopPropagation(), { |
| 131 |
capture: true, |
| 132 |
}); |
| 133 |
parent.addEventListener(eventName, () => (called = true), {capture: true}); |
| 134 |
child.dispatchEvent(new Event(eventName, {bubbles: true})); |
| 135 |
assert.equal(eventPath, ['parent:0/capture/CAPTURING_PHASE/child:1']); |
| 136 |
assert.ok( |
| 137 |
called, |
| 138 |
'Expected event listener after another event listener with stopPropagation to be called' |
| 139 |
); |
| 140 |
}); |
| 141 |
|
| 142 |
test('should handle stopPropagation during bubbling phase', () => { |
| 143 |
const [parent, child] = createElementsWithListener('parent', 'child'); |
| 144 |
child.__eventTargetParent = parent; |
| 145 |
let called = false; |
| 146 |
child.addEventListener(eventName, (e) => e.stopPropagation()); |
| 147 |
child.addEventListener(eventName, () => (called = true)); |
| 148 |
child.dispatchEvent(new Event(eventName, {bubbles: true})); |
| 149 |
assert.equal(eventPath, [ |
| 150 |
'parent:0/capture/CAPTURING_PHASE/child:1', |
| 151 |
'child:1/capture/AT_TARGET/child:1', |
| 152 |
'child:1/non-capture/AT_TARGET/child:1', |
| 153 |
]); |
| 154 |
assert.ok( |
| 155 |
called, |
| 156 |
'Expected event listener after another event listener with stopPropagation to be called' |
| 157 |
); |
| 158 |
}); |
| 159 |
|
| 160 |
test('should handle stopImmediatePropagation during capture phase', () => { |
| 161 |
const [parent, child] = createElementsWithListener('parent', 'child'); |
| 162 |
child.__eventTargetParent = parent; |
| 163 |
let called = false; |
| 164 |
parent.addEventListener(eventName, (e) => e.stopImmediatePropagation(), { |
| 165 |
capture: true, |
| 166 |
}); |
| 167 |
parent.addEventListener(eventName, () => (called = true), {capture: true}); |
| 168 |
child.dispatchEvent(new Event(eventName, {bubbles: true})); |
| 169 |
assert.equal(eventPath, ['parent:0/capture/CAPTURING_PHASE/child:1']); |
| 170 |
assert.not.ok( |
| 171 |
called, |
| 172 |
'Expected event listener after another event listener with stopImmediatePropagation not to be called' |
| 173 |
); |
| 174 |
}); |
| 175 |
|
| 176 |
test('should handle stopImmediatePropagation during bubbling phase', () => { |
| 177 |
const [parent, child] = createElementsWithListener('parent', 'child'); |
| 178 |
child.__eventTargetParent = parent; |
| 179 |
let called = false; |
| 180 |
child.addEventListener(eventName, (e) => e.stopImmediatePropagation()); |
| 181 |
child.addEventListener(eventName, () => (called = true)); |
| 182 |
child.dispatchEvent(new Event(eventName, {bubbles: true})); |
| 183 |
assert.equal(eventPath, [ |
| 184 |
'parent:0/capture/CAPTURING_PHASE/child:1', |
| 185 |
'child:1/capture/AT_TARGET/child:1', |
| 186 |
'child:1/non-capture/AT_TARGET/child:1', |
| 187 |
]); |
| 188 |
assert.not.ok( |
| 189 |
called, |
| 190 |
'Expected event listener after another event listener with stopImmediatePropagation not to be called' |
| 191 |
); |
| 192 |
}); |
| 193 |
|
| 194 |
test('should handle dispatched bubbling event through Shadow DOM', () => { |
| 195 |
// <host id="0"> |
| 196 |
// #shadow-root |
| 197 |
// <shadowElement id="1"> |
| 198 |
// <slot id="2"></slot> |
| 199 |
// </shadowElement> |
| 200 |
// <child id="3"></child> |
| 201 |
// </host> |
| 202 |
const [host, shadowElement, slot, child] = createElementsWithListener( |
| 203 |
'host', |
| 204 |
'shadowElement', |
| 205 |
'slot', |
| 206 |
'child' |
| 207 |
); |
| 208 |
shadowElement.__host = slot.__host = host; |
| 209 |
shadowElement.__eventTargetParent = host; |
| 210 |
slot.__eventTargetParent = shadowElement; |
| 211 |
child.__eventTargetParent = slot; |
| 212 |
child.dispatchEvent(new Event(eventName, {bubbles: true})); |
| 213 |
assert.equal(eventPath, [ |
| 214 |
'host:0/capture/CAPTURING_PHASE/child:3', |
| 215 |
'shadowElement:1/capture/CAPTURING_PHASE/child:3', |
| 216 |
'slot:2/capture/CAPTURING_PHASE/child:3', |
| 217 |
'child:3/capture/AT_TARGET/child:3', |
| 218 |
'child:3/non-capture/AT_TARGET/child:3', |
| 219 |
'slot:2/non-capture/BUBBLING_PHASE/child:3', |
| 220 |
'shadowElement:1/non-capture/BUBBLING_PHASE/child:3', |
| 221 |
'host:0/non-capture/BUBBLING_PHASE/child:3', |
| 222 |
]); |
| 223 |
}); |
| 224 |
|
| 225 |
test('should handle dispatched bubbling event from Shadow DOM without composed', () => { |
| 226 |
// <host id="0"> |
| 227 |
// #shadow-root |
| 228 |
// <shadowElement id="1"> |
| 229 |
// <slot id="2"> |
| 230 |
// <child id="3"></child> |
| 231 |
// </slot> |
| 232 |
// </shadowElement> |
| 233 |
// </host> |
| 234 |
const [host, shadowElement, slot, child] = createElementsWithListener( |
| 235 |
'host', |
| 236 |
'shadowElement', |
| 237 |
'slot', |
| 238 |
'child' |
| 239 |
); |
| 240 |
shadowElement.__host = slot.__host = child.__host = host; |
| 241 |
shadowElement.__eventTargetParent = host; |
| 242 |
slot.__eventTargetParent = shadowElement; |
| 243 |
child.__eventTargetParent = slot; |
| 244 |
child.dispatchEvent(new Event(eventName, {bubbles: true})); |
| 245 |
assert.equal(eventPath, [ |
| 246 |
'shadowElement:1/capture/CAPTURING_PHASE/child:3', |
| 247 |
'slot:2/capture/CAPTURING_PHASE/child:3', |
| 248 |
'child:3/capture/AT_TARGET/child:3', |
| 249 |
'child:3/non-capture/AT_TARGET/child:3', |
| 250 |
'slot:2/non-capture/BUBBLING_PHASE/child:3', |
| 251 |
'shadowElement:1/non-capture/BUBBLING_PHASE/child:3', |
| 252 |
]); |
| 253 |
}); |
| 254 |
|
| 255 |
test('should handle dispatched bubbling event from Shadow DOM with composed', () => { |
| 256 |
// <host id="0"> |
| 257 |
// #shadow-root |
| 258 |
// <shadowElement id="1"> |
| 259 |
// <slot id="2"> |
| 260 |
// <child id="3"></child> |
| 261 |
// </slot> |
| 262 |
// </shadowElement> |
| 263 |
// </host> |
| 264 |
const [host, shadowElement, slot, child] = createElementsWithListener( |
| 265 |
'host', |
| 266 |
'shadowElement', |
| 267 |
'slot', |
| 268 |
'child' |
| 269 |
); |
| 270 |
shadowElement.__host = slot.__host = child.__host = host; |
| 271 |
shadowElement.__eventTargetParent = host; |
| 272 |
slot.__eventTargetParent = shadowElement; |
| 273 |
child.__eventTargetParent = slot; |
| 274 |
child.dispatchEvent(new Event(eventName, {bubbles: true, composed: true})); |
| 275 |
assert.equal(eventPath, [ |
| 276 |
'host:0/capture/AT_TARGET/host:0', |
| 277 |
'shadowElement:1/capture/CAPTURING_PHASE/child:3', |
| 278 |
'slot:2/capture/CAPTURING_PHASE/child:3', |
| 279 |
'child:3/capture/AT_TARGET/child:3', |
| 280 |
'child:3/non-capture/AT_TARGET/child:3', |
| 281 |
'slot:2/non-capture/BUBBLING_PHASE/child:3', |
| 282 |
'shadowElement:1/non-capture/BUBBLING_PHASE/child:3', |
| 283 |
'host:0/non-capture/AT_TARGET/host:0', |
| 284 |
]); |
| 285 |
}); |
| 286 |
|
| 287 |
test('should handle dispatched bubbling event from nested Shadow DOM with composed', () => { |
| 288 |
// <host id="0"> |
| 289 |
// #shadow-root |
| 290 |
// <shadowHost id="1"> |
| 291 |
// #shadow-root |
| 292 |
// <shadowElement id="2"> |
| 293 |
// <slot id="3"></slot> // nestedSlot |
| 294 |
// </shadowElement> |
| 295 |
// <slot id="4"> |
| 296 |
// <child id="5"></child> |
| 297 |
// </slot> |
| 298 |
// </shadowHost> |
| 299 |
// </host> |
| 300 |
const [host, shadowHost, shadowElement, nestedSlot, slot, child] = |
| 301 |
createElementsWithListener( |
| 302 |
'host', |
| 303 |
'shadowHost', |
| 304 |
'shadowElement', |
| 305 |
'slot', |
| 306 |
'slot', |
| 307 |
'child' |
| 308 |
); |
| 309 |
shadowHost.__host = slot.__host = child.__host = host; |
| 310 |
shadowElement.__host = nestedSlot.__host = shadowHost; |
| 311 |
shadowHost.__eventTargetParent = host; |
| 312 |
shadowElement.__eventTargetParent = shadowHost; |
| 313 |
nestedSlot.__eventTargetParent = shadowElement; |
| 314 |
slot.__eventTargetParent = nestedSlot; |
| 315 |
child.__eventTargetParent = slot; |
| 316 |
child.dispatchEvent(new Event(eventName, {bubbles: true, composed: true})); |
| 317 |
assert.equal(eventPath, [ |
| 318 |
'host:0/capture/AT_TARGET/host:0', |
| 319 |
'shadowHost:1/capture/CAPTURING_PHASE/child:5', |
| 320 |
'shadowElement:2/capture/CAPTURING_PHASE/child:5', |
| 321 |
'slot:3/capture/CAPTURING_PHASE/child:5', |
| 322 |
'slot:4/capture/CAPTURING_PHASE/child:5', |
| 323 |
'child:5/capture/AT_TARGET/child:5', |
| 324 |
'child:5/non-capture/AT_TARGET/child:5', |
| 325 |
'slot:4/non-capture/BUBBLING_PHASE/child:5', |
| 326 |
'slot:3/non-capture/BUBBLING_PHASE/child:5', |
| 327 |
'shadowElement:2/non-capture/BUBBLING_PHASE/child:5', |
| 328 |
'shadowHost:1/non-capture/BUBBLING_PHASE/child:5', |
| 329 |
'host:0/non-capture/AT_TARGET/host:0', |
| 330 |
]); |
| 331 |
}); |
| 332 |
|
| 333 |
test('should handle event listeners on document', () => { |
| 334 |
const controller = new AbortController(); |
| 335 |
try { |
| 336 |
let result = ''; |
| 337 |
const event = new Event('test', {bubbles: true}); |
| 338 |
document.addEventListener( |
| 339 |
'test', |
| 340 |
(e) => { |
| 341 |
assert.equal(e, event); |
| 342 |
result += 'capture '; |
| 343 |
}, |
| 344 |
{capture: true, signal: controller.signal} |
| 345 |
); |
| 346 |
document.addEventListener( |
| 347 |
'test', |
| 348 |
(e) => { |
| 349 |
assert.equal(e, event); |
| 350 |
result += 'non-capture'; |
| 351 |
}, |
| 352 |
{signal: controller.signal} |
| 353 |
); |
| 354 |
new HTMLElement().dispatchEvent(event); |
| 355 |
assert.equal(result, 'capture non-capture'); |
| 356 |
} finally { |
| 357 |
controller.abort(); |
| 358 |
} |
| 359 |
}); |
| 360 |
|
| 361 |
test.run(); |