-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1445934 [wpt PR 9919] - DOM: reset target/relatedTarget, a=testonly
Automatic update from web-platform-testsDOM: reset target/relatedTarget For whatwg/dom#585. wpt-commits: 2f758ac5c16e44915f270ceec31a84eed8f85769 wpt-pr: 9919 wpt-commits: 2f758ac5c16e44915f270ceec31a84eed8f85769 wpt-pr: 9919
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
testing/web-platform/tests/dom/events/relatedTarget.window.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// https://dom.spec.whatwg.org/#concept-event-dispatch | ||
|
||
const host = document.createElement("div"), | ||
child = host.appendChild(document.createElement("p")), | ||
shadow = host.attachShadow({ mode: "closed" }), | ||
slot = shadow.appendChild(document.createElement("slot")); | ||
|
||
test(() => { | ||
for (target of [shadow, slot]) { | ||
for (relatedTarget of [new XMLHttpRequest(), self, host]) { | ||
const event = new FocusEvent("demo", { relatedTarget: relatedTarget }); | ||
target.dispatchEvent(event); | ||
assert_equals(event.target, null); | ||
assert_equals(event.relatedTarget, null); | ||
} | ||
} | ||
}, "Reset if target pointed to a shadow tree"); | ||
|
||
test(() => { | ||
for (relatedTarget of [shadow, slot]) { | ||
for (target of [new XMLHttpRequest(), self, host]) { | ||
const event = new FocusEvent("demo", { relatedTarget: relatedTarget }); | ||
target.dispatchEvent(event); | ||
assert_equals(event.target, null); | ||
assert_equals(event.relatedTarget, null); | ||
} | ||
} | ||
}, "Reset if relatedTarget pointed to a shadow tree"); | ||
|
||
async_test(t => { | ||
const shadowChild = shadow.appendChild(document.createElement("div")); | ||
shadowChild.addEventListener("demo", t.step_func(() => document.body.appendChild(shadowChild))); | ||
const event = new FocusEvent("demo", { relatedTarget: new XMLHttpRequest() }); | ||
shadowChild.dispatchEvent(event); | ||
assert_equals(shadowChild.parentNode, document.body); | ||
assert_equals(event.target, null); | ||
assert_equals(event.relatedTarget, null); | ||
shadowChild.remove(); | ||
t.done(); | ||
}, "Reset if target pointed to a shadow tree pre-dispatch"); | ||
|
||
async_test(t => { | ||
const shadowChild = shadow.appendChild(document.createElement("div")); | ||
shadowChild.addEventListener("demo", t.step_func(() => document.body.appendChild(shadowChild))); | ||
const event = new FocusEvent("demo", { relatedTarget: shadowChild }); | ||
document.body.dispatchEvent(event); | ||
assert_equals(shadowChild.parentNode, document.body); | ||
assert_equals(event.target, null); | ||
assert_equals(event.relatedTarget, null); | ||
shadowChild.remove(); | ||
t.done(); | ||
}, "Reset if relatedTarget pointed to a shadow tree pre-dispatch"); | ||
|
||
async_test(t => { | ||
const event = new FocusEvent("heya", { relatedTarget: shadow, cancelable: true }), | ||
callback = t.unreached_func(); | ||
host.addEventListener("heya", callback); | ||
t.add_cleanup(() => host.removeEventListener("heya", callback)); | ||
event.preventDefault(); | ||
assert_true(event.defaultPrevented); | ||
assert_false(host.dispatchEvent(event)); | ||
assert_equals(event.target, null); | ||
assert_equals(event.relatedTarget, null); | ||
// Check that the dispatch flag is cleared | ||
event.initEvent("x"); | ||
assert_equals(event.type, "x"); | ||
t.done(); | ||
}, "Reset targets on early return"); | ||
|
||
async_test(t => { | ||
const input = document.body.appendChild(document.createElement("input")), | ||
event = new MouseEvent("click", { relatedTarget: shadow }); | ||
let seen = false; | ||
t.add_cleanup(() => input.remove()); | ||
input.type = "checkbox"; | ||
input.oninput = t.step_func(() => { | ||
assert_equals(event.target, null); | ||
assert_equals(event.relatedTarget, null); | ||
assert_equals(event.composedPath().length, 0); | ||
seen = true; | ||
}); | ||
assert_true(input.dispatchEvent(event)); | ||
assert_true(seen); | ||
t.done(); | ||
}, "Reset targets before activation behavior"); |