Skip to content

Commit

Permalink
Bug 1885722 [wpt PR 45145] - Coalesce selectionchange events per w3c/…
Browse files Browse the repository at this point in the history
…selection-api#170, a=testonly

Automatic update from web-platform-tests
Coalesce selectionchange events per w3c/selection-api#170 (#45145)

--

wpt-commits: fd3510de36638b07103738b9ff2bfa9ede340797
wpt-pr: 45145
  • Loading branch information
rniwa authored and moz-wptsync-bot committed Mar 27, 2024
1 parent 234caba commit 68731b3
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="help" href="https://w3c.github.io/selection-api/#selectionchange-event">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<input id="input1" value="hello">
<input id="input2" value="world">
<textarea id="textarea1">hello</textarea>
<textarea id="textarea2">world</textarea>
<script>

promise_test(() => {
return (async function() {
let selectionChangeCount1 = 0;
let selectionChangeCount2 = 0;
input1.addEventListener("selectionchange", () => ++selectionChangeCount1);
input2.addEventListener("selectionchange", () => ++selectionChangeCount2);
input1.setSelectionRange(1, 2);
input1.setSelectionRange(2, 3);
input2.setSelectionRange(1, 3);
assert_equals(selectionChangeCount1, 0);
assert_equals(selectionChangeCount2, 0);
await new Promise(setTimeout);
assert_equals(selectionChangeCount1, 1);
assert_equals(selectionChangeCount2, 1);
})();
}, "selectionchange event on each input element fires independently");

promise_test(() => {
return (async function() {
let selectionChangeCount1 = 0;
let selectionChangeCount2 = 0;
textarea1.addEventListener("selectionchange", () => ++selectionChangeCount1);
textarea2.addEventListener("selectionchange", () => ++selectionChangeCount2);
textarea1.setSelectionRange(1, 2);
textarea1.setSelectionRange(2, 3);
textarea2.setSelectionRange(1, 3);
assert_equals(selectionChangeCount1, 0);
assert_equals(selectionChangeCount2, 0);
await new Promise(setTimeout);
assert_equals(selectionChangeCount1, 1);
assert_equals(selectionChangeCount2, 1);
})();
}, "selectionchange event on each textarea element fires independently");

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="help" href="https://w3c.github.io/selection-api/#selectionchange-event">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<div id="container"><br><br></div>
<script>

promise_test(() => {
return new Promise(resolve => {
let didFireSelectionChangeEvent = false;
document.addEventListener("selectionchange", () => { didFireSelectionChangeEvent = true; resolve(); }, {once: true});
getSelection().setPosition(container, 0);
assert_false(didFireSelectionChangeEvent);
});
}, "selectionchange event on document fires");

promise_test(() => {
return (async function() {
let selectionChangeCount = 0;
document.addEventListener("selectionchange", () => ++selectionChangeCount);
container.innerHTML = '<span><br></span><span><br></span>';
getSelection().setPosition(container, 0);
assert_equals(selectionChangeCount, 0);
getSelection().setPosition(container, 2);
assert_equals(selectionChangeCount, 0);
await new Promise(setTimeout);
assert_equals(selectionChangeCount, 1);
})();
}, "selectionchange event on document fires once");

promise_test(() => {
return (async function() {
let selectionChangeCount = 0;
document.addEventListener("selectionchange", () => ++selectionChangeCount);
container.innerHTML = '<span><br></span><span><br></span>';
getSelection().setPosition(container, 0);
assert_equals(selectionChangeCount, 0);
getSelection().setPosition(container, 2);
assert_equals(selectionChangeCount, 0);
await new Promise(setTimeout);
assert_equals(selectionChangeCount, 1);
getSelection().setPosition(container, 0);
assert_equals(selectionChangeCount, 1);
getSelection().setPosition(container, 2);
assert_equals(selectionChangeCount, 1);
await new Promise(setTimeout);
assert_equals(selectionChangeCount, 2);
})();
}, "task to fire selectionchange event gets queued each time selection is mutated");

promise_test(() => {
return (async function() {
let selectionChangeCount = 0;
document.addEventListener("selectionchange", () => {
if (!selectionChangeCount) {
getSelection().setPosition(container, 2);
getSelection().setPosition(container, 0);
}
++selectionChangeCount;
});
container.innerHTML = '<b><br></b><b><br></b>';
getSelection().setPosition(container, 0);
assert_equals(selectionChangeCount, 0);
await new Promise(setTimeout);
assert_equals(selectionChangeCount, 1);
await new Promise(setTimeout);
assert_equals(selectionChangeCount, 2);
})();
}, "has scheduled selectionchange event is set to false at the beginning of a task to fire selectionchange event");

</script>
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
target.setRangeText("foo", 2, 6);

await data.assert_empty_spin();
assert_equals(collector.events.length, 2);
assert_equals(collector.events.length, 1);
}, `Calling setRangeText() after select() on ${name}`);

promise_test(async () => {
Expand All @@ -196,7 +196,7 @@
target.setRangeText("", 10, 12);

await data.assert_empty_spin();
assert_equals(collector.events.length, 4);
assert_equals(collector.events.length, 1);
}, `Calling setRangeText() repeatedly on ${name}`);

promise_test(async () => {
Expand Down

0 comments on commit 68731b3

Please sign in to comment.