Skip to content

Commit

Permalink
Dispatch BeforeInput Event for number input spin button and Arrow key
Browse files Browse the repository at this point in the history
https://w3c.github.io/uievents/#event-type-beforeinput
A user agent MUST dispatch beforeinput event when the DOM is about to be updated.

Bug: 40948436
Change-Id: Idc894f4e7427a81e684b37cc10749b4386e5aa7d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5443103
Reviewed-by: Mason Freed <[email protected]>
Commit-Queue: Utkarsh Pathak <[email protected]>
Reviewed-by: Sanket Joshi <[email protected]>
Reviewed-by: Anupam Snigdha <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1308524}
  • Loading branch information
utpathak authored and chromium-wpt-export-bot committed May 31, 2024
1 parent a66ff7e commit 6f491c4
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<iframe id="iframe"></iframe>
<script>
promise_test(async function() {
const child = document.getElementById("iframe");
const childDocument = child.contentDocument;
const inputElement = childDocument.createElement('input');
inputElement.type = "number";
childDocument.body.appendChild(inputElement);
let events = [];
inputElement.addEventListener("beforeinput", () => {
child.remove();
events.push("beforeinput");
});
inputElement.addEventListener("input", () => {
evenst.push("input");
});
inputElement.addEventListener("change", () => {
events.push("change");
});

inputElement.focus();

await new test_driver.send_keys(inputElement, "\uE013");
assert_array_equals(events, ['beforeinput']);
}, "Number input should not crash and not fire subsequent events when event handler removes document");
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<input type="number" id="number_input">
<script>
promise_test(async function() {
const inputElement = document.getElementById("number_input");
let events = [];
inputElement.addEventListener("beforeinput", (e) => {
e.preventDefault();
events.push("beforeinput");
});
inputElement.addEventListener("input", () => {
events.push("input");
});
inputElement.addEventListener("change", () => {
events.push("change");
});

inputElement.focus();

await new test_driver.send_keys(inputElement, "\uE013");
assert_array_equals(events, ['beforeinput']);
}, "Number input should not fire input and change event if the beforeinput event is default prevented");
</script>
</body>
</html>
32 changes: 32 additions & 0 deletions input-events/input-events-arrow-key-on-number-input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<input type="number" id="number_input">
<script>
promise_test(async function() {
const inputElement = document.getElementById("number_input");
let events = [];
inputElement.addEventListener("beforeinput", () => {
events.push("beforeinput");
});
inputElement.addEventListener("input", () => {
events.push("input");
});
inputElement.addEventListener("change", () => {
events.push("change");
});

inputElement.focus();

await new test_driver.send_keys(inputElement, "\uE013");
assert_array_equals(events, ['beforeinput','input','change']);
}, "Number input should fire beforeinput event before the input and change event");
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<iframe id="iframe"></iframe>
<script>
promise_test(async function() {
const child = document.getElementById("iframe");
const childDocument = child.contentDocument;
const inputElement = childDocument.createElement('input');
inputElement.type = "number";
childDocument.body.appendChild(inputElement);
let events = [];
inputElement.addEventListener("beforeinput", () => {
child.remove();
events.push("beforeinput");
});
inputElement.addEventListener("input", () => {
events.push("input");
});
inputElement.addEventListener("change", () => {
events.push("change");
});

inputElement.focus();
// Get the spin button up arrow key location and adjust with iframe offset to get absolute position
const x1 = (inputElement.offsetLeft + inputElement.offsetWidth - 10) + child.offsetLeft;
const y1 = (inputElement.offsetTop + inputElement.offsetHeight / 4) + child.offsetTop;
await test_driver_internal.click(inputElement,{x: x1, y: y1});
assert_array_equals(events, ['beforeinput']);
assert_false(document.body.contains(child));
}, "Number input should not crash and not fire subsequent events when event handler removes document");
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<input type="number" id="number_input">
<script>
promise_test(async function() {
const inputElement = document.getElementById("number_input");
let events = [];
inputElement.addEventListener("beforeinput", (e) => {
e.preventDefault();
events.push("beforeinput");
});
inputElement.addEventListener("input", () => {
events.push("input");
});
inputElement.addEventListener("change", () => {
events.push("change");
});

inputElement.focus();
// Get the spin button up arrow key location
const x1 = (inputElement.offsetLeft + inputElement.offsetWidth - 10);
const y1 = (inputElement.offsetTop + inputElement.offsetHeight / 4);
await test_driver_internal.click(inputElement,{x: x1, y: y1});
assert_array_equals(events, ['beforeinput']);
}, "Number input should not fire input and change event if the beforeinput event is default prevented");
</script>
</body>
</html>
34 changes: 34 additions & 0 deletions input-events/input-events-spin-button-click-on-number-input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<input type="number" id="number_input">
<script>
promise_test(async function() {
const inputElement = document.getElementById("number_input");
let events = [];
inputElement.addEventListener("beforeinput", () => {
events.push("beforeinput");
});
inputElement.addEventListener("input", () => {
events.push("input");
});
inputElement.addEventListener("change", () => {
events.push("change");
});

inputElement.focus();
// Get the spin button up arrow key location
const x1 = (inputElement.offsetLeft + inputElement.offsetWidth - 10);
const y1 = (inputElement.offsetTop + inputElement.offsetHeight / 4);
await test_driver_internal.click(inputElement,{x: x1, y: y1});
assert_array_equals(events, ['beforeinput','input','change']);
}, "Number input should fire beforeinput event before the input and change event");
</script>
</body>
</html>

0 comments on commit 6f491c4

Please sign in to comment.