Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!doctype html>
<meta charset="utf-8" />
<meta name="author" title="Keith Cirkel" href="mailto:wpt@keithcirkel.co.uk" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/invoker-utils.js"></script>
<div id="div"></div>
<button id="button"></button>
<script>
test(function () {
const host = document.createElement("div");
const child = host.appendChild(document.createElement("p"));
const shadow = host.attachShadow({ mode: "closed" });
const slot = shadow.appendChild(document.createElement("slot"));
let childEvent = null;
let childEventTarget = null;
let childEventSource = null;
let hostEvent = null;
let hostEventTarget = null;
let hostEventSource = null;
slot.addEventListener(
"command",
(e) => {
childEvent = e;
childEventTarget = e.target;
childEventSource = e.source;
},
{ once: true },
);
host.addEventListener(
"command",
(e) => {
hostEvent = e;
hostEventTarget = e.target;
hostEventSource = e.source;
},
{ once: true },
);
const event = new CommandEvent("command", {
bubbles: true,
source: slot,
composed: true,
});
slot.dispatchEvent(event);
assert_true(childEvent instanceof CommandEvent, "slot saw invoke event");
assert_equals(
childEventTarget,
slot,
"target is child inside shadow boundary",
);
assert_equals(
childEventSource,
slot,
"source is child inside shadow boundary",
);
assert_equals(
hostEvent,
childEvent,
"event dispatch propagates across shadow boundary",
);
assert_equals(
hostEventTarget,
host,
"target is retargeted to shadowroot host",
);
assert_equals(
hostEventSource,
host,
"source is retargeted to shadowroot host",
);
}, "CommandEvent propagates across shadow boundaries retargeting source");
test(function (t) {
const host = document.createElement("div");
document.body.append(host);
t.add_cleanup(() => host.remove());
const shadow = host.attachShadow({ mode: "open" });
const button = shadow.appendChild(document.createElement("button"));
const invokee = host.appendChild(document.createElement("div"));
button.commandForElement = invokee;
button.command = '--test-command';
let event = null;
let eventTarget = null;
let eventSource = null;
invokee.addEventListener(
"command",
(e) => {
event = e;
eventTarget = e.target;
eventSource = e.source;
},
{ once: true },
);
button.click();
assert_true(event instanceof CommandEvent);
assert_equals(eventTarget, invokee, "target is invokee");
assert_equals(eventSource, host, "source is host");
}, "cross shadow CommandEvent retargets source to host element");
</script>