Skip to content

armgit / moli

Public Imported from GitHub
0

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

30.5 KB
Raw Blame
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// The sizing algorithm is adapted from DioxusLabs/blitz commit
// 5081c65811a4396f5a99b2e0aca542a4a4a6606f,
// packages/blitz-dom/src/layout/replaced.rs. It operates only on pass-local,
// DOM-neutral metrics and does not fetch or decode image content.

use style::Atom;
use taffy::{
    AvailableSpace, BoxSizing, CoreStyle as _, MaybeMath, MaybeResolve, RequestedAxis,
    ResolveOrZero as _, ResolvedAspectRatio, Size, SizingMode,
};

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>,
    attribute_size: Size<Option<f32>>,
    inherent_ratio: Option<f32>,
    svg_without_intrinsic_size: bool,
}

impl ReplacedContext {
    pub(crate) fn for_element(kind: LayoutReplacedKind, metrics: Option<ReplacedMetrics>) -> Self {
        let mut metrics = metrics.unwrap_or_default();
        metrics.intrinsic_width = valid_dimension(metrics.intrinsic_width);
        metrics.intrinsic_height = valid_dimension(metrics.intrinsic_height);
        metrics.attribute_width = valid_dimension(metrics.attribute_width);
        metrics.attribute_height = valid_dimension(metrics.attribute_height);
        metrics.intrinsic_ratio = metrics
            .intrinsic_ratio
            .filter(|ratio| ratio.is_finite() && *ratio > 0.0);
        let default_size = match kind {
            LayoutReplacedKind::FormControl => Size {
                width: 160.0,
                height: 24.0,
            },
            // An HTML image without available natural dimensions represents
            // no content. Blink and Blitz therefore use 0x0 here; the CSS
            // 300x150 default object size still applies to the other replaced
            // element categories below.
            LayoutReplacedKind::Image => Size::ZERO,
            LayoutReplacedKind::Svg
            | LayoutReplacedKind::Canvas
            | LayoutReplacedKind::Embedded
            | LayoutReplacedKind::Frame
            | LayoutReplacedKind::Media => Size {
                width: 300.0,
                height: 150.0,
            },
        };

        // Canvas width/height attributes define the intrinsic bitmap
        // coordinate space independently: specifying only width does not
        // scale the default 150px height. Inline SVG metrics are already
        // normalized by the source adapter because a viewBox can provide an
        // intrinsic ratio even when one or both dimensions are absent.
        if kind == LayoutReplacedKind::Canvas {
            metrics.intrinsic_width = metrics.attribute_width.or(Some(default_size.width));
            metrics.intrinsic_height = metrics.attribute_height.or(Some(default_size.height));
            metrics.attribute_width = None;
            metrics.attribute_height = None;
        }
        let inherent_ratio = metrics
            .intrinsic_ratio
            .or_else(|| {
                metrics
                    .intrinsic_width
                    .zip(metrics.intrinsic_height)
                    .filter(|(_, height)| *height > 0.0)
                    .map(|(width, height)| width / height)
            })
            .or_else(|| {
                // HTML dimension attributes provide an aspect-ratio hint for
                // image-like replaced elements before decoded pixels exist.
                matches!(kind, LayoutReplacedKind::Image | LayoutReplacedKind::Media)
                    .then(|| {
                        metrics
                            .attribute_width
                            .zip(metrics.attribute_height)
                            .filter(|(_, height)| *height > 0.0)
                            .map(|(width, height)| width / height)
                    })
                    .flatten()
            });
        let inherent_size = concrete_object_size(
            metrics.intrinsic_width,
            metrics.intrinsic_height,
            inherent_ratio,
            default_size,
        );
        let attribute_size = Size {
            width: metrics.attribute_width,
            height: metrics.attribute_height,
        };
        // Canvas dimensions define its intrinsic coordinate space even while
        // its pixels remain an unavailable placeholder in Phase 4.
        let inherent_ratio = inherent_ratio.or_else(|| {
            (kind == LayoutReplacedKind::Canvas)
                .then_some(inherent_size.width / inherent_size.height)
        });
        Self {
            inherent_size,
            attribute_size,
            inherent_ratio,
            // Keep this distinction after computing the fallback object size.
            // A viewBox supplies an aspect ratio, not natural dimensions.
            svg_without_intrinsic_size: kind == LayoutReplacedKind::Svg
                && metrics.intrinsic_width.is_none()
                && metrics.intrinsic_height.is_none(),
        }
    }

    pub(crate) fn form_control(size: Size<f32>) -> Self {
        Self {
            inherent_size: size,
            attribute_size: Size::NONE,
            inherent_ratio: None,
            svg_without_intrinsic_size: false,
        }
    }

    pub(crate) const fn inherent_ratio(&self) -> Option<f32> {
        self.inherent_ratio
    }
}

fn concrete_object_size(
    width: Option<f32>,
    height: Option<f32>,
    ratio: Option<f32>,
    default_size: Size<f32>,
) -> Size<f32> {
    match (width, height, ratio) {
        (Some(width), Some(height), _) => Size { width, height },
        (Some(width), None, Some(ratio)) => Size {
            width,
            height: width / ratio,
        },
        (Some(width), None, None) => Size {
            width,
            height: default_size.height,
        },
        (None, Some(height), Some(ratio)) => Size {
            width: height * ratio,
            height,
        },
        (None, Some(height), None) => Size {
            width: default_size.width,
            height,
        },
        (None, None, Some(ratio)) if default_size.width > 0.0 && default_size.height > 0.0 => {
            let width_at_default_height = default_size.height * ratio;
            if width_at_default_height <= default_size.width {
                Size {
                    width: width_at_default_height,
                    height: default_size.height,
                }
            } else {
                Size {
                    width: default_size.width,
                    height: default_size.width / ratio,
                }
            }
        }
        (None, None, _) => default_size,
    }
}

fn valid_dimension(value: Option<f32>) -> Option<f32> {
    value.filter(|value| value.is_finite() && *value >= 0.0)
}

enum Violation {
    None,
    Min,
    Max,
}

fn content_width_from_height(
    height: f32,
    aspect_ratio: ResolvedAspectRatio,
    padding_border: Size<f32>,
) -> Option<f32> {
    apply_aspect_ratio_to_content_size(
        Size {
            width: None,
            height: Some(height),
        },
        Some(aspect_ratio),
        padding_border,
    )
    .width
}

fn content_height_from_width(
    width: f32,
    aspect_ratio: ResolvedAspectRatio,
    padding_border: Size<f32>,
) -> Option<f32> {
    apply_aspect_ratio_to_content_size(
        Size {
            width: Some(width),
            height: None,
        },
        Some(aspect_ratio),
        padding_border,
    )
    .height
}

fn apply_aspect_ratio_to_content_size(
    size: Size<Option<f32>>,
    aspect_ratio: Option<ResolvedAspectRatio>,
    padding_border: Size<f32>,
) -> Size<Option<f32>> {
    size.maybe_add(padding_border)
        .maybe_apply_aspect_ratio_with_box_sizing(
            aspect_ratio,
            BoxSizing::BorderBox,
            padding_border,
        )
        .maybe_sub(padding_border)
        .maybe_max(Size::ZERO)
}

fn ratio_basis_scale(constrained: f32, original: f32, inset: f32, sizing_box: BoxSizing) -> f32 {
    match sizing_box {
        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>,
    available_space_margins: AvailableSpaceMargins,
    context: &ReplacedContext,
    resolved_aspect_ratio: Option<ResolvedAspectRatio>,
    style: &taffy::Style<Atom>,
    writing_mode: taffy::WritingMode,
    sizing_mode: SizingMode,
    requested_axis: RequestedAxis,
) -> Size<f32> {
    let padding = style
        .padding()
        .resolve_or_zero(parent_size.width, resolve_stylo_calc_value);
    let border = style
        .border()
        .resolve_or_zero(parent_size.width, resolve_stylo_calc_value);
    let padding_border = padding + border;
    let padding_border_sum = Size {
        width: padding_border.left + padding_border.right,
        height: padding_border.top + padding_border.bottom,
    };
    let box_sizing_adjustment = if style.box_sizing() == BoxSizing::BorderBox {
        padding_border_sum
    } else {
        Size::ZERO
    };
    // 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 preferred_basis = Size {
        width: if available_space.width == AvailableSpace::MinContent {
            Some(0.0)
        } else {
            parent_size.width
        },
        height: if available_space.height == AvailableSpace::MinContent {
            Some(0.0)
        } else {
            parent_size.height
        },
    };
    let mut preferred_size = style
        .size
        .maybe_resolve(preferred_basis, resolve_stylo_calc_value)
        .maybe_sub(box_sizing_adjustment);
    let mut min_size = style
        .min_size
        .maybe_resolve(parent_size, resolve_stylo_calc_value)
        .maybe_sub(box_sizing_adjustment);
    // Available space is not an implicit `max-width`/`max-height`. Blink
    // resolves a replaced atomic inline's used size before line breaking and
    // lets an oversized result overflow; only authored max-size constraints
    // belong in this clamp.
    let mut max_size = style
        .max_size
        .maybe_resolve(preferred_basis, resolve_stylo_calc_value)
        .maybe_sub(box_sizing_adjustment)
        .maybe_max(min_size);

    // 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(resolved_aspect_ratio) = resolved_aspect_ratio {
        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.into()) {
            min_size.width = transferred_width;
        }
        if is_min_or_max_content(style.min_size.height.into()) {
            min_size.height = transferred_height;
        }
        if is_min_or_max_content(style.max_size.width.into()) {
            max_size.width = transferred_width;
        }
        if is_min_or_max_content(style.max_size.height.into()) {
            max_size.height = transferred_height;
        }
    }

    if sizing_mode == SizingMode::ContentSize {
        match requested_axis {
            RequestedAxis::Horizontal => {
                preferred_size.width = None;
                min_size.width = None;
            }
            RequestedAxis::Vertical => {
                preferred_size.height = None;
                min_size.height = None;
            }
            RequestedAxis::Both => {}
        }
    }

    if known_dimensions.width.is_some() || known_dimensions.height.is_some() {
        let style_max_size = style
            .max_size
            .maybe_resolve(preferred_basis, resolve_stylo_calc_value)
            .maybe_sub(box_sizing_adjustment)
            .maybe_max(min_size);
        let content_known = known_dimensions.maybe_sub(padding_border_sum);
        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() {
        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() {
        apply_aspect_ratio_to_content_size(
            context.attribute_size,
            resolved_aspect_ratio,
            padding_border_sum,
        )
        .unwrap_or(context.inherent_size)
    } else if context.svg_without_intrinsic_size
        && let Some(aspect_ratio) = resolved_aspect_ratio
    {
        // Blink's ComputeReplacedSizeInternal stretches the inline axis when
        // only an aspect ratio is available. Falling back to a 300x150 object
        // here makes a viewBox-only icon 150x150 even inside a 24px container.
        // This is an auto-size fallback, not an available-space clamp: natural
        // dimensions and definite author sizes above may still overflow.
        let inline_axis = writing_mode.inline_axis();
        let margin = style
            .margin
            .resolve_or_zero(parent_size.width, resolve_stylo_calc_value)
            .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.is_out_of_flow() {
                    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 - 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
                .size
                .get_abs(inline_axis)
                .may_have_percentage_dependence() =>
            {
                if writing_mode.is_horizontal() {
                    300.0
                } else {
                    150.0
                }
            }
            None => 0.0,
        };
        let size = if writing_mode.is_horizontal() {
            Size {
                width: Some(inline_size),
                height: None,
            }
        } else {
            Size {
                width: None,
                height: Some(inline_size),
            }
        };
        apply_aspect_ratio_to_content_size(size, Some(aspect_ratio), padding_border_sum)
            .unwrap_or(context.inherent_size)
    } else {
        context.inherent_size
    };
    let size = unclamped.map(|value| value.max(0.0));
    let width_violation = if size.width < min_size.width.unwrap_or(0.0) {
        Violation::Min
    } else if size.width > max_size.width.unwrap_or(f32::INFINITY) {
        Violation::Max
    } else {
        Violation::None
    };
    let height_violation = if size.height < min_size.height.unwrap_or(0.0) {
        Violation::Min
    } else if size.height > max_size.height.unwrap_or(f32::INFINITY) {
        Violation::Max
    } else {
        Violation::None
    };
    let Some(resolved_aspect_ratio) = resolved_aspect_ratio else {
        return size.maybe_clamp(min_size, max_size) + padding_border_sum;
    };
    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: 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: 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: 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: 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 ratio_basis_scale(
                width,
                size.width,
                padding_border_sum.width,
                resolved_aspect_ratio.sizing_box(),
            ) <= ratio_basis_scale(
                height,
                size.height,
                padding_border_sum.height,
                resolved_aspect_ratio.sizing_box(),
            ) {
                Size {
                    width,
                    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: 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::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 ratio_basis_scale(
                width,
                size.width,
                padding_border_sum.width,
                resolved_aspect_ratio.sizing_box(),
            ) <= ratio_basis_scale(
                height,
                size.height,
                padding_border_sum.height,
                resolved_aspect_ratio.sizing_box(),
            ) {
                Size {
                    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: 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::Min, Violation::Max) => Size {
            width: min_size.width.expect("min-width violation has a bound"),
            height: max_size.height.expect("max-height violation has a bound"),
        },
        (Violation::Max, Violation::Min) => Size {
            width: max_size.width.expect("max-width violation has a bound"),
            height: min_size.height.expect("min-height violation has a bound"),
        },
    };
    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,
            },
            AvailableSpaceMargins::Included,
            &image_context(),
            style
                .aspect_ratio
                .filter(|ratio| ratio.is_finite() && *ratio > 0.0)
                .or(Some(1.0))
                .and_then(|ratio| ResolvedAspectRatio::new(ratio, style.box_sizing)),
            style,
            taffy::WritingMode::HorizontalTb,
            SizingMode::InherentSize,
            RequestedAxis::Both,
        )
    }

    #[test]
    fn viewbox_only_svg_stretches_the_logical_inline_axis() {
        let context = ReplacedContext::for_element(
            LayoutReplacedKind::Svg,
            Some(ReplacedMetrics {
                intrinsic_ratio: Some(2.0),
                ..ReplacedMetrics::default()
            }),
        );
        for (writing_mode, expected) in [
            (
                taffy::WritingMode::HorizontalTb,
                Size {
                    width: 24.0,
                    height: 12.0,
                },
            ),
            (
                taffy::WritingMode::VerticalRl,
                Size {
                    width: 96.0,
                    height: 48.0,
                },
            ),
            (
                taffy::WritingMode::VerticalLr,
                Size {
                    width: 96.0,
                    height: 48.0,
                },
            ),
        ] {
            assert_eq!(
                measure_replaced(
                    Size::NONE,
                    Size {
                        width: Some(24.0),
                        height: Some(48.0)
                    },
                    Size {
                        width: AvailableSpace::Definite(24.0),
                        height: AvailableSpace::Definite(48.0)
                    },
                    AvailableSpaceMargins::Included,
                    &context,
                    ResolvedAspectRatio::new(2.0, BoxSizing::ContentBox),
                    &taffy::Style::default(),
                    writing_mode,
                    SizingMode::InherentSize,
                    RequestedAxis::Both,
                ),
                expected,
                "the SVG must derive its auto block size from its available inline size",
            );
        }
    }

    #[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,
                },
                AvailableSpaceMargins::Included,
                &image_context(),
                ResolvedAspectRatio::new(2.0, box_sizing),
                &style,
                taffy::WritingMode::HorizontalTb,
                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> {
            size: Size {
                width: taffy::Dimension::length(30.0),
                height: taffy::Dimension::length(40.0),
            },
            min_size: Size {
                width: taffy::Dimension::min_content().into(),
                height: taffy::Dimension::auto().into(),
            },
            ..taffy::Style::default()
        };
        assert_eq!(
            measure(&min_width),
            Size {
                width: 40.0,
                height: 40.0
            }
        );

        min_width.min_size.width = taffy::Dimension::max_content().into();
        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().into(),
                height: taffy::Dimension::auto().into(),
            },
            ..taffy::Style::default()
        };
        assert_eq!(
            measure(&max_width),
            Size {
                width: 70.0,
                height: 70.0
            }
        );

        max_width.max_size.width = taffy::Dimension::max_content().into();
        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().into(),
                height: taffy::Dimension::min_content().into(),
            },
            ..taffy::Style::default()
        };
        assert_eq!(
            measure(&min_height),
            Size {
                width: 40.0,
                height: 40.0
            }
        );

        min_height.min_size.height = taffy::Dimension::max_content().into();
        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().into(),
                height: taffy::Dimension::min_content().into(),
            },
            ..taffy::Style::default()
        };
        assert_eq!(
            measure(&max_height),
            Size {
                width: 70.0,
                height: 70.0
            }
        );

        max_height.max_size.height = taffy::Dimension::max_content().into();
        assert_eq!(
            measure(&max_height),
            Size {
                width: 70.0,
                height: 70.0
            }
        );
    }
}