Skip to content

Truly independent web browser

LibWeb: Keep the target of an untrusted event dispatched unheard

sideshowbarker5c6970a9d437
diff --git a/Libraries/LibWeb/DOM/EventDispatcher.cpp b/Libraries/LibWeb/DOM/EventDispatcher.cpp
index 44aeac77c85..a072fe5186d 100644
--- a/Libraries/LibWeb/DOM/EventDispatcher.cpp
+++ b/Libraries/LibWeb/DOM/EventDispatcher.cpp
@@ -284,11 +284,12 @@ bool EventDispatcher::dispatch(GC::Ref<EventTarget> target, Event& event, bool l
// 5. Let clearTargets be false.
bool clear_targets = false;
- // OPTIMIZATION: Only dispatch events if there is at least one listener on the node or its ancestors. Activation
- // events are always dispatched. This saves us from going through the path building and dispatch
- // phases for events that will be dropped on the floor anyway.
+ // OPTIMIZATION: Only dispatch trusted events if there is at least one listener on the node or its ancestors.
+ // Activation events are always dispatched. This saves us from going through the path building and
+ // dispatch phases for events that will be dropped on the floor anyway. Untrusted events are always
+ // dispatched, since the script that dispatched one can read the target that dispatch leaves on it.
bool is_activation_event = is<UIEvents::MouseEvent>(event) && event.type() == HTML::EventNames::click;
- bool should_dispatch = is_activation_event;
+ bool should_dispatch = is_activation_event || !event.is_trusted();
if (!should_dispatch) {
auto const* node = as_if<Node>(*target);
auto has_listener_on_event_path = [&](Utf16FlyString const& type) {
diff --git a/Tests/LibWeb/Text/expected/DOM/Event-target-after-dispatch-without-listeners.txt b/Tests/LibWeb/Text/expected/DOM/Event-target-after-dispatch-without-listeners.txt
new file mode 100644
index 00000000000..c27da14d178
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/Event-target-after-dispatch-without-listeners.txt
@@ -0,0 +1,4 @@
+target is the element: true
+srcElement is the element: true
+currentTarget: null
+eventPhase: 0
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch-listenerless.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch-listenerless.txt
new file mode 100644
index 00000000000..ac61f6c245a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch-listenerless.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass Non-bubbling composed event is retargeted after listenerless dispatch
+Pass relatedTarget is preserved after listenerless dispatch
+Pass relatedTarget remains retargetable after listenerless dispatch
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch-no-listeners.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch-no-listeners.txt
new file mode 100644
index 00000000000..d19637da9f2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch-no-listeners.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 5 tests
+
+5 Pass
+Pass Event properties post dispatch with an open ShadowRoot and no listeners (composed: true)
+Pass Event properties post dispatch with an open ShadowRoot and no listeners (composed: false)
+Pass Event properties post dispatch with a closed ShadowRoot and no listeners (composed: true)
+Pass Event properties post dispatch with nested open ShadowRoots and no listeners (composed: true)
+Pass Event properties post dispatch with a disabled element in shadow tree and no listeners (composed: true)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch.txt
new file mode 100644
index 00000000000..3cf3dcfab07
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/event-post-dispatch.txt
@@ -0,0 +1,21 @@
+Harness status: OK
+
+Found 16 tests
+
+16 Pass
+Pass Event properties post dispatch without ShadowRoots (composed: true).
+Pass Event properties post dispatch without ShadowRoots (composed: false).
+Pass Event properties post dispatch with an open ShadowRoot (composed: true).
+Pass Event properties post dispatch with an open ShadowRoot (composed: false).
+Pass Event properties post dispatch with a closed ShadowRoot (composed: true).
+Pass Event properties post dispatch with a closed ShadowRoot (composed: false).
+Pass Event properties post dispatch with nested ShadowRoots (composed: true).
+Pass Event properties post dispatch with nested ShadowRoots (composed: false).
+Pass Event properties post dispatch with relatedTarget in the same shadow tree. (composed: true)
+Pass Event properties post dispatch with relatedTarget in the same shadow tree. (composed: false)
+Pass Event properties post dispatch with relatedTarget in the document tree and the shadow tree. (composed: true)
+Pass Event properties post dispatch with relatedTarget in the document tree and the shadow tree. (composed: false)
+Pass Event properties post dispatch with relatedTarget in the different shadow trees. (composed: true)
+Pass Event properties post dispatch with relatedTarget in the different shadow trees. (composed: false)
+Pass Event properties post dispatch when target get moved out of the shadow tree by event listener
+Pass Event properties post dispatch when target get moved into the shadow tree by event listener
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/DOM/Event-target-after-dispatch-without-listeners.html b/Tests/LibWeb/Text/input/DOM/Event-target-after-dispatch-without-listeners.html
new file mode 100644
index 00000000000..3b98642015c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/Event-target-after-dispatch-without-listeners.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="target"></div>
+<script>
+ test(() => {
+ // Nothing listens for this event type anywhere, so dispatch invokes no listener.
+ const target = document.getElementById("target");
+ const event = new Event("unheard", { bubbles: true });
+ target.dispatchEvent(event);
+ println(`target is the element: ${event.target === target}`);
+ println(`srcElement is the element: ${event.srcElement === target}`);
+ println(`currentTarget: ${event.currentTarget}`);
+ println(`eventPhase: ${event.eventPhase}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch-listenerless.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch-listenerless.html
new file mode 100644
index 00000000000..9708c0aa23e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch-listenerless.html
@@ -0,0 +1,75 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Event properties after listenerless shadow DOM dispatch</title>
+<link rel="help" href="https://dom.spec.whatwg.org/#concept-event-dispatch">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<body>
+<script>
+"use strict";
+
+function createShadowTree() {
+ const host = document.createElement("div");
+ const shadowRoot = host.attachShadow({ mode: "open" });
+ const target = document.createElement("div");
+ shadowRoot.append(target);
+ document.body.append(host);
+ return { host, target };
+}
+
+test(t => {
+ const { host, target } = createShadowTree();
+ t.add_cleanup(() => host.remove());
+
+ const event = new Event("test", { bubbles: false, composed: true });
+ target.dispatchEvent(event);
+
+ assert_equals(event.target, host, "target is retargeted to the host");
+ assert_equals(event.currentTarget, null, "currentTarget is cleared");
+ assert_equals(event.eventPhase, Event.NONE, "eventPhase is reset");
+}, "Non-bubbling composed event is retargeted after listenerless dispatch");
+
+test(t => {
+ const { host, target } = createShadowTree();
+ const relatedTarget = document.createElement("div");
+ document.body.append(relatedTarget);
+ t.add_cleanup(() => {
+ host.remove();
+ relatedTarget.remove();
+ });
+
+ const event = new MouseEvent("test", { bubbles: true, composed: true, relatedTarget });
+ target.dispatchEvent(event);
+
+ assert_equals(event.target, host, "target is retargeted to the host");
+ assert_equals(event.relatedTarget, relatedTarget, "relatedTarget retains its final retargeted value");
+}, "relatedTarget is preserved after listenerless dispatch");
+
+test(t => {
+ const { host: firstHost, target: firstTarget } = createShadowTree();
+ const { host: secondHost, target: secondTarget } = createShadowTree();
+ const reachedTargets = [];
+ function recordCurrentTarget(event) {
+ reachedTargets.push(event.currentTarget);
+ }
+ t.add_cleanup(() => {
+ firstHost.remove();
+ secondHost.remove();
+ document.removeEventListener("test", recordCurrentTarget);
+ });
+
+ const event = new MouseEvent("test", { bubbles: true, composed: true, relatedTarget: secondTarget });
+ firstTarget.dispatchEvent(event);
+ assert_equals(event.relatedTarget, secondHost, "relatedTarget is initially retargeted to the second host");
+
+ secondTarget.addEventListener("test", recordCurrentTarget);
+ secondHost.addEventListener("test", recordCurrentTarget);
+ document.addEventListener("test", recordCurrentTarget);
+ secondTarget.dispatchEvent(event);
+
+ assert_array_equals(reachedTargets, [secondTarget], "redispatched event stops before its relatedTarget");
+ assert_equals(event.target, null, "shadow-tree target is cleared after redispatch");
+ assert_equals(event.relatedTarget, null, "relatedTarget is cleared with the shadow-tree target");
+}, "relatedTarget remains retargetable after listenerless dispatch");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch-no-listeners.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch-no-listeners.html
new file mode 100644
index 00000000000..9205a8bf9dd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch-no-listeners.html
@@ -0,0 +1,141 @@
+<!DOCTYPE html>
+<title>Shadow DOM: Event properties post dispatch when there are no event listeners</title>
+<meta name="author" title="Chris Dumez" href="mailto:[email protected]">
+<link rel="help" href="https://dom.spec.whatwg.org/#concept-event-dispatch">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="resources/shadow-dom.js"></script>
+
+<div id="test1">
+ <div id="host">
+ <template id="sr" data-mode="open">
+ <div id="target"></div>
+ </template>
+ </div>
+</div>
+<script>
+test(() => {
+ const nodes = createTestTree(test1);
+ document.body.appendChild(nodes.test1);
+
+ // Use a unique event type with no listeners registered anywhere in the document.
+ const event = new Event('unheard-event', { bubbles: true, composed: true });
+ nodes.target.dispatchEvent(event);
+
+ // The event target should be retargeted to the host since the original target
+ // is inside the shadow tree and the event is composed.
+ assert_equals(event.target, nodes.host,
+ 'event.target should be the shadow host (retargeted) after dispatch');
+ assert_equals(event.eventPhase, 0);
+ assert_equals(event.currentTarget, null);
+ assert_array_equals(event.composedPath(), []);
+
+ nodes.test1.remove();
+}, 'Event properties post dispatch with an open ShadowRoot and no listeners (composed: true)');
+</script>
+
+<div id="test2">
+ <div id="host">
+ <template id="sr" data-mode="open">
+ <div id="target"></div>
+ </template>
+ </div>
+</div>
+<script>
+test(() => {
+ const nodes = createTestTree(test2);
+ document.body.appendChild(nodes.test2);
+
+ const event = new Event('unheard-event-2', { bubbles: true, composed: false });
+ nodes.target.dispatchEvent(event);
+
+ // Non-composed event stays inside the shadow tree; target should be cleared
+ // post dispatch since the target's root is a shadow root.
+ assert_equals(event.target, null,
+ 'event.target should be null for non-composed event inside shadow tree');
+ assert_equals(event.eventPhase, 0);
+ assert_equals(event.currentTarget, null);
+ assert_array_equals(event.composedPath(), []);
+
+ nodes.test2.remove();
+}, 'Event properties post dispatch with an open ShadowRoot and no listeners (composed: false)');
+</script>
+
+<div id="test3">
+ <div id="host">
+ <template id="sr" data-mode="closed">
+ <div id="target"></div>
+ </template>
+ </div>
+</div>
+<script>
+test(() => {
+ const nodes = createTestTree(test3);
+ document.body.appendChild(nodes.test3);
+
+ const event = new Event('unheard-event-3', { bubbles: true, composed: true });
+ nodes.target.dispatchEvent(event);
+
+ assert_equals(event.target, nodes.host,
+ 'event.target should be the shadow host (retargeted) after dispatch');
+ assert_equals(event.eventPhase, 0);
+ assert_equals(event.currentTarget, null);
+ assert_array_equals(event.composedPath(), []);
+
+ nodes.test3.remove();
+}, 'Event properties post dispatch with a closed ShadowRoot and no listeners (composed: true)');
+</script>
+
+<div id="test4">
+ <div id="host1">
+ <template id="sr1" data-mode="open">
+ <div id="host2">
+ <template id="sr2" data-mode="open">
+ <div id="target"></div>
+ </template>
+ </div>
+ </template>
+ </div>
+</div>
+<script>
+test(() => {
+ const nodes = createTestTree(test4);
+ document.body.appendChild(nodes.test4);
+
+ const event = new Event('unheard-event-4', { bubbles: true, composed: true });
+ nodes.target.dispatchEvent(event);
+
+ assert_equals(event.target, nodes.host1,
+ 'event.target should be the outermost shadow host after dispatch');
+ assert_equals(event.eventPhase, 0);
+ assert_equals(event.currentTarget, null);
+ assert_array_equals(event.composedPath(), []);
+
+ nodes.test4.remove();
+}, 'Event properties post dispatch with nested open ShadowRoots and no listeners (composed: true)');
+</script>
+
+<div id="test5">
+ <div id="host">
+ <template id="sr" data-mode="open">
+ <input id="target" disabled>
+ </template>
+ </div>
+</div>
+<script>
+test(() => {
+ const nodes = createTestTree(test5);
+ document.body.appendChild(nodes.test5);
+
+ const event = new Event('unheard-event-5', { bubbles: true, composed: true });
+ nodes.target.dispatchEvent(event);
+
+ assert_equals(event.target, nodes.host,
+ 'event.target should be the shadow host (retargeted) after dispatch');
+ assert_equals(event.eventPhase, 0);
+ assert_equals(event.currentTarget, null);
+ assert_array_equals(event.composedPath(), []);
+
+ nodes.test5.remove();
+}, 'Event properties post dispatch with a disabled element in shadow tree and no listeners (composed: true)');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch.html
new file mode 100644
index 00000000000..99eb75fc97f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/event-post-dispatch.html
@@ -0,0 +1,282 @@
+<!DOCTYPE html>
+<title>Shadow DOM: Event dispatch post result for event properties.</title>
+<meta name="author" title="Eriko Kurimoto" href="mailto:[email protected]">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/event-path-test-helpers.js"></script>
+
+<div id="test1">
+ <div id="d1">
+ <div id="target"></div>
+ </div>
+</div>
+
+<script>
+let n1 = createTestTree(test1);
+document.body.appendChild(n1.test1);
+test(() => {
+ let log = dispatchEventWithEventLog(n1, n1.target, new Event('my-event', { bubbles: true, composed: true }));
+ assert_equals(log.event.target, n1.target);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch without ShadowRoots (composed: true).');
+
+test(() => {
+ let log = dispatchEventWithEventLog(n1, n1.target, new Event('my-event', { bubbles: true, composed: false }));
+ assert_equals(log.event.target, n1.target);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch without ShadowRoots (composed: false).');
+document.body.removeChild(n1.test1);
+</script>
+
+<div id="test2">
+ <div id="host">
+ <template id="sr" data-mode="open">
+ <div id="target"></div>
+ </template>
+ </div>
+</div>
+
+<script>
+let n2 = createTestTree(test2);
+document.body.appendChild(n2.test2);
+test(() => {
+ let log = dispatchEventWithEventLog(n2, n2.target, new Event('my-event', { bubbles: true, composed: true }));
+ assert_equals(log.event.target, n2.host);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with an open ShadowRoot (composed: true).');
+
+test(() => {
+ let log = dispatchEventWithEventLog(n2, n2.target, new Event('my-event', { bubbles: true, composed: false }));
+ assert_equals(log.event.target, null);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with an open ShadowRoot (composed: false).');
+document.body.removeChild(n2.test2);
+</script>
+
+<div id="test3">
+ <div id="host">
+ <template id="sr" data-mode="closed">
+ <div id="target"></div>
+ </template>
+ </div>
+</div>
+
+<script>
+let n3 = createTestTree(test3);
+document.body.appendChild(n3.test3);
+test(() => {
+ let log = dispatchEventWithEventLog(n3, n3.target, new Event('my-event', { bubbles: true, composed: true }));
+ assert_equals(log.event.target, n3.host);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with a closed ShadowRoot (composed: true).');
+
+test(() => {
+ let log = dispatchEventWithEventLog(n3, n3.target, new Event('my-event', { bubbles: true, composed: false }));
+ assert_equals(log.event.target, null);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with a closed ShadowRoot (composed: false).');
+document.body.removeChild(n3.test3);
+</script>
+
+<div id="test4">
+ <div id="host1">
+ <template id="sr" data-mode="open">
+ <div id="host2">
+ <template id="sr" data-mode="open">
+ <div id="target"></div>
+ </template>
+ </div>
+ </template>
+ </div>
+</div>
+
+<script>
+let n4 = createTestTree(test4);
+document.body.appendChild(n4.test4);
+test(() => {
+ let log = dispatchEventWithEventLog(n4, n4.target, new Event('my-event', { bubbles: true, composed: true }));
+ assert_equals(log.event.target, n4.host1);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with nested ShadowRoots (composed: true).');
+
+test(() => {
+ let log = dispatchEventWithEventLog(n4, n4.target, new Event('my-event', { bubbles: true, composed: false }));
+ assert_equals(log.event.target, null);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with nested ShadowRoots (composed: false).');
+document.body.removeChild(n4.test4);
+</script>
+
+<div id="test5">
+ <div id="host">
+ <template id="sr" data-mode="open">
+ <div id="relatedTarget">
+ <div id="target"></div>
+ </div>
+ </template>
+ </div>
+</div>
+
+<script>
+let n5 = createTestTree(test5);
+document.body.appendChild(n5.test5);
+test(() => {
+ let log = dispatchEventWithEventLog(n5, n5.target, new MouseEvent('my-event', {bubbles: true, composed: true, relatedTarget: n5.relatedTarget}));
+ assert_equals(log.event.target, null);
+ assert_equals(log.event.relatedTarget, null);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with relatedTarget in the same shadow tree. (composed: true)');
+
+test(() => {
+ let log = dispatchEventWithEventLog(n5, n5.target, new MouseEvent('my-event', {bubbles: true, composed: false, relatedTarget: n5.relatedTarget}));
+ assert_equals(log.event.target, null);
+ assert_equals(log.event.relatedTarget, null);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with relatedTarget in the same shadow tree. (composed: false)');
+document.body.removeChild(n5.test5);
+</script>
+
+<div id="test6">
+ <div id="host">
+ <template id="sr" data-mode="open">
+ <div id="target"></div>
+ </template>
+ </div>
+ <div id="relatedTarget"></div>
+</div>
+
+<script>
+let n6 = createTestTree(test6);
+document.body.appendChild(n6.test6);
+test(() => {
+ let log = dispatchEventWithEventLog(n6, n6.target, new MouseEvent('my-event', {bubbles: true, composed: true, relatedTarget: n6.relatedTarget}));
+ assert_equals(log.event.target, n6.host);
+ assert_equals(log.event.relatedTarget, n6.relatedTarget);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with relatedTarget in the document tree and the shadow tree. (composed: true)');
+
+test(() => {
+ let log = dispatchEventWithEventLog(n6, n6.target, new MouseEvent('my-event', {bubbles: true, composed: false, relatedTarget: n6.relatedTarget}));
+ assert_equals(log.event.target, null);
+ assert_equals(log.event.relatedTarget, null);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with relatedTarget in the document tree and the shadow tree. (composed: false)');
+document.body.removeChild(n6.test6);
+</script>
+
+<div id="test7">
+ <div id="host1">
+ <template id="sr1" data-mode="open">
+ <div id="target"></div>
+ </template>
+ </div>
+ <div id="host2">
+ <template id="sr2" data-mode="open">
+ <div id="relatedTarget"></div>
+ </template>
+ </div>
+</div>
+
+<script>
+let n7 = createTestTree(test7);
+document.body.appendChild(n7.test7);
+test(() => {
+ let log = dispatchEventWithEventLog(n7, n7.target, new MouseEvent('my-event', {bubbles: true, composed: true, relatedTarget: n7.relatedTarget}));
+ assert_equals(log.event.target, n7.host1);
+ assert_equals(log.event.relatedTarget, n7.host2);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with relatedTarget in the different shadow trees. (composed: true)');
+
+test(() => {
+ let log = dispatchEventWithEventLog(n7, n7.target, new MouseEvent('my-event', {bubbles: true, composed: false, relatedTarget: n7.relatedTarget}));
+ assert_equals(log.event.target, null);
+ assert_equals(log.event.relatedTarget, null);
+ assert_equals(log.event.eventPhase, 0);
+ assert_equals(log.event.currentTarget, null);
+ assert_equals(log.event.composedPath().length, 0);
+}, 'Event properties post dispatch with relatedTarget in the different shadow trees. (composed: false)');
+document.body.removeChild(n7.test7);
+
+test(t => {
+ let host = document.createElement("div");
+ let shadow = host.attachShadow({ mode: "open" });
+ let target = shadow.appendChild(document.createElement("trala"));
+
+ let eventListenerCalled = false;
+ target.addEventListener("my-event", t.step_func(e => {
+ eventListenerCalled = true;
+ assert_equals(window.event, undefined);
+
+ // Move target node out of shadow tree.
+ host.appendChild(target);
+ }));
+
+ let event = new MouseEvent('my-event', {composed: false, relatedTarget: host});
+ target.dispatchEvent(event);
+ assert_true(eventListenerCalled);
+
+ assert_equals(event.target, null, "target should have been cleared");
+ assert_equals(event.relatedTarget, null, "relatedTarget should have been cleared");
+ assert_equals(event.eventPhase, 0);
+ assert_equals(event.currentTarget, null);
+ assert_equals(event.composedPath().length, 0);
+}, 'Event properties post dispatch when target get moved out of the shadow tree by event listener');
+
+test(t => {
+ let host = document.createElement("div");
+ let shadow = host.attachShadow({ mode: "open" });
+ let target = host.appendChild(document.createElement("trala"));
+
+ let eventListenerCalled = false;
+ target.addEventListener("my-event", t.step_func(e => {
+ assert_equals(window.event, e);
+ }));
+ target.addEventListener("my-event", t.step_func(e => {
+ eventListenerCalled = true;
+ assert_equals(window.event, e);
+ // Move target node into the shadow tree.
+ shadow.append(target);
+ }));
+ target.addEventListener("my-event", t.step_func(e => {
+ assert_equals(window.event, e);
+ }));
+
+ let event = new MouseEvent('my-event', {composed: false, relatedTarget: host});
+ target.dispatchEvent(event);
+ assert_true(eventListenerCalled);
+
+ // targets should not have been cleared since the node was not in the shadow tree when the event was initially dispatched.
+ assert_equals(event.target, target, "Target should not have been cleared");
+ assert_equals(event.relatedTarget, host, "relatedTarget should not have been cleared");
+ assert_equals(event.eventPhase, 0);
+ assert_equals(event.currentTarget, null);
+ assert_equals(event.composedPath().length, 0);
+}, 'Event properties post dispatch when target get moved into the shadow tree by event listener');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/resources/event-path-test-helpers.js b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/resources/event-path-test-helpers.js
new file mode 100644
index 00000000000..f0e8ec33ff7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/resources/event-path-test-helpers.js
@@ -0,0 +1,92 @@
+
+function dispatchEventWithEventLog(shadow, target, event) {
+ var eventPath = [];
+ var targets = [];
+ var relatedTargets = [];
+ var pathAtTargets = [];
+
+ var attachedNodes = [];
+ for (var nodeKey in shadow) {
+ var startingNode = shadow[nodeKey];
+ for (var node = startingNode; node; node = node.parentNode) {
+ if (attachedNodes.indexOf(node) >= 0)
+ continue;
+ attachedNodes.push(node);
+ node.addEventListener(event.type, (function (event) {
+ eventPath.push(this.label);
+ relatedTargets.push(event.relatedTarget ? event.relatedTarget.label : null);
+
+ pathAtTargets.push(event.composedPath().map(function (node) { return node.label; }));
+ targets.push(event.target);
+ }).bind(node));
+ }
+ }
+
+ target.dispatchEvent(event);
+
+ return {event: event, targets: targets, eventPath: eventPath, relatedTargets: relatedTargets, pathAtTargets: pathAtTargets};
+}
+
+/*
+-SR: ShadowRoot -S: Slot
+A ------------------------------- A-SR
++ B ------------ B-SR + A1 --- A1-SR
+ + C + B1 --- B1-SR + A2-S + A1a
+ + D --- D-SR + B1a + B1b --- B1b-SR
+ + D1 + B1c-S + B1b1
+ + B1b2
+*/
+function createFixedTestTree(mode) {
+ var namedNodes = {};
+
+ function element(name) {
+ var element = document.createElement(name.indexOf('-S') > 0 ? 'slot' : 'div');
+ element.label = name;
+ namedNodes[name] = element;
+ for (var i = 1; i < arguments.length; i++) {
+ var item = arguments[i];
+ if (typeof(item) == 'function')
+ item(element);
+ else
+ element.appendChild(item);
+ }
+ return element;
+ }
+
+ function shadow(name) {
+ var children = [];
+ for (var i = 1; i < arguments.length; i++)
+ children.push(arguments[i]);
+ return function (element) {
+ var shadowRoot = element.attachShadow({mode: mode});
+ shadowRoot.label = name;
+ namedNodes[name] = shadowRoot;
+ for (var child of children)
+ shadowRoot.appendChild(child);
+ }
+ }
+
+ var host = element('A',
+ shadow('A-SR',
+ element('A1',
+ shadow('A1-SR',
+ element('A1a'))),
+ element('A2-S')
+ ),
+ element('B',
+ shadow('B-SR',
+ element('B1',
+ shadow('B1-SR',
+ element('B1b',
+ shadow('B1b-SR',
+ element('B1b1'),
+ element('B1b2'))),
+ element('B1c-S')),
+ element('B1a'))),
+ element('C'),
+ element('D',
+ shadow('D-SR',
+ element('D1')))));
+
+ return namedNodes;
+}