Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(layout): avoid subtracting SVG margins twice

ldm0373edf56dde0
diff --git a/moli-layout/src/replaced.rs b/moli-layout/src/replaced.rs
index 8c4cd9c23..f66059e3c 100644
--- a/moli-layout/src/replaced.rs
+++ b/moli-layout/src/replaced.rs
@@ -13,6 +13,16 @@ use taffy::{
use crate::{LayoutReplacedKind, ReplacedMetrics, style::resolve_stylo_calc_value};
+/// Which margins the caller has already removed from the available space.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum AvailableSpaceMargins {
+ Included,
+ /// Block and float parents remove physical horizontal margins only.
+ HorizontalExcluded,
+ /// Grid's final item layout removes margins in both physical axes.
+ Excluded,
+}
+
#[derive(Clone, Copy, Debug)]
pub(crate) struct ReplacedContext {
inherent_size: Size<f32>,
@@ -236,6 +246,7 @@ pub(crate) fn measure_replaced(
known_dimensions: Size<Option<f32>>,
parent_size: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
+ available_space_margins: AvailableSpaceMargins,
context: &ReplacedContext,
resolved_aspect_ratio: Option<ResolvedAspectRatio>,
style: &taffy::Style<Atom>,
@@ -381,29 +392,34 @@ pub(crate) fn measure_replaced(
.resolve_or_zero(parent_size.width, resolve_stylo_calc_value)
.sum_axes()
.get_abs(inline_axis);
- let available_inline_size =
- available_space
- .get_abs(inline_axis)
- .into_option()
- .or_else(|| {
- // Absolute layout asks for min/max-content sizes before its final
- // fit-content pass. A ratio-only SVG still stretches against its
- // definite containing block in these probes (unlike an in-flow
- // SVG inside a shrink-to-fit container, which contributes zero).
- if style.position != taffy::Position::Absolute {
- return None;
- }
- let insets = style
- .inset
- .resolve_or_zero(parent_size, resolve_stylo_calc_value);
- parent_size
- .get_abs(inline_axis)
- .map(|size| size - insets.sum_axes().get_abs(inline_axis))
- });
+ let available_margin = match available_space_margins {
+ AvailableSpaceMargins::Excluded => 0.0,
+ AvailableSpaceMargins::HorizontalExcluded if writing_mode.is_horizontal() => 0.0,
+ _ => margin,
+ };
+ let available_inline_size = available_space
+ .get_abs(inline_axis)
+ .into_option()
+ .map(|size| size - available_margin)
+ .or_else(|| {
+ // Absolute layout asks for min/max-content sizes before its final
+ // fit-content pass. A ratio-only SVG still stretches against its
+ // definite containing block in these probes (unlike an in-flow
+ // SVG inside a shrink-to-fit container, which contributes zero).
+ if style.position != taffy::Position::Absolute {
+ return None;
+ }
+ let insets = style
+ .inset
+ .resolve_or_zero(parent_size, resolve_stylo_calc_value);
+ parent_size
+ .get_abs(inline_axis)
+ // This fallback starts from the containing block, so
+ // it still owns both the insets and the SVG's margins.
+ .map(|size| size - insets.sum_axes().get_abs(inline_axis) - margin)
+ });
let inline_size = match available_inline_size {
- Some(available) => {
- (available - margin - padding_border_sum.get_abs(inline_axis)).max(0.0)
- }
+ Some(available) => (available - padding_border_sum.get_abs(inline_axis)).max(0.0),
// An unresolved percentage uses the default dimension for an
// intrinsic contribution; an auto inline size contributes zero.
None if style
@@ -605,6 +621,7 @@ mod tests {
width: AvailableSpace::MaxContent,
height: AvailableSpace::MaxContent,
},
+ AvailableSpaceMargins::Included,
&image_context(),
style
.aspect_ratio
@@ -661,6 +678,7 @@ mod tests {
width: AvailableSpace::Definite(24.0),
height: AvailableSpace::Definite(48.0)
},
+ AvailableSpaceMargins::Included,
&context,
ResolvedAspectRatio::new(2.0, BoxSizing::ContentBox),
&taffy::Style::default(),
@@ -699,6 +717,7 @@ mod tests {
width: AvailableSpace::MaxContent,
height: AvailableSpace::MaxContent,
},
+ AvailableSpaceMargins::Included,
&image_context(),
ResolvedAspectRatio::new(2.0, box_sizing),
&style,
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index dfa1ee83a..e6d92b304 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -27,7 +27,7 @@ use crate::{
VerticalStaticEdge, flex_main_axis_static_edge, physical_static_position_from_logical,
resolve_absolute_axis_margins,
},
- replaced::measure_replaced,
+ replaced::{AvailableSpaceMargins, measure_replaced},
style::{InlineDirection, resolve_stylo_calc_value},
table::{compute_table_layout, prepare_table_layout_trees},
world::InlineStaticPosition,
@@ -2045,6 +2045,32 @@ where
let resolved_aspect_ratio = layout_box.resolved_aspect_ratio();
if let Some(context) = replaced_context {
+ // In-flow block children and floats receive space after their
+ // horizontal margins. Grid's final item layout (including its
+ // two-axis abspos measurement) removes margins in both axes;
+ // its single-axis track contributions still include them.
+ // Atomic inlines and flex items use margin-inclusive space.
+ // Select by the actual parent algorithm: flex/grid items are also
+ // blockified and ignore their authored float.
+ let parent_is_grid = layout_box.layout_parent.is_some_and(|parent| {
+ self.boxes[parent.index()]
+ .style
+ .display()
+ .is_grid_container()
+ });
+ let available_space_margins =
+ if parent_is_grid && inputs.axis == taffy::RequestedAxis::Both {
+ AvailableSpaceMargins::Excluded
+ } else if style.position != taffy::Position::Absolute
+ && (box_is_effectively_floated(self, id)
+ || layout_box
+ .layout_parent
+ .is_none_or(|parent| original_parent_uses_block_layout(self, parent)))
+ {
+ AvailableSpaceMargins::HorizontalExcluded
+ } else {
+ AvailableSpaceMargins::Included
+ };
// `measure_replaced` is the complete CSS replaced-element sizing
// algorithm ported from Blitz: it resolves preferred/min/max
// sizes and returns a border-box size. Taffy's generic leaf
@@ -2056,6 +2082,7 @@ where
inputs.known_dimensions,
inputs.parent_size,
inputs.available_space,
+ available_space_margins,
&context,
resolved_aspect_ratio,
&style,
@@ -2777,20 +2804,14 @@ where
let margin = style
.margin
.resolve_or_zero(child_inputs.parent_size.width, resolve_stylo_calc_value);
- // A non-replaced float's formatting-context algorithm
- // owns its content size; pass it the slot remaining after
- // margins just like Taffy's block-float parent does. A
- // replaced leaf retains Taffy's intrinsic-size adapter,
- // which consumes the full slot and subtracts its margin.
- let layout_inputs = if self.boxes[child.index()].is_replaced() {
- child_inputs
- } else {
- LayoutInput {
- available_space: child_inputs
- .available_space
- .map_width(|width| width.maybe_sub(margin.left + margin.right)),
- ..child_inputs
- }
+ // All floats receive the space remaining after horizontal
+ // margins, matching Taffy's block-float parent. Replaced
+ // measurement carries this ownership explicitly.
+ let layout_inputs = LayoutInput {
+ available_space: child_inputs
+ .available_space
+ .map_width(|width| width.maybe_sub(margin.left + margin.right)),
+ ..child_inputs
};
let output = self.compute_child_layout(child.to_taffy(), layout_inputs);
let state = breaker.state_mut();
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/inline_svg_paint.rs b/moli-renderer-v8/src/runtime/page_vm/tests/inline_svg_paint.rs
index eba68250b..5e0faf455 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/inline_svg_paint.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/inline_svg_paint.rs
@@ -48,6 +48,17 @@ async fn screenshot_sizes_viewbox_only_svg_from_available_inline_space() {
("absolute-margin", [48, 48]),
("shrink", [0, 0]),
("shrink-percent", [300, 300]),
+ ("block-margin", [80, 80]),
+ ("block-padding-margin", [80, 43]),
+ ("block-percent-margin", [80, 80]),
+ ("block-negative-margin", [120, 120]),
+ ("block-auto-margin", [100, 100]),
+ ("block-visible-margin", [80, 80]),
+ ("float-margin", [80, 80]),
+ ("block-float-margin", [80, 80]),
+ ("inline-float-margin", [80, 80]),
+ ("flex-margin", [80, 80]),
+ ("grid-margin", [80, 80]),
] {
assert_eq!(
geometry[id],
diff --git a/moli-renderer-v8/tests/fixtures/inline-svg-auto-size.html b/moli-renderer-v8/tests/fixtures/inline-svg-auto-size.html
index af4f02fbf..70bd29eee 100644
--- a/moli-renderer-v8/tests/fixtures/inline-svg-auto-size.html
+++ b/moli-renderer-v8/tests/fixtures/inline-svg-auto-size.html
@@ -26,4 +26,15 @@ svg{vertical-align:top}
<div style="position:absolute;left:740px;top:0;width:64px"><svg id=absolute-margin style="position:absolute;left:10px;top:0;margin:0 3px" viewBox="0 0 10 10"></svg></div>
<div style="position:absolute;left:420px;top:80px"><span style="display:inline-block"><svg id=shrink viewBox="0 0 10 10"></svg></span></div>
<div style="position:absolute;left:420px;top:100px"><span style="display:inline-block"><svg id=shrink-percent width="100%" viewBox="0 0 10 10"></svg></span></div>
+<div style="position:absolute;left:200px;top:0;width:100px"><svg id=block-margin style="display:block;margin:0 10px" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:200px;top:90px;width:100px"><svg id=block-padding-margin style="display:block;margin:0 10px;border:1px solid;padding:2px" viewBox="0 0 2 1"></svg></div>
+<div style="position:absolute;left:200px;top:140px;width:100px"><svg id=block-percent-margin style="display:block;margin:0 10%" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:200px;top:230px;width:100px"><svg id=block-negative-margin style="display:block;margin:0 -10px" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:200px;top:360px;width:100px"><svg id=block-auto-margin style="display:block;margin:0 auto" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:200px;top:470px;width:100px"><svg id=block-visible-margin style="display:block;overflow:visible;margin:0 10px" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:310px;top:0;width:100px"><svg id=float-margin style="float:left;margin:0 10px" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:310px;top:90px;width:100px"><div></div><svg id=block-float-margin style="float:left;margin:0 10px" viewBox="0 0 1 1"></svg><div></div></div>
+<div style="position:absolute;left:310px;top:180px;width:100px">x<svg id=inline-float-margin style="float:left;margin:0 10px" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:310px;top:290px;width:100px;display:flex;flex-direction:column;align-items:flex-start"><svg id=flex-margin style="float:left;margin:0 10px" viewBox="0 0 1 1"></svg></div>
+<div style="position:absolute;left:310px;top:380px;width:100px;display:grid;align-items:start;justify-items:start"><svg id=grid-margin style="margin:0 10px" viewBox="0 0 1 1"></svg></div>
</body>