Skip to content

Truly independent web browser

LibWeb: Answer scrollTop and scrollLeft reads from clean layout

Andreas Klingeaa563240fdd
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index fc74ee8efa8..2e7123d8cc6 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -2330,7 +2330,8 @@ void Document::update_layout_if_needed_for_node(Node const& node, UpdateLayoutRe
// would find no work to do. A geometry read also seals the before-change style that a later CSS
// transition starts from, but with no style input pending there is nothing left to seal, so a
// document that declares transitions does not have to give up its clean reads over it either.
- if (reads_layout_geometry && is_clean_for_layout_geometry_read(read) && embedding_document_chain_is_clean())
+ // A re-snap only scrolls the document it is pending in, so it does not stop the reads of embedded documents.
+ if (reads_layout_geometry && is_clean_for_layout_geometry_read(read) && !has_pending_scroll_container_resnap() && embedding_document_chain_is_clean())
return;
auto* document_element = this->document_element();
@@ -2343,6 +2344,7 @@ void Document::update_layout_if_needed_for_node(Node const& node, UpdateLayoutRe
&& !has_size_containers_needing_evaluation_after_layout(read)
&& m_elements_with_pending_top_layer_membership_change.is_empty()
&& !m_top_layer_needs_layout_zone_rebuild
+ && !has_pending_scroll_container_resnap()
&& !style_computer().style_engine().css_transitions_may_observe_style_changes()
&& !may_have_style_query_dependencies) {
if (embedding_document_chain_is_clean()) {
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index f418b516ffd..44290bdba8e 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -148,8 +148,8 @@ enum class InvalidateLayoutTreeReason {
X(ElementScroll, false) \
X(ElementScrollHeight, false) \
X(ElementScrollIntoView, false) \
- X(ElementScrollLeft, false) \
- X(ElementScrollTop, false) \
+ X(ElementScrollLeft, true) \
+ X(ElementScrollTop, true) \
X(ElementScrollWidth, false) \
X(ElementSetScrollLeft, false) \
X(ElementSetScrollTop, false) \
@@ -1382,6 +1382,8 @@ public:
void schedule_scroll_container_resnap() { m_needs_scroll_container_resnap = true; }
void cancel_scheduled_scroll_container_resnap() { m_needs_scroll_container_resnap = false; }
[[nodiscard]] bool needs_scroll_container_resnap() const { return m_needs_scroll_container_resnap; }
+ // A re-snap is scheduled after every layout change, but only one with snap areas to re-snap can move a scroll offset.
+ [[nodiscard]] bool has_pending_scroll_container_resnap() const { return m_needs_scroll_container_resnap && m_may_have_scroll_snap_areas; }
void set_may_have_scroll_snap_areas() { m_may_have_scroll_snap_areas = true; }
[[nodiscard]] bool may_have_scroll_snap_areas() const { return m_may_have_scroll_snap_areas; }
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 1f261a87231..5ff5b12d23d 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3707,7 +3707,7 @@ double Element::scroll_top() const
// NOTE: Ensure that layout is up-to-date before looking at metrics.
Layout::ForcedReadScope read { document };
- const_cast<Document&>(document).update_layout(UpdateLayoutReason::ElementScrollTop);
+ const_cast<Document&>(document).update_layout_if_needed_for_node(*this, UpdateLayoutReason::ElementScrollTop);
// 6. If the element is the root element return the value of scrollY on window.
if (document.document_element() == this)
@@ -3753,7 +3753,7 @@ double Element::scroll_left() const
// NOTE: Ensure that layout is up-to-date before looking at metrics.
Layout::ForcedReadScope read { document };
- const_cast<Document&>(document).update_layout(UpdateLayoutReason::ElementScrollLeft);
+ const_cast<Document&>(document).update_layout_if_needed_for_node(*this, UpdateLayoutReason::ElementScrollLeft);
// 6. If the element is the root element return the value of scrollX on window.
if (document.document_element() == this)
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index 8021e7355ef..6fb8752daea 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -1001,11 +1001,16 @@ fn generate_property_metadata(manifest_dir: &Path, out_dir: &Path) -> Result<(),
};
// Animation controls can select or retime an animation of any property, so their own
// direct layout metadata cannot bound the geometry they expose. Likewise, container-name
- // can change which descendant container queries apply without changing its own box, and
- // color can change SVG bounds when stroke resolves currentColor.
+ // can change which descendant container queries apply without changing its own box,
+ // color can change SVG bounds when stroke resolves currentColor, and the scroll-snap
+ // properties can re-snap a scroll container, which moves every box inside it. A test in
+ // property_metadata.rs checks this against property_may_resnap_scroll_container().
let may_affect_layout_geometry_indirectly = property_field(name, "style-group")
.is_some_and(|value| value.as_str() == Some("AnimationValues"))
- || matches!(name.as_str(), "color" | "container-name");
+ || matches!(
+ name.as_str(),
+ "color" | "container-name" | "scroll-snap-align" | "scroll-snap-stop" | "scroll-snap-type"
+ );
layout_geometry_effects[index] = metadata_flag("affects-layout", true)
|| metadata_flag("affects-accumulated-visual-contexts", false)
|| metadata_flag("affects-scrollable-overflow", false)
diff --git a/Libraries/LibWeb/Rust/src/css/property_metadata.rs b/Libraries/LibWeb/Rust/src/css/property_metadata.rs
index 6eb87e93034..8489f0c39fe 100644
--- a/Libraries/LibWeb/Rust/src/css/property_metadata.rs
+++ b/Libraries/LibWeb/Rust/src/css/property_metadata.rs
@@ -139,6 +139,25 @@ pub fn property_may_affect_layout_geometry(property_id: u16) -> bool {
PROPERTY_MAY_AFFECT_LAYOUT_GEOMETRY[longhand_index(property_id)]
}
+/// Whether changing a property may move where a scroll container snaps, besides the properties
+/// which affect its scrollable overflow.
+pub(crate) fn property_may_resnap_scroll_container(property_id: u16) -> bool {
+ matches!(
+ property_id,
+ property_id::SCROLL_SNAP_TYPE
+ | property_id::SCROLL_SNAP_ALIGN
+ | property_id::SCROLL_SNAP_STOP
+ | property_id::SCROLL_MARGIN_TOP
+ | property_id::SCROLL_MARGIN_RIGHT
+ | property_id::SCROLL_MARGIN_BOTTOM
+ | property_id::SCROLL_MARGIN_LEFT
+ | property_id::SCROLL_PADDING_TOP
+ | property_id::SCROLL_PADDING_RIGHT
+ | property_id::SCROLL_PADDING_BOTTOM
+ | property_id::SCROLL_PADDING_LEFT
+ )
+}
+
pub(crate) fn property_affects_layout(property_id: u16) -> bool {
PROPERTY_AFFECTS_LAYOUT[longhand_index(property_id)]
}
@@ -427,6 +446,20 @@ mod tests {
assert!(property_may_affect_layout_geometry(property_id::CUSTOM));
}
+ #[test]
+ fn resnapping_properties_affect_layout_geometry() {
+ // A re-snap moves a scroll offset, and with it every box in the scroll container.
+ for property_id in FIRST_LONGHAND_PROPERTY_ID..=LAST_LONGHAND_PROPERTY_ID {
+ if property_may_resnap_scroll_container(property_id) {
+ assert!(
+ property_may_affect_layout_geometry(property_id),
+ "{}",
+ property_name(property_id)
+ );
+ }
+ }
+ }
+
#[test]
fn exposes_property_keyword_metadata() {
assert!(property_accepted_keywords(property_id::APPEARANCE).contains(&keyword::NONE));
diff --git a/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs
index 7b797a0d748..4603c8e6bc6 100644
--- a/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs
@@ -572,20 +572,7 @@ fn property_invalidation(property: u16, old: ComputedValuesView<'_>, new: Comput
result.ensure_level(INVALIDATION_RELAYOUT);
}
if property_metadata::property_affects_scrollable_overflow(property)
- || matches!(
- property,
- property_id::SCROLL_SNAP_TYPE
- | property_id::SCROLL_SNAP_ALIGN
- | property_id::SCROLL_SNAP_STOP
- | property_id::SCROLL_MARGIN_TOP
- | property_id::SCROLL_MARGIN_RIGHT
- | property_id::SCROLL_MARGIN_BOTTOM
- | property_id::SCROLL_MARGIN_LEFT
- | property_id::SCROLL_PADDING_TOP
- | property_id::SCROLL_PADDING_RIGHT
- | property_id::SCROLL_PADDING_BOTTOM
- | property_id::SCROLL_PADDING_LEFT
- )
+ || property_metadata::property_may_resnap_scroll_container(property)
{
result.resnap_scroll_container = true;
}