Skip to content

Truly independent web browser

LibWeb: Find a view timeline’s scroller by its used overflow

sideshowbarker754514d7dc72
diff --git a/Libraries/LibWeb/Animations/ScrollTimeline.cpp b/Libraries/LibWeb/Animations/ScrollTimeline.cpp
index e9f445d7284..aeff2c077aa 100644
--- a/Libraries/LibWeb/Animations/ScrollTimeline.cpp
+++ b/Libraries/LibWeb/Animations/ScrollTimeline.cpp
@@ -55,10 +55,15 @@ GC::Ptr<DOM::Element const> ScrollTimeline::source() const
case CSS::Scroller::Nearest: {
GC::Ptr<DOM::Element const> ancestor = anonymous_source.target.parent_element();
- // NB: Elements inside a display:none subtree have no computed style. They have no box, so they
- // cannot be the nearest ancestor scroll container.
- while (ancestor && !(ancestor->has_style() && ancestor->is_scroll_container()))
+ // Whether a box is a scroll container depends on its used overflow, which is visible for a body whose
+ // overflow is propagated to the viewport. The document element stands for the viewport.
+ Layout::ForcedReadScope read { anonymous_source.target.document() };
+ while (ancestor && !ancestor->is_document_element()) {
+ auto const* layout_node = ancestor->unsafe_layout_node(read);
+ if (layout_node && layout_node->is_scroll_container())
+ break;
ancestor = ancestor->parent_element();
+ }
return ancestor;
}
@@ -69,6 +74,14 @@ GC::Ptr<DOM::Element const> ScrollTimeline::source() const
});
}
+GC::Ptr<DOM::Element const> ScrollTimeline::source_for_bindings() const
+{
+ // The nearest scroll container is the nearest one by used overflow, which layout decides.
+ if (auto const* anonymous_source = m_source.get_pointer<AnonymousSource>(); anonymous_source && anonymous_source->scroller == CSS::Scroller::Nearest)
+ anonymous_source->target.document().update_layout(DOM::UpdateLayoutReason::ScrollTimelineSource);
+ return source();
+}
+
struct ComputedScrollAxis {
bool is_vertical;
bool is_reversed;
diff --git a/Libraries/LibWeb/Animations/ScrollTimeline.h b/Libraries/LibWeb/Animations/ScrollTimeline.h
index acd056b4591..b333e142950 100644
--- a/Libraries/LibWeb/Animations/ScrollTimeline.h
+++ b/Libraries/LibWeb/Animations/ScrollTimeline.h
@@ -43,7 +43,7 @@ public:
virtual Optional<TimeValue> duration() const override { return TimeValue { TimeValue::Type::Percentage, 100 }; }
GC::Ptr<DOM::Element const> source() const;
- virtual GC::Ptr<DOM::Element const> source_for_bindings() const { return source(); }
+ virtual GC::Ptr<DOM::Element const> source_for_bindings() const;
ScrollAxis axis() const { return m_axis; }
void set_axis(ScrollAxis);
diff --git a/Libraries/LibWeb/Animations/ViewTimeline.cpp b/Libraries/LibWeb/Animations/ViewTimeline.cpp
index 90f583bd771..e4524441c55 100644
--- a/Libraries/LibWeb/Animations/ViewTimeline.cpp
+++ b/Libraries/LibWeb/Animations/ViewTimeline.cpp
@@ -116,7 +116,7 @@ GC::Ptr<DOM::Element const> ViewTimeline::source_for_bindings() const
{
// The values of subject, source, and currentTime are all computed when any of them is requested or updated.
auto subject = subject_internal();
- subject.document().update_style_for_element(subject, DOM::Document::StyleUpdateMode::OnlyIfNeeded);
+ subject.document().update_layout(DOM::UpdateLayoutReason::ViewTimelineSource);
// If the source or subject of a ViewTimeline is an element whose principal box does not exist, [...] then the
// ViewTimeline is inactive.
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index be1fc905627..a921aa07b97 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -223,6 +223,7 @@ enum class InvalidateLayoutTreeReason {
X(ResolvedCSSStyleDeclarationProperty, false) \
X(SVGDecodedImageDataRender, false) \
X(ScrollCursorIntoView, false) \
+ X(ScrollTimelineSource, false) \
X(ProcessScreenshot, false) \
X(SVGGraphicsElementGetBBox, true) \
X(SVGGraphicsElementGetCTM, false) \
@@ -230,6 +231,7 @@ enum class InvalidateLayoutTreeReason {
X(SVGLengthValue, false) \
X(SVGPathLength, false) \
X(SVGTextContentElementTextQuery, true) \
+ X(ViewTimelineSource, false) \
X(ViewTransitionCapture, false) \
X(ViewTransitionPseudoElementStyles, false) \
X(WindowScroll, false)
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 2fae304e4bf..213241808e0 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3602,19 +3602,6 @@ bool Element::is_potentially_scrollable(TreatOverflowClipOnBodyParentAsOverflowH
return true;
}
-bool Element::is_scroll_container() const
-{
- // NB: We should only call this if we know that computed_properties has already been computed
- auto reset_bits = style_reset_bits();
- VERIFY(reset_bits.has_value());
-
- if (is_document_element())
- return true;
-
- return Layout::overflow_value_makes_box_a_scroll_container(CSS::overflow_x_from_reset_bits(*reset_bits))
- || Layout::overflow_value_makes_box_a_scroll_container(CSS::overflow_y_from_reset_bits(*reset_bits));
-}
-
// https://drafts.csswg.org/cssom-view/#dom-element-scrolltop
double Element::scroll_top() const
{
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index 8abacfaaa14..d0c87b566b0 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -721,7 +721,6 @@ public:
Yes,
};
bool is_potentially_scrollable(TreatOverflowClipOnBodyParentAsOverflowHidden) const;
- bool is_scroll_container() const;
bool is_viewport_propagation_source() const;
double scroll_top() const;
diff --git a/Tests/LibWeb/Text/expected/css/view-timeline-source-skips-body-with-propagated-overflow.txt b/Tests/LibWeb/Text/expected/css/view-timeline-source-skips-body-with-propagated-overflow.txt
new file mode 100644
index 00000000000..c776efc03f6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/view-timeline-source-skips-body-with-propagated-overflow.txt
@@ -0,0 +1,3 @@
+new ViewTimeline: source HTML, currentTime 50%
+new ViewTimeline: transform matrix(1, 0, 0, 1, 100, 0)
+animation-timeline: view(): transform matrix(1, 0, 0, 1, 50, 0)
diff --git a/Tests/LibWeb/Text/expected/scroll-timeline-nearest-source-reads-updated-layout.txt b/Tests/LibWeb/Text/expected/scroll-timeline-nearest-source-reads-updated-layout.txt
new file mode 100644
index 00000000000..23b0523a7ea
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-timeline-nearest-source-reads-updated-layout.txt
@@ -0,0 +1,2 @@
+before: HTML
+after: outer
diff --git a/Tests/LibWeb/Text/input/css/view-timeline-source-skips-body-with-propagated-overflow.html b/Tests/LibWeb/Text/input/css/view-timeline-source-skips-body-with-propagated-overflow.html
new file mode 100644
index 00000000000..1432dad4490
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/view-timeline-source-skips-body-with-propagated-overflow.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ overflow-y: scroll;
+ }
+ .box {
+ height: 200px;
+ }
+ #css-subject {
+ animation: move linear both;
+ animation-timeline: view();
+ }
+ @keyframes move {
+ from {
+ transform: translateX(0px);
+ }
+ to {
+ transform: translateX(200px);
+ }
+ }
+ .fill {
+ height: 150vh;
+ }
+</style>
+<div class="fill"></div>
+<div class="box" id="subject"></div>
+<div class="box" id="css-subject"></div>
+<div class="fill"></div>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const subject = document.getElementById("subject");
+ subject.scrollIntoView({ block: "center" });
+ const timeline = new ViewTimeline({ subject, axis: "block" });
+ subject.animate({ transform: ["translateX(0px)", "translateX(200px)"] }, { timeline, fill: "both" });
+ await animationFrame();
+ await animationFrame();
+ println(`new ViewTimeline: source ${timeline.source?.tagName}, currentTime ${timeline.currentTime}`);
+ println(`new ViewTimeline: transform ${getComputedStyle(subject).transform}`);
+ println(`animation-timeline: view(): transform ${getComputedStyle(document.getElementById("css-subject")).transform}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/scroll-timeline-nearest-source-reads-updated-layout.html b/Tests/LibWeb/Text/input/scroll-timeline-nearest-source-reads-updated-layout.html
new file mode 100644
index 00000000000..52c710bbd1e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-timeline-nearest-source-reads-updated-layout.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ @keyframes grow {
+ from { width: 10px; }
+ to { width: 20px; }
+ }
+ #outer {
+ height: 100px;
+ }
+ #target {
+ height: 200px;
+ animation: grow 1s linear;
+ animation-timeline: scroll(nearest);
+ }
+</style>
+<div id="outer"><div id="target"></div></div>
+<script>
+ test(() => {
+ const timeline = document.getElementById("target").getAnimations()[0].timeline;
+ println(`before: ${timeline.source.tagName}`);
+ // The ancestor becomes the nearest scroll container; reading the source next must see that.
+ document.getElementById("outer").style.overflow = "scroll";
+ println(`after: ${timeline.source.id || timeline.source.tagName}`);
+ });
+</script>