|
| 1 | +<!DOCTYPE html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<title>EventTarget in Detached iframe</title> |
| 4 | +<link rel="help" href="https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke"> |
| 5 | +<script src="/resources/testharness.js"></script> |
| 6 | +<script src="/resources/testharnessreport.js"></script> |
| 7 | + |
| 8 | +<script> |
| 9 | +"use strict"; |
| 10 | + |
| 11 | +async function addFrameAndWaitForLoad(srcdoc) { |
| 12 | + const frame = document.createElement('iframe'); |
| 13 | + frame.srcdoc = srcdoc; |
| 14 | + await new Promise((resolve) => { |
| 15 | + frame.addEventListener('load', resolve); |
| 16 | + document.body.appendChild(frame); |
| 17 | + }); |
| 18 | + return frame; |
| 19 | +} |
| 20 | + |
| 21 | +window.onload = () => { |
| 22 | + promise_test(async t => { |
| 23 | + for (let eventType of ["inner", "outer"]) { |
| 24 | + const frame = await addFrameAndWaitForLoad(`<script> |
| 25 | + window.parent.innerEventTarget = new EventTarget(); |
| 26 | + window.parent.innerEvent = new Event('event'); |
| 27 | + <\/script>`); |
| 28 | + |
| 29 | + let fireCount = 0; |
| 30 | + window.innerEventTarget.addEventListener('event', () => { |
| 31 | + ++fireCount; |
| 32 | + }); |
| 33 | + |
| 34 | + const event = eventType == "inner" ? window.innerEvent : new Event('event'); |
| 35 | + window.innerEventTarget.dispatchEvent(event); |
| 36 | + frame.remove(); |
| 37 | + window.innerEventTarget.dispatchEvent(event); |
| 38 | + |
| 39 | + assert_equals(fireCount, 1, `Expected one listener to run for type "${eventType}"`); |
| 40 | + } |
| 41 | + }, "Event handlers for EventTargets in detached iframes do not fire"); |
| 42 | + |
| 43 | + promise_test(async t => { |
| 44 | + const frame = await addFrameAndWaitForLoad(`<script> |
| 45 | + window.parent.innerEventTarget = new EventTarget(); |
| 46 | + <\/script>`); |
| 47 | + |
| 48 | + let fireCount = 0; |
| 49 | + window.innerEventTarget.addEventListener('event', () => { |
| 50 | + ++fireCount; |
| 51 | + frame.remove(); |
| 52 | + }); |
| 53 | + window.innerEventTarget.addEventListener('event', () => { |
| 54 | + ++fireCount; |
| 55 | + }); |
| 56 | + |
| 57 | + window.innerEventTarget.dispatchEvent(new Event('event')); |
| 58 | + |
| 59 | + assert_equals(fireCount, 1); |
| 60 | + }, "Event listeners do not run after detaching an EventTarget's frame during event dispatch"); |
| 61 | +}; |
| 62 | + |
| 63 | +</script> |
0 commit comments