Skip to content

armgit / moli

Public Imported from GitHub
0

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

Fix intrinsic image sizing precision

ldm046f05183b8e5
diff --git a/moli-layout/src/replaced.rs b/moli-layout/src/replaced.rs
index 325b46fb6..6b274f0fc 100644
--- a/moli-layout/src/replaced.rs
+++ b/moli-layout/src/replaced.rs
@@ -220,7 +220,7 @@ pub(crate) fn measure_replaced(
.min_size
.maybe_resolve(parent_size, resolve_stylo_calc_value)
.maybe_sub(box_sizing_adjustment);
- let max_size = style
+ let mut max_size = style
.max_size
.maybe_resolve(preferred_basis, resolve_stylo_calc_value)
.or(available_space.into_options())
@@ -228,6 +228,38 @@ pub(crate) fn measure_replaced(
.maybe_max(min_size)
.maybe_sub(box_sizing_adjustment);
+ // A replaced element's intrinsic min/max-content constraint transfers a
+ // definite preferred size in the opposite axis through its preferred
+ // aspect ratio. It must not fall back to the resource's natural width or
+ // height. For example, a 60x60 image with `height:40px; width:30px;
+ // min-width:min-content` has a transferred min-width of 40px, not 60px.
+ // Taffy's generic horizontal intrinsic probe cannot preserve this once it
+ // 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 is_min_or_max_content(style.min_size.width) {
+ min_size.width = transferred_width;
+ }
+ if is_min_or_max_content(style.min_size.height) {
+ min_size.height = transferred_height;
+ }
+ if is_min_or_max_content(style.max_size.width) {
+ max_size.width = transferred_width;
+ }
+ if is_min_or_max_content(style.max_size.height) {
+ max_size.height = transferred_height;
+ }
+ }
+
if sizing_mode == SizingMode::ContentSize {
match requested_axis {
RequestedAxis::Horizontal => {
@@ -359,3 +391,154 @@ pub(crate) fn measure_replaced(
};
size + padding_border_sum
}
+
+fn is_min_or_max_content(dimension: taffy::Dimension) -> bool {
+ dimension.is_min_content() || dimension.is_max_content()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn image_context() -> ReplacedContext {
+ ReplacedContext::for_element(
+ LayoutReplacedKind::Image,
+ Some(ReplacedMetrics {
+ intrinsic_width: Some(60.0),
+ intrinsic_height: Some(60.0),
+ intrinsic_ratio: Some(1.0),
+ ..ReplacedMetrics::default()
+ }),
+ )
+ }
+
+ fn measure(style: &taffy::Style<Atom>) -> Size<f32> {
+ measure_replaced(
+ Size::NONE,
+ Size::NONE,
+ Size {
+ width: AvailableSpace::MaxContent,
+ height: AvailableSpace::MaxContent,
+ },
+ &image_context(),
+ style,
+ SizingMode::InherentSize,
+ RequestedAxis::Both,
+ )
+ }
+
+ #[test]
+ fn intrinsic_min_max_constraints_transfer_the_opposite_preferred_axis() {
+ let mut min_width = taffy::Style::<Atom> {
+ size: Size {
+ width: taffy::Dimension::length(30.0),
+ height: taffy::Dimension::length(40.0),
+ },
+ min_size: Size {
+ width: taffy::Dimension::min_content(),
+ height: taffy::Dimension::auto(),
+ },
+ ..taffy::Style::default()
+ };
+ assert_eq!(
+ measure(&min_width),
+ Size {
+ width: 40.0,
+ height: 40.0
+ }
+ );
+
+ min_width.min_size.width = taffy::Dimension::max_content();
+ assert_eq!(
+ measure(&min_width),
+ Size {
+ width: 40.0,
+ height: 40.0
+ }
+ );
+
+ let mut max_width = taffy::Style::<Atom> {
+ size: Size {
+ width: taffy::Dimension::length(80.0),
+ height: taffy::Dimension::length(70.0),
+ },
+ max_size: Size {
+ width: taffy::Dimension::min_content(),
+ height: taffy::Dimension::auto(),
+ },
+ ..taffy::Style::default()
+ };
+ assert_eq!(
+ measure(&max_width),
+ Size {
+ width: 70.0,
+ height: 70.0
+ }
+ );
+
+ max_width.max_size.width = taffy::Dimension::max_content();
+ assert_eq!(
+ measure(&max_width),
+ Size {
+ width: 70.0,
+ height: 70.0
+ }
+ );
+
+ let mut min_height = taffy::Style::<Atom> {
+ size: Size {
+ width: taffy::Dimension::length(40.0),
+ height: taffy::Dimension::length(30.0),
+ },
+ min_size: Size {
+ width: taffy::Dimension::auto(),
+ height: taffy::Dimension::min_content(),
+ },
+ ..taffy::Style::default()
+ };
+ assert_eq!(
+ measure(&min_height),
+ Size {
+ width: 40.0,
+ height: 40.0
+ }
+ );
+
+ min_height.min_size.height = taffy::Dimension::max_content();
+ assert_eq!(
+ measure(&min_height),
+ Size {
+ width: 40.0,
+ height: 40.0
+ }
+ );
+
+ let mut max_height = taffy::Style::<Atom> {
+ size: Size {
+ width: taffy::Dimension::length(70.0),
+ height: taffy::Dimension::length(80.0),
+ },
+ max_size: Size {
+ width: taffy::Dimension::auto(),
+ height: taffy::Dimension::min_content(),
+ },
+ ..taffy::Style::default()
+ };
+ assert_eq!(
+ measure(&max_height),
+ Size {
+ width: 70.0,
+ height: 70.0
+ }
+ );
+
+ max_height.max_size.height = taffy::Dimension::max_content();
+ assert_eq!(
+ measure(&max_height),
+ Size {
+ width: 70.0,
+ height: 70.0
+ }
+ );
+ }
+}
diff --git a/moli-layout/src/style.rs b/moli-layout/src/style.rs
index 6190c6ec7..e54c3b7c9 100644
--- a/moli-layout/src/style.rs
+++ b/moli-layout/src/style.rs
@@ -606,28 +606,17 @@ impl ResolvedLayoutStyle {
}
};
let mut taffy = stylo_taffy::to_taffy_style(&computed);
- taffy.size.width = match &position_style.width {
- GenericSize::MinContent => taffy::Dimension::min_content(),
- GenericSize::MaxContent => taffy::Dimension::max_content(),
- GenericSize::FitContent => taffy::Dimension::fit_content(),
- GenericSize::Stretch | GenericSize::WebkitFillAvailable => taffy::Dimension::stretch(),
- _ => taffy.size.width,
+ taffy.size = Size {
+ width: taffy_size_dimension(&position_style.width, taffy.size.width),
+ height: taffy_size_dimension(&position_style.height, taffy.size.height),
};
- taffy.min_size.width = match &position_style.min_width {
- GenericSize::MinContent => taffy::Dimension::min_content(),
- GenericSize::MaxContent => taffy::Dimension::max_content(),
- GenericSize::FitContent => taffy::Dimension::fit_content(),
- GenericSize::Stretch | GenericSize::WebkitFillAvailable => taffy::Dimension::stretch(),
- _ => taffy.min_size.width,
+ taffy.min_size = Size {
+ width: taffy_size_dimension(&position_style.min_width, taffy.min_size.width),
+ height: taffy_size_dimension(&position_style.min_height, taffy.min_size.height),
};
- taffy.max_size.width = match &position_style.max_width {
- GenericMaxSize::MinContent => taffy::Dimension::min_content(),
- GenericMaxSize::MaxContent => taffy::Dimension::max_content(),
- GenericMaxSize::FitContent => taffy::Dimension::fit_content(),
- GenericMaxSize::Stretch | GenericMaxSize::WebkitFillAvailable => {
- taffy::Dimension::stretch()
- }
- _ => taffy.max_size.width,
+ taffy.max_size = Size {
+ width: taffy_max_size_dimension(&position_style.max_width, taffy.max_size.width),
+ height: taffy_max_size_dimension(&position_style.max_height, taffy.max_size.height),
};
// Taffy's generic leaf algorithm transfers aspect ratios before a
// replaced-element measure callback runs. CSS Sizing 4 defines zero,
@@ -1569,6 +1558,34 @@ fn usable_aspect_ratio(ratio: Option<f32>) -> Option<f32> {
ratio.filter(|ratio| ratio.is_finite() && *ratio > 0.0)
}
+fn taffy_size_dimension(
+ size: &GenericSize<style::values::computed::NonNegativeLengthPercentage>,
+ fallback: taffy::Dimension,
+) -> taffy::Dimension {
+ match size {
+ GenericSize::MinContent => taffy::Dimension::min_content(),
+ GenericSize::MaxContent => taffy::Dimension::max_content(),
+ GenericSize::FitContent => taffy::Dimension::fit_content(),
+ GenericSize::Stretch | GenericSize::WebkitFillAvailable => taffy::Dimension::stretch(),
+ _ => fallback,
+ }
+}
+
+fn taffy_max_size_dimension(
+ size: &GenericMaxSize<style::values::computed::NonNegativeLengthPercentage>,
+ fallback: taffy::Dimension,
+) -> taffy::Dimension {
+ match size {
+ GenericMaxSize::MinContent => taffy::Dimension::min_content(),
+ GenericMaxSize::MaxContent => taffy::Dimension::max_content(),
+ GenericMaxSize::FitContent => taffy::Dimension::fit_content(),
+ GenericMaxSize::Stretch | GenericMaxSize::WebkitFillAvailable => {
+ taffy::Dimension::stretch()
+ }
+ _ => fallback,
+ }
+}
+
fn taffy_display(display: LayoutDisplay) -> TaffyDisplay {
match display {
LayoutDisplay::None => TaffyDisplay::None,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/image_resources/state.rs b/moli-renderer-v8/src/native_bridge/context_host/image_resources/state.rs
index 0e5dbea4c..36285ad08 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/image_resources/state.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/image_resources/state.rs
@@ -313,8 +313,48 @@ pub(super) fn intrinsic_dimensions(resource: &ReadyImageResource) -> (f32, f32)
} else {
1.0
};
- (
- resource.descriptor.width as f32 / density,
- resource.descriptor.height as f32 / density,
- )
+ // `ImageResponseDescriptor::{width,height}` deliberately stores integer
+ // dimensions for the HTMLImageElement naturalWidth/naturalHeight surface.
+ // SVG layout cannot reuse those rounded values: a viewBox-only 96:12 SVG
+ // has a 300x37.5 concrete object size even though the DOM reports 300x38.
+ // Preserve the metadata's fractional concrete size until the DOM getter's
+ // explicit integer conversion boundary.
+ let (width, height) = match resource.descriptor.decode_metadata {
+ super::ImageDecodeMetadata::Raster(_) => (
+ resource.descriptor.width as f32,
+ resource.descriptor.height as f32,
+ ),
+ super::ImageDecodeMetadata::Svg(metadata) => {
+ (metadata.concrete_width, metadata.concrete_height)
+ }
+ };
+ (width / density, height / density)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn svg_layout_dimensions_preserve_fractional_concrete_size() {
+ let metadata =
+ moli_image::svg_image_metadata_from_root_attributes(None, None, Some("0 0 96 12"));
+ let descriptor = ImageResponseDescriptor::svg(metadata).expect("valid SVG descriptor");
+ assert_eq!((descriptor.width, descriptor.height), (300, 38));
+
+ let resource = ReadyImageResource {
+ descriptor,
+ density: 1.0,
+ pixels: None,
+ svg: None,
+ _decoded_bytes_permit: None,
+ };
+ assert_eq!(intrinsic_dimensions(&resource), (300.0, 37.5));
+
+ let high_density_resource = ReadyImageResource {
+ density: 2.0,
+ ..resource
+ };
+ assert_eq!(intrinsic_dimensions(&high_density_resource), (150.0, 18.75));
+ }
}
diff --git a/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs b/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs
index 1923b6b21..ab0a4fe6a 100644
--- a/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs
+++ b/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs
@@ -2179,10 +2179,14 @@ fn html_image_data_svg_exposes_intrinsic_dimensions() {
const noSize = new Image();
noSize.src = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>';
+ const fractionalConcreteHeight = new Image();
+ fractionalConcreteHeight.src = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 12"/>';
+
return [
[sized.naturalWidth, sized.naturalHeight].join('x'),
[ratioWidth.naturalWidth, ratioWidth.naturalHeight].join('x'),
- [noSize.naturalWidth, noSize.naturalHeight].join('x')
+ [noSize.naturalWidth, noSize.naturalHeight].join('x'),
+ [fractionalConcreteHeight.naturalWidth, fractionalConcreteHeight.naturalHeight].join('x')
].join('|');
})()
"#,
@@ -2191,7 +2195,9 @@ fn html_image_data_svg_exposes_intrinsic_dimensions() {
// Blink resolves an external SVG with neither dimensions nor a viewBox
// against the replaced-element default object size.
- assert_eq!(result, "500x400|400x300|300x150");
+ // Its DOM natural dimensions remain integers even when layout retains the
+ // precise fractional concrete object size for aspect-ratio calculations.
+ assert_eq!(result, "500x400|400x300|300x150|300x38");
}
#[test]