diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index a305a316c5..616ba89cbe 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -943,11 +943,9 @@ double Element::scroll_top() const if (!layout_node() || !is(layout_node())) return 0.0; - auto const* box = static_cast(layout_node()); - // 9. Return the y-coordinate of the scrolling area at the alignment point with the top of the padding edge of the element. // FIXME: Is this correct? - return box->scroll_offset().y().to_double(); + return paintable_box()->scroll_offset().y().to_double(); } double Element::scroll_left() const @@ -985,11 +983,9 @@ double Element::scroll_left() const if (!layout_node() || !is(layout_node())) return 0.0; - auto const* box = static_cast(layout_node()); - // 9. Return the x-coordinate of the scrolling area at the alignment point with the left of the padding edge of the element. // FIXME: Is this correct? - return box->scroll_offset().x().to_double(); + return paintable_box()->scroll_offset().x().to_double(); } // https://drafts.csswg.org/cssom-view/#dom-element-scrollleft @@ -1056,7 +1052,7 @@ void Element::set_scroll_left(double x) // 11. Scroll the element to x,scrollTop, with the scroll behavior being "auto". // FIXME: Implement this in terms of calling "scroll the element". - auto scroll_offset = box->scroll_offset(); + auto scroll_offset = paintable_box()->scroll_offset(); scroll_offset.set_x(static_cast(x)); box->set_scroll_offset(scroll_offset); } @@ -1124,7 +1120,7 @@ void Element::set_scroll_top(double y) // 11. Scroll the element to scrollLeft,y, with the scroll behavior being "auto". // FIXME: Implement this in terms of calling "scroll the element". - auto scroll_offset = box->scroll_offset(); + auto scroll_offset = paintable_box()->scroll_offset(); scroll_offset.set_y(static_cast(y)); box->set_scroll_offset(scroll_offset); } diff --git a/Userland/Libraries/LibWeb/Layout/Box.cpp b/Userland/Libraries/LibWeb/Layout/Box.cpp index 11c3044fc2..ef556dfe1e 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.cpp +++ b/Userland/Libraries/LibWeb/Layout/Box.cpp @@ -61,23 +61,10 @@ bool Box::is_scrollable() const return computed_values().overflow_y() == CSS::Overflow::Scroll; } -CSSPixelPoint Box::scroll_offset() const -{ - if (is_generated_for_before_pseudo_element()) - return pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore); - if (is_generated_for_after_pseudo_element()) - return pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter); - - if (!is(*dom_node())) - return {}; - - return static_cast(dom_node())->scroll_offset(DOM::Element::ScrollOffsetFor::Self); -} - void Box::set_scroll_offset(CSSPixelPoint offset) { // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset - if (offset.y() < 0 || scroll_offset() == offset) + if (offset.y() < 0 || paintable_box()->scroll_offset() == offset) return; if (is_generated_for_before_pseudo_element()) { diff --git a/Userland/Libraries/LibWeb/Layout/Box.h b/Userland/Libraries/LibWeb/Layout/Box.h index c54c551bc3..c46e1dc57c 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.h +++ b/Userland/Libraries/LibWeb/Layout/Box.h @@ -54,7 +54,6 @@ public: bool is_scroll_container() const; bool is_scrollable() const; - CSSPixelPoint scroll_offset() const; void set_scroll_offset(CSSPixelPoint); protected: diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp index cba8554846..5241589ef8 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -57,6 +57,20 @@ PaintableWithLines::~PaintableWithLines() { } +CSSPixelPoint PaintableBox::scroll_offset() const +{ + auto const& node = layout_node(); + if (node.is_generated_for_before_pseudo_element()) + return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore); + if (node.is_generated_for_after_pseudo_element()) + return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter); + + if (!is(*dom_node())) + return {}; + + return static_cast(dom_node())->scroll_offset(DOM::Element::ScrollOffsetFor::Self); +} + void PaintableBox::scroll_by(int delta_x, int delta_y) { auto scrollable_overflow_rect = this->scrollable_overflow_rect(); @@ -64,7 +78,7 @@ void PaintableBox::scroll_by(int delta_x, int delta_y) return; auto max_x_offset = scrollable_overflow_rect->width() - content_size().width(); auto max_y_offset = scrollable_overflow_rect->height() - content_size().height(); - auto current_offset = layout_box().scroll_offset(); + auto current_offset = scroll_offset(); auto new_offset_x = clamp(current_offset.x() + delta_x, 0, max_x_offset); auto new_offset_y = clamp(current_offset.y() + delta_y, 0, max_y_offset); layout_box().set_scroll_offset({ new_offset_x, new_offset_y }); @@ -610,7 +624,7 @@ void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const // FIXME: Handle overflow-x and overflow-y being different values. auto clip_box = context.rounded_device_rect(absolute_padding_box_rect()); context.painter().add_clip_rect(clip_box.to_type()); - auto scroll_offset = context.rounded_device_point(static_cast(layout_box()).scroll_offset()); + auto scroll_offset = context.rounded_device_point(this->scroll_offset()); context.painter().translate(-scroll_offset.to_type()); auto border_radii = normalized_border_radii_data(ShrinkRadiiForBorders::Yes); diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.h b/Userland/Libraries/LibWeb/Painting/PaintableBox.h index 626a55a11c..065359be10 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.h +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.h @@ -38,6 +38,7 @@ public: CSSPixelRect absolute_rect() const; CSSPixelPoint effective_offset() const; + CSSPixelPoint scroll_offset() const; void scroll_by(int delta_x, int delta_y); void set_offset(CSSPixelPoint); diff --git a/Userland/Libraries/LibWeb/Painting/StackingContext.cpp b/Userland/Libraries/LibWeb/Painting/StackingContext.cpp index 3a2bea00af..f616047656 100644 --- a/Userland/Libraries/LibWeb/Painting/StackingContext.cpp +++ b/Userland/Libraries/LibWeb/Painting/StackingContext.cpp @@ -29,7 +29,7 @@ static void paint_node(Layout::Node const& layout_node, PaintContext& context, P if (auto const* paintable = layout_node.paintable()) { if (paintable->containing_block() && paintable->containing_block()->is_scrollable()) { Gfx::PainterStateSaver saver(context.painter()); - auto scroll_offset = -paintable->containing_block()->scroll_offset(); + auto scroll_offset = -paintable->containing_block()->paintable_box()->scroll_offset(); context.painter().translate({ context.enclosing_device_pixels(scroll_offset.x()), context.enclosing_device_pixels(scroll_offset.y()) }); paintable->paint(context, phase); } else {