Skip to content

Truly independent web browser

Tests/LibWeb: Import WPT tests for sequential focus navigation

Andreas Kling78a1e8d089f5
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/customizable-select/select-tabindex-order.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/customizable-select/select-tabindex-order.txt
new file mode 100644
index 00000000000..eb6d0ba840b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/customizable-select/select-tabindex-order.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Check that tabindex applies to <select>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback-default-tabindex.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback-default-tabindex.txt
new file mode 100644
index 00000000000..3f221bb0fe4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback-default-tabindex.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Default tabindex for a slot node should be 0.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback.txt
new file mode 100644
index 00000000000..e0991ded71c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus should jump to fallback elements when a slot does not have any assigned nodes.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-2levels.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-2levels.txt
new file mode 100644
index 00000000000..0d67306cfed
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-2levels.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus controller should treat each slot as a focus scope.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-delegatesFocus.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-delegatesFocus.txt
new file mode 100644
index 00000000000..0d67306cfed
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-delegatesFocus.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus controller should treat each slot as a focus scope.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-fallback.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-fallback.txt
new file mode 100644
index 00000000000..53a0ba67c13
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-fallback.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus should cover assigned elements of an assigned slot espacially there are fallback contents.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested.txt
new file mode 100644
index 00000000000..0d67306cfed
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus controller should treat each slot as a focus scope.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-fallback.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-fallback.txt
new file mode 100644
index 00000000000..1443b7e4051
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-fallback.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus should cover assigned elements of an assigned slot, as well as elements that are directly assigned to a slot.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-slot.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-slot.txt
new file mode 100644
index 00000000000..1443b7e4051
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-slot.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus should cover assigned elements of an assigned slot, as well as elements that are directly assigned to a slot.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-with-tabindex.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-with-tabindex.txt
new file mode 100644
index 00000000000..d573850fd0d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-with-tabindex.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Slots tabindex should be considred in focus navigation.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slots.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slots.txt
new file mode 100644
index 00000000000..1443b7e4051
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-slots.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus should cover assigned elements of an assigned slot, as well as elements that are directly assigned to a slot.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-web-component-radio.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-web-component-radio.txt
new file mode 100644
index 00000000000..951bacaa9b7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation-web-component-radio.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus for web component input type elements should be bound by <form> inside shadow DOM
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation.txt
new file mode 100644
index 00000000000..bcdfdcb8701
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-navigation.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Focus controller should treat slots as a focus scope.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-negative-tabindex-at-end-of-shadow-tree.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-negative-tabindex-at-end-of-shadow-tree.txt
new file mode 100644
index 00000000000..62f9fd56434
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-negative-tabindex-at-end-of-shadow-tree.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Fail
+Fail Tab moves focus past the shadow host, not back to the start of the shadow tree
+Fail Shift+Tab moves focus before the shadow host, not back to the end of the shadow tree
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-reverse-unassigned-slot.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-reverse-unassigned-slot.txt
new file mode 100644
index 00000000000..5dc4542cf41
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus-navigation/focus-reverse-unassigned-slot.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Verifies that focusing backwards from a button inside a slot which has no assigned nodes goes to the previous focusable element.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative-delegatesFocus.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative-delegatesFocus.txt
new file mode 100644
index 00000000000..47800a7e661
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative-delegatesFocus.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=-1 is and delegatesFocus = true
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative.txt
new file mode 100644
index 00000000000..ef0c4f9dccd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all elements in shadow tree has negative tabindex
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-slot-one.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-slot-one.txt
new file mode 100644
index 00000000000..190a5405f0d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-slot-one.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0, except for one slot that has tabindex=1
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-delegatesFocus.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-delegatesFocus.txt
new file mode 100644
index 00000000000..5bccccf9636
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-delegatesFocus.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when tabindex varies and delegatesFocus = true
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-2.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-2.txt
new file mode 100644
index 00000000000..831037d28c5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-2.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order with different tabindex on host
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-3.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-3.txt
new file mode 100644
index 00000000000..831037d28c5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-3.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order with different tabindex on host
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex.txt
new file mode 100644
index 00000000000..2b50cfae68a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order with various tabindex values
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-delegatesFocus.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-delegatesFocus.txt
new file mode 100644
index 00000000000..c4f505fb8ff
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-delegatesFocus.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0 is and delegatesFocus = true
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-negative.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-negative.txt
new file mode 100644
index 00000000000..b071d0d9dac
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-negative.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0 except for host, which has tabindex=-1
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set-scrollable.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set-scrollable.txt
new file mode 100644
index 00000000000..c0ec3b6a38f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set-scrollable.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0 except scrollable host (tabindex not set)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set.txt
new file mode 100644
index 00000000000..0ab2d445d6c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0 except host (tabindex not set)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-one.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-one.txt
new file mode 100644
index 00000000000..f36f213d1f6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-one.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0 except for host, which has tabindex=1
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-scrollable.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-scrollable.txt
new file mode 100644
index 00000000000..87f39c4ffe6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-scrollable.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0 and host is scrollable
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero.txt
new file mode 100644
index 00000000000..6c8fb1c2f46
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Fail
+Fail Order when all tabindex=0 is and delegatesFocus = false
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/customizable-select/select-tabindex-order.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/customizable-select/select-tabindex-order.html
new file mode 100644
index 00000000000..1cbadbeb1a1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/forms/the-select-element/customizable-select/select-tabindex-order.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<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>
+
+<input id="input1" tabindex="1">
+<select id="select" tabindex="3">
+ <option>one</option>
+ <option>two</option>
+ <option>three</option>
+</select>
+<input id="input3" tabindex="2">
+
+<style>
+ select, ::picker(select) {
+ appearance: base-select;
+ }
+</style>
+
+<script>
+promise_test(async () => {
+ const TAB_KEY = "\uE004";
+
+ const input1 = document.getElementById("input1");
+ const input3 = document.getElementById("input3");
+
+ input1.focus();
+ assert_equals(document.activeElement.id, "input1", "input1 should be active");
+
+ await test_driver.send_keys(input1, TAB_KEY);
+ assert_equals(document.activeElement.id, "input3", "input3 should be active");
+
+ await test_driver.send_keys(input3, TAB_KEY);
+ assert_equals(document.activeElement.id, "select", "select should be active");
+}, "Check that tabindex applies to <select>");
+</script>
+
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback-default-tabindex.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback-default-tabindex.html
new file mode 100644
index 00000000000..bfd00263755
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback-default-tabindex.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<p>Tests for moving focus by pressing tab key across shadow boundaries.<br>
+To manually test, press tab key six times then shift+tab seven times.<br>
+It should traverse focusable elements in the increasing numerical order and then in the reverse order.</p>
+
+<div id='host'>
+ <div slot='slot5' id='i2' tabindex=4>2. Assigned to slot5 whose tabindex is 2.</div>
+ <template data-mode='open'>
+ <slot name='slot1'>
+ x. The default tabindex for a slot node is set to 0.
+ <div id='i5' tabindex=0>5. The parent slot node's tabindex is 0. Second.</div>
+ <div id='i4' tabindex=2>4. The parent slot node's tabindex is 0. First.</div>
+ </slot>
+
+ <slot name='slot2' id='x1' tabindex=3>
+ x. The tabindex is 3. The slot node should be ignored.
+ <div id='i3' tabindex=10>3. The parent slot node's tabindex is 3. The slot node's tabindex matters. This element's tabindex comes after.</div>
+ </slot>
+
+ <slot name='slot3' id='x2' tabindex=0>
+ x. The tabindex is 0. The slot node should be ignored. If there is another slot node in same tabindex, the younger child comes first.
+ <div id='i6' tabindex=1>6. The parent slot node's tabindex is 0. First.</div>
+ <div id='i7' tabindex=1>7. The parent slot node's tabindex is 0. Second.</div>
+ </slot>
+
+ <slot name='slot4' id='x3' tabindex=1>
+ x. The tabindex is 1. The slot node should be ignored.
+ <div id='i1' tabindex=5>1. The slot node tabindex is 1.</div>
+ </slot>
+
+ <slot name='slot5' id='x5' tabindex=2>
+ x. The tabindex is 2. The slot node should be ignored. The host child is assigned to this slot node.
+ <div id='-' tabindex=1>-. The host child is assigned to the parent slot node. This text shouldn't appear.</div>
+ </slot>
+
+ <slot name='slot6' id='x6' tabindex=5>
+ x. The tabindex is 5. The slot node should be ignored.
+ <div id='x6' tabindex=-1>x. tabindex is -1. Should be skipped.</div>
+ </slot>
+
+ <slot name='slot7' id='x7' tabindex=-1>
+ x. tabindex is -1. Should be skipped.
+ <div id='x8' tabindex=1>x. The parent slot node is skipped.</div>
+ </slot>
+ </template>
+</div>
+<script>
+
+promise_test(async () => {
+ convertTemplatesToShadowRootsWithin(host);
+
+ let elements = [
+ 'host/i1',
+ 'i2',
+ 'host/i3',
+ 'host/i4',
+ 'host/i5',
+ 'host/i6',
+ 'host/i7'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Default tabindex for a slot node should be 0.');
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback.html
new file mode 100644
index 00000000000..9df9addff89
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-fallback.html
@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id='log'></div>
+<p>
+ document tree: [i0 -> [x-foo]]<br>
+ x-foo's shadow tree: [j1 -> j2 -> [s1]]<br>
+ <b>slot #s1: [k1 -> [x-bar] -> k0 -> [s2] -> [s3]]</b><br>
+ x-bar's shadow tree: [m1 -> m2]<br>
+ slot #s2: [i1 -> i2]<br>
+ <b>slot #s3: [l1]<b><br><br>
+ <b>v1 ideal nav forward: [i0 -> j1 -> j2 -> k1 -> x-bar -> m1 -> m2 -> k0 -> i1 -> i2 -> l1]</b><br>
+</p>
+
+<input id='i0' tabindex=0 value='i0'>
+<div id='x-foo'>
+ <input id='i2' slot='s2' tabindex=2 value='i2'>
+ <input id='i1' slot='s2' tabindex=1 value='i1'>
+ <template data-mode='open'>
+ <input id='j1' tabindex=1 value='j1'>
+ <slot id='s1' name='s1'> <!-- This slot does not have any assigned elements -->
+ <input id='k0' tabindex=0 value='k0'>
+ <input id='k1' tabindex=1 value='k1'>
+ <slot id='s2' name='s2'>
+ <input id='should-be-ignored'>
+ </slot>
+ <slot id='s3' name='s3'> <!-- This slot does not have any assigned elements -->
+ <input id='l1' value='l1'>
+ </slot>
+ <div id='x-bar' tabindex=2>
+ <template data-mode='open'>
+ <input id='m2' value='m2' tabindex=2>
+ <input id='m1' value='m1' tabindex=1>
+ </template>
+ </div>
+ </slot>
+ <input id='j2' tabindex=2 value='j2'>
+ </template>
+</div>
+
+<script>
+'use strict';
+
+promise_test(async () => {
+ let xfoo = document.getElementById('x-foo');
+ convertTemplatesToShadowRootsWithin(xfoo);
+
+ let elements = [
+ 'i0',
+ 'x-foo/j1',
+ 'x-foo/j2',
+ 'x-foo/k1',
+ 'x-foo/x-bar',
+ 'x-foo/x-bar/m1',
+ 'x-foo/x-bar/m2',
+ 'x-foo/k0',
+ 'i1',
+ 'i2',
+ 'x-foo/l1'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus should jump to fallback elements when a slot does not have any assigned nodes.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-2levels.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-2levels.html
new file mode 100644
index 00000000000..dcf01b8cb78
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-2levels.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<meta name="timeout" content="long">
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+
+<input id='i0'>
+<div id='outer'>
+ <template data-mode='open'>
+ <input id='outer-before'>
+ <slot></slot>
+ <input id='outer-after'>
+ </template>
+ <div id='dummy1'></div>
+ <div id='nested1'>
+ <template data-mode='open'>
+ <input id='inner-before'>
+ <div id='inner-div' tabindex='0'><slot></slot></div>
+ <input id='inner-after'>
+ </template>
+ <div id='dummy2'></div>
+ <div id='nested2'>
+ <template data-mode='open'>
+ <input id='innermost-before'>
+ <slot></slot>
+ <input id='innermost-after'>
+ </template>
+ <input id='innermost1'>
+ <input id='innermost2'>
+ </div>
+ <span>button</span>
+ </div>
+</div>
+<input id='i1'>
+
+<script>
+promise_test(async () => {
+ var outer = document.querySelector('#outer');
+ convertTemplatesToShadowRootsWithin(outer);
+
+ var elements = [
+ 'i0',
+ 'outer/outer-before',
+ 'nested1/inner-before',
+ 'nested1/inner-div',
+ 'nested2/innermost-before',
+ 'innermost1',
+ 'innermost2',
+ 'nested2/innermost-after',
+ 'nested1/inner-after',
+ 'outer/outer-after',
+ 'i1'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus controller should treat each slot as a focus scope.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-delegatesFocus.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-delegatesFocus.html
new file mode 100644
index 00000000000..5c9e24bcc40
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-delegatesFocus.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+<!--
+This test case is based on the crbug.com/618587 reproduction case:
+http://jsbin.com/bonudiwagu/1/edit?html,output
+-->
+<input id='i0'>
+<div id='x-foo'>
+ <template data-mode='open' data-delegatesFocus>
+ <input id='inner-before'>
+ <slot></slot>
+ <input id='inner-after'>
+ </template>
+ <div id='nested'>
+ <template data-mode='open' data-delegatesFocus>
+ <input id='nested-x'>
+ <slot></slot>
+ <input id='nested-y'>
+ </template>
+ <input id='light'>
+ </div>
+</div>
+<input id='i1'>
+
+<script>
+promise_test(async () => {
+ var xFoo = document.querySelector('#x-foo');
+ convertTemplatesToShadowRootsWithin(xFoo);
+
+ var elements = [
+ 'i0',
+ 'x-foo/inner-before',
+ 'nested/nested-x',
+ 'light',
+ 'nested/nested-y',
+ 'x-foo/inner-after',
+ 'i1'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus controller should treat each slot as a focus scope.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-fallback.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-fallback.html
new file mode 100644
index 00000000000..ff97a212c64
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested-fallback.html
@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<p>Tests for moving focus by pressing tab key across shadow boundaries.<br>
+To manually test, press tab key six times then shift+tab six times.<br>
+It should traverse focusable elements in the increasing numerical order and then in the reverse order.</p>
+<div id='host'>
+ <template data-mode='open'>
+ <slot name='slot1'>Slot shouldn't be focused.
+ <slot name='slot2'>Slot shouldn't be focused.
+ <div id='non1' tabindex=0>This text shouldn't appear.</div>
+ </slot>
+ <slot name='slot3'>Slot shouldn't be focused.
+ <div id='second' tabindex=0>2. No host child is assigned to slot3.</div>
+ </slot>
+ </slot>
+
+ <div id='third' tabindex=0>3. Inner Shadow Host.
+ <template data-mode='open'>
+ <slot name='slot4'>Slot shouldn't be focused.
+ <slot name='slot5'>Slot shouldn't be focused.
+ <div id='non2' tabindex=0>This text shouldn't appear.</div>
+ </slot>
+ </slot>
+ </template>
+ <div id='fourth' slot='slot4' tabindex=0>4. Assigned to slot4.</div>
+ <div id='non3' slot='slot5' tabindex=0>
+ This text shouldn't appear. slot5 is in the fallback content of slot4 which has assigned nodes.</div>
+ </div>
+
+ <div id='fifth' tabindex=0>5. Inner Shadow Host.
+ <template data-mode='open'>
+ <slot name='slot6'>Slot shouldn't be focused.
+ <div id='non4' tabindex=0>This text shouldn't appear.</div>
+ </slot>
+ </template>
+ <slot name='slot7' slot='slot6'>Slot shouldn't be focused. Assigned to slot6.
+ <div id='non5' tabindex=0>This text shouldn't appear.</div>
+ </slot>
+ </div>
+ </template>
+ <div id='first' slot='slot2' tabindex=0>1. Assigned to slot2.</div>
+ <div id='sixth' slot='slot7' tabindex=0>6. Assigned to slot7 which is assigned to slot6.</div>
+</div>
+<script>
+'use strict';
+
+promise_test(async () => {
+ let host = document.getElementById('host');
+ convertTemplatesToShadowRootsWithin(host);
+
+ let elements = [
+ 'first',
+ 'host/second',
+ 'host/third',
+ 'host/fourth',
+ 'host/fifth',
+ 'sixth'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus should cover assigned elements of an assigned slot espacially there are fallback contents.');
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested.html
new file mode 100644
index 00000000000..81d5a411f6f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-nested.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+<!--
+This test case is based on the crbug.com/618587 reproduction case:
+http://jsbin.com/bonudiwagu/1/edit?html,output
+-->
+<input id='i0'>
+<div id='x-foo'>
+ <template data-mode='open'>
+ <input id='inner-before'>
+ <slot></slot>
+ <input id='inner-after'>
+ </template>
+ <div id='nested'>
+ <template data-mode='open'>
+ <input id='nested-x'>
+ <slot></slot>
+ <input id='nested-y'>
+ </template>
+ <input id='light'>
+ </div>
+</div>
+<input id='i1'>
+
+<script>
+promise_test(async () => {
+ var xFoo = document.querySelector('#x-foo');
+ convertTemplatesToShadowRootsWithin(xFoo);
+
+ var elements = [
+ 'i0',
+ 'x-foo/inner-before',
+ 'nested/nested-x',
+ 'light',
+ 'nested/nested-y',
+ 'x-foo/inner-after',
+ 'i1'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus controller should treat each slot as a focus scope.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-fallback.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-fallback.html
new file mode 100644
index 00000000000..bf8b6e70c93
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-fallback.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+
+<input id="i0" tabindex=0 value="i0">
+<div id="fallback">
+ <template data-mode="open">
+ <slot id="s0" name="s0">
+ <div id="x-foo">
+ <template data-mode="open">
+ <input id="a2" tabindex=3 value="a2">
+ <slot id="s1" name="s1" tabindex=2>
+ <slot id="s2" name="s2">
+ <input id="a1" slot="s2" value="a1">
+ </slot>
+ </slot>
+ <input id="a0" tabindex=1 value="a0">
+ </template>
+ </div>
+ </slot>
+ </template>
+</div>
+
+<script>
+'use strict';
+
+promise_test(async () => {
+ let fallback = document.getElementById('fallback');
+ convertTemplatesToShadowRootsWithin(fallback);
+
+ let elements = [
+ 'i0',
+ 'fallback/x-foo/a0',
+ 'fallback/x-foo/a1',
+ 'fallback/x-foo/a2'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus should cover assigned elements of an assigned slot, as well as elements that are directly assigned to a slot.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-slot.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-slot.html
new file mode 100644
index 00000000000..6d2458c45fc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-shadow-in-slot.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+
+<input id="i0" tabindex=0 value="i0">
+<div id="assigned">
+ <template data-mode="open">
+ <slot id="s0" name="s0">
+ <div id="x-foo">
+ <input id="a1" slot="s2" value="a1">
+ <template data-mode="open">
+ <input id="a2" tabindex=3 value="a2">
+ <slot id="s1" name="s1" tabindex=2>
+ <slot id="s2" name="s2"></slot>
+ </slot>
+ <input id="a0" tabindex=1 value="a0">
+ </template>
+ </div>
+ </slot>
+ </template>
+</div>
+
+<script>
+'use strict';
+
+promise_test(async () => {
+ let assigned = document.getElementById('assigned');
+ convertTemplatesToShadowRootsWithin(assigned);
+
+ let elements = [
+ 'i0',
+ 'assigned/x-foo/a0',
+ 'assigned/a1',
+ 'assigned/x-foo/a2'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus should cover assigned elements of an assigned slot, as well as elements that are directly assigned to a slot.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-with-tabindex.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-with-tabindex.html
new file mode 100644
index 00000000000..a3f73995742
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slot-with-tabindex.html
@@ -0,0 +1,63 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id='log'></div>
+<p>
+ document tree: [i0 -> [x-foo]]<br>
+ x-foo's shadow tree: [j1 -> [s1] -> [s2] -> j2 ->[x-bar]]<br>
+ x-bar's shadow tree: [[s3] -> k1]<br>
+ slot #s1: [i1 -> i2]<br>
+ slot #s2: [i3]<br>
+ slot #s3: [l1 -> l2]<br><br>
+ <b>v1 ideal nav forward: [i0 -> j1 -> i1 -> i2 -> i3 -> j2 -> x-bar -> l1 -> l2 -> k1]</b><br>
+</p>
+
+<input id='i0' tabindex=0 value='i0'>
+<div id='x-foo'>
+ <input id='i2' slot='s1' tabindex=2 value='i2'>
+ <input id='i1' slot='s1' tabindex=1 value='i1'>
+ <input id='i3' slot='s2' tabindex=3 value='i3'>
+ <template data-mode='open'>
+ <div id='x-bar' tabindex=5>
+ <input id='l2' slot='s3' tabindex=2 value='l2'>
+ <input id='l1' slot='s3' tabindex=1 value='l1'>
+ <template data-mode='open'>
+ <slot id='s3' name='s3' tabindex=1></slot>
+ <input id='k1' tabindex=2 value='k1'>
+ </template>
+ </div>
+ <input id='j1' tabindex=1 value='j1'>
+ <slot id='s2' name='s2' tabindex=3></slot>
+ <slot id='s1' name='s1' tabindex=2></slot>
+ <input id='j2' tabindex=4 value='j2'>
+ </template>
+</div>
+
+<script>
+'use strict';
+
+promise_test(async () => {
+ let xfoo = document.getElementById('x-foo');
+ convertTemplatesToShadowRootsWithin(xfoo);
+
+ let elements = [
+ 'i0',
+ 'x-foo/j1',
+ 'i1',
+ 'i2',
+ 'i3',
+ 'x-foo/j2',
+ 'x-foo/x-bar',
+ 'x-foo/l1',
+ 'x-foo/l2',
+ 'x-foo/x-bar/k1',
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Slots tabindex should be considred in focus navigation.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slots.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slots.html
new file mode 100644
index 00000000000..61b6587f3a0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-slots.html
@@ -0,0 +1,65 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+<p>
+ document tree: [i0 -> [x-foo]]<br>
+ x-foo's shadow tree: [j1 -> [s1] -> [s2] -> j2 ->[x-bar]]<br>
+ x-bar's shadow tree: [k1 -> [s3]]<br>
+ slot #s1: [i1 -> i2]<br>
+ slot #s2: [i3]<br>
+ slot #s3: [[s4]]<br>
+ slot #s4: [i4 -> i5]<br><br>
+ <b>v1 ideal nav forward: [i0 -> j1 -> i1 -> i2 -> i3 -> j2 -> x-bar -> k1 -> i4 -> i5]</b><br>
+</p>
+
+<input id="i0" tabindex=0 value="i0">
+<div id="x-foo">
+ <input id="i2" slot="s1" tabindex=2 value="i2">
+ <input id="i1" slot="s1" tabindex=1 value="i1">
+ <input id="i3" slot="s2" tabindex=3 value="i3">
+ <input id="i4" slot="s4" tabindex=4 value="i4">
+ <input id="i5" slot="s4" tabindex=5 value="i5">
+ <template data-mode="open">
+ <div id="x-bar" tabindex=5>
+ <slot id="s4" name="s4" slot="s3"></slot>
+ <template data-mode="open">
+ <slot id="s3" name="s3" tabindex=2></slot>
+ <input id="k1" tabindex=1 value="k1">
+ </template>
+ </div>
+ <input id="j1" tabindex=1 value="j1">
+ <slot id="s2" name="s2" tabindex=3></slot>
+ <slot id="s1" name="s1" tabindex=2></slot>
+ <input id="j2" tabindex=4 value="j2">
+ </template>
+</div>
+
+<script>
+'use strict';
+
+promise_test(async () => {
+ let xfoo = document.getElementById('x-foo');
+ convertTemplatesToShadowRootsWithin(xfoo);
+
+ let elements = [
+ 'i0',
+ 'x-foo/j1',
+ 'i1',
+ 'i2',
+ 'i3',
+ 'x-foo/j2',
+ 'x-foo/x-bar',
+ 'x-foo/x-bar/k1',
+ 'i4',
+ 'i5'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus should cover assigned elements of an assigned slot, as well as elements that are directly assigned to a slot.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-web-component-radio.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-web-component-radio.html
new file mode 100644
index 00000000000..11976de0a05
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation-web-component-radio.html
@@ -0,0 +1,70 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+
+<template id="custom-radio">
+ <span aria-role="radio" tabindex="0">&#x1F518;</span>
+ <slot></slot>
+</template>
+
+<div tabindex="0" id="start">OUT</div>
+<form>
+ <custom-radio name="radio" id="A">A</x-radio>
+ <custom-radio name="radio" id="B">B</x-radio>
+</form>
+<form>
+ <custom-radio name="radio" id="C">C</x-radio>
+ <custom-radio name="radio" id="D">D</x-radio>
+</form>
+<div tabindex="0" id="end">OUT</div>
+
+<script>
+const template = document.querySelector('#custom-radio');
+
+class CustomRadio extends HTMLElement {
+ constructor() {
+ super();
+ this.attachShadow({ mode: 'open', delegatesFocus: true }).appendChild(
+ template.content.cloneNode(true),
+ );
+ }
+}
+customElements.define('custom-radio', CustomRadio);
+
+async function assert_web_component_focus_navigation_forward(elements) {
+ let start = document.getElementById(elements[0]);
+ start.focus();
+ for (let i = 1; i < elements.length; i++) {
+ await navigateFocusForward();
+ assert_equals(document.activeElement.id, elements[i]);
+ }
+}
+
+async function assert_web_component_focus_navigation_backward(elements) {
+ let end = document.getElementById(elements[elements.length - 1]);
+ end.focus();
+ for (let i = elements.length - 2; i >= 0; i--) {
+ await navigateFocusBackward();
+ assert_equals(document.activeElement.id, elements[i]);
+ }
+}
+
+promise_test(async () => {
+ let elements = [
+ 'start',
+ 'A',
+ 'B',
+ 'C',
+ 'D',
+ 'end'
+ ];
+
+ await assert_web_component_focus_navigation_forward(elements);
+ await assert_web_component_focus_navigation_backward(elements);
+}, 'Focus for web component input type elements should be bound by <form> inside shadow DOM');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation.html
new file mode 100644
index 00000000000..0499e951eb5
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-navigation.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<meta name="timeout" content="long">
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+<p>
+ document tree: [i0 -> [x-foo]]<br>
+ x-foo's shadow tree: [j5 -> [x-bar] -> j6]<br>
+ x-bar's shadow tree: [k1 -> k0 -> [s2]]<br>
+ slot #s2: [j1 -> j2 -> j3 -> j4 -> [s1] -> j0]<br><br>
+ slot #s1: [i1 -> i2]<br>
+ <b>v1 ideal nav forward: [i0 -> j5 -> xbar -> k1 -> k0 -> j1 -> j2 -> j3 -> j4 -> i1 -> i2 -> j0 -> j6]</b><br>
+</p>
+
+<input id="i0" tabindex=0 value="i0">
+<div id="x-foo">
+ <input id="i2" slot="s1" tabindex=2 value="i2">
+ <input id="i1" slot="s1" tabindex=1 value="i1">
+ <template data-mode="open">
+ <div id="x-bar" tabindex=4>
+ <input id="j1" slot="s2" tabindex=1 value="j1">
+ <slot id="s1" name="s1" slot="s2"></slot>
+ <input id="j0" slot="s2" tabindex=0 value="j0">
+ <input id="j3" slot="s2" tabindex=2 value="j3">
+ <div id="j4" slot="s2" tabindex=3>
+ <input id="j2" tabindex=1 value="j2">
+ </div>
+ <template data-mode="open">
+ <input id="k0" tabindex=0 value="k0">
+ <slot id="s2" name="s2"></slot>
+ <input id="k1" tabindex=1 value="k1">
+ </template>
+ </div>
+ <input id="j6" tabindex=4 value="j6">
+ <input id="j5" tabindex=3 value="j5">
+ </template>
+</div>
+
+<script>
+'use strict';
+
+promise_test(async () => {
+ let xfoo = document.getElementById('x-foo');
+ convertTemplatesToShadowRootsWithin(xfoo);
+ let sr = xfoo.shadowRoot;
+ let xbar = sr.querySelector('div');
+ convertTemplatesToShadowRootsWithin(xbar);
+
+ let elements = [
+ 'i0',
+ 'x-foo/j5',
+ 'x-foo/x-bar',
+ 'x-foo/x-bar/k1',
+ 'x-foo/x-bar/k0',
+ 'x-foo/j1',
+ 'x-foo/j2',
+ 'x-foo/j3',
+ 'x-foo/j4',
+ 'i1',
+ 'i2',
+ 'x-foo/j0',
+ 'x-foo/j6'
+ ];
+
+ await assert_focus_navigation_bidirectional(elements);
+}, 'Focus controller should treat slots as a focus scope.');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-negative-tabindex-at-end-of-shadow-tree.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-negative-tabindex-at-end-of-shadow-tree.html
new file mode 100644
index 00000000000..8ead031fe98
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-negative-tabindex-at-end-of-shadow-tree.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Tab/Shift+Tab from a tabindex=-1 element at either end of a shadow tree</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-navigation-search-algorithm">
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/focus-utils.js"></script>
+
+<button id="before">before</button>
+<div id="host">
+ <template shadowrootmode="open">
+ <span id="first" tabindex="-1">first</span>
+ <button id="inner">inner</button>
+ <span id="last" tabindex="-1">last</span>
+ </template>
+</div>
+<button id="after">after</button>
+
+<script>
+promise_test(async () => {
+ host.shadowRoot.getElementById("last").focus();
+ await navigateFocusForward();
+ assert_equals(document.activeElement, after);
+}, "Tab moves focus past the shadow host, not back to the start of the shadow tree");
+
+promise_test(async () => {
+ host.shadowRoot.getElementById("first").focus();
+ await navigateFocusBackward();
+ assert_equals(document.activeElement, before);
+}, "Shift+Tab moves focus before the shadow host, not back to the end of the shadow tree");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-reverse-unassigned-slot.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-reverse-unassigned-slot.html
new file mode 100644
index 00000000000..e12f9591b0a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/focus-reverse-unassigned-slot.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<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>
+<script src="../../resources/testdriver-actions.js"></script>
+<script src="resources/shadow-dom.js"></script>
+<script src="resources/focus-utils.js"></script>
+<div id="log"></div>
+
+<input id=i0 value=i0>
+<div id=outer>
+ <template data-mode=open>
+ <div id=inner>
+ <template data-mode=open>
+ <div>
+ <slot name=inside></slot>
+ </div>
+ </template>
+ <slot name=inside slot=inside>
+ <input id=i1 value=i1>
+ </slot>
+ </div>
+ </template>
+</div>
+<input id=i2 value=i2>
+
+<script>
+promise_test(async () => {
+ convertTemplatesToShadowRootsWithin(document.getElementById('outer'));
+
+ const elements = [
+ 'i2',
+ 'outer/i1',
+ 'i0'
+ ];
+ await assert_focus_navigation_backward(elements);
+}, `Verifies that focusing backwards from a button inside a slot which has no assigned nodes goes to the previous focusable element.`);
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/resources/focus-utils.js b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/resources/focus-utils.js
new file mode 100644
index 00000000000..6a52524aa6d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/resources/focus-utils.js
@@ -0,0 +1,239 @@
+'use strict';
+
+// --- Generic focus testing primitives ---
+
+function waitForRender() {
+ return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+}
+
+// https://w3c.github.io/webdriver/#keyboard-actions
+const kArrowLeft = '\uE012';
+const kArrowUp = '\uE013';
+const kArrowRight = '\uE014';
+const kArrowDown = '\uE015';
+const kHome = '\uE011';
+const kEnd = '\uE010';
+const kTab = '\uE004';
+const kShift = '\uE008';
+
+// Focus an element and wait for the focus state to propagate via double-rAF.
+// Use this instead of bare element.focus() to prevent flaky failures on
+// slower CI environments where focus may not be fully established before
+// subsequent key dispatches.
+async function focusAndWait(element) {
+ element.focus();
+ await waitForRender();
+}
+
+// Focus target and send a key press from it. Waits one animation frame after
+// focusing to ensure focus is established before dispatching the key.
+async function focusAndKeyPress(target, key) {
+ target.focus();
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ return test_driver.send_keys(target, key);
+}
+
+// Send a single key press using the Actions API. Does not change focus before
+// dispatching, unlike test_driver.send_keys().
+function sendKey(key) {
+ return new test_driver.Actions().keyDown(key).keyUp(key).send();
+}
+
+// Send Tab using the Actions API without targeting a specific element.
+// Unlike navigateFocusForward, this does NOT call focus() on any element
+// first, so it will not trigger blur events that could interfere with
+// focusgroup exit behaviour on key-conflict elements.
+async function sendTabForward() {
+ await waitForRender();
+ await sendKey(kTab);
+ await waitForRender();
+}
+
+async function navigateFocusForward() {
+ await waitForRender();
+ // Use test_driver.send_keys for reliable synchronization rather than the
+ // lower-level Actions API which can drop key events on slower CI
+ // environments. We target document.body because body.focus() is a no-op,
+ // which preserves the current focus state. Targeting activeElement would
+ // break shadow hosts with delegatesFocus (re-delegating on focus()).
+ await test_driver.send_keys(document.body, kTab);
+ await waitForRender();
+}
+
+// Shift+Tab via the Actions API. Does not call focus() on any element, so
+// it will not trigger blur events that could interfere with focusgroup exit
+// behaviour on key-conflict elements.
+async function navigateFocusBackward() {
+ await waitForRender();
+ await new test_driver.Actions()
+ .keyDown(kShift)
+ .keyDown(kTab)
+ .keyUp(kTab)
+ .keyUp(kShift)
+ .send();
+ await waitForRender();
+}
+
+// If shadow root is open, can find element using element path
+// If shadow root is open, can find the shadowRoot from the element
+
+function innermostActiveElement(element) {
+ element = element || document.activeElement;
+ if (isIFrameElement(element)) {
+ if (element.contentDocument.activeElement)
+ return innermostActiveElement(element.contentDocument.activeElement);
+ return element;
+ }
+ if (isShadowHost(element)) {
+ let shadowRoot = element.shadowRoot;
+ if (shadowRoot) {
+ if (shadowRoot.activeElement)
+ return innermostActiveElement(shadowRoot.activeElement);
+ }
+ }
+ return element;
+}
+
+function isInnermostActiveElement(path) {
+ const element = getNodeInComposedTree(path);
+ if (!element)
+ return false;
+ return element === innermostActiveElement();
+}
+
+async function shouldNavigateFocus(fromElement, direction) {
+ if (!fromElement)
+ return false;
+
+ fromElement.focus();
+ if (fromElement !== innermostActiveElement())
+ return false;
+
+ if (direction == 'forward')
+ await navigateFocusForward();
+ else
+ await navigateFocusBackward();
+
+ return true;
+}
+
+async function assert_focus_navigation_element(fromPath, toPath, direction) {
+ const fromElement = getNodeInComposedTree(fromPath);
+ const result = await shouldNavigateFocus(fromElement, direction);
+ assert_true(result, 'Failed to focus ' + fromPath);
+
+ const message =
+ `Focus should move ${direction} from ${fromPath} to ${toPath}`;
+ const toElement = getNodeInComposedTree(toPath);
+ assert_equals(innermostActiveElement(), toElement, message);
+}
+
+async function assert_focus_navigation_elements(elements, direction) {
+ assert_true(
+ elements.length >= 2,
+ 'length of elements should be greater than or equal to 2.');
+ for (var i = 0; i + 1 < elements.length; ++i)
+ await assert_focus_navigation_element(elements[i], elements[i + 1], direction);
+
+}
+
+async function assert_focus_navigation_forward(elements) {
+ return assert_focus_navigation_elements(elements, 'forward');
+}
+
+async function assert_focus_navigation_backward(elements) {
+ return assert_focus_navigation_elements(elements, 'backward');
+}
+
+async function assert_focus_navigation_bidirectional(elements) {
+ await assert_focus_navigation_forward(elements);
+ elements.reverse();
+ await assert_focus_navigation_backward(elements);
+}
+
+
+// If shadow root is closed, need to pass shadowRoot and element to find
+// innermost active element
+
+function isShadowHostOfRoot(shadowRoot, node) {
+ return shadowRoot && shadowRoot.host.isEqualNode(node);
+}
+
+function innermostActiveElementWithShadowRoot(shadowRoot, element) {
+ element = element || document.activeElement;
+ if (isIFrameElement(element)) {
+ if (element.contentDocument.activeElement)
+ return innermostActiveElementWithShadowRoot(shadowRoot, element.contentDocument.activeElement);
+ return element;
+ }
+ if (isShadowHostOfRoot(shadowRoot, element)) {
+ if (shadowRoot.activeElement)
+ return innermostActiveElementWithShadowRoot(shadowRoot, shadowRoot.activeElement);
+ }
+ return element;
+}
+
+async function shouldNavigateFocusWithShadowRoot(from, direction) {
+ const [fromElement, shadowRoot] = from;
+ if (!fromElement)
+ return false;
+
+ fromElement.focus();
+ if (fromElement !== innermostActiveElementWithShadowRoot(shadowRoot))
+ return false;
+
+ if (direction == 'forward')
+ await navigateFocusForward();
+ else
+ await navigateFocusBackward();
+
+ return true;
+}
+
+async function assert_focus_navigation_element_with_shadow_root(from, to, direction) {
+ const result = await shouldNavigateFocusWithShadowRoot(from, direction);
+ const [fromElement] = from;
+ const [toElement, toShadowRoot] = to;
+ assert_true(result, 'Failed to focus ' + fromElement.id);
+ const message =
+ `Focus should move ${direction} from ${fromElement.id} to ${toElement.id}`;
+ assert_equals(innermostActiveElementWithShadowRoot(toShadowRoot), toElement, message);
+}
+
+async function assert_focus_navigation_elements_with_shadow_root(elements, direction) {
+ assert_true(
+ elements.length >= 2,
+ 'length of elements should be greater than or equal to 2.');
+ for (var i = 0; i + 1 < elements.length; ++i)
+ await assert_focus_navigation_element_with_shadow_root(elements[i], elements[i + 1], direction);
+}
+
+async function assert_focus_navigation_forward_with_shadow_root(elements) {
+ return assert_focus_navigation_elements_with_shadow_root(elements, 'forward');
+}
+
+async function assert_focus_navigation_backward_with_shadow_root(elements) {
+ return assert_focus_navigation_elements_with_shadow_root(elements, 'backward');
+}
+
+async function assert_focus_navigation_bidirectional_with_shadow_root(elements) {
+ await assert_focus_navigation_forward_with_shadow_root(elements);
+ elements.reverse();
+ await assert_focus_navigation_backward_with_shadow_root(elements);
+}
+
+// This Promise will run each test case that is:
+// 1. Wrapped in an element with class name "test-case".
+// 2. Has data-expect attribute be an ordered list of elements to focus.
+// 3. Has data-description attribute be a string explaining the test.
+// e.g <div class="test-case" data-expect="b,a,c"
+// data-description="Focus navigation">
+async function runFocusTestCases() {
+ const testCases = Array.from(document.querySelectorAll('.test-case'));
+ for (let testCase of testCases) {
+ promise_test(async () => {
+ const expected = testCase.dataset.expect.split(',');
+ await assert_focus_navigation_bidirectional(expected);
+ }, testCase.dataset.description);
+ }
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/resources/shadow-dom.js b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/resources/shadow-dom.js
new file mode 100644
index 00000000000..2789f058505
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus-navigation/resources/shadow-dom.js
@@ -0,0 +1,165 @@
+function removeWhiteSpaceOnlyTextNodes(node) {
+ for (var i = 0; i < node.childNodes.length; i++) {
+ var child = node.childNodes[i];
+ if (child.nodeType === Node.TEXT_NODE &&
+ child.nodeValue.trim().length == 0) {
+ node.removeChild(child);
+ i--;
+ } else if (
+ child.nodeType === Node.ELEMENT_NODE ||
+ child.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
+ removeWhiteSpaceOnlyTextNodes(child);
+ }
+ }
+ if (node.shadowRoot) {
+ removeWhiteSpaceOnlyTextNodes(node.shadowRoot);
+ }
+}
+
+function convertTemplatesToShadowRootsWithin(node) {
+ var nodes = node.querySelectorAll('template');
+ for (var i = 0; i < nodes.length; ++i) {
+ var template = nodes[i];
+ var mode = template.getAttribute('data-mode');
+ var delegatesFocus = template.hasAttribute('data-delegatesFocus');
+ var parent = template.parentNode;
+ parent.removeChild(template);
+ var shadowRoot;
+ if (!mode) {
+ shadowRoot = parent.attachShadow({ mode: 'open' });
+ } else {
+ shadowRoot =
+ parent.attachShadow({ 'mode': mode, 'delegatesFocus': delegatesFocus });
+ }
+ var expose = template.getAttribute('data-expose-as');
+ if (expose)
+ window[expose] = shadowRoot;
+ if (template.id)
+ shadowRoot.id = template.id;
+ var fragments = document.importNode(template.content, true);
+ shadowRoot.appendChild(fragments);
+
+ convertTemplatesToShadowRootsWithin(shadowRoot);
+ }
+}
+
+function isShadowHost(node) {
+ return node && node.nodeType == Node.ELEMENT_NODE && node.shadowRoot;
+}
+
+function isIFrameElement(element) {
+ return element && element.nodeName == 'IFRAME';
+}
+
+// Returns node from shadow/iframe tree "path".
+function getNodeInComposedTree(path) {
+ var ids = path.split('/');
+ var node = document.getElementById(ids[0]);
+ for (var i = 1; node != null && i < ids.length; ++i) {
+ if (isIFrameElement(node))
+ node = node.contentDocument.getElementById(ids[i]);
+ else if (isShadowHost(node))
+ node = node.shadowRoot.getElementById(ids[i]);
+ else
+ return null;
+ }
+ return node;
+}
+
+function createTestTree(node) {
+ let ids = {};
+
+ function attachShadowFromTemplate(template) {
+ let parent = template.parentNode;
+ parent.removeChild(template);
+ let shadowRoot;
+ if (template.getAttribute('data-slot-assignment') === 'manual') {
+ shadowRoot =
+ parent.attachShadow({
+ mode: template.getAttribute('data-mode'),
+ slotAssignment: 'manual'
+ });
+ } else {
+ shadowRoot =
+ parent.attachShadow({ mode: template.getAttribute('data-mode') });
+ }
+ let id = template.id;
+ if (id) {
+ shadowRoot.id = id;
+ ids[id] = shadowRoot;
+ }
+ shadowRoot.appendChild(document.importNode(template.content, true));
+ return shadowRoot;
+ }
+
+ function walk(root) {
+ if (root.id) {
+ ids[root.id] = root;
+ }
+ for (let e of Array.from(root.querySelectorAll('[id]'))) {
+ ids[e.id] = e;
+ }
+ for (let e of Array.from(root.querySelectorAll('template'))) {
+ walk(attachShadowFromTemplate(e));
+ }
+ }
+
+ walk(node.cloneNode(true));
+ return ids;
+}
+
+function dispatchEventWithLog(nodes, target, event) {
+ function labelFor(e) {
+ return e.id || e.tagName;
+ }
+
+ let log = [];
+ let attachedNodes = [];
+ for (let label in nodes) {
+ let startingNode = nodes[label];
+ for (let node = startingNode; node; node = node.parentNode) {
+ if (attachedNodes.indexOf(node) >= 0)
+ continue;
+ let id = node.id;
+ if (!id)
+ continue;
+ attachedNodes.push(node);
+ node.addEventListener(event.type, (e) => {
+ // Record [currentTarget, target, relatedTarget, composedPath()]
+ log.push([
+ id, labelFor(e.target),
+ e.relatedTarget ? labelFor(e.relatedTarget) : null,
+ e.composedPath().map((n) => {
+ return labelFor(n);
+ })
+ ]);
+ });
+ }
+ }
+ target.dispatchEvent(event);
+ return log;
+}
+
+// This function assumes that testharness.js is available.
+function assert_event_path_equals(actual, expected) {
+ assert_equals(actual.length, expected.length);
+ for (let i = 0; i < actual.length; ++i) {
+ assert_equals(
+ actual[i][0], expected[i][0],
+ 'currentTarget at ' + i + ' should be same');
+ assert_equals(
+ actual[i][1], expected[i][1], 'target at ' + i + ' should be same');
+ assert_equals(
+ actual[i][2], expected[i][2],
+ 'relatedTarget at ' + i + ' should be same');
+ assert_array_equals(
+ actual[i][3], expected[i][3],
+ 'composedPath at ' + i + ' should be same');
+ }
+}
+
+function assert_background_color(path, color) {
+ assert_equals(
+ window.getComputedStyle(getNodeInComposedTree(path)).backgroundColor,
+ color, 'backgroundColor for ' + path + ' should be ' + color);
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative-delegatesFocus.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative-delegatesFocus.html
new file mode 100644
index 00000000000..cbf6684554a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative-delegatesFocus.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom that delegates focus and all tabindex=-1 in shadow tree</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=0>
+// #shadowRoot (delegatesFocus)
+// <div #aboveSlot tabindex=-1>
+// <slot #slotAbove tabindex=-1>
+// (slotted) <div #slottedAbove tabindex=-1>
+// <slot #slotBelow tabindex=-1>
+// (slotted) <div #slottedBelow tabindex=-1>
+// <div #belowSlot tabindex=-1>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] = elementsInFlatTreeOrder = prepareDOM(document.body, true);
+ setTabIndex(elementsInFlatTreeOrder, -1);
+ setTabIndex([aboveHost, host, belowHost], 0);
+ resetFocus();
+ // Focus should only land on #aboveHost and #belowHost (all others are non-sequentially focusable).
+ return assertFocusOrder([aboveHost, belowHost]);
+}, "Order when all tabindex=-1 is and delegatesFocus = true");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative.html
new file mode 100644
index 00000000000..7636953d0ee
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-negative.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and negative tabindex in shadow scope</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=0>
+// #shadowRoot
+// <div #aboveSlot tabindex=-1>
+// <slot #slotAbove tabindex=-1>
+// (slotted) <div #slottedAbove tabindex=-1>
+// <slot #slotBelow tabindex=-1>
+// (slotted) <div #slottedBelow tabindex=-1>
+// <div #belowSlot tabindex=-1>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, -1);
+ setTabIndex([aboveHost, host, belowHost], 0);
+ resetFocus();
+ return assertFocusOrder([aboveHost, host, belowHost]);
+}, "Order when all elements in shadow tree has negative tabindex");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-slot-one.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-slot-one.html
new file mode 100644
index 00000000000..995464409a7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-slot-one.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and all tabindex=0 except for one of the slot</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=0>
+// #shadowRoot
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=1>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ slotBelow.tabIndex = 1;
+ resetFocus();
+ // Focus should move first according to flat tree order to #aboveHost and #host, then into #host's focus scope.
+ // It will then move to #slottedBelow because #slotBelow has tabindex=1 (though we actually won't focus on the slot),
+ // and then back to #host's focus scope again, finally getting out to the document focus scope.
+ return assertFocusOrder([aboveHost, host, slottedBelow, aboveSlot, slottedAbove, belowSlot, belowHost]);
+}, "Order when all tabindex=0, except for one slot that has tabindex=1");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-delegatesFocus.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-delegatesFocus.html
new file mode 100644
index 00000000000..428f01b0480
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-delegatesFocus.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom that delegates focus and tabindex in shadow tree varies</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=0>
+// #shadowRoot (delegatesFocus)
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=1>
+// (slotted) <div #slottedAbove tabindex=3>
+// <slot #slotBelow tabindex=2>
+// (slotted) <div #slottedBelow tabindex=1>
+// <div #belowSlot tabindex=-1>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] = elementsInFlatTreeOrder = prepareDOM(document.body, true);
+ setTabIndex(elementsInFlatTreeOrder, -1);
+ setTabIndex([aboveHost, host, aboveSlot, belowHost], 0);
+ setTabIndex([slotAbove, slottedBelow], 1);
+ setTabIndex([slotBelow], 2);
+ setTabIndex([slottedAbove], 3);
+ resetFocus();
+ return assertFocusOrder([aboveHost, slottedAbove, slottedBelow, aboveSlot, belowHost]);
+}, "Order when tabindex varies and delegatesFocus = true");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-2.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-2.html
new file mode 100644
index 00000000000..608479be78f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-2.html
@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom with varying tabindex values</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+promise_test(async () => {
+ function createButtonInShadowDOM(host) {
+ const root = host.attachShadow({mode: "open"});
+ root.innerHTML = "<input>";
+ document.body.appendChild(host);
+ return root;
+ }
+ const host1 = document.createElement("div");
+ const root1 = createButtonInShadowDOM(host1);
+
+ const forwarder = document.createElement("div");
+ forwarder.setAttribute("tabindex", 0);
+ document.body.appendChild(forwarder);
+
+ const host2 = document.createElement("div");
+ host2.setAttribute("tabindex", -1);
+ const root2 = createButtonInShadowDOM(host2);
+
+ const host3 = document.createElement("div");
+ const root3 = createButtonInShadowDOM(host3);
+
+ root1.querySelector("input").focus();
+
+ let forwarderFocused = false;
+ forwarder.addEventListener("focus", () => {
+ forwarderFocused = true;
+ root2.querySelector("input").focus();
+ });
+
+ // Structure:
+ // <div #host1></div>
+ // #ShadowRoot
+ // <button>Button</button>
+ // <div #forwarder tabindex=0></div>
+ // <div #host2 tabindex=-1></div>
+ // #ShadowRoot
+ // <button>Button</button>
+ // <div #host3></div>
+ // #ShadowRoot
+ // <button>Button</button>
+ assert_equals(document.activeElement, host1);
+ assert_equals(root1.activeElement, root1.querySelector("input"));
+
+ await navigateFocusForward();
+ assert_true(forwarderFocused);
+ assert_equals(document.activeElement, host2);
+ assert_equals(root2.activeElement, root2.querySelector("input"));
+
+ // In buggy Firefox build, the following focus navigation will
+ // move the focus back to #host1's button.
+ await navigateFocusForward();
+ assert_equals(document.activeElement, host3);
+ assert_equals(root3.activeElement, root3.querySelector("input"));
+}, "Order with different tabindex on host")
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-3.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-3.html
new file mode 100644
index 00000000000..fb930a8fc05
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex-3.html
@@ -0,0 +1,80 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with nested shadow dom with varying tabindex values</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+promise_test(async () => {
+ function createButtonInShadowDOM(host, parent) {
+ const root = host.attachShadow({mode: "open"});
+ root.innerHTML = "<input>";
+ parent.appendChild(host);
+ return root;
+ }
+
+ const host1 = document.createElement("div");
+ const root1 = createButtonInShadowDOM(host1, document.body);
+
+ const forwarder = document.createElement("div");
+ forwarder.setAttribute("tabindex", 0);
+ document.body.appendChild(forwarder);
+
+ const host2 = document.createElement("div");
+ host2.setAttribute("tabindex", -1);
+ const root2 = host2.attachShadow({mode: "open"});
+ document.body.appendChild(host2);
+
+ const host2_1 = document.createElement("div");
+ const root2_1 = createButtonInShadowDOM(host2_1, root2);
+
+ const host2_2 = document.createElement("div");
+ host2_2.setAttribute("tabindex", -1);
+ const root2_2 = createButtonInShadowDOM(host2_2, root2);
+
+ const host2_3 = document.createElement("div");
+ const root2_3 = createButtonInShadowDOM(host2_3, root2);
+
+ root1.querySelector("input").focus();
+
+ let forwarderFocused = false;
+ forwarder.addEventListener("focus", () => {
+ forwarderFocused = true;
+ root2_2.querySelector("input").focus();
+ });
+
+ // Structure:
+ // <div #host1></div>
+ // #ShadowRoot
+ // <button>Button</button>
+ // <div #forwarder tabindex=0></div>
+ // <div #host2 tabindex=-1></div>
+ // #ShadowRoot
+ // <div #host2_1></div>
+ // #ShadowRoot
+ // <button>Button</button>
+ // <div #host2_2 tabindex=-1></div>
+ // #ShadowRoot
+ // <button>Button</button>
+ // <div #host2_3></div>
+ // #ShadowRoot
+ // <button>Button</button>
+ assert_equals(document.activeElement, host1);
+ assert_equals(root1.activeElement, root1.querySelector("input"));
+
+ await navigateFocusForward();
+ assert_true(forwarderFocused);
+ assert_equals(document.activeElement, host2);
+ assert_equals(root2_2.activeElement, root2_2.querySelector("input"));
+
+ // In buggy Firefox build, the following focus navigation will
+ // move the focus back to #host1_1's button.
+ await navigateFocusForward();
+ assert_equals(document.activeElement, host2);
+ assert_equals(root2_3.activeElement, root2_3.querySelector("input"));
+}, "Order with different tabindex on host")
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex.html
new file mode 100644
index 00000000000..d6654d6a0eb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-varying-tabindex.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom with varying tabindex values</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=3>
+// <div #host tabindex=3>
+// #shadowRoot
+// <div #aboveSlot tabindex=2>
+// <slot #slotAbove tabindex=1>
+// (slotted) <div #slottedAbove tabindex=4>
+// <slot #slotBelow tabindex=1>
+// (slotted) <div #slottedBelow tabindex=4>
+// <div #belowSlot tabindex=2>
+// <div #belowHost tabindex=3>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex([slotAbove, slotBelow], 1);
+ setTabIndex([aboveSlot, belowSlot], 2);
+ setTabIndex([aboveHost, host, belowHost], 3);
+ setTabIndex([slottedAbove, slottedBelow], 4);
+ resetFocus();
+ return assertFocusOrder([aboveHost, host, slottedAbove, slottedBelow, aboveSlot, belowSlot, belowHost]);
+}, "Order with various tabindex values");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-delegatesFocus.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-delegatesFocus.html
new file mode 100644
index 00000000000..b8f1b770f73
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-delegatesFocus.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom that delegates focus and all tabindex=0</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=0>
+// #shadowRoot (delegatesFocus)
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=0>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] = elementsInFlatTreeOrder = prepareDOM(document.body, true);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ resetFocus();
+ // Focus should move in flat tree order since every one of them has tabindex ==0,
+ // but doesn't include slots and #host.
+ return assertFocusOrder([aboveHost, aboveSlot, slottedAbove, slottedBelow, belowSlot, belowHost]);
+}, "Order when all tabindex=0 is and delegatesFocus = true");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-negative.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-negative.html
new file mode 100644
index 00000000000..afb7d179257
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-negative.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and negative host tabindex</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=-1>
+// #shadowRoot
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=0>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ host.tabIndex = -1;
+ resetFocus();
+ // Focus willl only move within the focus navigation scope of the document (not going to get into #host).
+ return assertFocusOrder([aboveHost, belowHost]);
+}, "Order when all tabindex=0 except for host, which has tabindex=-1");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set-scrollable.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set-scrollable.html
new file mode 100644
index 00000000000..3fc70ee4179
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set-scrollable.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and scrollable/non-focusable host</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host style="overflow: scroll">
+// #shadowRoot
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=0>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ removeTabIndex([host]);
+ host.style = "overflow: scroll";
+ resetFocus();
+ // Focus should move in flat tree order since every one of them has tabindex ==0,
+ // but doesn't include #slot since it's not rendered and #host since its tabindex is not set
+ // (but #host is considered as 0 in focus scope navigation, keeping the flat tree order for the shadow root's descendants).
+ return assertFocusOrder(elementsInFlatTreeOrder.filter(el => (el !== slotAbove && el !== slotBelow && el !== host)));
+}, "Order when all tabindex=0 except scrollable host (tabindex not set)");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set.html
new file mode 100644
index 00000000000..b3d8b068610
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-not-set.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and non-focusable host</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host>
+// #shadowRoot
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=0>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ removeTabIndex([host]);
+ resetFocus();
+ // Focus should move in flat tree order since every one of them has tabindex ==0,
+ // but doesn't include #slot since it's not rendered and #host since its tabindex is not set
+ // (but #host is considered as 0 in focus scope navigation, keeping the flat tree order for the shadow root's descendants).
+ return assertFocusOrder(elementsInFlatTreeOrder.filter(el => (el !== slotAbove && el !== slotBelow && el !== host)));
+}, "Order when all tabindex=0 except host (tabindex not set)");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-one.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-one.html
new file mode 100644
index 00000000000..141be9b48d9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-one.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and all tabindex=0 except host (tabindex=1)</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=1>
+// #shadowRoot
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=0>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ host.tabIndex = 1;
+ resetFocus();
+ // Focus should move first to #host because it has tabindex=1, and then to the contents of its scope
+ // (e.g. the contents of its shadow tree) in flat tree order, and then outside to the document scope
+ // again (aboveHost & belowHost).
+ return assertFocusOrder([host, aboveSlot, slottedAbove, slottedBelow, belowSlot, aboveHost, belowHost]);
+}, "Order when all tabindex=0 except for host, which has tabindex=1");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-scrollable.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-scrollable.html
new file mode 100644
index 00000000000..c26d343ce9a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero-host-scrollable.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and host is scrollable</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=0 style="overflow: scroll">
+// #shadowRoot
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=0>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ host.style = "overflow: scroll";
+ resetFocus();
+ // Focus should move in flat tree order since every one of them has tabindex==0,
+ // but doesn't include slots.
+ return assertFocusOrder(elementsInFlatTreeOrder.filter(el => (el !== slotAbove && el !== slotBelow)));
+}, "Order when all tabindex=0 and host is scrollable");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero.html b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero.html
new file mode 100644
index 00000000000..4be32c6c5eb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/focus-tabindex-order-shadow-zero.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>HTML Test: focus - the sequential focus navigation order with shadow dom and all tabindex=0</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation">
+<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>
+<script src="resources/shadow-utils.js"></script>
+<body>
+<script>
+// Structure:
+// <div #aboveHost tabindex=0>
+// <div #host tabindex=0>
+// #shadowRoot
+// <div #aboveSlot tabindex=0>
+// <slot #slotAbove tabindex=0>
+// (slotted) <div #slottedAbove tabindex=0>
+// <slot #slotBelow tabindex=0>
+// (slotted) <div #slottedBelow tabindex=0>
+// <div #belowSlot tabindex=0>
+// <div #belowHost tabindex=0>
+
+promise_test(() => {
+ let elementsInFlatTreeOrder;
+ let [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost] =
+ elementsInFlatTreeOrder = prepareDOM(document.body, false);
+ setTabIndex(elementsInFlatTreeOrder, 0);
+ resetFocus();
+ // Focus should move in flat tree order since every one of them has tabindex==0,
+ // but doesn't include slots.
+ return assertFocusOrder(elementsInFlatTreeOrder.filter(el => (el !== slotAbove && el !== slotBelow)));
+}, "Order when all tabindex=0 is and delegatesFocus = false");
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/resources/shadow-utils.js b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/resources/shadow-utils.js
new file mode 100644
index 00000000000..8033ce0169f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/shadow-dom/focus/resources/shadow-utils.js
@@ -0,0 +1,80 @@
+// Structure:
+// <div #aboveHost>
+// <div #host>
+// #shadowRoot
+// <div #aboveSlot>
+// <slot #slotAbove>
+// (slotted) <div #slottedAbove>
+// <slot #slotBelow>
+// (slotted) <div #slottedBelow>
+// <div #belowSlot>
+// <div #belowHost>
+function prepareDOM(container, delegatesFocus) {
+
+ const aboveHost = document.createElement("div");
+ aboveHost.innerText = "aboveHost";
+ const host = document.createElement("div");
+ host.id = "host";
+ const slottedBelow = document.createElement("div");
+ slottedBelow.innerText = "slotted below";
+ slottedBelow.slot = "below";
+ const slottedAbove = document.createElement("div");
+ slottedAbove.innerText = "slotted above";
+ slottedAbove.slot = "above";
+
+ const belowHost = document.createElement("div");
+ belowHost.innerText = "belowHost";
+ container.appendChild(aboveHost);
+ container.appendChild(host);
+ container.appendChild(belowHost);
+ host.appendChild(slottedBelow);
+ host.appendChild(slottedAbove);
+ const shadowRoot = host.attachShadow({ mode: "open", delegatesFocus: delegatesFocus});
+ const aboveSlot = document.createElement("div");
+ aboveSlot.innerText = "aboveSlot";
+
+ const slotAbove = document.createElement("slot");
+ slotAbove.name = "above";
+ const slotBelow = document.createElement("slot");
+ slotBelow.name = "below";
+
+ const belowSlot = document.createElement("div");
+ belowSlot.innerText = "belowSlot";
+ shadowRoot.appendChild(aboveSlot);
+ shadowRoot.appendChild(slotAbove);
+ shadowRoot.appendChild(slotBelow);
+ shadowRoot.appendChild(belowSlot);
+
+ return [aboveHost, host, aboveSlot, slotAbove, slottedAbove, slotBelow, slottedBelow, belowSlot, belowHost];
+}
+
+function setTabIndex(elements, value) {
+ for (const el of elements) {
+ el.tabIndex = value;
+ }
+}
+
+function removeTabIndex(elements) {
+ for (const el of elements) {
+ el.removeAttribute("tabindex");
+ }
+}
+
+function resetFocus(root = document) {
+ if (root.activeElement)
+ root.activeElement.blur();
+}
+
+function navigateFocusForward() {
+ // TAB = '\ue004'
+ return test_driver.send_keys(document.body, "\ue004");
+}
+
+async function assertFocusOrder(expectedOrder) {
+ const shadowRoot = document.getElementById("host").shadowRoot;
+ for (const el of expectedOrder) {
+ await navigateFocusForward();
+ const focused = shadowRoot.activeElement ? shadowRoot.activeElement : document.activeElement;
+ assert_equals(focused, el);
+ }
+}