Skip to content

Truly independent web browser

LibWeb: Scroll ranges into view

Sam Atkins57f67b5ca6ab
diff --git a/Libraries/LibUnicode/Collator.cpp b/Libraries/LibUnicode/Collator.cpp
index f9265cf6b13..dc039f445fa 100644
--- a/Libraries/LibUnicode/Collator.cpp
+++ b/Libraries/LibUnicode/Collator.cpp
@@ -165,6 +165,8 @@ static bool ignore_punctuation_for_collator(icu::Collator const& collator)
class SubstringSearcherImpl final : public Collator::SubstringSearcher {
public:
+ AK_ALLOC_WITH_KMALLOC;
+
SubstringSearcherImpl(icu::RuleBasedCollator const& collator, Utf16View const& haystack, Utf16View const& needle)
{
auto to_unicode_string = [](Utf16View const& string) {
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 308563a8502..8f14dd390f9 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -73,6 +73,7 @@
#include <LibWeb/DOM/MutationObserver.h>
#include <LibWeb/DOM/MutationType.h>
#include <LibWeb/DOM/NamedNodeMap.h>
+#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/SelectorQuery.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Text.h>
@@ -4350,7 +4351,7 @@ 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, Element& 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)
{
// 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,
@@ -4388,16 +4389,18 @@ static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead c
// 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).
- auto scroll_margin = target.computed_style()->scroll_margin();
- auto scroll_margin_top = scroll_margin.top().to_px_or_zero(CSSPixels { 0 });
- auto scroll_margin_right = scroll_margin.right().to_px_or_zero(CSSPixels { 0 });
- auto scroll_margin_bottom = scroll_margin.bottom().to_px_or_zero(CSSPixels { 0 });
- auto scroll_margin_left = scroll_margin.left().to_px_or_zero(CSSPixels { 0 });
+ if (target.has<Element*>()) {
+ auto scroll_margin = target.get<Element*>()->computed_style()->scroll_margin();
+ auto scroll_margin_top = scroll_margin.top().to_px_or_zero(CSSPixels { 0 });
+ auto scroll_margin_right = scroll_margin.right().to_px_or_zero(CSSPixels { 0 });
+ auto scroll_margin_bottom = scroll_margin.bottom().to_px_or_zero(CSSPixels { 0 });
+ auto scroll_margin_left = scroll_margin.left().to_px_or_zero(CSSPixels { 0 });
- target_bounding_border_box.set_top(target_bounding_border_box.top() - scroll_margin_top);
- target_bounding_border_box.set_right(target_bounding_border_box.right() + scroll_margin_left + scroll_margin_right);
- target_bounding_border_box.set_bottom(target_bounding_border_box.bottom() + scroll_margin_top + scroll_margin_bottom);
- target_bounding_border_box.set_left(target_bounding_border_box.left() - scroll_margin_left);
+ target_bounding_border_box.set_top(target_bounding_border_box.top() - scroll_margin_top);
+ target_bounding_border_box.set_right(target_bounding_border_box.right() + scroll_margin_left + scroll_margin_right);
+ target_bounding_border_box.set_bottom(target_bounding_border_box.bottom() + scroll_margin_top + scroll_margin_bottom);
+ target_bounding_border_box.set_left(target_bounding_border_box.left() - scroll_margin_left);
+ }
// 2. Let scrolling box edge A be the beginning edge in the block flow direction of scrolling box, and
// let element edge A be target bounding border box’s edge on the same physical side as that of
@@ -4518,8 +4521,20 @@ static CSSPixelPoint determine_the_scroll_into_view_position(Layout::BegunRead c
}
// https://drafts.csswg.org/cssom-view-1/#scroll-a-target-into-view
-static void scroll_an_element_into_view(Layout::BegunRead const& read, Element& 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)
{
+ auto target_document = [](ScrollIntoViewTarget current_target) -> Document& {
+ return current_target.visit(
+ [](Element* element) -> Document& { return element->document(); },
+ [](Range* range) -> Document& { return range->start_container()->document(); });
+ };
+
+ // AD-HOC: This algorithm is also invoked by internal navigation and parser tasks, which do
+ // not necessarily have a JavaScript execution context. The scrolling algorithms create
+ // promises even when their caller does not request one, so provide the target Document's
+ // relevant settings object while those promises are created and resolved.
+ HTML::TemporaryExecutionContext temporary_execution_context { target_document(target).relevant_settings_object() };
+
// 1. Let ancestorPromises be an empty set of Promises.
GC::RootVector<GC::Ref<WebIDL::Promise>> ancestor_promises;
@@ -4535,29 +4550,54 @@ static void scroll_an_element_into_view(Layout::BegunRead const& read, Element&
auto const* containing_block = layout_node->containing_block();
return containing_block && containing_block->is_viewport();
};
- Vector<Node&> scrolling_boxes;
- if (!is_fixed_to_the_viewport(target)) {
- for (auto* ancestor = target.parent(); ancestor; ancestor = ancestor->parent()) {
- auto const* ancestor_layout_node = ancestor->layout_node(read);
- if (ancestor_layout_node && Painting::has_committed_box(*ancestor_layout_node) && Painting::has_scrollable_overflow(*ancestor_layout_node))
- scrolling_boxes.append(*ancestor);
- if (is_fixed_to_the_viewport(*ancestor))
- break;
- }
- }
+ auto current_target = target;
+ bool reached_container = false;
+
+ auto target_is_fixed_to_the_viewport = [&](ScrollIntoViewTarget current_target) {
+ return current_target.visit(
+ [&](Element* element) {
+ for (Node const* node = element; node; node = node->flat_tree_parent()) {
+ if (is_fixed_to_the_viewport(*node))
+ return true;
+ }
+ return false;
+ },
+ [&](Range* range) {
+ for (auto* node = range->start_container().ptr(); node; node = node->flat_tree_parent()) {
+ if (is_fixed_to_the_viewport(*node))
+ return true;
+ }
+ return false;
+ });
+ };
- auto target_bounding_border_box = target.get_bounding_client_rect();
+ auto get_target_bounding_border_box = [](ScrollIntoViewTarget current_target) {
+ return current_target.visit(
+ [](Element* element) {
+ return element->get_bounding_client_rect();
+ },
+ [](Range* range) {
+ auto rect = range->get_bounding_client_rect();
+ return CSSPixelRect {
+ CSSPixels::nearest_value_for(rect->x()),
+ CSSPixels::nearest_value_for(rect->y()),
+ CSSPixels::nearest_value_for(rect->width()),
+ CSSPixels::nearest_value_for(rect->height()),
+ };
+ });
+ };
+ auto target_bounding_border_box = get_target_bounding_border_box(current_target);
- for (auto& scrolling_box : scrolling_boxes) {
+ auto scroll_scrolling_box = [&](Node& scrolling_box) {
// 1. If the Document associated with target is not same origin with the Document associated with the element
// or viewport associated with scrolling box, abort any remaining iteration of this loop.
- if (target.document().origin() != scrolling_box.document().origin())
- break;
+ if (target_document(current_target).origin() != scrolling_box.document().origin())
+ return IterationDecision::Break;
// 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, 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);
// 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
@@ -4572,10 +4612,16 @@ static void scroll_an_element_into_view(Layout::BegunRead const& read, Element&
}
}
- // 3. If position is not the same as scrolling box’s current scroll position, or scrolling box has an ongoing
- // smooth scroll,
- // FIXME: Actually check this condition.
- if (true) {
+ // 3. If position is not the same as scrolling box's current scroll position, then:
+ auto current_scroll_position = [&] {
+ if (scrolling_box.is_document()) {
+ auto& document = static_cast<Document&>(scrolling_box);
+ return document.navigable()->viewport_scroll_offset() + document.visual_viewport()->offset();
+ }
+ auto const* layout_node = scrolling_box.layout_node(read);
+ return layout_node ? Painting::scroll_offset(*layout_node) : CSSPixelPoint {};
+ }();
+ if (position != current_scroll_position) {
// -> If scrolling box is associated with an element
if (auto* element = as_if<Element>(scrolling_box)) {
if (auto navigable = element->document().navigable())
@@ -4597,8 +4643,48 @@ static void scroll_an_element_into_view(Layout::BegunRead const& read, Element&
// or is a viewport whose document is a shadow-including inclusive ancestor of container, abort any
// remaining iteration of this loop.
// NB: Our viewports *are* Documents in the DOM, so both checks are equivalent.
- if (container != nullptr && scrolling_box.is_shadow_including_inclusive_ancestor_of(*container))
+ if (container != nullptr && scrolling_box.is_shadow_including_inclusive_ancestor_of(*container)) {
+ reached_container = true;
+ return IterationDecision::Break;
+ }
+
+ return IterationDecision::Continue;
+ };
+
+ for (;;) {
+ auto* ancestor = current_target.visit(
+ [](Element* element) -> Node* { return element->flat_tree_parent(); },
+ [](Range* range) -> Node* { return range->common_ancestor_container().ptr(); });
+
+ auto const target_is_fixed = target_is_fixed_to_the_viewport(current_target);
+ for (; ancestor && !ancestor->is_document(); ancestor = ancestor->flat_tree_parent()) {
+ auto const* ancestor_layout_node = ancestor->layout_node(read);
+ if (ancestor_layout_node && Painting::has_committed_box(*ancestor_layout_node) && Painting::has_scrollable_overflow(*ancestor_layout_node)) {
+ if (scroll_scrolling_box(*ancestor) == IterationDecision::Break)
+ break;
+ }
+ if (is_fixed_to_the_viewport(*ancestor))
+ break;
+ }
+ if (reached_container)
+ break;
+
+ auto& document = target_document(current_target);
+ if (!target_is_fixed && scroll_scrolling_box(document) == IterationDecision::Break)
break;
+
+ // Element scrolling retains CSSOM View's per-document behavior. Text-fragment ranges
+ // additionally bubble through same-origin iframe containers to bring the match into view.
+ if (current_target.has<Element*>())
+ break;
+
+ auto navigable = document.navigable();
+ auto container_element = navigable ? navigable->container() : nullptr;
+ if (!container_element || document.origin() != container_element->document().origin())
+ break;
+
+ current_target = ScrollIntoViewTarget { container_element.ptr() };
+ target_bounding_border_box = get_target_bounding_border_box(current_target);
}
// 4. Return scrollPromise, and run the remaining steps in parallel.
@@ -4647,7 +4733,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_an_element_into_view(read, *this, behavior, block, inline_, container, promise);
+ scroll_a_target_into_view(read, ScrollIntoViewTarget { this }, behavior, block, inline_, container, promise);
// 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 a9753bae2f9..ffd80fd08e0 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -95,6 +95,10 @@ WEB_API WebIDL::ExceptionOr<void> set_attribute_ns(DOM::Element&, Optional<Utf16
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);
+
// 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
struct CustomElementUpgradeReaction {