Skip to content

Truly independent web browser

LibWeb: Keep the committed layout of SVG shapes nothing changed

Andreas Kling1f360db42797
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 3a8319885f7..b2f22f27d6e 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -633,6 +633,9 @@ pub(super) struct SvgFormattingContext<'pass> {
quirks_mode_percentage_basis_block_size: Option<CssPixels>,
viewport_width: CssPixels,
viewport_height: CssPixels,
+ /// Whether this context's box is an <svg> viewport whose view box and size are the ones it was
+ /// last committed with, so that a shape below it resolves against the viewport it did then.
+ viewport_is_committed_viewport: bool,
current_text_position: FfiFloatPoint,
}
@@ -652,6 +655,7 @@ impl<'pass> SvgFormattingContext<'pass> {
quirks_mode_percentage_basis_block_size: None,
viewport_width: CssPixels::default(),
viewport_height: CssPixels::default(),
+ viewport_is_committed_viewport: false,
current_text_position: FfiFloatPoint::default(),
}
}
@@ -1774,6 +1778,24 @@ impl<'pass> SvgFormattingContext<'pass> {
},
|view_box| CssPixels::nearest_value_for(view_box.height),
);
+ self.viewport_is_committed_viewport = kind == NodeKind::SVGSVGBox
+ && self.run.purpose == formatting_context::LayoutPurpose::Commit
+ && self.run.layout_mode == LayoutMode::Normal
+ && crate::painting::paint_read::GeometryRead::with_committed_fragment_link(
+ self.run.callbacks.arena(),
+ self.run.box_,
+ |link| {
+ link.is_some_and(|link| {
+ let committed = link.fragment.svg.as_deref().copied().unwrap_or_default();
+ committed.view_box == attributes.has_active_view_box.then_some(attributes.active_view_box)
+ && committed.viewport_size
+ == Some(FfiCssPixelSize {
+ width: used.content_inline_size.get(),
+ height: used.content_block_size.get(),
+ })
+ })
+ },
+ );
self.available_space = Some(input.available_space);
self.quirks_mode_percentage_basis_block_size = input
.containing_block_constraints
@@ -1885,6 +1907,9 @@ impl<'pass> SvgFormattingContext<'pass> {
fn layout_graphics_element(&mut self, run: &FormattingContextRun<'pass>, graphics_box: Node, input: LayoutInput) {
self.create_used_values(graphics_box);
let facts = self.svg_facts(graphics_box);
+ if self.place_unchanged_committed_shape(graphics_box, facts) {
+ return;
+ }
self.commit_svg_element_facts(graphics_box, facts, self.svg_attributes(graphics_box));
let kind = self.node_kind(graphics_box);
@@ -1917,6 +1942,67 @@ impl<'pass> SvgFormattingContext<'pass> {
}
}
+ /// Places a shape with the fragment it last committed, where laying it out again would produce that fragment: the
+ /// shape has no boxes below it, nothing marked it for layout since that commit, and it resolves against the viewport
+ /// it was committed in. Then neither this pass nor the commit redoes a shape that a change to a sibling did not
+ /// touch. Answers whether it placed the shape.
+ fn place_unchanged_committed_shape(&self, shape: Node, facts: SvgElementFacts) -> bool {
+ if !self.viewport_is_committed_viewport
+ || self.node_kind(shape) != NodeKind::SVGGeometryBox
+ || !self.first_child(shape).is_invalid()
+ {
+ return false;
+ }
+ // An anchor registers itself only when its box is placed with a fragment of its own.
+ if NodeFacts::new(&self.run.callbacks, shape).has_anchor_names() {
+ return false;
+ }
+ // A line's coordinates are attribute lengths resolved here against the initial containing block, the root font
+ // and size query containers, and a change to any of those does not mark the line for layout.
+ if self.svg_attributes(shape).geometry_kind == SVG_GEOMETRY_KIND_LINE {
+ return false;
+ }
+ let Some(fragments) = self.run.fragments.as_deref() else {
+ return false;
+ };
+ // A committed fragment stops being current when something marks its box for layout.
+ let Some(fragment) = self
+ .run
+ .callbacks
+ .arena()
+ .with_current_committed_fragment(shape, |fragment| fragment.clone())
+ else {
+ return false;
+ };
+ if fragment.computed_svg_path.is_none() {
+ return false;
+ }
+ let Some(committed_offset) = crate::painting::paint_read::GeometryRead::with_committed_fragment_link(
+ self.run.callbacks.arena(),
+ shape,
+ |link| link.map(|link| link.committed_offset),
+ ) else {
+ return false;
+ };
+ // The transforms and the stroke's percentage basis are what the shape's facts take from outside its own
+ // geometry.
+ let committed = fragment.svg.as_deref().copied().unwrap_or_default();
+ if committed.element_transform != (!facts.element_transform.is_identity()).then_some(facts.element_transform)
+ || committed.additional_element_transform
+ != (!facts.additional_element_transform.is_identity()).then_some(facts.additional_element_transform)
+ || committed.viewport_percentage_basis != facts.viewport_percentage_basis
+ {
+ return false;
+ }
+ let used = self.used_values(shape);
+ used.set_box_metrics_from_fragment(&fragment);
+ used.has_definite_inline_size.set(true);
+ used.has_definite_block_size.set(true);
+ fragments.hold_committed_fragment_of_empty_box(used, fragment);
+ self.place_child(shape, committed_offset.x, committed_offset.y);
+ true
+ }
+
fn layout_path_like_element(
&mut self,
run: &FormattingContextRun<'pass>,
diff --git a/Tests/LibWeb/Text/expected/SVG/svg-unchanged-shapes-follow-their-viewport-during-relayout.txt b/Tests/LibWeb/Text/expected/SVG/svg-unchanged-shapes-follow-their-viewport-during-relayout.txt
new file mode 100644
index 00000000000..6f6bd40b0bf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/svg-unchanged-shapes-follow-their-viewport-during-relayout.txt
@@ -0,0 +1,15 @@
+sized-rect: 200x50
+transposed-rect: 50x100
+transposed-view-box-rect: 100x100
+view-boxed-rect: 40x40
+first: 11x11
+second: 11x11
+third: 20x20
+first: 11x11
+second: 20x20
+third: 20x20
+second: 11x11
+anchored moved: 0,0
+anchor moved: 0,0
+relative-rect moved: 0,0
+grouped-rect: 20x20
diff --git a/Tests/LibWeb/Text/input/SVG/svg-unchanged-shapes-follow-their-viewport-during-relayout.html b/Tests/LibWeb/Text/input/SVG/svg-unchanged-shapes-follow-their-viewport-during-relayout.html
new file mode 100644
index 00000000000..8880b8ca991
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/svg-unchanged-shapes-follow-their-viewport-during-relayout.html
@@ -0,0 +1,117 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .wide {
+ stroke-width: 10px;
+ }
+</style>
+<svg id="sized" width="200" height="100">
+ <rect id="sized-rect" width="50%" height="50%" />
+ <path id="sized-sibling" d="M0,0L10,10" stroke="black" />
+</svg>
+<svg id="transposed" width="200" height="100">
+ <rect id="transposed-rect" width="50%" height="50%" />
+ <path id="transposed-sibling" d="M0,0L10,10" stroke="black" />
+</svg>
+<svg id="transposed-view-box" width="200" height="200" viewBox="0 0 100 50" preserveAspectRatio="none">
+ <rect id="transposed-view-box-rect" width="50%" height="50%" />
+ <path id="transposed-view-box-sibling" d="M0,0L10,10" stroke="black" />
+</svg>
+<svg id="view-boxed" width="200" height="200" viewBox="0 0 100 100">
+ <rect id="view-boxed-rect" width="10" height="10" />
+ <path id="view-boxed-sibling" d="M0,0L10,10" stroke="black" />
+</svg>
+<svg id="siblings" width="200" height="100">
+ <path id="first" d="M10,10L20,20" stroke="black" />
+ <path id="second" d="M30,10L40,20" stroke="black" />
+ <circle id="third" cx="70" cy="20" r="10" />
+</svg>
+<svg id="anchoring" width="200" height="100">
+ <rect id="anchor" x="20" y="10" width="30" height="20" style="anchor-name: --shape" />
+ <path id="anchor-sibling" d="M0,0L10,10" stroke="black" />
+</svg>
+<div id="anchored" style="position: absolute; position-anchor: --shape; left: anchor(right); top: anchor(bottom); width: 5px; height: 5px"></div>
+<svg id="relative" width="200" height="100">
+ <rect id="relative-rect" width="10" height="10" style="position: relative; left: 10px; top: 10px" />
+ <path id="relative-sibling" d="M0,0L10,10" stroke="black" />
+</svg>
+<svg width="200" height="100">
+ <g id="group">
+ <rect id="grouped-rect" width="10" height="10" />
+ <path id="grouped-sibling" d="M0,0L10,10" stroke="black" />
+ </g>
+</svg>
+<script>
+ function box(id) {
+ const rect = document.getElementById(id).getBoundingClientRect();
+ return `${id}: ${rect.width}x${rect.height}`;
+ }
+
+ test(() => {
+ const lines = [];
+ document.body.offsetWidth;
+
+ // The viewport a percentage resolves against grows while a sibling changes.
+ document.getElementById("sized").setAttribute("width", "400");
+ document.getElementById("sized-sibling").classList.add("wide");
+ document.body.offsetWidth;
+ lines.push(box("sized-rect"));
+
+ // The viewport turns on its side while a sibling changes. Its diagonal, which a percentage stroke width resolves
+ // against, stays the same.
+ document.getElementById("transposed").setAttribute("width", "100");
+ document.getElementById("transposed").setAttribute("height", "200");
+ document.getElementById("transposed-sibling").classList.add("wide");
+ document.body.offsetWidth;
+ lines.push(box("transposed-rect"));
+
+ // The same, for a view box.
+ document.getElementById("transposed-view-box").setAttribute("viewBox", "0 0 50 100");
+ document.getElementById("transposed-view-box-sibling").classList.add("wide");
+ document.body.offsetWidth;
+ lines.push(box("transposed-view-box-rect"));
+
+ // The view box shrinks while a sibling changes.
+ document.getElementById("view-boxed").setAttribute("viewBox", "0 0 50 50");
+ document.getElementById("view-boxed-sibling").classList.add("wide");
+ document.body.offsetWidth;
+ lines.push(box("view-boxed-rect"));
+
+ // Only one shape changes, and its siblings keep their boxes.
+ const before = [box("first"), box("second"), box("third")];
+ document.getElementById("second").classList.add("wide");
+ document.body.offsetWidth;
+ lines.push(...before, box("first"), box("second"), box("third"));
+ document.getElementById("second").classList.remove("wide");
+ document.body.offsetWidth;
+ lines.push(box("second"));
+
+ // An element anchored to a shape keeps its anchor while a sibling changes.
+ const anchoredBefore = document.getElementById("anchored").getBoundingClientRect();
+ const anchorBefore = document.getElementById("anchor").getBoundingClientRect();
+ document.getElementById("anchor-sibling").classList.add("wide");
+ document.body.offsetWidth;
+ const anchoredAfter = document.getElementById("anchored").getBoundingClientRect();
+ const anchorAfter = document.getElementById("anchor").getBoundingClientRect();
+ lines.push(`anchored moved: ${anchoredAfter.x - anchoredBefore.x},${anchoredAfter.y - anchoredBefore.y}`);
+ lines.push(`anchor moved: ${anchorAfter.x - anchorBefore.x},${anchorAfter.y - anchorBefore.y}`);
+
+ // A relatively positioned shape stays put while a sibling changes.
+ const relativeBefore = document.getElementById("relative-rect").getBoundingClientRect();
+ document.getElementById("relative-sibling").classList.add("wide");
+ document.body.offsetWidth;
+ document.getElementById("relative-sibling").classList.remove("wide");
+ document.body.offsetWidth;
+ const relativeAfter = document.getElementById("relative-rect").getBoundingClientRect();
+ lines.push(`relative-rect moved: ${relativeAfter.x - relativeBefore.x},${relativeAfter.y - relativeBefore.y}`);
+
+ // A group's transform moves the shapes it holds while one of them changes.
+ document.getElementById("group").setAttribute("transform", "scale(2)");
+ document.getElementById("grouped-sibling").classList.add("wide");
+ document.body.offsetWidth;
+ lines.push(box("grouped-rect"));
+
+ for (const line of lines)
+ println(line);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/display_list/painted-as-stacking-context-capture-svg-mask-area-change.html b/Tests/LibWeb/Text/input/display_list/painted-as-stacking-context-capture-svg-mask-area-change.html
index 4ea1eddc58d..20534807bde 100644
--- a/Tests/LibWeb/Text/input/display_list/painted-as-stacking-context-capture-svg-mask-area-change.html
+++ b/Tests/LibWeb/Text/input/display_list/painted-as-stacking-context-capture-svg-mask-area-change.html
@@ -30,7 +30,7 @@ displayListTest({
mutate: () => { m.setAttribute("width", "200"); },
check: equal => { equal(document.elementFromPoint(50, 50), masked, "expanded mask includes hit"); },
expect: `
- damage: rows=8 moved=0 order=0 eligibility=0 all=false
+ damage: rows=7 moved=0 order=0 eligibility=0 all=false
@canvas RECORD (empty)
@viewport ASSEMBLE
@viewport/preamble COPY
diff --git a/Tests/LibWeb/Text/input/display_list/svg-resource-mask-content-change.html b/Tests/LibWeb/Text/input/display_list/svg-resource-mask-content-change.html
index 51aab13c8db..8786e84d4b9 100644
--- a/Tests/LibWeb/Text/input/display_list/svg-resource-mask-content-change.html
+++ b/Tests/LibWeb/Text/input/display_list/svg-resource-mask-content-change.html
@@ -32,7 +32,7 @@ displayListTest({
name: "widen the mask content",
mutate: () => { document.getElementById("mask-rect").setAttribute("width", "200"); },
expect: `
- damage: rows=9 moved=0 order=0 eligibility=0 all=false
+ damage: rows=8 moved=0 order=0 eligibility=0 all=false
@canvas RECORD (empty)
@viewport ASSEMBLE
@viewport/preamble COPY