Skip to content

armgit / moli

Public Imported from GitHub
0

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

Preserve preferred aspect ratio semantics

ldm059daf682c98e
diff --git a/moli-layout/src/builder.rs b/moli-layout/src/builder.rs
index 70a621bf3..5419a4641 100644
--- a/moli-layout/src/builder.rs
+++ b/moli-layout/src/builder.rs
@@ -16,7 +16,7 @@ use crate::{
LayoutAnonymousReason, LayoutBoxId, LayoutBoxKind, LayoutCapabilityDiagnostic, LayoutDisplay,
LayoutElementCategory, LayoutElementSemantics, LayoutError, LayoutPseudo, LayoutSource,
LayoutSourceKind, LayoutStyleResolver, LayoutWorld, ResolvedLayoutStyle,
- style::InlineWhiteSpaceCollapse,
+ replaced::ReplacedContext, style::InlineWhiteSpaceCollapse,
};
use style::values::generics::image::GenericImage;
@@ -91,7 +91,7 @@ where
root_style.force_display_none();
}
if root_semantics.is_replaced() {
- root_style.mark_replaced();
+ root_style.mark_replaced(natural_replaced_ratio(&root_semantics, root_metrics));
} else if matches!(
root_semantics.category,
crate::LayoutElementCategory::FormControl(crate::LayoutFormControlKind::Button)
@@ -214,7 +214,7 @@ where
};
let metrics = self.source.replaced_metrics(source_node);
if semantics.is_replaced() {
- style.mark_replaced();
+ style.mark_replaced(natural_replaced_ratio(&semantics, metrics));
} else if matches!(
semantics.category,
crate::LayoutElementCategory::FormControl(crate::LayoutFormControlKind::Button)
@@ -1178,6 +1178,23 @@ where
}
}
+fn natural_replaced_ratio(
+ semantics: &LayoutElementSemantics,
+ metrics: Option<crate::ReplacedMetrics>,
+) -> Option<f32> {
+ let sizing_kind = if matches!(
+ semantics.category,
+ crate::LayoutElementCategory::FormControl(crate::LayoutFormControlKind::Input(
+ crate::LayoutInputControlKind::Image
+ ))
+ ) {
+ crate::LayoutReplacedKind::Image
+ } else {
+ semantics.replaced?
+ };
+ ReplacedContext::for_element(sizing_kind, metrics).inherent_ratio()
+}
+
fn whitespace_text_is_ignorable(text: &str, mode: InlineWhiteSpaceCollapse) -> bool {
if text.is_empty() {
return true;
diff --git a/moli-layout/src/replaced.rs b/moli-layout/src/replaced.rs
index 8aa26d5b2..27430ced9 100644
--- a/moli-layout/src/replaced.rs
+++ b/moli-layout/src/replaced.rs
@@ -11,7 +11,10 @@ use taffy::{
ResolveOrZero as _, Size, SizingMode,
};
-use crate::{LayoutReplacedKind, ReplacedMetrics, style::resolve_stylo_calc_value};
+use crate::{
+ LayoutReplacedKind, ReplacedMetrics,
+ style::{ResolvedAspectRatio, resolve_stylo_calc_value},
+};
#[derive(Clone, Copy, Debug)]
pub(crate) struct ReplacedContext {
@@ -113,6 +116,10 @@ impl ReplacedContext {
inherent_ratio: None,
}
}
+
+ pub(crate) const fn inherent_ratio(&self) -> Option<f32> {
+ self.inherent_ratio
+ }
}
fn concrete_object_size(
@@ -167,11 +174,69 @@ enum Violation {
Max,
}
+fn content_width_from_height(
+ height: f32,
+ aspect_ratio: ResolvedAspectRatio,
+ padding_border: Size<f32>,
+) -> Option<f32> {
+ let ratio = aspect_ratio.ratio?;
+ let width = match aspect_ratio.box_sizing {
+ BoxSizing::ContentBox => height * ratio,
+ BoxSizing::BorderBox => (height + padding_border.height) * ratio - padding_border.width,
+ };
+ width.is_finite().then_some(width.max(0.0))
+}
+
+fn content_height_from_width(
+ width: f32,
+ aspect_ratio: ResolvedAspectRatio,
+ padding_border: Size<f32>,
+) -> Option<f32> {
+ let ratio = aspect_ratio.ratio?;
+ let height = match aspect_ratio.box_sizing {
+ BoxSizing::ContentBox => width / ratio,
+ BoxSizing::BorderBox => (width + padding_border.width) / ratio - padding_border.height,
+ };
+ height.is_finite().then_some(height.max(0.0))
+}
+
+fn apply_aspect_ratio_to_content_size(
+ size: Size<Option<f32>>,
+ aspect_ratio: ResolvedAspectRatio,
+ padding_border: Size<f32>,
+) -> Size<Option<f32>> {
+ match size {
+ Size {
+ width: Some(width),
+ height: None,
+ } => Size {
+ width: Some(width),
+ height: content_height_from_width(width, aspect_ratio, padding_border),
+ },
+ Size {
+ width: None,
+ height: Some(height),
+ } => Size {
+ width: content_width_from_height(height, aspect_ratio, padding_border),
+ height: Some(height),
+ },
+ _ => size,
+ }
+}
+
+fn ratio_basis_scale(constrained: f32, original: f32, inset: f32, box_sizing: BoxSizing) -> f32 {
+ match box_sizing {
+ BoxSizing::ContentBox => constrained / original,
+ BoxSizing::BorderBox => (constrained + inset) / (original + inset),
+ }
+}
+
pub(crate) fn measure_replaced(
known_dimensions: Size<Option<f32>>,
parent_size: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
context: &ReplacedContext,
+ resolved_aspect_ratio: ResolvedAspectRatio,
style: &taffy::Style<Atom>,
sizing_mode: SizingMode,
requested_axis: RequestedAxis,
@@ -192,14 +257,10 @@ pub(crate) fn measure_replaced(
} else {
Size::ZERO
};
- // CSS Sizing 4 makes zero, infinite and NaN ratios degenerate. Filter the
- // author ratio before falling back to the resource's intrinsic ratio so
- // `aspect-ratio: 0 / 1` behaves like `auto` on a replaced element.
- let is_usable_ratio = |ratio: &f32| ratio.is_finite() && *ratio > 0.0;
- let aspect_ratio = style
- .aspect_ratio
- .filter(is_usable_ratio)
- .or(context.inherent_ratio.filter(is_usable_ratio));
+ // The browser-owned style seam has already resolved the three CSS states
+ // (`auto`, `<ratio>`, and `auto <ratio>`) against the natural ratio. Do not
+ // reconstruct that precedence from Taffy's lossy numeric field here.
+ let aspect_ratio = resolved_aspect_ratio.ratio;
let preferred_basis = Size {
width: if available_space.width == AvailableSpace::MinContent {
Some(0.0)
@@ -239,15 +300,13 @@ pub(crate) fn measure_replaced(
// reaches the complete replaced-element measure callback, and it has no
// vertical intrinsic-keyword resolver, so resolve both physical axes at
// this browser-owned sizing boundary.
- if let Some(ratio) = aspect_ratio {
- let transferred_width = preferred_size
- .height
- .map(|height| height * ratio)
- .filter(|width| width.is_finite() && *width >= 0.0);
- let transferred_height = preferred_size
- .width
- .map(|width| width / ratio)
- .filter(|height| height.is_finite() && *height >= 0.0);
+ if aspect_ratio.is_some() {
+ let transferred_width = preferred_size.height.and_then(|height| {
+ content_width_from_height(height, resolved_aspect_ratio, padding_border_sum)
+ });
+ let transferred_height = preferred_size.width.and_then(|width| {
+ content_height_from_width(width, resolved_aspect_ratio, padding_border_sum)
+ });
if is_min_or_max_content(style.min_size.width) {
min_size.width = transferred_width;
}
@@ -283,23 +342,30 @@ pub(crate) fn measure_replaced(
.maybe_sub(box_sizing_adjustment)
.maybe_max(min_size);
let content_known = known_dimensions.maybe_sub(padding_border_sum);
- let transferred = content_known
- .maybe_clamp(min_size, style_max_size)
- .maybe_apply_aspect_ratio(aspect_ratio)
- .unwrap_or(context.inherent_size);
+ let transferred = apply_aspect_ratio_to_content_size(
+ content_known.maybe_clamp(min_size, style_max_size),
+ resolved_aspect_ratio,
+ padding_border_sum,
+ )
+ .unwrap_or(context.inherent_size);
let size = content_known.unwrap_or(transferred.maybe_clamp(min_size, style_max_size));
return size.map(|value| value.max(0.0)) + padding_border_sum;
}
let unclamped = if preferred_size.width.is_some() || preferred_size.height.is_some() {
- preferred_size
- .maybe_apply_aspect_ratio(aspect_ratio)
- .unwrap_or(context.inherent_size)
+ apply_aspect_ratio_to_content_size(
+ preferred_size,
+ resolved_aspect_ratio,
+ padding_border_sum,
+ )
+ .unwrap_or(context.inherent_size)
} else if context.attribute_size.width.is_some() || context.attribute_size.height.is_some() {
- context
- .attribute_size
- .maybe_apply_aspect_ratio(aspect_ratio)
- .unwrap_or(context.inherent_size)
+ apply_aspect_ratio_to_content_size(
+ context.attribute_size,
+ resolved_aspect_ratio,
+ padding_border_sum,
+ )
+ .unwrap_or(context.inherent_size)
} else {
context.inherent_size
};
@@ -318,51 +384,80 @@ pub(crate) fn measure_replaced(
} else {
Violation::None
};
- let Some(aspect_ratio) = aspect_ratio else {
+ let Some(_) = aspect_ratio else {
return size.maybe_clamp(min_size, max_size) + padding_border_sum;
};
- let inverse_ratio = 1.0 / aspect_ratio;
let size = match (width_violation, height_violation) {
(Violation::None, Violation::None) => size,
(Violation::Max, Violation::None) => {
let width = max_size.width.expect("max-width violation has a bound");
Size {
width,
- height: (width * inverse_ratio).maybe_max(min_size.height),
+ height: content_height_from_width(width, resolved_aspect_ratio, padding_border_sum)
+ .expect("a resolved ratio transfers width to height")
+ .maybe_max(min_size.height),
}
}
(Violation::Min, Violation::None) => {
let width = min_size.width.expect("min-width violation has a bound");
Size {
width,
- height: (width * inverse_ratio).maybe_min(max_size.height),
+ height: content_height_from_width(width, resolved_aspect_ratio, padding_border_sum)
+ .expect("a resolved ratio transfers width to height")
+ .maybe_min(max_size.height),
}
}
(Violation::None, Violation::Max) => {
let height = max_size.height.expect("max-height violation has a bound");
Size {
- width: (height * aspect_ratio).maybe_max(min_size.width),
+ width: content_width_from_height(height, resolved_aspect_ratio, padding_border_sum)
+ .expect("a resolved ratio transfers height to width")
+ .maybe_max(min_size.width),
height,
}
}
(Violation::None, Violation::Min) => {
let height = min_size.height.expect("min-height violation has a bound");
Size {
- width: (height * aspect_ratio).maybe_min(max_size.width),
+ width: content_width_from_height(height, resolved_aspect_ratio, padding_border_sum)
+ .expect("a resolved ratio transfers height to width")
+ .maybe_min(max_size.width),
height,
}
}
(Violation::Max, Violation::Max) => {
let width = max_size.width.expect("max-width violation has a bound");
let height = max_size.height.expect("max-height violation has a bound");
- if width / size.width <= height / size.height {
+ if ratio_basis_scale(
+ width,
+ size.width,
+ padding_border_sum.width,
+ resolved_aspect_ratio.box_sizing,
+ ) <= ratio_basis_scale(
+ height,
+ size.height,
+ padding_border_sum.height,
+ resolved_aspect_ratio.box_sizing,
+ ) {
Size {
width,
- height: (width * inverse_ratio).maybe_max(min_size.height),
+ height: content_height_from_width(
+ width,
+ resolved_aspect_ratio,
+ padding_border_sum,
+ )
+ .expect("a resolved ratio transfers width to height")
+ .maybe_max(min_size.height),
}
} else {
Size {
- width: (height * aspect_ratio).maybe_max(min_size.width),
+ width: content_width_from_height(
+ height,
+ resolved_aspect_ratio,
+ padding_border_sum,
+ )
+ .expect("a resolved ratio transfers height to width")
+ .maybe_max(min_size.width),
height,
}
}
@@ -370,15 +465,37 @@ pub(crate) fn measure_replaced(
(Violation::Min, Violation::Min) => {
let width = min_size.width.expect("min-width violation has a bound");
let height = min_size.height.expect("min-height violation has a bound");
- if width / size.width <= height / size.height {
+ if ratio_basis_scale(
+ width,
+ size.width,
+ padding_border_sum.width,
+ resolved_aspect_ratio.box_sizing,
+ ) <= ratio_basis_scale(
+ height,
+ size.height,
+ padding_border_sum.height,
+ resolved_aspect_ratio.box_sizing,
+ ) {
Size {
- width: (height * aspect_ratio).maybe_min(max_size.width),
+ width: content_width_from_height(
+ height,
+ resolved_aspect_ratio,
+ padding_border_sum,
+ )
+ .expect("a resolved ratio transfers height to width")
+ .maybe_min(max_size.width),
height,
}
} else {
Size {
width,
- height: (width * inverse_ratio).maybe_min(max_size.height),
+ height: content_height_from_width(
+ width,
+ resolved_aspect_ratio,
+ padding_border_sum,
+ )
+ .expect("a resolved ratio transfers width to height")
+ .maybe_min(max_size.height),
}
}
}
@@ -423,12 +540,71 @@ mod tests {
height: AvailableSpace::MaxContent,
},
&image_context(),
+ ResolvedAspectRatio {
+ ratio: style
+ .aspect_ratio
+ .filter(|ratio| ratio.is_finite() && *ratio > 0.0)
+ .or(Some(1.0)),
+ box_sizing: style.box_sizing,
+ },
style,
SizingMode::InherentSize,
RequestedAxis::Both,
)
}
+ #[test]
+ fn preferred_ratio_uses_its_selected_box_sizing_basis() {
+ let style = taffy::Style::<Atom> {
+ box_sizing: BoxSizing::BorderBox,
+ size: Size {
+ width: taffy::Dimension::length(100.0),
+ height: taffy::Dimension::auto(),
+ },
+ padding: taffy::Rect {
+ left: taffy::LengthPercentage::length(10.0),
+ right: taffy::LengthPercentage::length(10.0),
+ top: taffy::LengthPercentage::length(10.0),
+ bottom: taffy::LengthPercentage::length(10.0),
+ },
+ aspect_ratio: Some(2.0),
+ ..taffy::Style::default()
+ };
+ let measure_with_basis = |box_sizing| {
+ measure_replaced(
+ Size::NONE,
+ Size::NONE,
+ Size {
+ width: AvailableSpace::MaxContent,
+ height: AvailableSpace::MaxContent,
+ },
+ &image_context(),
+ ResolvedAspectRatio {
+ ratio: Some(2.0),
+ box_sizing,
+ },
+ &style,
+ SizingMode::InherentSize,
+ RequestedAxis::Both,
+ )
+ };
+
+ assert_eq!(
+ measure_with_basis(BoxSizing::BorderBox),
+ Size {
+ width: 100.0,
+ height: 50.0,
+ }
+ );
+ assert_eq!(
+ measure_with_basis(BoxSizing::ContentBox),
+ Size {
+ width: 100.0,
+ height: 60.0,
+ }
+ );
+ }
+
#[test]
fn intrinsic_min_max_constraints_transfer_the_opposite_preferred_axis() {
let mut min_width = taffy::Style::<Atom> {
diff --git a/moli-layout/src/style.rs b/moli-layout/src/style.rs
index fff3969af..6d7e9d57c 100644
--- a/moli-layout/src/style.rs
+++ b/moli-layout/src/style.rs
@@ -34,6 +34,7 @@ use style::{
grid::GenericGridTemplateComponent,
image::GenericImage,
length::{GenericMaxSize, GenericSize},
+ position::PreferredRatio,
transform::{Rotate, Scale, Translate},
},
specified::box_::{DisplayInside, DisplayOutside},
@@ -43,7 +44,7 @@ use style::{
},
},
};
-use taffy::{Display as TaffyDisplay, Position as TaffyPosition, Size, Style};
+use taffy::{BoxSizing, Display as TaffyDisplay, Position as TaffyPosition, Size, Style};
use crate::{
LayoutPoint, LayoutRect, LayoutTransform2D, PaintBlendMode, PaintBorderColors,
@@ -155,6 +156,73 @@ pub(crate) struct InlineVerticalAlign {
pub(crate) baseline_shift: f32,
}
+/// The two independent components retained from CSS `aspect-ratio`.
+///
+/// Taffy's public style currently stores only the numeric ratio. Keeping the
+/// `auto` component here is essential for replaced elements: `1 / 1` replaces
+/// an image's natural ratio, while `auto 1 / 1` uses that natural ratio and
+/// only falls back to 1:1 when no natural ratio exists.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub(crate) enum PreferredAspectRatio {
+ #[default]
+ Auto,
+ Ratio(f32),
+ AutoAndRatio(f32),
+}
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub(crate) struct ResolvedAspectRatio {
+ pub(crate) ratio: Option<f32>,
+ /// The box whose dimensions the ratio constrains.
+ pub(crate) box_sizing: BoxSizing,
+}
+
+impl PreferredAspectRatio {
+ fn from_components(auto: bool, ratio: Option<f32>) -> Self {
+ match (auto, usable_aspect_ratio(ratio)) {
+ (_, None) => Self::Auto,
+ (false, Some(ratio)) => Self::Ratio(ratio),
+ (true, Some(ratio)) => Self::AutoAndRatio(ratio),
+ }
+ }
+
+ fn from_taffy(ratio: Option<f32>) -> Self {
+ Self::from_components(false, ratio)
+ }
+
+ fn numeric_ratio(self) -> Option<f32> {
+ match self {
+ Self::Auto => None,
+ Self::Ratio(ratio) | Self::AutoAndRatio(ratio) => Some(ratio),
+ }
+ }
+
+ fn resolve_for_replaced(
+ self,
+ inherent_ratio: Option<f32>,
+ authored_box_sizing: BoxSizing,
+ ) -> ResolvedAspectRatio {
+ let inherent_ratio = usable_aspect_ratio(inherent_ratio);
+ match self {
+ Self::Ratio(ratio) => ResolvedAspectRatio {
+ ratio: Some(ratio),
+ box_sizing: authored_box_sizing,
+ },
+ Self::Auto => ResolvedAspectRatio {
+ ratio: inherent_ratio,
+ box_sizing: BoxSizing::ContentBox,
+ },
+ // Blink's BoxSizingForAspectRatio() uses content-box for the
+ // combined `auto <ratio>` value, including when its ratio is used
+ // as the fallback because no natural ratio is available.
+ Self::AutoAndRatio(fallback) => ResolvedAspectRatio {
+ ratio: inherent_ratio.or(Some(fallback)),
+ box_sizing: BoxSizing::ContentBox,
+ },
+ }
+ }
+}
+
/// CSS display classification retained across box construction.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LayoutDisplay {
@@ -330,6 +398,7 @@ impl ResolvedLayoutTransform {
pub struct ResolvedLayoutStyle {
pub(crate) computed: Option<ServoArc<ComputedValues>>,
pub(crate) taffy: Style<Atom>,
+ preferred_aspect_ratio: PreferredAspectRatio,
display: LayoutDisplay,
background_color: PaintColor,
border_colors: PaintBorderColors,
@@ -380,6 +449,7 @@ impl std::fmt::Debug for ResolvedLayoutStyle {
.debug_struct("ResolvedLayoutStyle")
.field("has_computed_values", &self.computed.is_some())
.field("display", &self.display)
+ .field("preferred_aspect_ratio", &self.preferred_aspect_ratio)
.field("background_color", &self.background_color)
.field("border_colors", &self.border_colors)
.field("generated_content", &self.generated_content)
@@ -601,6 +671,14 @@ impl ResolvedLayoutStyle {
LayoutListMarkerPosition::Outside
}
};
+ let specified_aspect_ratio = match position_style.aspect_ratio.ratio {
+ PreferredRatio::None => None,
+ PreferredRatio::Ratio(ratio) => Some(ratio.0.0 / ratio.1.0),
+ };
+ let preferred_aspect_ratio = PreferredAspectRatio::from_components(
+ position_style.aspect_ratio.auto,
+ specified_aspect_ratio,
+ );
let mut taffy = stylo_taffy::to_taffy_style(&computed);
taffy.size = Size {
width: taffy_size_dimension(&position_style.width, taffy.size.width),
@@ -618,7 +696,7 @@ impl ResolvedLayoutStyle {
// replaced-element measure callback runs. CSS Sizing 4 defines zero,
// infinite and NaN ratios as degenerate, so normalize them at the
// Stylo/Taffy seam instead of relying only on replaced measurement.
- taffy.aspect_ratio = usable_aspect_ratio(taffy.aspect_ratio);
+ taffy.aspect_ratio = preferred_aspect_ratio.numeric_ratio();
if matches!(position_style.flex_basis, GenericFlexBasis::Content) {
// Blitz's fixed stylo_taffy revision predates Taffy's typed
// `content` flex-basis. Preserve the Stylo distinction here so
@@ -657,6 +735,7 @@ impl ResolvedLayoutStyle {
Self {
computed: Some(computed),
taffy,
+ preferred_aspect_ratio,
display,
background_color,
border_colors,
@@ -707,6 +786,7 @@ impl ResolvedLayoutStyle {
background_color: PaintColor,
) -> Self {
taffy.aspect_ratio = usable_aspect_ratio(taffy.aspect_ratio);
+ let preferred_aspect_ratio = PreferredAspectRatio::from_taffy(taffy.aspect_ratio);
taffy.display = taffy_display(display);
taffy.item_is_table = matches!(display, LayoutDisplay::Table | LayoutDisplay::InlineTable);
let overflow_clips = taffy.overflow.x != taffy::Overflow::Visible
@@ -721,6 +801,7 @@ impl ResolvedLayoutStyle {
Self {
computed: None,
taffy,
+ preferred_aspect_ratio,
display,
background_color,
border_colors: PaintBorderColors::all(PaintColor::BLACK),
@@ -1279,6 +1360,7 @@ impl ResolvedLayoutStyle {
bottom: zero_length,
};
placeholder.taffy.aspect_ratio = None;
+ placeholder.preferred_aspect_ratio = PreferredAspectRatio::Auto;
placeholder.background_color = PaintColor::TRANSPARENT;
placeholder.border_colors = PaintBorderColors::all(PaintColor::TRANSPARENT);
placeholder.generated_content = GeneratedContent::None;
@@ -1421,6 +1503,7 @@ impl ResolvedLayoutStyle {
display: TaffyDisplay::Block,
..Style::default()
},
+ preferred_aspect_ratio: PreferredAspectRatio::Auto,
display: LayoutDisplay::Inline,
background_color: PaintColor::TRANSPARENT,
border_colors: PaintBorderColors::default(),
@@ -1478,6 +1561,7 @@ impl ResolvedLayoutStyle {
display: taffy_display(display),
..Style::default()
},
+ preferred_aspect_ratio: PreferredAspectRatio::Auto,
display,
background_color: PaintColor::TRANSPARENT,
border_colors: PaintBorderColors::default(),
@@ -1537,8 +1621,20 @@ impl ResolvedLayoutStyle {
}
}
- pub(crate) fn mark_replaced(&mut self) {
+ pub(crate) fn mark_replaced(&mut self, inherent_ratio: Option<f32>) {
self.taffy.item_is_replaced = true;
+ self.taffy.aspect_ratio = self
+ .preferred_aspect_ratio
+ .resolve_for_replaced(inherent_ratio, self.taffy.box_sizing)
+ .ratio;
+ }
+
+ pub(crate) fn resolved_replaced_aspect_ratio(
+ &self,
+ inherent_ratio: Option<f32>,
+ ) -> ResolvedAspectRatio {
+ self.preferred_aspect_ratio
+ .resolve_for_replaced(inherent_ratio, self.taffy.box_sizing)
}
pub(crate) fn mark_intrinsic_form_control_container(&mut self) {
@@ -1975,3 +2071,26 @@ pub(crate) fn resolve_stylo_calc_value(calc_ptr: *const (), parent_size: f32) ->
let calc = unsafe { &*(calc_ptr as *const CalcLengthPercentage) };
calc.resolve(CSSPixelLength::new(parent_size)).px()
}
+
+#[cfg(test)]
+mod aspect_ratio_tests {
+ use super::*;
+
+ #[test]
+ fn replaced_ratio_resolution_preserves_auto_precedence_and_box_basis() {
+ let specified =
+ PreferredAspectRatio::Ratio(1.0).resolve_for_replaced(Some(2.0), BoxSizing::BorderBox);
+ assert_eq!(specified.ratio, Some(1.0));
+ assert_eq!(specified.box_sizing, BoxSizing::BorderBox);
+
+ let natural = PreferredAspectRatio::AutoAndRatio(1.0)
+ .resolve_for_replaced(Some(2.0), BoxSizing::BorderBox);
+ assert_eq!(natural.ratio, Some(2.0));
+ assert_eq!(natural.box_sizing, BoxSizing::ContentBox);
+
+ let fallback = PreferredAspectRatio::AutoAndRatio(1.0)
+ .resolve_for_replaced(None, BoxSizing::BorderBox);
+ assert_eq!(fallback.ratio, Some(1.0));
+ assert_eq!(fallback.box_sizing, BoxSizing::ContentBox);
+ }
+}
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index 8dbee65f8..aca68804c 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -1531,11 +1531,15 @@ where
// adds CSS padding and borders itself. Routing the complete
// replaced result through that helper would therefore apply the
// box model twice (most visibly on padded form controls).
+ let resolved_aspect_ratio = layout_box
+ .style
+ .resolved_replaced_aspect_ratio(context.inherent_ratio());
let size = measure_replaced(
inputs.known_dimensions,
inputs.parent_size,
inputs.available_space,
&context,
+ resolved_aspect_ratio,
&style,
inputs.sizing_mode,
inputs.axis,