Skip to content

Truly independent web browser

LibWeb: Cover focus fixup and focused node removal

Andreas Klinga5d3594700cc
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/focus-preserve.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/focus-preserve.txt
new file mode 100644
index 00000000000..7cff4eca7c5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/focus-preserve.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 5 tests
+
+2 Pass
+3 Fail
+Pass moveBefore() preserves focus when the focused element remains focusable
+Pass moveBefore() preserves focus and a backward selection in a text input
+Fail Focus is fixed up after moveBefore() moves the focused element into an inert subtree
+Fail Focus is fixed up after moveBefore() moves the focused element into a hidden parent
+Fail Focus is fixed up after moveBefore() moves an ancestor of the focused element into a hidden parent
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/editing/focus/focus-management/active-element.txt b/Tests/LibWeb/Text/expected/wpt-import/html/editing/focus/focus-management/active-element.txt
new file mode 100644
index 00000000000..078fb02dc23
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/editing/focus/focus-management/active-element.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 3 tests
+
+1 Pass
+2 Fail
+Pass activeElement initially starts as the document body
+Fail activeElement gets set to a focused element and reset to the body when the focused element disappears
+Fail re-adding the formerly active element to the document does not make it active again
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/editing/focus/focus-management/focused-area-removal.txt b/Tests/LibWeb/Text/expected/wpt-import/html/editing/focus/focus-management/focused-area-removal.txt
new file mode 100644
index 00000000000..49720149b96
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/editing/focus/focus-management/focused-area-removal.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail removing the activeElement keeps focus on its document
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/interaction/focus/processing-model/focus-fixup-rule-one-no-dialogs.txt b/Tests/LibWeb/Text/expected/wpt-import/html/interaction/focus/processing-model/focus-fixup-rule-one-no-dialogs.txt
new file mode 100644
index 00000000000..bd4b7f219e6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/interaction/focus/processing-model/focus-fixup-rule-one-no-dialogs.txt
@@ -0,0 +1,115 @@
+Harness status: OK
+
+Found 110 tests
+
+110 Fail
+Fail #button1
+Fail #button2
+Fail #button3
+Fail #button4
+Fail #button5
+Fail #div
+Fail #editable
+Fail #button6
+Fail Textarea untouched: change events after becoming hidden
+Fail Textarea untouched: change events after becoming disabled
+Fail Textarea untouched: change events after becoming inert
+Fail Textarea programmatic value: change events after becoming hidden
+Fail Textarea programmatic value: change events after becoming disabled
+Fail Textarea programmatic value: change events after becoming inert
+Fail Textarea programmatic defaultValue: change events after becoming hidden
+Fail Textarea programmatic defaultValue: change events after becoming disabled
+Fail Textarea programmatic defaultValue: change events after becoming inert
+Fail Textarea user edit: change events after becoming hidden
+Fail Textarea user edit: change events after becoming disabled
+Fail Textarea user edit: change events after becoming inert
+Fail Textarea reverted user edit: change events after becoming hidden
+Fail Textarea reverted user edit: change events after becoming disabled
+Fail Textarea reverted user edit: change events after becoming inert
+Fail Textarea programmatic value followed by user edit: change events after becoming hidden
+Fail Textarea programmatic value followed by user edit: change events after becoming disabled
+Fail Textarea programmatic value followed by user edit: change events after becoming inert
+Fail Textarea user edit with an input handler changing defaultValue: change events after becoming hidden
+Fail Textarea user edit with an input handler changing defaultValue: change events after becoming disabled
+Fail Textarea user edit with an input handler changing defaultValue: change events after becoming inert
+Fail Textarea user edit followed by form reset: change events after becoming hidden
+Fail Textarea user edit followed by form reset: change events after becoming disabled
+Fail Textarea user edit followed by form reset: change events after becoming inert
+Fail Textarea removal discards its pending change before reinsertion
+Fail Cloning an edited textarea does not copy its pending change
+Fail Untouched input type=text: no change after becoming hidden
+Fail Untouched input type=text: no change after becoming disabled
+Fail Untouched input type=text: no change after becoming inert
+Fail Untouched input type=search: no change after becoming hidden
+Fail Untouched input type=search: no change after becoming disabled
+Fail Untouched input type=search: no change after becoming inert
+Fail Untouched input type=tel: no change after becoming hidden
+Fail Untouched input type=tel: no change after becoming disabled
+Fail Untouched input type=tel: no change after becoming inert
+Fail Untouched input type=url: no change after becoming hidden
+Fail Untouched input type=url: no change after becoming disabled
+Fail Untouched input type=url: no change after becoming inert
+Fail Untouched input type=email: no change after becoming hidden
+Fail Untouched input type=email: no change after becoming disabled
+Fail Untouched input type=email: no change after becoming inert
+Fail Untouched input type=password: no change after becoming hidden
+Fail Untouched input type=password: no change after becoming disabled
+Fail Untouched input type=password: no change after becoming inert
+Fail Untouched input type=date: no change after becoming hidden
+Fail Untouched input type=date: no change after becoming disabled
+Fail Untouched input type=date: no change after becoming inert
+Fail Untouched input type=month: no change after becoming hidden
+Fail Untouched input type=month: no change after becoming disabled
+Fail Untouched input type=month: no change after becoming inert
+Fail Untouched input type=week: no change after becoming hidden
+Fail Untouched input type=week: no change after becoming disabled
+Fail Untouched input type=week: no change after becoming inert
+Fail Untouched input type=time: no change after becoming hidden
+Fail Untouched input type=time: no change after becoming disabled
+Fail Untouched input type=time: no change after becoming inert
+Fail Untouched input type=datetime-local: no change after becoming hidden
+Fail Untouched input type=datetime-local: no change after becoming disabled
+Fail Untouched input type=datetime-local: no change after becoming inert
+Fail Untouched input type=number: no change after becoming hidden
+Fail Untouched input type=number: no change after becoming disabled
+Fail Untouched input type=number: no change after becoming inert
+Fail Untouched input type=range: no change after becoming hidden
+Fail Untouched input type=range: no change after becoming disabled
+Fail Untouched input type=range: no change after becoming inert
+Fail Untouched input type=color: no change after becoming hidden
+Fail Untouched input type=color: no change after becoming disabled
+Fail Untouched input type=color: no change after becoming inert
+Fail Untouched input type=checkbox: no change after becoming hidden
+Fail Untouched input type=checkbox: no change after becoming disabled
+Fail Untouched input type=checkbox: no change after becoming inert
+Fail Untouched input type=radio: no change after becoming hidden
+Fail Untouched input type=radio: no change after becoming disabled
+Fail Untouched input type=radio: no change after becoming inert
+Fail Untouched input type=file: no change after becoming hidden
+Fail Untouched input type=file: no change after becoming disabled
+Fail Untouched input type=file: no change after becoming inert
+Fail Untouched input type=submit: no change after becoming hidden
+Fail Untouched input type=submit: no change after becoming disabled
+Fail Untouched input type=submit: no change after becoming inert
+Fail Untouched input type=image: no change after becoming hidden
+Fail Untouched input type=image: no change after becoming disabled
+Fail Untouched input type=image: no change after becoming inert
+Fail Untouched input type=reset: no change after becoming hidden
+Fail Untouched input type=reset: no change after becoming disabled
+Fail Untouched input type=reset: no change after becoming inert
+Fail Untouched input type=button: no change after becoming hidden
+Fail Untouched input type=button: no change after becoming disabled
+Fail Untouched input type=button: no change after becoming inert
+Fail Programmatic text input value: no change after becoming hidden
+Fail User-edited text input: one change after becoming hidden
+Fail User-edited range input: one change after becoming hidden
+Fail Programmatic text input value: no change after becoming disabled
+Fail User-edited text input: one change after becoming disabled
+Fail User-edited range input: one change after becoming disabled
+Fail Programmatic text input value: no change after becoming inert
+Fail User-edited text input: one change after becoming inert
+Fail User-edited range input: one change after becoming inert
+Fail Text input removal discards its pending change before reinsertion
+Fail Changing an edited input to hidden discards its pending change
+Fail Textarea undo after a committed edit creates a new pending change
+Fail Textarea redo after a committed edit creates a new pending change
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/inert/dynamic-inert-on-focused-element.txt b/Tests/LibWeb/Text/expected/wpt-import/inert/dynamic-inert-on-focused-element.txt
new file mode 100644
index 00000000000..271e35e1d66
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/inert/dynamic-inert-on-focused-element.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Fail
+Fail <input> that gets 'inert' attribute
+Fail <input> whose parent gets 'inert' attribute
+Fail <button> that gets 'inert' attribute
+Fail <div> that gets 'inert' attribute
+Fail <div> whose parent gets 'inert' attribute
+Fail <div> whose grandparent gets 'inert' attribute
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/common/rendering-utils.js b/Tests/LibWeb/Text/input/wpt-import/common/rendering-utils.js
new file mode 100644
index 00000000000..46283bd5d07
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/common/rendering-utils.js
@@ -0,0 +1,19 @@
+"use strict";
+
+/**
+ * Waits until we have at least one frame rendered, regardless of the engine.
+ *
+ * @returns {Promise}
+ */
+function waitForAtLeastOneFrame() {
+ return new Promise(resolve => {
+ // Different web engines work slightly different on this area but waiting
+ // for two requestAnimationFrames() to happen, one after another, should be
+ // sufficient to ensure at least one frame has been generated anywhere.
+ window.requestAnimationFrame(() => {
+ window.requestAnimationFrame(() => {
+ resolve();
+ });
+ });
+ });
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/focus-preserve.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/focus-preserve.html
new file mode 100644
index 00000000000..3bf64d756ca
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/focus-preserve.html
@@ -0,0 +1,113 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Focus preservation and focus fixup after moveBefore()</title>
+<link rel="help" href="https://dom.spec.whatwg.org/#dom-parentnode-movebefore">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#focus-fixup-rule">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script src="../../../common/rendering-utils.js"></script>
+<body>
+<script>
+"use strict";
+
+function createTestNodes(t) {
+ const fixture = document.createElement("div");
+ const sourceParent = document.createElement("div");
+ const destinationParent = document.createElement("div");
+ const button = document.createElement("button");
+ button.textContent = "Button";
+
+ sourceParent.append(button);
+ fixture.append(sourceParent, destinationParent);
+ document.body.append(fixture);
+ t.add_cleanup(() => fixture.remove());
+
+ return {button, sourceParent, destinationParent};
+}
+
+function assertFocusPreservedDuringMove(focusedElement, movedNode,
+ destinationParent) {
+ focusedElement.focus();
+ assert_equals(document.activeElement, focusedElement,
+ "The element starts focused");
+
+ destinationParent.moveBefore(movedNode, null);
+ assert_equals(movedNode.parentNode, destinationParent,
+ "moveBefore() moves the node");
+ assert_equals(document.activeElement, focusedElement,
+ "moveBefore() preserves focus synchronously");
+}
+
+async function assertFocusFixupAfterRendering(focusedElement, movedNode,
+ destinationParent) {
+ assertFocusPreservedDuringMove(focusedElement, movedNode, destinationParent);
+
+ // The focus fixup rule runs during a rendering update when the move makes
+ // the focused element non-focusable.
+ await waitForAtLeastOneFrame();
+ assert_equals(document.activeElement, document.body,
+ "Focus is fixed up after a rendering update");
+}
+
+promise_test(async t => {
+ const {button, destinationParent} = createTestNodes(t);
+
+ assertFocusPreservedDuringMove(button, button, destinationParent);
+ await waitForAtLeastOneFrame();
+ assert_equals(document.activeElement, button,
+ "Focus remains on the button after a rendering update");
+}, "moveBefore() preserves focus when the focused element remains focusable");
+
+promise_test(async t => {
+ const {sourceParent, destinationParent} = createTestNodes(t);
+ const input = document.createElement("input");
+ input.value = "0123456789";
+ sourceParent.append(input);
+
+ input.focus();
+ assert_equals(document.activeElement, input, "The input is focused");
+
+ input.setSelectionRange(2, 7, "backward");
+ assert_equals(input.selectionStart, 2, "selectionStart was set");
+ assert_equals(input.selectionEnd, 7, "selectionEnd was set");
+ assert_equals(input.selectionDirection, "backward",
+ "selectionDirection was set");
+
+ destinationParent.moveBefore(input, null);
+ assert_equals(input.parentNode, destinationParent,
+ "The input was moved to the destination parent");
+
+ await waitForAtLeastOneFrame();
+
+ assert_equals(document.activeElement, input,
+ "The input remains focused");
+ assert_equals(input.selectionStart, 2, "selectionStart is preserved");
+ assert_equals(input.selectionEnd, 7, "selectionEnd is preserved");
+ assert_equals(input.selectionDirection, "backward",
+ "selectionDirection is preserved");
+}, "moveBefore() preserves focus and a backward selection in a text input");
+
+promise_test(async t => {
+ const {button, destinationParent} = createTestNodes(t);
+ destinationParent.inert = true;
+
+ await assertFocusFixupAfterRendering(button, button, destinationParent);
+}, "Focus is fixed up after moveBefore() moves the focused element into an inert subtree");
+
+promise_test(async t => {
+ const {button, destinationParent} = createTestNodes(t);
+ destinationParent.hidden = true;
+
+ await assertFocusFixupAfterRendering(button, button, destinationParent);
+}, "Focus is fixed up after moveBefore() moves the focused element into a hidden parent");
+
+promise_test(async t => {
+ const {button, sourceParent, destinationParent} = createTestNodes(t);
+ destinationParent.hidden = true;
+
+ await assertFocusFixupAfterRendering(
+ button, sourceParent, destinationParent);
+}, "Focus is fixed up after moveBefore() moves an ancestor of the focused element into a hidden parent");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/editing/focus/focus-management/active-element.html b/Tests/LibWeb/Text/input/wpt-import/html/editing/focus/focus-management/active-element.html
new file mode 100644
index 00000000000..da72ce4d92e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/editing/focus/focus-management/active-element.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>document.activeElement</title>
+<link rel="author" title="Domenic Denicola" href="mailto:[email protected]">
+<link rel="help" href="https://html.spec.whatwg.org/#dom-document-activeelement">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+
+<div id="parent">
+ <div id="child" tabindex="0"></div>
+</div>
+
+<body>
+<script>
+"use strict";
+
+test(() => {
+
+ assert_equals(document.activeElement, document.body);
+
+}, "activeElement initially starts as the document body");
+
+test(() => {
+
+ const child = document.querySelector("#child");
+ child.focus();
+ assert_equals(document.activeElement, child);
+
+ document.querySelector("#parent").innerHTML = "";
+ assert_equals(document.activeElement, document.body);
+
+}, "activeElement gets set to a focused element and reset to the body when the focused element disappears");
+
+test(() => {
+
+ const el = document.querySelector("#parent");
+ el.tabIndex = 0;
+ el.focus();
+ assert_equals(document.activeElement, el);
+
+ document.body.removeChild(el);
+ document.body.appendChild(el);
+
+ assert_equals(document.activeElement, document.body);
+
+}, "re-adding the formerly active element to the document does not make it active again");
+
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/editing/focus/focus-management/focused-area-removal.html b/Tests/LibWeb/Text/input/wpt-import/html/editing/focus/focus-management/focused-area-removal.html
new file mode 100644
index 00000000000..a5f09cf9162
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/editing/focus/focus-management/focused-area-removal.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Focused area removal</title>
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<body>
+<script>
+"use strict";
+promise_test(async t => {
+ // Wait for the test window to gain focus before testing synchronous focus changes.
+ const sentinel = document.createElement("button");
+ document.body.append(sentinel);
+ t.add_cleanup(() => sentinel.remove());
+ await new Promise(resolve => {
+ sentinel.addEventListener("focus", resolve, { once: true });
+ sentinel.focus();
+ });
+ sentinel.blur();
+
+ const button = document.createElement("button");
+ document.body.appendChild(button);
+ t.add_cleanup(() => button.remove());
+ button.focus();
+
+ assert_equals(document.activeElement, button);
+ assert_equals(document.hasFocus(), true);
+
+ button.remove();
+
+ assert_equals(document.activeElement, document.body);
+ assert_equals(document.hasFocus(), true);
+}, "removing the activeElement keeps focus on its document");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/interaction/focus/processing-model/focus-fixup-rule-one-no-dialogs.html b/Tests/LibWeb/Text/input/wpt-import/html/interaction/focus/processing-model/focus-fixup-rule-one-no-dialogs.html
new file mode 100644
index 00000000000..bcf0bf1d60b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/interaction/focus/processing-model/focus-fixup-rule-one-no-dialogs.html
@@ -0,0 +1,343 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Focus fixup rule one (no &lt;dialog>s involved)</title>
+<link rel="author" title="Domenic Denicola" href="mailto:[email protected]">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#focus-fixup-rule">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/forms.html#attr-fieldset-disabled">
+<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>
+
+<div>
+ <button id="button1">Button 1</button>
+ <button id="button2">Button 2</button>
+ <button id="button3">Button 3</button>
+ <fieldset id="fieldset1"><button id="button4">Button 4</button></fieldset>
+ <fieldset id="fieldset2" disabled><legend><button id="button5">Button 5</button></legend></fieldset>
+ <div id="div" tabindex="0">Div</div>
+ <div id="editable" contenteditable=true>editor</div>
+ <button id="button6">Button 6</button>
+</div>
+
+<script>
+"use strict";
+
+function test_focus_fixup(selector, change, expectSync = false) {
+ promise_test(async function(t) {
+ // Make sure we're not running from a ResizeObserver / etc notification, and
+ // that the document is rendering already.
+ await new Promise(r => {
+ requestAnimationFrame(() => {
+ t.step_timeout(r, 0);
+ });
+ });
+
+ const el = document.querySelector(selector);
+ el.focus();
+
+ assert_equals(document.activeElement, el, `Sanity check: ${selector} must start focused`);
+
+ change(el);
+
+ {
+ const fn = expectSync ? assert_not_equals : assert_equals;
+ fn(document.activeElement, el, `active element ${expectSync ? "is" : "isn't"} fixed-up sync`);
+ }
+
+ // We don't expect blur events in the sync case per spec yet, at least.
+ if (!expectSync) {
+ // Fixup should run after animation frame callbacks, right before the end of
+ // "update the rendering", so after resize observations.
+ let ranFirstRaf = false;
+ let ranRO = false;
+
+ let resolveDone;
+ let done = new Promise(r => { resolveDone = r; });
+
+ requestAnimationFrame(t.step_func(() => {
+ ranFirstRaf = true;
+ assert_equals(document.activeElement, el, "activeElement shouldn't have changed yet (rAF)");
+ requestAnimationFrame(t.step_func(() => {
+ assert_true(ranRO, "ResizeObserver should've ran on the previous frame");
+ resolveDone();
+ }));
+ }));
+
+ let ro = new ResizeObserver(t.step_func(() => {
+ assert_true(ranFirstRaf, "requestAnimationFrame should run before ResizeObserver");
+ ranRO = true;
+ assert_equals(document.activeElement, el, "activeElement shouldn't have changed yet (ResizeObserver)");
+ }));
+
+ // TODO: Test IntersectionObserver timing. It's a bit trickier because it
+ // uses its own task source and so on.
+ ro.observe(document.documentElement);
+
+ await done;
+
+ ro.disconnect();
+ }
+
+ assert_not_equals(document.activeElement, el, "focus is fixed up");
+ assert_equals(document.activeElement, document.body, "Body is focused");
+ }, selector);
+}
+
+test_focus_fixup("#button1", function(button) {
+ button.disabled = true;
+});
+
+test_focus_fixup("#button2", function(button) {
+ button.hidden = true;
+});
+
+test_focus_fixup("#button3", function(button) {
+ button.remove();
+}, /* expectSync = */ true);
+
+test_focus_fixup("#button4", function(button) {
+ document.querySelector("#fieldset1").disabled = true;
+});
+
+test_focus_fixup("#button5", function(button) {
+ const fieldset = document.querySelector("#fieldset2");
+ fieldset.insertBefore(document.createElement("legend"), fieldset.firstChild);
+});
+
+test_focus_fixup("#div", function(div) {
+ div.removeAttribute("tabindex");
+});
+
+test_focus_fixup("#editable", function(div) {
+ div.contentEditable = false;
+});
+
+test_focus_fixup("#button6", function(button) {
+ button.style.visibility = "hidden";
+});
+</script>
+
+<script>
+function after_focus_fixup_rendering() {
+ return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+}
+
+function create_textarea_change_fixture(t) {
+ const form = document.createElement("form");
+ const textarea = document.createElement("textarea");
+ form.append(textarea);
+ document.body.append(form);
+ t.add_cleanup(() => form.remove());
+ const changes = [];
+ textarea.addEventListener("change", event => changes.push(event));
+ return { form, textarea, changes };
+}
+
+const textarea_change_cases = [
+ ["untouched", async () => {}, 0],
+ ["programmatic value", async textarea => { textarea.value = "script value"; }, 0],
+ ["programmatic defaultValue", async textarea => { textarea.defaultValue = "script default"; }, 0],
+ ["user edit", async textarea => { await test_driver.send_keys(textarea, "x"); }, 1],
+ ["reverted user edit", async textarea => {
+ await test_driver.send_keys(textarea, "x\uE003");
+ assert_equals(textarea.value, "", "The user restored the initial value");
+ }, 0],
+ ["programmatic value followed by user edit", async textarea => {
+ textarea.value = "script value";
+ await test_driver.send_keys(textarea, "x");
+ }, 1],
+ ["user edit with an input handler changing defaultValue", async textarea => {
+ textarea.addEventListener("input", () => { textarea.defaultValue = "script default"; });
+ await test_driver.send_keys(textarea, "x");
+ assert_equals(textarea.value, "x", "Changing defaultValue must not replace the user's value");
+ }, 1],
+ ["user edit followed by form reset", async (textarea, form) => {
+ await test_driver.send_keys(textarea, "x");
+ form.reset();
+ assert_equals(textarea.value, "", "Reset restored the default value");
+ }, 0],
+];
+
+for (const [state, prepare, expected_changes] of textarea_change_cases) {
+ for (const [reason, make_nonfocusable] of [
+ ["hidden", textarea => { textarea.hidden = true; }],
+ ["disabled", textarea => { textarea.disabled = true; }],
+ ["inert", (textarea, form) => { form.inert = true; }],
+ ]) {
+ promise_test(async t => {
+ const { form, textarea, changes } = create_textarea_change_fixture(t);
+ await after_focus_fixup_rendering();
+ textarea.focus();
+ assert_equals(document.activeElement, textarea, "The textarea starts focused");
+ await prepare(textarea, form);
+ assert_equals(changes.length, 0, "No change event fires before focus is fixed up");
+ make_nonfocusable(textarea, form);
+ await after_focus_fixup_rendering();
+ assert_equals(document.activeElement, document.body, "Focus is fixed up after rendering");
+ assert_equals(changes.length, expected_changes, "Only an uncommitted user edit fires change");
+ }, `Textarea ${state}: change events after becoming ${reason}`);
+ }
+}
+
+promise_test(async t => {
+ const { form, textarea, changes } = create_textarea_change_fixture(t);
+ textarea.focus();
+ await test_driver.send_keys(textarea, "x");
+ textarea.remove();
+ assert_equals(changes.length, 0, "Removal does not fire change");
+ form.append(textarea);
+ textarea.focus();
+ textarea.blur();
+ assert_equals(changes.length, 0, "Reinsertion does not retain the removed pending edit");
+}, "Textarea removal discards its pending change before reinsertion");
+
+promise_test(async t => {
+ const { form, textarea } = create_textarea_change_fixture(t);
+ textarea.focus();
+ await test_driver.send_keys(textarea, "x");
+ const clone = textarea.cloneNode(true);
+ form.append(clone);
+ let changes = 0;
+ clone.addEventListener("change", () => { ++changes; });
+ assert_equals(clone.value, textarea.value, "Cloning preserves the current value");
+ clone.focus();
+ clone.blur();
+ assert_equals(changes, 0, "Cloning does not copy pending user interaction state");
+}, "Cloning an edited textarea does not copy its pending change");
+</script>
+
+<script>
+function create_input_change_fixture(t, type = "text") {
+ const container = document.createElement("div");
+ const input = document.createElement("input");
+ input.type = type;
+ container.append(input);
+ document.body.append(container);
+ t.add_cleanup(() => container.remove());
+ const changes = [];
+ input.addEventListener("change", event => changes.push(event));
+ return { container, input, changes };
+}
+
+const input_focus_fixup_reasons = [
+ ["hidden", input => { input.hidden = true; }],
+ ["disabled", input => { input.disabled = true; }],
+ ["inert", (input, container) => { container.inert = true; }],
+];
+
+for (const type of [
+ "text", "search", "tel", "url", "email", "password", "date", "month", "week",
+ "time", "datetime-local", "number", "range", "color", "checkbox", "radio",
+ "file", "submit", "image", "reset", "button",
+]) {
+ for (const [reason, make_nonfocusable] of input_focus_fixup_reasons) {
+ promise_test(async t => {
+ const { container, input, changes } = create_input_change_fixture(t, type);
+ await after_focus_fixup_rendering();
+ input.focus();
+ assert_equals(document.activeElement, input, "The input starts focused");
+ make_nonfocusable(input, container);
+ await after_focus_fixup_rendering();
+ assert_equals(document.activeElement, document.body, "Focus is fixed up after rendering");
+ assert_equals(changes.length, 0, "An untouched input must not fire change");
+ }, `Untouched input type=${type}: no change after becoming ${reason}`);
+ }
+}
+
+for (const [reason, make_nonfocusable] of input_focus_fixup_reasons) {
+ promise_test(async t => {
+ const { container, input, changes } = create_input_change_fixture(t);
+ input.focus();
+ input.value = "script value";
+ make_nonfocusable(input, container);
+ await after_focus_fixup_rendering();
+ assert_equals(document.activeElement, document.body, "Focus is fixed up after rendering");
+ assert_equals(changes.length, 0, "Programmatic changes must not start a pending change");
+ }, `Programmatic text input value: no change after becoming ${reason}`);
+
+ promise_test(async t => {
+ const { container, input, changes } = create_input_change_fixture(t);
+ input.focus();
+ await test_driver.send_keys(input, "x");
+ assert_equals(changes.length, 0, "Typing does not commit before focus loss");
+ make_nonfocusable(input, container);
+ await after_focus_fixup_rendering();
+ assert_equals(document.activeElement, document.body, "Focus is fixed up after rendering");
+ assert_equals(changes.length, 1, "An edited text input fires exactly one change");
+ }, `User-edited text input: one change after becoming ${reason}`);
+
+ promise_test(async t => {
+ const { container, input, changes } = create_input_change_fixture(t, "range");
+ input.min = "0";
+ input.max = "10";
+ input.value = "5";
+ input.focus();
+ const previous_value = Number(input.value);
+ await new test_driver.Actions().keyDown("\uE014").keyUp("\uE014").send();
+ assert_greater_than(Number(input.value), previous_value, "ArrowRight changes the range value");
+ assert_equals(changes.length, 0, "The range edit remains pending before focus loss");
+ make_nonfocusable(input, container);
+ await after_focus_fixup_rendering();
+ assert_equals(document.activeElement, document.body, "Focus is fixed up after rendering");
+ assert_equals(changes.length, 1, "An edited range preserves its focus-loss commit");
+ }, `User-edited range input: one change after becoming ${reason}`);
+}
+
+promise_test(async t => {
+ const { container, input, changes } = create_input_change_fixture(t);
+ input.focus();
+ await test_driver.send_keys(input, "x");
+ input.remove();
+ assert_equals(changes.length, 0, "Removal does not fire change");
+ container.append(input);
+ input.focus();
+ input.blur();
+ assert_equals(changes.length, 0, "Reinsertion does not retain the removed pending edit");
+}, "Text input removal discards its pending change before reinsertion");
+
+promise_test(async t => {
+ const { input, changes } = create_input_change_fixture(t);
+ input.focus();
+ await test_driver.send_keys(input, "x");
+ input.type = "hidden";
+ await after_focus_fixup_rendering();
+ assert_equals(document.activeElement, document.body, "The hidden input loses focus");
+ assert_equals(changes.length, 0, "Change does not apply to hidden inputs");
+ input.type = "text";
+ input.focus();
+ input.blur();
+ assert_equals(changes.length, 0, "The inapplicable state discarded the pending change");
+}, "Changing an edited input to hidden discards its pending change");
+</script>
+
+<script>
+for (const action of ["undo", "redo"]) {
+ promise_test(async t => {
+ const { textarea, changes } = create_textarea_change_fixture(t);
+ textarea.focus();
+ await test_driver.send_keys(textarea, "x");
+ textarea.blur();
+ assert_equals(changes.length, 1, "The original edit was committed");
+
+ textarea.focus();
+ assert_true(document.execCommand("undo"), "Undo is available after refocusing");
+ assert_equals(textarea.value, "", "Undo restores the initial value");
+ let expected_changes = 2;
+ if (action === "redo") {
+ textarea.blur();
+ assert_equals(changes.length, 2, "Undo creates a new pending change");
+ textarea.focus();
+ assert_true(document.execCommand("redo"), "Redo is available after refocusing");
+ assert_equals(textarea.value, "x", "Redo restores the edited value");
+ expected_changes = 3;
+ }
+
+ textarea.hidden = true;
+ await after_focus_fixup_rendering();
+ assert_equals(document.activeElement, document.body, "Focus is fixed up after rendering");
+ assert_equals(changes.length, expected_changes, "The history edit commits exactly once on focus loss");
+ }, `Textarea ${action} after a committed edit creates a new pending change`);
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/inert/dynamic-inert-on-focused-element.html b/Tests/LibWeb/Text/input/wpt-import/inert/dynamic-inert-on-focused-element.html
new file mode 100644
index 00000000000..ac54a24d62a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/inert/dynamic-inert-on-focused-element.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>Dynamic inertness on focused element</title>
+<link rel="author" title="Oriol Brufau" href="mailto:[email protected]">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#focus-fixup-rule">
+<meta name="assert" content="If a focused element becomes inert, it stops being focused.">
+<div id="log"></div>
+
+<div class="test-wrapper" data-name="<input> that gets 'inert' attribute">
+ <input class="becomes-inert check-focus">
+</div>
+
+<div class="test-wrapper" data-name="<input> whose parent gets 'inert' attribute">
+ <div class="becomes-inert">
+ <input class="check-focus">
+ </div>
+</div>
+
+<div class="test-wrapper" data-name="<button> that gets 'inert' attribute">
+ <button class="becomes-inert check-focus">foo</button>
+</div>
+
+<div class="test-wrapper" data-name="<div> that gets 'inert' attribute">
+ <div class="becomes-inert check-focus" tabindex="-1"></div>
+</div>
+
+<div class="test-wrapper" data-name="<div> whose parent gets 'inert' attribute">
+ <div class="becomes-inert">
+ <div class="check-focus" tabindex="-1">bar</div>
+ </div>
+</div>
+
+<div class="test-wrapper" data-name="<div> whose grandparent gets 'inert' attribute">
+ <div class="becomes-inert">
+ <p>
+ <span class="check-focus" tabindex="-1">baz</span>
+ </p>
+ </div>
+</div>
+
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script>
+function nextRepaint() {
+ return new Promise(resolve => {
+ requestAnimationFrame(() => {
+ requestAnimationFrame(resolve);
+ });
+ });
+}
+
+const loaded = new Promise(resolve => {
+ addEventListener("load", resolve, {once: true});
+});
+promise_setup(() => loaded);
+
+for (const testWrapper of document.querySelectorAll(".test-wrapper")) {
+ const becomesInert = testWrapper.querySelector(".becomes-inert");
+ const checkFocus = testWrapper.querySelector(".check-focus");
+ promise_test(async function() {
+ checkFocus.focus();
+ assert_equals(document.activeElement, checkFocus, "The element is focused");
+
+ becomesInert.inert = true;
+
+ // The focus fixup rule should blur the element since it's no longer focusable.
+ // In Chromium this happens after a repaint (https://crbug.com/1275097).
+ await nextRepaint();
+ assert_equals(document.activeElement, document.body, "The element stops being focused");
+ }, testWrapper.dataset.name);
+}
+</script>