chromium/third_party/blink/web_tests/external/wpt/html/semantics/invokers/interestevent-dispatch-shadow.tentative.html

<!doctype html>
<meta charset="utf-8" />
<meta name="author" title="Keith Cirkel" href="mailto:[email protected]" />
<meta name="author" title="Luke Warlow" href="mailto:[email protected]" />
<link rel="help" href="https://open-ui.org/components/interest-invokers.explainer/" />
<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 shadow = host.attachShadow({ mode: "closed" });
    const slot = shadow.appendChild(document.createElement("slot"));
    let childEvent = null;
    let childEventTarget = null;
    let childEventInvoker = null;
    let hostEvent = null;
    let hostEventTarget = null;
    let hostEventInvoker = null;
    slot.addEventListener(
      "interest",
      (e) => {
        childEvent = e;
        childEventTarget = e.target;
        childEventInvoker = e.invoker;
      },
      { once: true },
    );
    host.addEventListener(
      "interest",
      (e) => {
        hostEvent = e;
        hostEventTarget = e.target;
        hostEventInvoker = e.invoker;
      },
      { once: true },
    );
    const event = new InterestEvent("interest", {
      bubbles: true,
      invoker: slot,
      composed: true,
    });
    slot.dispatchEvent(event);
    assert_true(childEvent instanceof InterestEvent, "slot saw interest event");
    assert_equals(
      childEventTarget,
      slot,
      "target is child inside shadow boundary",
    );
    assert_equals(
      childEventInvoker,
      slot,
      "invoker 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(
      hostEventInvoker,
      host,
      "invoker is retargeted to shadowroot host",
    );
  }, "InterestEvent propagates across shadow boundaries retargeting invoker");

  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 interestee = host.appendChild(document.createElement("div"));
    button.interestTargetElement = interestee;
    let event = null;
    let eventTarget = null;
    let eventInvoker = null;
    interestee.addEventListener(
      "interest",
      (e) => {
        event = e;
        eventTarget = e.target;
        eventInvoker = e.invoker;
      },
      { once: true },
    );
    button.focus();
    assert_true(event instanceof InterestEvent);
    assert_equals(eventTarget, interestee, "target is interestee");
    assert_equals(eventInvoker, host, "interestee is host");
  }, "cross shadow InterestEvent retargets interestee to host element");
</script>