Skip to content

Truly independent web browser

LibWeb: Center offscreen elements during focus

sideshowbarker432aa560bd2b
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 60f1dd489ab..5a029f66281 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -4162,13 +4162,14 @@ WebIDL::ExceptionOr<void> Element::insert_adjacent_text(Utf16View where, Utf16Vi
}
// https://drafts.csswg.org/cssom-view-1/#determine-the-scroll-into-view-position
-static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead const& read, ScrollIntoViewTarget target, CSSPixelRect target_bounding_border_box, Element::ScrollLogicalPosition block, Element::ScrollLogicalPosition inline_, Node& scrolling_box)
+static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead const& read, ScrollIntoViewTarget target, CSSPixelRect target_bounding_border_box, Element::ScrollLogicalPosition block, Element::ScrollLogicalPosition inline_, Node& scrolling_box, ScrollIntoViewMode scroll_mode)
{
// To determine the scroll-into-view position of a target, which is an Element, pseudo-element, or Range, with a
// block flow direction position block, an inline base direction position inline, and a scrolling box scrolling box,
// run the following steps:
CSSPixelRect scrolling_box_rect;
+ CSSPixelRect visible_scrollport_rect;
CSSPixelPoint current_scroll_position;
Layout::NodeWithStyle const* scrolling_box_layout_node = nullptr;
if (scrolling_box.is_document()) {
@@ -4181,6 +4182,7 @@ static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead c
CSSPixels::nearest_value_for(visual_viewport.height()),
};
scrolling_box_rect = { visual_viewport.offset(), visible_size };
+ visible_scrollport_rect = scrolling_box_rect;
if (auto* layout_node = document.layout_node(read)) {
scrolling_box_rect = Painting::scroll_snapport_rect(*layout_node, scrolling_box_rect);
scrolling_box_layout_node = as_if<Layout::NodeWithStyle>(layout_node);
@@ -4193,6 +4195,8 @@ static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead c
current_scroll_position = Painting::presented_scroll_offset(*layout_node, Painting::scroll_offset(*layout_node));
scrolling_box_layout_node = as_if<Layout::NodeWithStyle>(layout_node);
scrolling_box_rect = Painting::rect_to_viewport(*layout_node, Painting::scroll_snapport_rect(*layout_node), Layout::RustFFI::ViewportSpace::LayoutViewport);
+ if (scroll_mode == ScrollIntoViewMode::IfNeeded)
+ visible_scrollport_rect = Painting::rect_to_viewport(*layout_node, Painting::absolute_padding_box_rect(*layout_node), Layout::RustFFI::ViewportSpace::LayoutViewport);
} else {
return {};
}
@@ -4222,6 +4226,19 @@ static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead c
// AD-HOC: The caller performs this step once and passes the result in, moving it outward as each scrolling box is
// scrolled, so that an outer scrolling box sees where the target is going to be.
+ // AD-HOC: focus() requires centered alignment, but for interoperability, leave each axis unchanged if the target is
+ // fully visible or covers the scrollport. Compare the border box without scroll-margin or scroll-padding.
+ bool preserve_x = scroll_mode == ScrollIntoViewMode::IfNeeded
+ && ((target_bounding_border_box.left() >= visible_scrollport_rect.left()
+ && target_bounding_border_box.right() <= visible_scrollport_rect.right())
+ || (target_bounding_border_box.left() <= visible_scrollport_rect.left()
+ && target_bounding_border_box.right() >= visible_scrollport_rect.right()));
+ bool preserve_y = scroll_mode == ScrollIntoViewMode::IfNeeded
+ && ((target_bounding_border_box.top() >= visible_scrollport_rect.top()
+ && target_bounding_border_box.bottom() <= visible_scrollport_rect.bottom())
+ || (target_bounding_border_box.top() <= visible_scrollport_rect.top()
+ && target_bounding_border_box.bottom() >= visible_scrollport_rect.bottom()));
+
// AD-HOC: The spec doesn't specify when to do this, but we need to apply scroll-margin and scroll-margin to target
// bounding border box (https://drafts.csswg.org/cssom-view-1/#example-51af1565).
if (target.has<Element*>()) {
@@ -4355,7 +4372,7 @@ static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead c
inline_delta = -inline_delta;
auto x = current_scroll_position.x() + (block_axis_is_horizontal ? block_delta : inline_delta);
auto y = current_scroll_position.y() + (block_axis_is_horizontal ? inline_delta : block_delta);
- return CSSPixelPoint { x, y };
+ return CSSPixelPoint { preserve_x ? current_scroll_position.x() : x, preserve_y ? current_scroll_position.y() : y };
}();
// 11. Return position.
@@ -4363,7 +4380,7 @@ static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead c
}
// https://drafts.csswg.org/cssom-view-1/#scroll-a-target-into-view
-void scroll_a_target_into_view(Layout::BegunRead const& read, ScrollIntoViewTarget target, Element::ScrollBehavior behavior, Element::ScrollLogicalPosition block, Element::ScrollLogicalPosition inline_, GC::Ptr<Element> container, GC::Ptr<WebIDL::Promise> promise)
+void scroll_a_target_into_view(Layout::BegunRead const& read, ScrollIntoViewTarget target, Element::ScrollBehavior behavior, Element::ScrollLogicalPosition block, Element::ScrollLogicalPosition inline_, GC::Ptr<Element> container, GC::Ptr<WebIDL::Promise> promise, ScrollIntoViewMode scroll_mode)
{
auto target_document = [](ScrollIntoViewTarget current_target) -> Document& {
return current_target.visit(
@@ -4445,7 +4462,7 @@ void scroll_a_target_into_view(Layout::BegunRead const& read, ScrollIntoViewTarg
// 2. Let position be the scroll position resulting from running the steps to determine the scroll-into-view
// position of target with behavior as the scroll behavior, block as the block flow position, inline as the
// inline base direction position and scrolling box as the scrolling box.
- auto position = determine_the_scroll_into_view_position(read, current_target, target_bounding_border_box, block, inline_, scrolling_box);
+ auto position = determine_the_scroll_into_view_position(read, current_target, target_bounding_border_box, block, inline_, scrolling_box, scroll_mode);
// AD-HOC: A smooth scroll leaves the scrolling box at its old scroll position while the outer scrolling boxes
// are considered, so move the target to where this scroll is going to leave it. The viewport is always
@@ -4550,7 +4567,7 @@ void scroll_a_target_into_view(Layout::BegunRead const& read, ScrollIntoViewTarg
}
// https://drafts.csswg.org/cssom-view/#dom-element-scrollintoview
-void Element::scroll_into_view(Element::ScrollIntoViewOptions const& options, GC::Ptr<WebIDL::Promise> promise)
+void Element::scroll_into_view(Element::ScrollIntoViewOptions const& options, GC::Ptr<WebIDL::Promise> promise, ScrollIntoViewMode scroll_mode)
{
// 1. Let behavior be "auto".
auto behavior = options.behavior;
@@ -4581,7 +4598,7 @@ void Element::scroll_into_view(Element::ScrollIntoViewOptions const& options, GC
// 8. Scroll the element into view with behavior, block, inline, and container. Let scrollPromise be the Promise
// returned from this step.
- scroll_a_target_into_view(read, ScrollIntoViewTarget { this }, behavior, block, inline_, container, promise);
+ scroll_a_target_into_view(read, ScrollIntoViewTarget { this }, behavior, block, inline_, container, promise, scroll_mode);
// FIXME: 9. Optionally perform some other action that brings the element to the user’s attention.
}
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index 247d1e2cde3..bdc4a953dbb 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -107,7 +107,12 @@ namespace Web::DOM {
using ScrollIntoViewTarget = Variant<Element*, Range*>;
-void scroll_a_target_into_view(Layout::BegunRead const&, ScrollIntoViewTarget, Bindings::ScrollBehavior, Bindings::ScrollLogicalPosition block, Bindings::ScrollLogicalPosition inline_, GC::Ptr<Element> container = nullptr, GC::Ptr<WebIDL::Promise> promise = nullptr);
+enum class ScrollIntoViewMode {
+ Always,
+ IfNeeded,
+};
+
+void scroll_a_target_into_view(Layout::BegunRead const&, ScrollIntoViewTarget, Bindings::ScrollBehavior, Bindings::ScrollLogicalPosition block, Bindings::ScrollLogicalPosition inline_, GC::Ptr<Element> container = nullptr, GC::Ptr<WebIDL::Promise> promise = nullptr, ScrollIntoViewMode = ScrollIntoViewMode::Always);
// https://html.spec.whatwg.org/multipage/custom-elements.html#upgrade-reaction
// An upgrade reaction, which will upgrade the custom element and contains a custom element definition; or
@@ -746,7 +751,7 @@ public:
// https://w3c.github.io/csswg-drafts/cssom-view-1/#dom-element-scrollintoview
void scroll_into_view(Variant<bool, ScrollIntoViewOptions> const&, GC::Ptr<WebIDL::Promise>);
- void scroll_into_view(ScrollIntoViewOptions const&, GC::Ptr<WebIDL::Promise>);
+ void scroll_into_view(ScrollIntoViewOptions const&, GC::Ptr<WebIDL::Promise>, ScrollIntoViewMode = ScrollIntoViewMode::Always);
// https://www.w3.org/TR/wai-aria-1.2/#ARIAMixin
#define __ENUMERATE_ARIA_ATTRIBUTE(name, attribute) \
diff --git a/Libraries/LibWeb/HTML/Focus.cpp b/Libraries/LibWeb/HTML/Focus.cpp
index 11c54841ba0..bcb7440412d 100644
--- a/Libraries/LibWeb/HTML/Focus.cpp
+++ b/Libraries/LibWeb/HTML/Focus.cpp
@@ -871,9 +871,20 @@ void run_focusing_steps(GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<DOM::Node>
return;
Bindings::ScrollIntoViewOptions scroll_options;
- scroll_options.block = Bindings::ScrollLogicalPosition::Nearest;
- scroll_options.inline_ = Bindings::ScrollLogicalPosition::Nearest;
- (void)focused_element->scroll_into_view(scroll_options, {});
+ auto scroll_mode = DOM::ScrollIntoViewMode::Always;
+ if (focus_trigger == FocusTrigger::Script || focus_trigger == FocusTrigger::Key) {
+ // https://html.spec.whatwg.org/multipage/interaction.html#dom-focus
+ // 4. If options["preventScroll"] is false, then scroll a target into view given this, "auto",
+ // "center", and "center".
+ // AD-HOC: Use the same alignment for sequential keyboard focus, for interoperability.
+ scroll_options.block = Bindings::ScrollLogicalPosition::Center;
+ scroll_options.inline_ = Bindings::ScrollLogicalPosition::Center;
+ scroll_mode = DOM::ScrollIntoViewMode::IfNeeded;
+ } else {
+ scroll_options.block = Bindings::ScrollLogicalPosition::Nearest;
+ scroll_options.inline_ = Bindings::ScrollLogicalPosition::Nearest;
+ }
+ (void)focused_element->scroll_into_view(scroll_options, {}, scroll_mode);
}
// https://html.spec.whatwg.org/multipage/interaction.html#unfocusing-steps
diff --git a/Tests/LibWeb/Text/expected/focus-centers-only-outside-scrollport.txt b/Tests/LibWeb/Text/expected/focus-centers-only-outside-scrollport.txt
new file mode 100644
index 00000000000..3358f5f4b54
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/focus-centers-only-outside-scrollport.txt
@@ -0,0 +1,36 @@
+visible: x=unchanged, y=unchanged
+below: x=unchanged, y=centered
+input-below: x=unchanged, y=centered
+above: x=unchanged, y=centered
+right: x=centered, y=unchanged
+below-right: x=centered, y=centered
+partially-visible: x=unchanged, y=centered
+input-partially-visible: x=unchanged, y=centered
+preventScroll: x=unchanged, y=unchanged
+nested-visible-target: inner-x=unchanged, inner-y=unchanged, viewport-x=unchanged, viewport-y=centered
+nested-clipped-target: inner-x=centered, inner-y=centered, viewport-x=unchanged, viewport-y=unchanged
+nested-both-offscreen: inner-x=centered, inner-y=centered, viewport-x=unchanged, viewport-y=centered
+vertical-rl-both: inner-x=centered, inner-y=centered, viewport-x=unchanged, viewport-y=unchanged
+vertical-lr-both: inner-x=centered, inner-y=centered, viewport-x=unchanged, viewport-y=unchanged
+vertical-rl-block-only: inner-x=centered, inner-y=unchanged, viewport-x=unchanged, viewport-y=unchanged
+vertical-lr-inline-only: inner-x=unchanged, inner-y=centered, viewport-x=unchanged, viewport-y=unchanged
+nested-preventScroll: inner-x=unchanged, inner-y=unchanged, viewport-x=unchanged, viewport-y=unchanged
+visible-with-scroll-margin: x=100, y=100
+visible-with-scroll-padding: x=100, y=20
+offscreen-with-scroll-margin: x=100, y=140
+offscreen-with-scroll-padding: x=100, y=140
+rtl: x=150, y=20
+two-scrollers-20: inner=true, outer=true, viewport-y=unchanged
+two-scrollers-2000: inner=true, outer=true, viewport-y=centered
+oversized-covers-y-edge: x=unchanged, y=unchanged
+oversized-covers-y-end-edge: x=unchanged, y=unchanged
+oversized-covers-y: x=unchanged, y=unchanged
+oversized-covers-x-edge: x=unchanged, y=unchanged
+oversized-covers-x-end-edge: x=unchanged, y=unchanged
+oversized-covers-x: x=unchanged, y=unchanged
+oversized-covers-both: x=unchanged, y=unchanged
+oversized-covers-y-offscreen-x: x=centered, y=unchanged
+oversized-covers-x-offscreen-y: x=unchanged, y=centered
+oversized-offscreen-y: x=unchanged, y=centered
+oversized-offscreen-x: x=centered, y=unchanged
+scrollIntoView-visible: y=centered
diff --git a/Tests/LibWeb/Text/expected/focus-keyboard-scroll-center.txt b/Tests/LibWeb/Text/expected/focus-keyboard-scroll-center.txt
new file mode 100644
index 00000000000..840c520d363
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/focus-keyboard-scroll-center.txt
@@ -0,0 +1,14 @@
+visible: focused=true, x=unchanged, y=unchanged
+below: focused=true, x=unchanged, y=centered
+backward-below: focused=true, x=unchanged, y=centered
+right: focused=true, x=centered, y=unchanged
+below-right: focused=true, x=centered, y=centered
+covers-y: focused=true, x=unchanged, y=unchanged
+covers-x: focused=true, x=unchanged, y=unchanged
+covers-both: focused=true, x=unchanged, y=unchanged
+oversized-offscreen: focused=true, x=unchanged, y=centered
+nested-visible: focused=true, inner-x=unchanged, inner-y=unchanged, viewport-x=unchanged, viewport-y=unchanged
+nested-clipped: focused=true, inner-x=centered, inner-y=centered, viewport-x=unchanged, viewport-y=unchanged
+nested-offscreen: focused=true, inner-x=centered, inner-y=centered, viewport-x=unchanged, viewport-y=centered
+nested-covers-y: focused=true, inner-x=centered, inner-y=unchanged, viewport-x=unchanged, viewport-y=unchanged
+nested-covers-x: focused=true, inner-x=unchanged, inner-y=centered, viewport-x=unchanged, viewport-y=unchanged
diff --git a/Tests/LibWeb/Text/expected/wpt-import/focus/focus-centers-element.txt b/Tests/LibWeb/Text/expected/wpt-import/focus/focus-centers-element.txt
new file mode 100644
index 00000000000..1e53e7a4f57
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/focus/focus-centers-element.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Element.focus() center in both directions
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/focus/scroll-matches-focus.txt b/Tests/LibWeb/Text/expected/wpt-import/focus/scroll-matches-focus.txt
new file mode 100644
index 00000000000..d6a8e7e9dc8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/focus/scroll-matches-focus.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass :focus applies before scrolling into view
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/focus-centers-only-outside-scrollport.html b/Tests/LibWeb/Text/input/focus-centers-only-outside-scrollport.html
new file mode 100644
index 00000000000..3329bf11d92
--- /dev/null
+++ b/Tests/LibWeb/Text/input/focus-centers-only-outside-scrollport.html
@@ -0,0 +1,198 @@
+<!doctype html>
+<style>
+ body {
+ margin: 0;
+ width: 5000px;
+ height: 5000px;
+ }
+ .target {
+ position: absolute;
+ box-sizing: border-box;
+ width: 100px;
+ height: 40px;
+ }
+ .scroller {
+ position: absolute;
+ left: 20px;
+ top: 20px;
+ width: 400px;
+ height: 240px;
+ overflow: auto;
+ scrollbar-width: none;
+ }
+ .content {
+ position: relative;
+ width: 2000px;
+ height: 2000px;
+ }
+</style>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ const frame = () => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ const near = (a, b) => Math.abs(a - b) <= 1;
+ const center = rect => [rect.x + rect.width / 2, rect.y + rect.height / 2];
+ const target = (parent, tag = "div") => {
+ const element = document.createElement(tag);
+ element.className = "target";
+ element.tabIndex = 0;
+ parent.append(element);
+ return element;
+ };
+ for (const [name, x, y, startY, options] of [
+ ["visible", 100, 100, 0],
+ ["below", 100, 2000, 0],
+ ["input-below", 100, 2000, 0],
+ ["above", 100, 1500, 2500],
+ ["right", 3000, 100, 0],
+ ["below-right", 3000, 2000, 0],
+ ["partially-visible", 100, innerHeight - 20, 0],
+ ["input-partially-visible", 100, innerHeight - 10, 0],
+ ["preventScroll", 3000, 2000, 0, { preventScroll: true }],
+ ]) {
+ const element = target(document.body, name.startsWith("input-") ? "input" : "div");
+ element.style.left = `${x}px`;
+ element.style.top = `${y}px`;
+ scrollTo(0, startY);
+ element.focus(options);
+ await frame();
+ const [cx, cy] = center(element.getBoundingClientRect());
+ println(
+ `${name}: x=${near(cx, x + 50) ? "unchanged" : near(cx, innerWidth / 2) ? "centered" : cx}, y=${near(cy, y + 20 - startY) ? "unchanged" : near(cy, innerHeight / 2) ? "centered" : cy}`
+ );
+ element.remove();
+ }
+ for (const [name, outerY, block, inline, writingMode, preventScroll] of [
+ ["nested-visible-target", 2000, 20, 20, "horizontal-tb", false],
+ ["nested-clipped-target", 20, 900, 900, "horizontal-tb", false],
+ ["nested-both-offscreen", 2000, 900, 900, "horizontal-tb", false],
+ ["vertical-rl-both", 20, 900, 900, "vertical-rl", false],
+ ["vertical-lr-both", 20, 900, 900, "vertical-lr", false],
+ ["vertical-rl-block-only", 20, 900, 20, "vertical-rl", false],
+ ["vertical-lr-inline-only", 20, 20, 900, "vertical-lr", false],
+ ["nested-preventScroll", 2000, 900, 900, "horizontal-tb", true],
+ ]) {
+ const scroller = document.createElement("div");
+ scroller.className = "scroller";
+ scroller.style.top = `${outerY}px`;
+ scroller.style.writingMode = writingMode;
+ const content = document.createElement("div");
+ content.className = "content";
+ scroller.append(content);
+ document.body.append(scroller);
+ const element = target(content);
+ element.style.insetBlockStart = `${block}px`;
+ element.style.insetInlineStart = `${inline}px`;
+ scrollTo(0, 0);
+ scroller.scrollTo(0, 0);
+ const before = center(element.getBoundingClientRect());
+ const scrollportBefore = scroller.getBoundingClientRect();
+ element.focus({ preventScroll });
+ await frame();
+ const after = center(element.getBoundingClientRect());
+ const scrollport = scroller.getBoundingClientRect();
+ const relative = [after[0] - scrollport.x, after[1] - scrollport.y];
+ const original = [before[0] - scrollportBefore.x, before[1] - scrollportBefore.y];
+ println(
+ `${name}: inner-x=${near(relative[0], original[0]) ? "unchanged" : near(relative[0], scroller.clientWidth / 2) ? "centered" : relative[0]}, inner-y=${near(relative[1], original[1]) ? "unchanged" : near(relative[1], scroller.clientHeight / 2) ? "centered" : relative[1]}, viewport-x=${scrollX === 0 ? "unchanged" : "scrolled"}, viewport-y=${scrollY === 0 ? "unchanged" : near(after[1], innerHeight / 2) ? "centered" : after[1]}`
+ );
+ scroller.remove();
+ }
+ for (const [name, targetStyle, scrollStyle] of [
+ ["visible-with-scroll-margin", "left:100px;top:100px;scroll-margin-top:150px", ""],
+ ["visible-with-scroll-padding", "left:100px;top:20px", "scroll-padding-top:80px"],
+ ["offscreen-with-scroll-margin", "left:100px;top:900px;scroll-margin-top:80px", ""],
+ ["offscreen-with-scroll-padding", "left:100px;top:900px", "scroll-padding-top:80px"],
+ ["rtl", "left:900px;top:20px", "direction:rtl"],
+ ]) {
+ const scroller = document.createElement("div");
+ scroller.className = "scroller";
+ scroller.style.cssText = scrollStyle;
+ const content = document.createElement("div");
+ content.className = "content";
+ scroller.append(content);
+ document.body.append(scroller);
+ const element = target(content);
+ element.style.cssText = targetStyle;
+ scrollTo(0, 0);
+ element.focus();
+ await frame();
+ const rect = element.getBoundingClientRect();
+ const port = scroller.getBoundingClientRect();
+ println(`${name}: x=${rect.x - port.x}, y=${rect.y - port.y}`);
+ scroller.remove();
+ }
+ for (const outerY of [20, 2000]) {
+ const outer = document.createElement("div");
+ outer.className = "scroller";
+ outer.style.top = `${outerY}px`;
+ const outerContent = document.createElement("div");
+ outerContent.className = "content";
+ outer.append(outerContent);
+ document.body.append(outer);
+ const inner = document.createElement("div");
+ inner.className = "scroller";
+ inner.style.cssText = "left:900px;top:900px;width:240px;height:160px";
+ const innerContent = document.createElement("div");
+ innerContent.className = "content";
+ inner.append(innerContent);
+ outerContent.append(inner);
+ const element = target(innerContent);
+ element.style.cssText = "left:900px;top:900px";
+ scrollTo(0, 0);
+ element.focus();
+ await frame();
+ const point = center(element.getBoundingClientRect());
+ const innerCenter = center(inner.getBoundingClientRect());
+ const outerCenter = center(outer.getBoundingClientRect());
+ println(
+ `two-scrollers-${outerY}: inner=${near(point[0], innerCenter[0]) && near(point[1], innerCenter[1])}, outer=${near(point[0], outerCenter[0]) && near(point[1], outerCenter[1])}, viewport-y=${scrollY === 0 ? "unchanged" : near(point[1], innerHeight / 2) ? "centered" : point[1]}`
+ );
+ outer.remove();
+ }
+ for (const [name, left, top, width, height, initialX, initialY] of [
+ ["covers-y-edge", 100, 0, 100, 500, 0, 0],
+ ["covers-y-end-edge", 100, 0, 100, 500, 0, 260],
+ ["covers-y", 100, 0, 100, 500, 0, 100],
+ ["covers-x-edge", 0, 100, 800, 40, 0, 0],
+ ["covers-x-end-edge", 0, 100, 800, 40, 400, 0],
+ ["covers-x", 0, 100, 800, 40, 100, 0],
+ ["covers-both", 0, 0, 800, 500, 100, 100],
+ ["covers-y-offscreen-x", 900, 0, 100, 500, 0, 100],
+ ["covers-x-offscreen-y", 0, 900, 800, 40, 100, 0],
+ ["offscreen-y", 100, 900, 100, 500, 0, 0],
+ ["offscreen-x", 900, 100, 800, 40, 0, 0],
+ ]) {
+ const scroller = document.createElement("div");
+ scroller.className = "scroller";
+ const content = document.createElement("div");
+ content.className = "content";
+ scroller.append(content);
+ document.body.append(scroller);
+ const element = target(content);
+ element.style.cssText = `left:${left}px;top:${top}px;width:${width}px;height:${height}px`;
+ scroller.scrollTo(initialX, initialY);
+ scrollTo(0, 0);
+ element.focus();
+ await frame();
+ const point = center(element.getBoundingClientRect());
+ const port = center(scroller.getBoundingClientRect());
+ const x = scroller.scrollLeft === initialX ? "unchanged" : near(point[0], port[0]) ? "centered" : point[0];
+ const y = scroller.scrollTop === initialY ? "unchanged" : near(point[1], port[1]) ? "centered" : point[1];
+ println(`oversized-${name}: x=${x}, y=${y}`);
+ scroller.remove();
+ }
+ const visible = target(document.body);
+ visible.style.left = "100px";
+ visible.style.top = "2000px";
+ scrollTo(0, 1900);
+ visible.scrollIntoView({ block: "center", inline: "center" });
+ await frame();
+ println(
+ `scrollIntoView-visible: y=${near(center(visible.getBoundingClientRect())[1], innerHeight / 2) ? "centered" : "not centered"}`
+ );
+ visible.remove();
+ scrollTo(0, 0);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/focus-keyboard-scroll-center.html b/Tests/LibWeb/Text/input/focus-keyboard-scroll-center.html
new file mode 100644
index 00000000000..f506d9e6961
--- /dev/null
+++ b/Tests/LibWeb/Text/input/focus-keyboard-scroll-center.html
@@ -0,0 +1,100 @@
+<!doctype html>
+<style>
+ body {
+ margin: 0;
+ width: 5000px;
+ height: 5000px;
+ }
+ button {
+ position: absolute;
+ box-sizing: border-box;
+ left: 100px;
+ top: 100px;
+ width: 100px;
+ height: 40px;
+ }
+ .scroller {
+ position: absolute;
+ left: 20px;
+ width: 400px;
+ height: 240px;
+ overflow: auto;
+ scrollbar-width: none;
+ }
+ .content {
+ position: relative;
+ width: 2000px;
+ height: 2000px;
+ }
+</style>
+<button id="first">First</button>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ const frame = () => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ const near = (a, b) => Math.abs(a - b) <= 1;
+ const position = (offset, initial, center, expectedCenter) =>
+ near(offset, initial) ? "unchanged" : near(center, expectedCenter) ? "centered" : center;
+ for (const [name, left, top, width, height, startX, startY, backwards] of [
+ ["visible", 200, 100, 100, 40, 0, 0],
+ ["below", 100, 2000, 100, 40, 0, 0],
+ ["backward-below", 100, 2000, 100, 40, 0, 0, true],
+ ["right", 3000, 100, 100, 40, 0, 0],
+ ["below-right", 3000, 2000, 100, 40, 0, 0],
+ ["covers-y", 100, 0, 100, innerHeight + 400, 0, 100],
+ ["covers-x", 0, 100, innerWidth + 400, 40, 100, 0],
+ ["covers-both", 0, 0, innerWidth + 400, innerHeight + 400, 100, 100],
+ ["oversized-offscreen", 100, 2000, 100, innerHeight + 400, 0, 0],
+ ]) {
+ const target = document.createElement("button");
+ target.textContent = "Target";
+ target.style.cssText = `left:${left}px;top:${top}px;width:${width}px;height:${height}px`;
+ if (backwards) first.before(target);
+ else document.body.append(target);
+ scrollTo(startX, startY);
+ first.focus({ preventScroll: true });
+ internals.sendKey(first, "Tab", backwards ? internals.MOD_SHIFT : 0);
+ await frame();
+ const rect = target.getBoundingClientRect();
+ const x = position(scrollX, startX, rect.x + rect.width / 2, innerWidth / 2);
+ const y = position(scrollY, startY, rect.y + rect.height / 2, innerHeight / 2);
+ println(`${name}: focused=${document.activeElement === target}, x=${x}, y=${y}`);
+ target.remove();
+ }
+ for (const [name, outerY, left, top, width, height] of [
+ ["nested-visible", 20, 100, 100, 100, 40],
+ ["nested-clipped", 20, 900, 900, 100, 40],
+ ["nested-offscreen", 2000, 900, 900, 100, 40],
+ ["nested-covers-y", 20, 900, 0, 100, 500],
+ ["nested-covers-x", 20, 0, 900, 800, 40],
+ ]) {
+ const scroller = document.createElement("div");
+ scroller.className = "scroller";
+ scroller.tabIndex = -1;
+ scroller.style.top = `${outerY}px`;
+ const content = document.createElement("div");
+ content.className = "content";
+ scroller.append(content);
+ document.body.append(scroller);
+ const target = document.createElement("button");
+ target.textContent = "Target";
+ target.style.cssText = `left:${left}px;top:${top}px;width:${width}px;height:${height}px`;
+ content.append(target);
+ scrollTo(0, 0);
+ first.focus({ preventScroll: true });
+ internals.sendKey(first, "Tab");
+ await frame();
+ const rect = target.getBoundingClientRect();
+ const port = scroller.getBoundingClientRect();
+ const x = position(scroller.scrollLeft, 0, rect.x + rect.width / 2 - port.x, scroller.clientWidth / 2);
+ const y = position(scroller.scrollTop, 0, rect.y + rect.height / 2 - port.y, scroller.clientHeight / 2);
+ const viewportY = position(scrollY, 0, rect.y + rect.height / 2, innerHeight / 2);
+ println(
+ `${name}: focused=${document.activeElement === target}, inner-x=${x}, inner-y=${y}, viewport-x=${scrollX === 0 ? "unchanged" : "scrolled"}, viewport-y=${viewportY}`
+ );
+ scroller.remove();
+ }
+ scrollTo(0, 0);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/focus/focus-centers-element.html b/Tests/LibWeb/Text/input/wpt-import/focus/focus-centers-element.html
new file mode 100644
index 00000000000..07fbdde5884
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/focus/focus-centers-element.html
@@ -0,0 +1,84 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset=utf-8>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<title>focus() centers element outside displayport</title>
+<script src=../resources/testharness.js></script>
+<script src=../resources/testharnessreport.js></script>
+<style>
+
+#container {
+ height: 25vh;
+ width: 100vw;
+ background: yellow;
+ overflow: auto;
+ white-space: nowrap;
+}
+
+.spacer {
+ width: 150vw;
+ height: 150vh;
+ background: green;
+}
+
+#target {
+ height: 5vh;
+ width: 5vw;
+ background: red;
+ margin: 0 auto;
+}
+
+#container > div {
+ display: inline-block;
+}
+
+</style>
+</head>
+<body>
+ <div id="container">
+ <div class="spacer"></div>
+ <!-- Center the target element -->
+ <div style="width: 5vw">
+ <div style="height: 70vh"></div>
+ <div tabindex=0 id="target"></div>
+ <div style="height: 70vh"></div>
+ </div>
+ <div class="spacer"></div>
+ </div>
+</body>
+<script>
+
+function promiseFrame() {
+ return new Promise(resolve => {
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
+ });
+}
+
+promise_test(async (t) => {
+ let target = document.getElementById("target");
+
+ // Focus the element and record the scroll position
+ target.focus();
+ await promiseFrame();
+
+ let focusLeft = container.scrollLeft;
+ let focusTop = container.scrollTop;
+
+ container.scroll(0, 0);
+
+ // scrollIntoView the element and record the scroll position
+ target.scrollIntoView({block: "center", inline: "center"});
+ await promiseFrame();
+ let scrollLeft = container.scrollLeft;
+ let scrollTop = container.scrollTop;
+
+ // Ensure that both scroll positions are within +/- 1
+ assert_approx_equals(focusLeft, scrollLeft, 1.0,
+ "focus() inline direction is within +/- 1 of a centered scrollIntoView()");
+ assert_approx_equals(focusTop, scrollTop, 1.0,
+ "focus() block direction is within +/- 1 of a centered scrollIntoView()");
+}, "Element.focus() center in both directions");
+
+</script>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/focus/scroll-matches-focus.html b/Tests/LibWeb/Text/input/wpt-import/focus/scroll-matches-focus.html
new file mode 100644
index 00000000000..1996ac72645
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/focus/scroll-matches-focus.html
@@ -0,0 +1,47 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>:focus applies before scrolling into view</title>
+<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1775451">
+<link href="mailto:[email protected]" title="Emilio Cobos Álvarez">
+<link href="https://mozilla.com" title="Mozilla">
+<script src=../resources/testharness.js></script>
+<script src=../resources/testharnessreport.js></script>
+<style>
+body {
+ margin: 0;
+}
+.padding {
+ height: 200vh;
+ background-color: purple;
+}
+#focusable {
+ z-index: 0;
+ position: absolute;
+ top: 0;
+ left: 0;
+}
+#focusable:focus {
+ z-index: 1;
+ left: auto;
+ top: auto;
+}
+</style>
+<div class="padding"></div>
+<div id="focusable" tabindex=0>I am focusable</div>
+<div class="padding"></div>
+<script>
+onload = function() {
+ async_test(function(t) {
+ let focusable = document.getElementById("focusable");
+ assert_equals(getComputedStyle(focusable).zIndex, "0", "focusable style is correct");
+
+ window.addEventListener("scroll", t.step_func_done(function(e) {
+ assert_equals(document.activeElement, focusable, "activeElement should be set");
+ assert_true(focusable.matches(":focus"), ":focus should match by the time we scroll");
+ assert_equals(getComputedStyle(focusable).zIndex, "1", "focusable style is correct");
+ }), { once: true });
+
+ focusable.focus();
+ });
+};
+</script>