Skip to content

Truly independent web browser

LibWeb: Resolve calc-size() in intrinsic sizes, flex, tables and images

Andreas Klinga64de743156c
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
index 77c44010edf..0e55707581f 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
@@ -1081,7 +1081,8 @@ impl<'a> ComputedValuesView<'a> {
pub(crate) fn flex_basis(self) -> &'a ComputedSize {
let flex_basis = &self.alignment().flex_basis;
- if flex_basis.is_content {
+ // NB: A calc-size() based on content keeps its function for resolving the size.
+ if flex_basis.is_content && flex_basis.size.calc_size().is_none() {
auto_computed_size()
} else {
&flex_basis.size
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index 7e0c549fba1..a5e90a0713a 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -2728,10 +2728,17 @@ impl ComputedFlexBasis {
data.optional_data(),
Some(StyleValueData::Keyword { keyword }) if *keyword == keyword::CONTENT
);
+ // A calc-size() with a content basis acts as content, and keeps the function for resolving the size.
+ let is_calc_size_of_content = data
+ .optional_data()
+ .and_then(crate::css::calc_size::calc_size_basis_keyword)
+ == Some(keyword::CONTENT);
Self {
- is_content,
+ is_content: is_content || is_calc_size_of_content,
size: if is_content {
ComputedSize::keyword(ComputedSizeKind::Auto)
+ } else if is_calc_size_of_content {
+ ComputedSize::retained(ComputedSizeKind::Auto, data)
} else {
ComputedSize::from_data(data)
},
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 217d70e3552..c53253af81c 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -1344,7 +1344,7 @@ impl AbsposEngine<'_> {
);
}
}
- if !style.min_width().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_width()) {
let min_inline_size =
sizing.calculate_inner_inline_size(node, available_space.inline_size, style.min_width(), constraints);
if auto_px_value(used_inline_size) < min_inline_size {
@@ -1464,7 +1464,7 @@ impl AbsposEngine<'_> {
constrained = Some(maximum);
}
}
- if !style.min_height().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_height()) {
let minimum = sizing.calculate_inner_block_size(node, available_space, style.min_height(), constraints);
if minimum > auto_px_value(constrained) {
constrained = Some(minimum);
@@ -1652,7 +1652,7 @@ impl AbsposEngine<'_> {
);
}
}
- if solution.block_size.is_some() && !style.min_height().is_auto() {
+ if solution.block_size.is_some() && !crate::layout::sizing_context::is_plain_auto(style.min_height()) {
let min_block_size = self.sizing().calculate_inner_block_size(
node,
intrinsic_available_space,
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index fc8304cd57e..87e6865b004 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -934,7 +934,7 @@ impl<'pass> BlockFormattingContext<'pass> {
}
// 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
// but this time using the value of 'min-width' as the computed value for 'width'.
- if !style.min_width().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_width()) {
let minimum =
sizing.calculate_inner_inline_size(node, available_space.inline_size, style.min_width(), constraints);
if inline_size < minimum {
@@ -1804,8 +1804,10 @@ impl<'pass> BlockFormattingContext<'pass> {
.expect("a block container flow cursor is active");
let style = self.style(node);
- let box_is_html_element_in_quirks_mode =
- facts.document_in_quirks_mode() && facts.is_html_html_element() && style.height().is_auto();
+ let box_is_html_element_in_quirks_mode = facts.document_in_quirks_mode()
+ && facts.is_html_html_element()
+ && style.height().is_auto()
+ && style.height().calc_size().is_none();
let block_size_is_definite_from_aspect_ratio = self.used(node).has_definite_inline_size()
&& facts.has_preferred_aspect_ratio()
&& self.sizing().box_is_sized_as_replaced_element(
@@ -2516,6 +2518,18 @@ impl<'pass> BlockFormattingContext<'pass> {
// was zero-sized in that axis.
if self.used(block_container).inline_size_constraint.get() == SizeConstraint::None {
let sizing = self.sizing();
+ // https://drafts.csswg.org/css-values-5/#calc-size
+ // For the purpose of sizing (including for intrinsic size contributions), calc-size() resolves to the
+ // result of its calc-size calculation.
+ if style.width().is_auto() {
+ inline_size = sizing.resolve_calc_size(
+ block_container,
+ SizingAxis::Inline,
+ style.width(),
+ inline_size,
+ input.containing_block_constraints.percentage_basis_inline_size,
+ );
+ }
if !sizing.should_treat_max_inline_size_as_none(
block_container,
available_space.inline_size,
@@ -2535,7 +2549,7 @@ impl<'pass> BlockFormattingContext<'pass> {
) {
inline_size = limits.clamp(inline_size);
}
- if !style.min_width().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_width()) {
inline_size = inline_size.max(sizing.calculate_inner_inline_size(
block_container,
available_space.inline_size,
@@ -2629,7 +2643,7 @@ impl<'pass> BlockFormattingContext<'pass> {
input.containing_block_constraints,
));
}
- if !style.min_width().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_width()) {
inline_size = inline_size.max(sizing.calculate_inner_inline_size(
fieldset,
available_space.inline_size,
@@ -3092,6 +3106,18 @@ impl<'pass> BlockFormattingContext<'pass> {
let available_inline_size = AvailableSize::definite(containing_inline_size);
let sizing = self.sizing();
let style = self.style(block_container);
+ // https://drafts.csswg.org/css-values-5/#calc-size
+ // For the purpose of sizing (including for intrinsic size contributions), calc-size() resolves to the
+ // result of its calc-size calculation.
+ if style.width().is_auto() {
+ used_inline_size = sizing.resolve_calc_size(
+ block_container,
+ SizingAxis::Inline,
+ style.width(),
+ used_inline_size,
+ input.containing_block_constraints.percentage_basis_inline_size,
+ );
+ }
if !sizing.should_treat_max_inline_size_as_none(
block_container,
input.available_space.inline_size,
@@ -3105,7 +3131,7 @@ impl<'pass> BlockFormattingContext<'pass> {
);
used_inline_size = used_inline_size.min(maximum);
}
- let min_is_auto = style.min_width().is_auto()
+ let min_is_auto = crate::layout::sizing_context::is_plain_auto(style.min_width())
|| (style.min_width().is_fit_content()
&& input.available_space.inline_size.is_intrinsic_sizing_constraint())
|| (style.min_width().is_max_content()
@@ -3453,7 +3479,7 @@ impl<'pass> BlockFormattingContext<'pass> {
style.max_height(),
input.containing_block_constraints,
);
- if !style.min_height().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_height()) {
block_size = block_size.max(sizing.calculate_inner_block_size(
self.run.box_,
input.available_space,
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 280bce3c7e4..046b29a3646 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -1145,6 +1145,38 @@ impl<'pass> FlexFormattingContext<'pass> {
UsedFlexBasis::Size { .. } => self.calculate_fit_content_main_size(index),
};
+ // https://drafts.csswg.org/css-values-5/#calc-size
+ // A calc-size() with a sizing keyword basis resolves against the size that keyword gave the item. The main
+ // size property acts as the flex basis where the flex basis is auto, and the flex basis then applies to that.
+ if self.flex_items[index].main_size_was_resolved_from_aspect_ratio {
+ // NB: The main size of this item came through its aspect ratio, not from its keyword size.
+ } else {
+ let style = self.style(node);
+ let main_axis = if self.main_axis_is_horizontal() {
+ SizingAxis::Inline
+ } else {
+ SizingAxis::Block
+ };
+ let percentage_basis = self
+ .has_definite_main_size_used(self.container_used())
+ .then(|| self.inner_main_size_used(self.container_used()));
+ let has_keyword_basis =
+ |size: &ComputedSize| size.calc_size().is_some() && size.kind != ComputedSizeKind::Calculated;
+ let flex_basis = style.flex_basis();
+ let basis_retrieves_main_size = !style.flex_basis_is_content() && flex_basis.is_auto();
+ let (main_size, _) = self.computed_main_size(node);
+ if basis_retrieves_main_size && has_keyword_basis(main_size) {
+ flex_base_size =
+ self.sizing()
+ .resolve_calc_size(node, main_axis, main_size, flex_base_size, percentage_basis);
+ }
+ if has_keyword_basis(flex_basis) {
+ flex_base_size =
+ self.sizing()
+ .resolve_calc_size(node, main_axis, flex_basis, flex_base_size, percentage_basis);
+ }
+ }
+
// AD-HOC: This is not mentioned in the spec, but if the item has an aspect ratio, we may need
// to adjust the main size in these ways:
// - using stretch-fit main size if the flex basis is indefinite, there is no
@@ -1181,11 +1213,31 @@ impl<'pass> FlexFormattingContext<'pass> {
// To provide a more reasonable default minimum size for flex items,
// the used value of a main axis automatic minimum size on a flex item that is not a scroll container is its content-based minimum size;
// for scroll containers the automatic minimum size is zero, as usual.
- if !self.facts(self.flex_items[index].box_).is_scroll_container() {
+ let node = self.flex_items[index].box_;
+ let size = if !self.facts(node).is_scroll_container() {
self.content_based_minimum_size(index)
} else {
CssPixels::default()
+ };
+ // https://drafts.csswg.org/css-values-5/#calc-size
+ // NB: A calc-size() with an auto basis resolves against the automatic minimum size, and its percentages against
+ // the flex container's inner main size, where that is definite.
+ let style = self.style(node);
+ let min_size = self.select_main(style.min_width(), style.min_height());
+ if min_size.calc_size().is_none() {
+ return size;
}
+ let axis = if self.main_axis_is_horizontal() {
+ SizingAxis::Inline
+ } else {
+ SizingAxis::Block
+ };
+ let container = self.container_used();
+ let percentage_basis = self
+ .has_definite_main_size_used(container)
+ .then(|| self.inner_main_size_used(container));
+ self.sizing()
+ .resolve_calc_size(node, axis, min_size, size, percentage_basis)
}
// https://drafts.csswg.org/css-flexbox-1/#specified-size-suggestion
@@ -1947,6 +1999,34 @@ impl<'pass> FlexFormattingContext<'pass> {
}
}
}
+
+ // https://drafts.csswg.org/css-values-5/#resolving-calc-size
+ // NB: A calc-size() with an auto basis resolves against the cross size auto gives the item, stretched or not.
+ for index in 0..self.flex_items.len() {
+ let node = self.flex_items[index].box_;
+ let cross_size = self.computed_cross_size(node).0;
+ if !cross_size.is_auto() || cross_size.calc_size().is_none() {
+ continue;
+ }
+ let Some(auto_size) = self.flex_items[index].cross_size else {
+ continue;
+ };
+ let container = self.container_used();
+ let percentage_basis = self
+ .has_definite_cross_size_used(container)
+ .then(|| self.inner_cross_size_used(container));
+ let axis = if self.cross_axis_is_horizontal() {
+ SizingAxis::Inline
+ } else {
+ SizingAxis::Block
+ };
+ let size = self
+ .sizing()
+ .resolve_calc_size(node, axis, cross_size, auto_size, percentage_basis);
+ self.flex_items[index].cross_size = Some(size);
+ self.set_cross_size(index, size);
+ self.set_has_definite_cross_size(index);
+ }
}
fn set_main_axis_first_margin(&mut self, index: usize, margin: CssPixels) {
@@ -3294,9 +3374,15 @@ impl<'pass> FlexFormattingContext<'pass> {
&& !facts.has_preferred_aspect_ratio()
&& self.flex_items[index].used_flex_basis_is_definite
&& self.specified_size_suggestion(index) == Some(self.flex_items[index].flex_base_size);
+ // NB: A calc-size() minimum can resolve to more than the automatic minimum size.
+ let style = self.style(node);
+ let min_is_calc_size = self
+ .select_main(style.min_width(), style.min_height())
+ .calc_size()
+ .is_some();
let clamp_min = if self.has_main_min_size(node) {
self.specified_main_min_size(index)
- } else if should_skip_automatic_minimum_size_clamp || can_skip_for_item {
+ } else if !min_is_calc_size && (should_skip_automatic_minimum_size_clamp || can_skip_for_item) {
CssPixels::default()
} else {
self.automatic_minimum_size(index)
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index c66421c7655..b0f612a0e60 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1295,7 +1295,7 @@ fn flex_self_block_size_resolution_space(
return None;
}
let style = sizing.style(node);
- if !style.display().is_flex_inside() || !style.min_height().is_auto() {
+ if !style.display().is_flex_inside() || !crate::layout::sizing_context::is_plain_auto(style.min_height()) {
return None;
}
if sizing.facts(node).document_in_quirks_mode() {
@@ -1440,7 +1440,7 @@ fn dimension_block_level_root(
}
let mut measured_content_block_size = None;
if sizing.should_treat_block_size_as_auto(node, available_space, constraints)
- && !style.min_height().is_auto()
+ && !crate::layout::sizing_context::is_plain_auto(style.min_height())
&& lays_out_content_against_min_block_size(NodeFacts::new(&run.callbacks, node), fc_type)
{
let content_block_size =
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index fbf5d2d5435..9194eb43579 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -2506,7 +2506,20 @@ impl<'pass> GridFormattingContext<'pass> {
)
}
+ /// Whether the grid container's maximum size in an axis is a calc-size() of an intrinsic sizing keyword that
+ /// cannot resolve while the grid measures its intrinsic size, which that keyword would measure again.
+ fn maximum_size_is_unresolvable_during_measurement(&self, axis: Axis) -> bool {
+ let style = self.style(self.run.box_);
+ let maximum = axis.select(style.max_width(), style.max_height());
+ self.run.layout_mode == LayoutMode::IntrinsicSizing
+ && maximum.calc_size().is_some()
+ && maximum.is_intrinsic_sizing_constraint()
+ }
+
fn grid_container_maximum_size(&self, axis: Axis) -> Option<CssPixels> {
+ if self.maximum_size_is_unresolvable_during_measurement(axis) {
+ return None;
+ }
let space = self.container_available_space();
let available = axis.select(space.inline_size, space.block_size);
let constraints = self.layout_input.unwrap().containing_block_constraints;
@@ -2536,7 +2549,9 @@ impl<'pass> GridFormattingContext<'pass> {
!computed_values.max_height().is_auto()
};
- if should_treat_grid_container_maximum_size_as_none {
+ if should_treat_grid_container_maximum_size_as_none
+ || self.maximum_size_is_unresolvable_during_measurement(axis)
+ {
return None;
}
Some(self.calculate_grid_container_maximum_size(axis))
@@ -3107,7 +3122,7 @@ impl<'pass> GridFormattingContext<'pass> {
area_constraints,
));
}
- if !wrapper_style.min_width().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(wrapper_style.min_width()) {
wrapper_size = wrapper_size.max(sizing.calculate_inner_size_for_property(
item.box_,
SizingAxis::Inline,
@@ -3287,6 +3302,21 @@ impl<'pass> GridFormattingContext<'pass> {
)
};
let mut resolved = resolve_alignment(size, preferred.is_auto());
+ // https://drafts.csswg.org/css-values-5/#resolving-calc-size
+ // NB: A calc-size() with an auto basis resolves against the size auto gives the item, stretched or not.
+ if preferred.is_auto() && preferred.calc_size().is_some() {
+ let size = self.sizing().resolve_calc_size(
+ item.box_,
+ axis.sizing_axis(),
+ preferred,
+ resolved.size,
+ axis.select(
+ constraints.percentage_basis_inline_size,
+ constraints.percentage_basis_block_size,
+ ),
+ );
+ resolved = resolve_alignment(size, false);
+ }
let maximum_is_none = if axis.is_column() {
self.sizing()
@@ -3358,7 +3388,7 @@ impl<'pass> GridFormattingContext<'pass> {
}
if self.use_row_alignment_container_size {
let style = self.style(self.run.box_);
- if !style.min_height().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_height()) {
return self
.row_alignment_container_size
.max(self.container_used().content_block_size.get());
@@ -3406,7 +3436,7 @@ impl<'pass> GridFormattingContext<'pass> {
constraints,
));
}
- if !style.min_height().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(style.min_height()) {
block_size = block_size.max(sizing.calculate_inner_size_for_property(
self.run.box_,
SizingAxis::Block,
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 29750a45536..328b9fb30db 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -65,6 +65,14 @@ pub(crate) struct SizingContext<'pass> {
style_cache: Cell<Option<(Node, &'pass FfiStylePayloads)>>,
}
+/// Whether a size is auto without a calc-size() that changes the size auto gives.
+/// NB: A replaced element's size given as calc-size() resolves to a used size that the other size follows through
+/// the aspect ratio.
+/// Whether a size is `auto` itself, rather than a calc-size() with an auto basis.
+pub(crate) fn is_plain_auto(size: &ComputedSize) -> bool {
+ size.is_auto() && size.calc_size().is_none()
+}
+
impl<'pass> SizingContext<'pass> {
pub(crate) fn new(
purpose: formatting_context::LayoutPurpose,
@@ -372,6 +380,31 @@ impl<'pass> SizingContext<'pass> {
computed_inline_size: &ComputedSize,
available_space: AvailableSpace,
constraints: ContainingBlockConstraints,
+ ) -> CssPixels {
+ let inline_size = self.tentative_inline_size_for_replaced_element_before_calc_size(
+ node,
+ computed_inline_size,
+ available_space,
+ constraints,
+ );
+ if !computed_inline_size.is_auto() {
+ return inline_size;
+ }
+ self.resolve_calc_size(
+ node,
+ SizingAxis::Inline,
+ computed_inline_size,
+ inline_size,
+ constraints.percentage_basis_inline_size,
+ )
+ }
+
+ fn tentative_inline_size_for_replaced_element_before_calc_size(
+ &self,
+ node: Node,
+ computed_inline_size: &ComputedSize,
+ available_space: AvailableSpace,
+ constraints: ContainingBlockConstraints,
) -> CssPixels {
// 10.3.2 Inline, replaced elements, https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-width
// Treat percentages of indefinite containing block widths as 0 (the initial width).
@@ -379,11 +412,16 @@ impl<'pass> SizingContext<'pass> {
return CssPixels::default();
}
let style = self.style(node);
- let computed_block_size = if self.should_treat_block_size_as_auto(node, available_space, constraints) {
+ let preferred_block_size = self.preferred_block_size(node, constraints);
+ let computed_block_size = if preferred_block_size.is_auto() {
+ preferred_block_size
+ } else if self.should_treat_block_size_as_auto(node, available_space, constraints) {
auto_computed_size()
} else {
- self.preferred_block_size(node, constraints)
+ preferred_block_size
};
+ // NB: A height given as calc-size() resolves to a used height that the width follows through the ratio.
+ let block_size_is_auto = computed_block_size.is_auto() && computed_block_size.calc_size().is_none();
let used_inline_size = if computed_inline_size.is_auto() {
computed_inline_size.to_px(available_space.inline_size.to_px_or_zero())
} else {
@@ -392,7 +430,7 @@ impl<'pass> SizingContext<'pass> {
// If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
// then that intrinsic width is the used value of 'width'.
let intrinsic = self.intrinsic_size_for_replaced_sizing(node);
- if computed_block_size.is_auto()
+ if block_size_is_auto
&& computed_inline_size.is_auto()
&& let Some(width) = intrinsic.width
{
@@ -405,12 +443,12 @@ impl<'pass> SizingContext<'pass> {
//
// (used height) * (intrinsic ratio)
let has_ratio = self.facts(node).has_preferred_aspect_ratio();
- if (computed_block_size.is_auto()
+ if (block_size_is_auto
&& computed_inline_size.is_auto()
&& intrinsic.width.is_none()
&& intrinsic.height.is_some()
&& has_ratio)
- || (computed_inline_size.is_auto() && !computed_block_size.is_auto() && has_ratio)
+ || (computed_inline_size.is_auto() && !block_size_is_auto && has_ratio)
{
let block_size = self.compute_block_size_for_replaced_element(node, available_space, constraints);
let inline_size = self.content_inline_size_from_aspect_ratio(node, block_size);
@@ -430,7 +468,7 @@ impl<'pass> SizingContext<'pass> {
// then the used value of 'width' is undefined in CSS 2.2. However, it is suggested that, if the containing block's width does not itself
// depend on the replaced element's width, then the used value of 'width' is calculated from the constraint equation used for block-level,
// non-replaced elements in normal flow.
- if computed_block_size.is_auto()
+ if block_size_is_auto
&& computed_inline_size.is_auto()
&& intrinsic.width.is_none()
&& intrinsic.height.is_none()
@@ -473,14 +511,44 @@ impl<'pass> SizingContext<'pass> {
computed_block_size: &ComputedSize,
available_space: AvailableSpace,
constraints: ContainingBlockConstraints,
+ ) -> CssPixels {
+ let block_size = self.tentative_block_size_for_replaced_element_before_calc_size(
+ node,
+ computed_block_size,
+ available_space,
+ constraints,
+ );
+ if !computed_block_size.is_auto() {
+ return block_size;
+ }
+ self.resolve_calc_size(
+ node,
+ SizingAxis::Block,
+ computed_block_size,
+ block_size,
+ constraints.percentage_basis_block_size,
+ )
+ }
+
+ fn tentative_block_size_for_replaced_element_before_calc_size(
+ &self,
+ node: Node,
+ computed_block_size: &ComputedSize,
+ available_space: AvailableSpace,
+ constraints: ContainingBlockConstraints,
) -> CssPixels {
// 10.6.2 Inline replaced elements, block-level replaced elements in normal flow, 'inline-block' replaced elements in normal flow and floating replaced elements
// https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-height
let intrinsic = self.intrinsic_size_for_replaced_sizing(node);
// If 'height' and 'width' both have computed values of 'auto' and the element also has
// an intrinsic height, then that intrinsic height is the used value of 'height'.
+ // NB: The automatic height a calc-size() height resolves against is the intrinsic one, not one that follows
+ // the width, which itself follows the resolved height.
if self.should_treat_inline_size_as_auto(node, available_space)
- && self.should_treat_block_size_as_auto(node, available_space, constraints)
+ && self.style(node).width().calc_size().is_none()
+ && ((self.should_treat_block_size_as_auto(node, available_space, constraints)
+ && computed_block_size.calc_size().is_none())
+ || (computed_block_size.is_auto() && computed_block_size.calc_size().is_some()))
&& let Some(height) = intrinsic.height
{
return height;
@@ -514,7 +582,7 @@ impl<'pass> SizingContext<'pass> {
// 10.4 Minimum and maximum widths: 'min-width' and 'max-width'
// https://www.w3.org/TR/CSS22/visudet.html#min-max-widths
let style = self.style(node);
- let min_inline = if style.min_width().is_auto() {
+ let min_inline = if is_plain_auto(style.min_width()) {
CssPixels::default()
} else {
self.calculate_inner_inline_size(node, available_space.inline_size, style.min_width(), constraints)
@@ -527,7 +595,7 @@ impl<'pass> SizingContext<'pass> {
self.calculate_inner_inline_size(node, available_space.inline_size, style.max_width(), constraints)
};
let max_inline = min_inline.max(specified_max_inline);
- let min_block = if style.min_height().is_auto() {
+ let min_block = if is_plain_auto(style.min_height()) {
CssPixels::default()
} else {
self.calculate_inner_block_size(node, available_space, style.min_height(), constraints)
@@ -620,15 +688,22 @@ impl<'pass> SizingContext<'pass> {
constraints: ContainingBlockConstraints,
) -> (&'pass ComputedSize, &'pass ComputedSize) {
let style = self.style(node);
- let inline = if self.should_treat_inline_size_as_auto(node, available_space) {
+ // NB: A size that is auto itself keeps its value, as does a calc-size(), which resolves against the size its
+ // basis gives.
+ let inline = if style.width().is_auto() || style.width().calc_size().is_some() {
+ style.width()
+ } else if self.should_treat_inline_size_as_auto(node, available_space) {
auto_computed_size()
} else {
style.width()
};
- let block = if self.should_treat_block_size_as_auto(node, available_space, constraints) {
+ let preferred_block_size = self.preferred_block_size(node, constraints);
+ let block = if preferred_block_size.is_auto() || preferred_block_size.calc_size().is_some() {
+ preferred_block_size
+ } else if self.should_treat_block_size_as_auto(node, available_space, constraints) {
auto_computed_size()
} else {
- self.preferred_block_size(node, constraints)
+ preferred_block_size
};
(inline, block)
}
@@ -647,7 +722,10 @@ impl<'pass> SizingContext<'pass> {
// 1. The tentative used width is calculated (without 'min-width' and 'max-width')
let mut used =
self.tentative_inline_size_for_replaced_element(node, computed_inline, available_space, constraints);
- if computed_inline.is_auto() && computed_block.is_auto() && self.facts(node).has_preferred_aspect_ratio() {
+ if is_plain_auto(computed_inline)
+ && is_plain_auto(computed_block)
+ && self.facts(node).has_preferred_aspect_ratio()
+ {
let block =
self.tentative_block_size_for_replaced_element(node, computed_block, available_space, constraints);
used = self
@@ -696,7 +774,7 @@ impl<'pass> SizingContext<'pass> {
}
// 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
// but this time using the value of 'min-width' as the computed value for 'width'.
- if !style.min_width().is_auto() && !is_transferred_intrinsic_limit(style.min_width()) {
+ if !is_plain_auto(style.min_width()) && !is_transferred_intrinsic_limit(style.min_width()) {
if let Some(min) = transferred_intrinsic_limit(style.min_width()) {
used = used.max(min);
} else {
@@ -736,8 +814,8 @@ impl<'pass> SizingContext<'pass> {
// through the ratio before its min/max block sizes apply on their own.
// https://drafts.csswg.org/css-sizing-4/#aspect-ratio-automatic
let inline_size_is_already_used = !facts.is_replaced_box() && self.used(node).has_definite_inline_size();
- if computed_inline.is_auto()
- && computed_block.is_auto()
+ if is_plain_auto(computed_inline)
+ && is_plain_auto(computed_block)
&& facts.has_preferred_aspect_ratio()
&& !inline_size_is_already_used
{
@@ -775,7 +853,7 @@ impl<'pass> SizingContext<'pass> {
}
// 3. If the resulting height is smaller than 'min-height', the rules above are applied again,
// but this time using the value of 'min-height' as the computed value for 'height'.
- if !style.min_height().is_auto() {
+ if !is_plain_auto(style.min_height()) {
let min = self.calculate_inner_block_size(node, available_space, style.min_height(), constraints);
if used < min {
used = self.tentative_block_size_for_replaced_element(
@@ -870,7 +948,7 @@ impl<'pass> SizingContext<'pass> {
let quirks_body_block = (facts.document_in_quirks_mode()
&& facts.is_html_body_element()
&& facts.is_block_container()
- && style.height().is_auto()
+ && is_plain_auto(style.height())
&& !used.has_definite_block_size()
&& !facts.is_absolutely_positioned()
&& !facts.is_floating()
@@ -1298,8 +1376,10 @@ impl<'pass> SizingContext<'pass> {
) -> bool {
let size = self.style(node).max_width();
// NB: A keyword limit is no limit under the same intrinsic constraint, unless calc-size() changes its size.
- let is_plain_keyword = size.calc_size().is_none();
- if size.is_none() || (available == AvailableSize::MaxContent && size.is_max_content() && is_plain_keyword) {
+ let keyword_limit_is_cyclic = size.calc_size().is_none();
+ if size.is_none()
+ || (available == AvailableSize::MaxContent && size.is_max_content() && keyword_limit_is_cyclic)
+ {
return true;
}
// https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
@@ -1322,7 +1402,7 @@ impl<'pass> SizingContext<'pass> {
return true;
}
}
- size.calc_size().is_none()
+ keyword_limit_is_cyclic
&& ((size.is_fit_content() && available.is_intrinsic_sizing_constraint())
|| (size.is_max_content() && available == AvailableSize::MaxContent)
|| (size.is_min_content() && available == AvailableSize::MinContent))
@@ -1350,7 +1430,9 @@ impl<'pass> SizingContext<'pass> {
return true;
}
}
- size.calc_size().is_none()
+ // NB: As for the inline axis, calc-size() can change the size of a keyword limit.
+ let keyword_limit_is_cyclic = size.calc_size().is_none();
+ keyword_limit_is_cyclic
&& ((size.is_fit_content() && available.is_intrinsic_sizing_constraint())
|| (size.is_max_content() && available == AvailableSize::MaxContent)
|| (size.is_min_content() && available == AvailableSize::MinContent))
@@ -1404,7 +1486,7 @@ impl<'pass> SizingContext<'pass> {
let max = self.calculate_inner_block_size(node, available_space, style.max_height(), constraints);
block_size = block_size.min(max);
}
- if !style.min_height().is_auto() {
+ if !is_plain_auto(style.min_height()) {
let min = self.calculate_inner_block_size(node, available_space, style.min_height(), constraints);
block_size = block_size.max(min);
}
@@ -1455,6 +1537,12 @@ impl<'pass> SizingContext<'pass> {
constraints,
)
});
+ // NB: A calc-size() with an auto basis resolves against the automatic minimum size.
+ let percentage_basis = match axis {
+ SizingAxis::Inline => constraints.percentage_basis_inline_size,
+ SizingAxis::Block => constraints.percentage_basis_block_size,
+ };
+ let minimum = self.resolve_calc_size(node, axis, min_size, minimum, percentage_basis);
// NB: An intrinsic size keyword maximum is at least the box's min-content size, which is no smaller than this
// minimum, so it never caps it. Resolving it would measure the box's intrinsic size, which uses this minimum.
let maximum = match axis {
@@ -1615,21 +1703,21 @@ impl<'pass> SizingContext<'pass> {
let block_size_is_definite_from_aspect_ratio = self.used(node).has_definite_inline_size()
&& facts.has_preferred_aspect_ratio()
&& box_is_sized_as_replaced_element;
+ // NB: Replaced sizing resolves a calc-size() itself.
let mut block_size = if box_is_sized_as_replaced_element {
self.compute_block_size_for_replaced_element(node, available_space, constraints)
} else {
- child_automatic_block_size.unwrap_or_else(automatic_block_size_fallback)
+ self.resolve_calc_size(
+ node,
+ SizingAxis::Block,
+ style.height(),
+ child_automatic_block_size.unwrap_or_else(automatic_block_size_fallback),
+ constraints.percentage_basis_block_size,
+ )
};
- block_size = self.resolve_calc_size(
- node,
- SizingAxis::Block,
- style.height(),
- block_size,
- constraints.percentage_basis_block_size,
- );
block_size = self.clamp_block_size_to_min_max(node, block_size, available_space, constraints);
- if facts.document_in_quirks_mode() && facts.is_html_html_element() && style.height().is_auto() {
+ if facts.document_in_quirks_mode() && facts.is_html_html_element() && is_plain_auto(style.height()) {
// 3.6. The html element fills the viewport quirk
// https://quirks.spec.whatwg.org/#the-html-element-fills-the-viewport-quirk
// FIXME: Handle vertical writing mode.
@@ -1649,7 +1737,7 @@ impl<'pass> SizingContext<'pass> {
self.used(node).has_definite_block_size.set(true);
}
- if facts.document_in_quirks_mode() && facts.is_html_body_element() && style.height().is_auto() {
+ if facts.document_in_quirks_mode() && facts.is_html_body_element() && is_plain_auto(style.height()) {
// 3.7. The body element fills the html element quirk
// https://quirks.spec.whatwg.org/#the-body-element-fills-the-html-element-quirk
// FIXME: Handle vertical writing mode.
@@ -1725,8 +1813,8 @@ impl<'pass> SizingContext<'pass> {
let style = self.style(node);
if !(style.width().is_auto() || style.width().is_length())
|| !(style.height().is_auto() || style.height().is_length())
- || !(style.min_width().is_auto() || style.min_width().is_length())
- || !(style.min_height().is_auto() || style.min_height().is_length())
+ || !(is_plain_auto(style.min_width()) || style.min_width().is_length())
+ || !(is_plain_auto(style.min_height()) || style.min_height().is_length())
|| !(style.max_width().is_none() || style.max_width().is_length())
|| !(style.max_height().is_none() || style.max_height().is_length())
|| Self::box_edges_contain_percentage(style)
@@ -1795,7 +1883,7 @@ impl<'pass> SizingContext<'pass> {
let native_control_block_size_constraint_is_active = facts.is_native_form_control_box()
&& facts.has_auto_content_height()
&& !style.height().is_intrinsic_sizing_constraint()
- && ((!style.min_height().is_auto() && block_size > facts.auto_content_height())
+ && ((!is_plain_auto(style.min_height()) && block_size > facts.auto_content_height())
|| (!style.max_height().is_auto()
&& !self.should_treat_max_block_size_as_none(node, available_space.block_size, constraints)
&& block_size < facts.auto_content_height()));
@@ -1951,7 +2039,7 @@ impl<'pass> SizingContext<'pass> {
if let Some(limits) = self.transferred_inline_size_limits(node, available_space, constraints) {
inline_size = limits.clamp(inline_size);
}
- if !style.min_width().is_auto() {
+ if !is_plain_auto(style.min_width()) {
inline_size = inline_size.max(self.calculate_inner_inline_size(
node,
available_space.inline_size,
@@ -2922,7 +3010,7 @@ impl<'pass> SizingContext<'pass> {
// no extra space to center within and no need to force a definite content box. A text input's max-height can
// still make its content box shorter than its content.
let max_height_can_clamp_text_input_content = is_primitive_text_input && !style.max_height().is_auto();
- if style.height().is_auto() && style.min_height().is_auto() && !max_height_can_clamp_text_input_content {
+ if style.height().is_auto() && is_plain_auto(style.min_height()) && !max_height_can_clamp_text_input_content {
return;
}
if self.used(node).has_definite_block_size() {
@@ -2958,7 +3046,7 @@ impl<'pass> SizingContext<'pass> {
constraints,
));
}
- if !style.min_height().is_auto() {
+ if !is_plain_auto(style.min_height()) {
used_block_size = used_block_size.max(self.calculate_inner_block_size(
node,
available_space,
@@ -3259,7 +3347,18 @@ impl<'pass> SizingContext<'pass> {
preferred_size: &ComputedSize,
constraints: ContainingBlockConstraints,
) -> CssPixels {
- assert!(!preferred_size.is_auto());
+ // NB: Outside flex and grid layout, the automatic minimum size is zero, which a minimum given as a calc-size()
+ // with an auto basis resolves against.
+ if preferred_size.is_auto() {
+ assert!(preferred_size.calc_size().is_some());
+ return self.resolve_calc_size(
+ node,
+ SizingAxis::Inline,
+ preferred_size,
+ CssPixels::default(),
+ constraints.percentage_basis_inline_size,
+ );
+ }
let basis = if preferred_size.contains_percentage() {
if let Some(basis) = constraints.percentage_basis_inline_size {
basis
@@ -3318,9 +3417,32 @@ impl<'pass> SizingContext<'pass> {
constraints: ContainingBlockConstraints,
) -> CssPixels {
if preferred_size.is_auto() && self.facts(node).has_preferred_aspect_ratio() {
- return self.content_block_size_from_aspect_ratio(node, self.used(node).content_inline_size.get());
+ // NB: A replaced element's calc-size() height resolves against its own automatic height, which its width
+ // then follows through the ratio.
+ if preferred_size.calc_size().is_some() && self.facts(node).is_replaced_box() {
+ return self.compute_block_size_for_replaced_element(node, available_space, constraints);
+ }
+ let block_size = self.content_block_size_from_aspect_ratio(node, self.used(node).content_inline_size.get());
+ return self.resolve_calc_size(
+ node,
+ SizingAxis::Block,
+ preferred_size,
+ block_size,
+ constraints.percentage_basis_block_size,
+ );
+ }
+ // NB: Outside flex and grid layout, the automatic minimum size is zero, which a minimum given as a calc-size()
+ // with an auto basis resolves against.
+ if preferred_size.is_auto() {
+ assert!(preferred_size.calc_size().is_some());
+ return self.resolve_calc_size(
+ node,
+ SizingAxis::Block,
+ preferred_size,
+ CssPixels::default(),
+ constraints.percentage_basis_block_size,
+ );
}
- assert!(!preferred_size.is_auto());
let facts = self.facts(node);
let keyword_size = if facts.is_replaced_box()
&& facts.has_auto_content_box_size()
@@ -3330,7 +3452,7 @@ impl<'pass> SizingContext<'pass> {
// its used width through its aspect ratio.
let intrinsic = self.intrinsic_size_for_replaced_sizing(node);
match intrinsic.height {
- Some(height) if self.style(node).width().is_auto() => Some(height),
+ Some(height) if is_plain_auto(self.style(node).width()) => Some(height),
_ if facts.has_preferred_aspect_ratio() => {
Some(self.content_block_size_from_aspect_ratio(node, self.used(node).content_inline_size.get()))
}
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
index 65096e60017..b97cc5f4024 100644
--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
@@ -864,6 +864,17 @@ pub(crate) fn calculate_table_grid<T: TableTree>(tree: &T, table: Node, missing_
}
}
+/// The size a calc-size() with a sizing keyword basis resolves to, given the size of that keyword, or that size for
+/// a plain keyword.
+/// https://drafts.csswg.org/css-values-5/#resolving-calc-size
+fn resolve_calc_size_of_keyword(size: &ComputedSize, keyword_size: CssPixels, basis: CssPixels) -> CssPixels {
+ size.calc_size()
+ .and_then(|calc_size| {
+ crate::css::calc_size::resolve_calc_size(calc_size, Some(basis.to_double()), &|_| keyword_size.to_double())
+ })
+ .map_or(keyword_size, CssPixels::nearest_value_for)
+}
+
const BORDER_COLLAPSE_SEPARATE: u8 = 0;
const TABLE_LAYOUT_FIXED: u8 = 1;
@@ -2130,10 +2141,10 @@ impl<'pass> TableFormattingContext<'pass> {
basis: CssPixels,
) -> CssPixels {
if constraint.is_min_content() {
- return grid_min;
+ return resolve_calc_size_of_keyword(constraint, grid_min, basis);
}
if constraint.is_max_content() {
- return grid_max;
+ return resolve_calc_size_of_keyword(constraint, grid_max, basis);
}
if constraint.is_fit_content() {
let limit = if constraint.fit_content_available_space().is_some() {
@@ -2141,7 +2152,7 @@ impl<'pass> TableFormattingContext<'pass> {
} else {
basis
};
- return grid_max.min(grid_min.max(limit));
+ return resolve_calc_size_of_keyword(constraint, grid_max.min(grid_min.max(limit)), basis);
}
// CSS Sizing says box-sizing:border-box applies length/percentage width/min-width/max-width constraints to
// the border box. The table inline-size algorithm compares content inline sizes, so convert them before comparing.
@@ -2186,7 +2197,7 @@ impl<'pass> TableFormattingContext<'pass> {
let capmin = self.compute_capmin();
// The used minimum inline size of a table is the greater of the resolved min-width, CAPMIN, and GRIDMIN.
let mut used_min = grid_min.max(capmin);
- if !table_style.min_width().is_auto() {
+ if !crate::layout::sizing_context::is_plain_auto(table_style.min_width()) {
used_min = used_min.max(self.resolve_inline_constraint(table_style.min_width(), grid_min, grid_max, basis));
}
let width_is_auto_or_indefinite_percentage = table_style.width().is_auto()
@@ -2232,7 +2243,7 @@ impl<'pass> TableFormattingContext<'pass> {
}
value
} else if table_style.width().is_max_content() {
- grid_max
+ resolve_calc_size_of_keyword(table_style.width(), grid_max, basis)
} else {
// If the table-root’s width property has a computed value (resolving to the table inline size) other than auto,
// the used inline size is the greater of the resolved table inline size and the used minimum inline size.
@@ -2258,7 +2269,10 @@ impl<'pass> TableFormattingContext<'pass> {
return false;
}
let style = self.style(self.rows[row_index].box_);
- if !style.height().is_auto() || !style.min_height().is_auto() || !style.max_height().is_none() {
+ if !style.height().is_auto()
+ || !crate::layout::sizing_context::is_plain_auto(style.min_height())
+ || !style.max_height().is_none()
+ {
return false;
}
}
@@ -2268,7 +2282,10 @@ impl<'pass> TableFormattingContext<'pass> {
return false;
}
let style = self.style(cell.box_);
- if !style.height().is_auto() || !style.min_height().is_auto() || !style.max_height().is_none() {
+ if !style.height().is_auto()
+ || !crate::layout::sizing_context::is_plain_auto(style.min_height())
+ || !style.max_height().is_none()
+ {
return false;
}
}
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-values/calc-size/calc-size-no-body-height-quirk-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-values/calc-size/calc-size-no-body-height-quirk-001-ref.html
new file mode 100644
index 00000000000..0c0d6b3607b
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-values/calc-size/calc-size-no-body-height-quirk-001-ref.html
@@ -0,0 +1,11 @@
+<!-- This test is intentionally in quirks mode. -->
+<title>calc-size() and the body fills html quirk</title>
+<style>
+
+div {
+ border: medium solid;
+}
+
+</style>
+
+<div>The bottom border of this box should be close to the text, not at the bottom of the viewport.</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-001.html
new file mode 100644
index 00000000000..cb44323dcb1
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-001.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#aspect-ratio">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<!-- Without calc-size() "auto" would resolve to 50px, and passing through calc-size() will yield 100px. -->
+<div style="aspect-ratio: 1/2; height: 100px; width: calc-size(auto, size + 50px); background: green;"></div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-002.html
new file mode 100644
index 00000000000..938f192d0c5
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-002.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#aspect-ratio">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<!-- Without calc-size() "auto" would resolve to 50px, and passing through calc-size() will yield 100px. -->
+<div style="aspect-ratio: 2/1; width: 100px; height: calc-size(auto, size + 50px); background: green;"></div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-003.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-003.html
new file mode 100644
index 00000000000..29b604222df
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-003.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#aspect-ratio">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<!-- The auto height is 50px, the auto min-size is 150px, which passing through the calc-size() yields 100px. -->
+<div style="aspect-ratio: 2/1; width: 100px; min-height: calc-size(auto, size - 50px); background: green;">
+ <div style="height: 150px;"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-004.html
new file mode 100644
index 00000000000..5daabb42ed3
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-004.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#aspect-ratio">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<!-- The auto width is 50px, the auto min-size is 150px, which passing through the calc-size() yields 100px. -->
+<div style="aspect-ratio: 1/2; height: 100px; min-width: calc-size(auto, size - 50px); background: green;">
+ <div style="width: 150px;"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-005.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-005.html
new file mode 100644
index 00000000000..994d179de2d
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-aspect-ratio-005.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-sizing-4/#aspect-ratio">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="width: fit-content; background: green;">
+ <!-- The auto width is 50px, the auto min-size is 150px, which passing through the calc-size() yields 100px. -->
+ <div style="aspect-ratio: 1/2; height: 100px; min-width: calc-size(auto, size - 50px);">
+ <div style="width: 150px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-001.html
new file mode 100644
index 00000000000..fcfb229ce21
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-001.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#min-size-auto">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="display: flex; width: 0px; height: 100px;">
+ <!-- The auto min-size is 80px, then through the calc-size() is 100px. -->
+ <div style="min-width: calc-size(auto, size + 20px); background: green;">
+ <div style="width: 80px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-002.html
new file mode 100644
index 00000000000..d012bc625dc
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-002.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#min-size-auto">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="display: flex; width: 0px; height: 100px;">
+ <!-- The auto min-size is 60px (the specified-size suggestion), then through the calc-size() is 100px. -->
+ <div style="min-width: calc-size(auto, size + 40px); width: 60px; background: green;">
+ <div style="width: 80px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-003.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-003.html
new file mode 100644
index 00000000000..d2208e5c7ba
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-003.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#min-size-auto">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="display: flex; width: 0px; height: 100px;">
+ <!-- The auto min-size is 60px (the content-size suggestion), then through the calc-size() is 100px. -->
+ <div style="min-width: calc-size(auto, size + 40px); width: 80px; background: green;">
+ <div style="width: 60px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-004.html
new file mode 100644
index 00000000000..b80e608eb5d
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-004.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#min-size-auto">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="display: flex; flex-direction: column; width: 100px; height: 0px;">
+ <!-- The auto min-size is 80px, then through the calc-size() is 100px. -->
+ <div style="min-height: calc-size(auto, size + 20px); background: green;">
+ <div style="height: 80px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-005.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-005.html
new file mode 100644
index 00000000000..d535085552e
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-005.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#min-size-auto">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="display: flex; flex-direction: column; height: 0px; width: 100px;">
+ <!-- The auto min-size is 60px (the specified-size suggestion), then through the calc-size() is 100px. -->
+ <div style="min-height: calc-size(auto, size + 40px); height: 60px; background: green;">
+ <div style="height: 80px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-006.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-006.html
new file mode 100644
index 00000000000..c19e726dcb8
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-006.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#min-size-auto">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="display: flex; flex-direction: column; height: 0px; width: 100px;">
+ <!-- The auto min-size is 60px (the content-size suggestion), then through the calc-size() is 100px. -->
+ <div style="min-height: calc-size(auto, size + 40px); height: 80px; background: green;">
+ <div style="height: 60px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-007.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-007.html
new file mode 100644
index 00000000000..c4531a0884a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-007.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square.xht">
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+<div style="display: flex; flex-wrap: wrap-reverse; width: 100px; height: 100px; position: relative;">
+ <div style="background: red; max-height: 80px; height: calc-size(auto, size / 2); flex: 1;"></div>
+ <div style="background: green; position: absolute; inset: 0;"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-008.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-008.html
new file mode 100644
index 00000000000..8df02f910a0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-008.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square.xht">
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+<style>
+#flex {
+ display: flex;
+ flex-direction: column;
+ flex-wrap: wrap;
+
+ width: 100px;
+ height: calc-size(auto, min(size, 100px));
+ background: red;
+}
+
+#flex > div {
+ width: 100px;
+ height: 60px;
+ background: green;
+}
+</style>
+<!-- There should only be one flex-line as at the time of determining the
+ available main-size it is indefinite, and calc-size() shouldn't resolve.
+ See: https://drafts.csswg.org/css-flexbox-1/#algo-available -->
+<div id="flex">
+ <div></div>
+ <div></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-009.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-009.html
new file mode 100644
index 00000000000..6bf3cd9bc56
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-flex-009.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square.xht">
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+<div style="display: flex; width: 100px; height: 100px; background: red;">
+ <!-- The auto min-size will be min(80px, 50px) == 50px.
+ Then passed through the calc-size, it'll be 100px. -->
+ <div style="min-width: calc-size(auto, size * 2); width: 80px; background: green;">
+ <div style="width: 50px;"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-grid-repeat.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-grid-repeat.html
new file mode 100644
index 00000000000..b72ca80818f
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-grid-repeat.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="width: fit-content; background: green; height: 100px;">
+ <!-- During the min/max content size calculation, the min-content isn't resolvable. -->
+ <div style="box-sizing: border-box; padding: 5px; display: grid; grid-template-columns: repeat(auto-fill, 90px); min-width: 60px; max-width: calc-size(min-content, size * 1000);"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-001.html
new file mode 100644
index 00000000000..f39169636ca
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-001.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="width: 0px;">
+ <!-- The min-content, max-content size of this div should be: 100px, 120px respectively. -->
+ <div style="background: green; width: fit-content; height: 100px;">
+ <div style="width: calc-size(auto, size + 80px);">
+ <span style="display: inline-block; width: 20px;"></span><span style="display: inline-block; width: 20px;"></span>
+ </div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-002.html
new file mode 100644
index 00000000000..398977a6abf
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-002.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="width: 200px;">
+ <!-- The min-content, max-content size of this div should be: 80px, 100px respectively. -->
+ <div style="background: green; width: fit-content; height: 100px;">
+ <div style="width: calc-size(auto, size + 60px);">
+ <span style="display: inline-block; width: 20px;"></span><span style="display: inline-block; width: 20px;"></span>
+ </div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-003.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-003.html
new file mode 100644
index 00000000000..894c8b9a63d
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-003.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/../../reference/ref-filled-green-100px-square-only.html">
+<p>Test passes if there is a filled green square.</p>
+<div style="width: 100px;">
+ <!-- The min-content, max-content size of this div should be: 90px, 110px respectively. -->
+ <div style="background: green; width: fit-content; height: 100px;">
+ <div style="width: calc-size(auto, size + 70px);">
+ <span style="display: inline-block; width: 20px;"></span><span style="display: inline-block; width: 20px;"></span>
+ </div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-no-body-height-quirk-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-no-body-height-quirk-001.html
new file mode 100644
index 00000000000..514db3897de
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-no-body-height-quirk-001.html
@@ -0,0 +1,15 @@
+<!-- This test is intentionally in quirks mode. -->
+<title>calc-size() and the body fills html quirk</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://quirks.spec.whatwg.org/#the-body-element-fills-the-html-element-quirk">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-values/calc-size/calc-size-no-body-height-quirk-001-ref.html">
+<style>
+
+body {
+ height: calc-size(auto, size);
+ border: medium solid;
+}
+
+</style>
+
+<body>The bottom border of this box should be close to the text, not at the bottom of the viewport.</body>
diff --git a/Tests/LibWeb/Text/expected/css/calc-size-auto-basis-in-min-sizes-grid-and-flex.txt b/Tests/LibWeb/Text/expected/css/calc-size-auto-basis-in-min-sizes-grid-and-flex.txt
new file mode 100644
index 00000000000..8820217a992
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/calc-size-auto-basis-in-min-sizes-grid-and-flex.txt
@@ -0,0 +1,5 @@
+min-width: 50x5
+min-height: 800x30
+grid-item: 800x100
+flex-cross: 0x100
+flex-min-percentage: 40x0
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-column.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-column.txt
new file mode 100644
index 00000000000..287f2ee8b63
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-column.txt
@@ -0,0 +1,39 @@
+Harness status: OK
+
+Found 33 tests
+
+32 Pass
+1 Fail
+Pass resolved value for height resulting from flex-basis: 274px
+Pass resolved value for height resulting from flex-basis: min-content
+Pass resolved value for height resulting from flex-basis: fit-content
+Pass resolved value for height resulting from flex-basis: max-content
+Pass resolved value for height resulting from flex-basis: content
+Pass resolved value for height resulting from flex-basis: auto
+Pass resolved value for height resulting from flex-basis: calc-size(any, 357px)
+Pass resolved value for height resulting from flex-basis: calc-size(any, 220%)
+Pass resolved value for height resulting from flex-basis: calc-size(max-content, 350%)
+Pass resolved value for height resulting from flex-basis: calc-size(fit-content, 172px)
+Pass resolved value for height resulting from flex-basis: calc-size(37px, 193px)
+Pass resolved value for height resulting from flex-basis: calc-size(83px, size * 3)
+Pass resolved value for height resulting from flex-basis: calc-size(min-content, size / 0.25)
+Pass resolved value for height resulting from flex-basis: calc-size(max-content, size * 5.2)
+Pass resolved value for height resulting from flex-basis: calc-size(fit-content, size / 4 + 60px)
+Fail resolved value for height resulting from flex-basis: calc-size(stretch, size * 2 - 10%)
+Pass resolved value for height resulting from flex-basis: calc-size(30px, 15em)
+Pass resolved value for height resulting from flex-basis: calc-size(calc-size(any, 30px), 15em)
+Pass resolved value for height resulting from flex-basis: calc-size(calc-size(2in, 30px), 15em)
+Pass resolved value for height resulting from flex-basis: calc-size(calc-size(min-content, 30px), 15em)
+Pass resolved value for height resulting from flex-basis: calc-size(calc-size(min-content, size), size * 3)
+Pass resolved value for height resulting from flex-basis: calc-size(auto, size)
+Pass resolved value for height resulting from flex-basis: calc-size(auto, size * 1.6 + 23px)
+Pass resolved value for height resulting from flex-basis: calc-size(content, size)
+Pass resolved value for height resulting from flex-basis: calc-size(content, size * 1.6 + 23px)
+Pass resolved value for height resulting from flex-basis: auto and height: auto
+Pass resolved value for height resulting from flex-basis: calc-size(auto, size * 3) and height: auto
+Pass resolved value for height resulting from flex-basis: auto and height: calc-size(auto, size * 7)
+Pass resolved value for height resulting from flex-basis: calc-size(auto, size * 7) and height: calc-size(auto, size * 3)
+Pass resolved value for height resulting from flex-basis: auto and height: calc-size(max-content, size + 12px)
+Pass resolved value for height resulting from flex-basis: calc-size(auto, size + 4px) and height: calc-size(fit-content, size + 12px)
+Pass resolved value for height resulting from flex-basis: 372px and height: calc-size(fit-content, size + 12px)
+Pass resolved value for height resulting from flex-basis: calc-size(content, size * 7) and height: 321px
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-row.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-row.txt
new file mode 100644
index 00000000000..6d4ad1015a0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-row.txt
@@ -0,0 +1,39 @@
+Harness status: OK
+
+Found 33 tests
+
+32 Pass
+1 Fail
+Pass resolved value for width resulting from flex-basis: 274px
+Pass resolved value for width resulting from flex-basis: min-content
+Pass resolved value for width resulting from flex-basis: fit-content
+Pass resolved value for width resulting from flex-basis: max-content
+Pass resolved value for width resulting from flex-basis: content
+Pass resolved value for width resulting from flex-basis: auto
+Pass resolved value for width resulting from flex-basis: calc-size(any, 357px)
+Pass resolved value for width resulting from flex-basis: calc-size(any, 31%)
+Pass resolved value for width resulting from flex-basis: calc-size(max-content, 31%)
+Pass resolved value for width resulting from flex-basis: calc-size(fit-content, 172px)
+Pass resolved value for width resulting from flex-basis: calc-size(37px, 193px)
+Pass resolved value for width resulting from flex-basis: calc-size(83px, size * 3)
+Pass resolved value for width resulting from flex-basis: calc-size(min-content, size / 2)
+Pass resolved value for width resulting from flex-basis: calc-size(max-content, size * 1.2)
+Pass resolved value for width resulting from flex-basis: calc-size(fit-content, size / 4 + 30px)
+Fail resolved value for width resulting from flex-basis: calc-size(stretch, size / 2 - 10%)
+Pass resolved value for width resulting from flex-basis: calc-size(30px, 15em)
+Pass resolved value for width resulting from flex-basis: calc-size(calc-size(any, 30px), 15em)
+Pass resolved value for width resulting from flex-basis: calc-size(calc-size(2in, 30px), 15em)
+Pass resolved value for width resulting from flex-basis: calc-size(calc-size(min-content, 30px), 15em)
+Pass resolved value for width resulting from flex-basis: calc-size(calc-size(min-content, size), size)
+Pass resolved value for width resulting from flex-basis: calc-size(auto, size)
+Pass resolved value for width resulting from flex-basis: calc-size(auto, size * 1.6 + 23px)
+Pass resolved value for width resulting from flex-basis: calc-size(content, size)
+Pass resolved value for width resulting from flex-basis: calc-size(content, size / 2)
+Pass resolved value for width resulting from flex-basis: auto and width: auto
+Pass resolved value for width resulting from flex-basis: calc-size(auto, size * 2) and width: auto
+Pass resolved value for width resulting from flex-basis: auto and width: calc-size(auto, size * 1.5 + 5px)
+Pass resolved value for width resulting from flex-basis: calc-size(auto, size + 14px) and width: calc-size(auto, size * 1.5)
+Pass resolved value for width resulting from flex-basis: auto and width: calc-size(max-content, size + 12px)
+Pass resolved value for width resulting from flex-basis: calc-size(auto, size + 4px) and width: calc-size(fit-content, size + 12px)
+Pass resolved value for width resulting from flex-basis: 472px and width: calc-size(fit-content, size + 12px)
+Pass resolved value for width resulting from flex-basis: calc-size(content, size * 1.5 + 4px) and width: 321px
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-list-box.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-list-box.txt
new file mode 100644
index 00000000000..80eb1c8ea74
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-list-box.txt
@@ -0,0 +1,15 @@
+Harness status: OK
+
+Found 10 tests
+
+10 Pass
+Pass shrink-to-fit parent of a list box with width: calc-size(max-content, size)
+Pass shrink-to-fit parent of a list box with width: calc-size(max-content, size * 2)
+Pass shrink-to-fit parent of a list box with width: calc-size(max-content, size + 25px)
+Pass shrink-to-fit parent of a list box with width: calc-size(min-content, size)
+Pass shrink-to-fit parent of a list box with width: calc-size(min-content, size + 25px)
+Pass shrink-to-fit parent of a drop-down box with width: calc-size(max-content, size)
+Pass shrink-to-fit parent of a drop-down box with width: calc-size(max-content, size * 2)
+Pass shrink-to-fit parent of a drop-down box with width: calc-size(max-content, size + 25px)
+Pass shrink-to-fit parent of a drop-down box with width: calc-size(min-content, size)
+Pass shrink-to-fit parent of a drop-down box with width: calc-size(min-content, size + 25px)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-replaced-element.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-replaced-element.txt
new file mode 100644
index 00000000000..e6f4a351539
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-replaced-element.txt
@@ -0,0 +1,38 @@
+Harness status: OK
+
+Found 33 tests
+
+33 Pass
+Pass used width, width: max-content — keyword on its own, for comparison
+Pass shrink-to-fit parent, width: max-content — keyword on its own, for comparison
+Pass used width, width: calc-size(max-content, size) — identity
+Pass shrink-to-fit parent, width: calc-size(max-content, size) — identity
+Pass used width, width: calc-size(max-content, size * 2)
+Pass shrink-to-fit parent, width: calc-size(max-content, size * 2)
+Pass used width, width: calc-size(min-content, size / 2)
+Pass shrink-to-fit parent, width: calc-size(min-content, size / 2)
+Pass used width, width: calc-size(min-content, size + 15px)
+Pass shrink-to-fit parent, width: calc-size(min-content, size + 15px)
+Pass used width, width: calc-size(fit-content, size + 10px)
+Pass shrink-to-fit parent, width: calc-size(fit-content, size + 10px)
+Pass used width, width: calc-size(max-content, 90px) — calculation never mentions size
+Pass shrink-to-fit parent, width: calc-size(max-content, 90px) — calculation never mentions size
+Pass used width, width: calc-size(calc-size(min-content, size * 2), size + 5px) — nested: the outer size is the inner result
+Pass shrink-to-fit parent, width: calc-size(calc-size(min-content, size * 2), size + 5px) — nested: the outer size is the inner result
+Pass used width, width: calc-size(auto, size) — an auto basis is the intrinsic width
+Pass shrink-to-fit parent, width: calc-size(auto, size) — an auto basis is the intrinsic width
+Pass used width, width: calc-size(auto, size * 2)
+Pass shrink-to-fit parent, width: calc-size(auto, size * 2)
+Pass used width, width: calc-size(auto, size / 2)
+Pass shrink-to-fit parent, width: calc-size(auto, size / 2)
+Pass used width, width: calc-size(auto, size + 15px)
+Pass shrink-to-fit parent, width: calc-size(auto, size + 15px)
+Pass the image has the intrinsic width the expectations are built on
+Pass used height, height: calc-size(auto, size)
+Pass width from the aspect ratio, height: calc-size(auto, size)
+Pass used height, height: calc-size(auto, size * 2)
+Pass width from the aspect ratio, height: calc-size(auto, size * 2)
+Pass used height, height: calc-size(max-content, size / 2)
+Pass width from the aspect ratio, height: calc-size(max-content, size / 2)
+Pass used height, height: calc-size(min-content, size + 10px)
+Pass width from the aspect ratio, height: calc-size(min-content, size + 10px)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-table.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-table.txt
new file mode 100644
index 00000000000..210707dac67
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-table.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Pass
+Pass shrink-to-fit parent of a table with width: calc-size(max-content, size)
+Pass shrink-to-fit parent of a table with width: calc-size(max-content, size * 2)
+Pass shrink-to-fit parent of a table with width: calc-size(max-content, size + 25px)
+Pass shrink-to-fit parent of a table with width: calc-size(min-content, size)
+Pass shrink-to-fit parent of a table with width: calc-size(min-content, size + 25px)
+Pass shrink-to-fit parent of a table with width: calc-size(max-content, 90px) — calculation never mentions size
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-typed-om.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-typed-om.txt
new file mode 100644
index 00000000000..76443b38051
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-typed-om.txt
@@ -0,0 +1,25 @@
+Harness status: OK
+
+Found 19 tests
+
+18 Pass
+1 Fail
+Pass computedStyleMap serialization of width: calc-size(auto, size)
+Pass computedStyleMap serialization of width: calc-size(fit-content, size)
+Pass computedStyleMap serialization of height: calc-size(min-content, size * 2)
+Fail computedStyleMap serialization of max-width: calc-size(max-content, size / 2)
+Pass computedStyleMap serialization of max-height: calc-size(fit-content, 30px + size / 2)
+Pass computedStyleMap serialization of width: calc-size(fit-content, 50% + size / 2)
+Pass computedStyleMap serialization of width: calc-size(any, 30px)
+Pass computedStyleMap serialization of width: calc-size(any, 25em)
+Pass computedStyleMap serialization of width: calc-size(any, 40%)
+Pass computedStyleMap serialization of width: calc-size(any, 50px + 30%)
+Pass computedStyleMap serialization of width: calc-size(any, min(30px, 2em))
+Pass computedStyleMap serialization of width: calc-size(10px, sign(size) * size)
+Pass computedStyleMap serialization of width: calc-size(30px, 25em)
+Pass computedStyleMap serialization of width: calc-size(calc-size(any, 30px), size)
+Pass computedStyleMap serialization of width: calc-size(calc-size(any, 30px), 25em)
+Pass computedStyleMap serialization of width: calc-size(calc-size(2in, 30px), 25em)
+Pass computedStyleMap serialization of width: calc-size(calc-size(min-content, 30px), 25em)
+Pass computedStyleMap serialization of width: calc-size(calc-size(min-content, size), size)
+Pass computedStyleMap serialization of width: calc-size(calc-size(fit-content, size * 2), size)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/css/calc-size-auto-basis-in-min-sizes-grid-and-flex.html b/Tests/LibWeb/Text/input/css/calc-size-auto-basis-in-min-sizes-grid-and-flex.html
new file mode 100644
index 00000000000..2cefa6988ca
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/calc-size-auto-basis-in-min-sizes-grid-and-flex.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+</style>
+<!-- A minimum given as a calc-size() with an auto basis resolves against the automatic minimum size, which is zero for
+ these boxes. -->
+<div style="width: 300px"><div id="min-width" style="width: 0; min-width: calc-size(auto, size + 50px); height: 5px; float: left"></div></div>
+<div id="min-height" style="height: 0; min-height: calc-size(auto, size + 30px)"><div style="height: 10px"></div></div>
+<!-- A preferred size given as a calc-size() with an auto basis resolves against the stretched size of a grid item, and
+ of a flex item in the cross axis. -->
+<div style="display: grid; height: 200px"><div id="grid-item" style="height: calc-size(auto, size * 0.5)"><div style="height: 40px"></div></div></div>
+<div style="display: flex; height: 200px"><div id="flex-cross" style="height: calc-size(auto, size * 0.5)"><div style="height: 40px"></div></div></div>
+<!-- The percentages of a flex item's calc-size() minimum resolve against the flex container's main size. -->
+<div style="display: flex; width: 400px"><div id="flex-min-percentage" style="min-width: calc-size(auto, size + 10%); width: 0"><div style="width: 20px"></div></div><div style="width: 400px; flex-shrink: 0"></div></div>
+<script>
+ test(() => {
+ for (const id of ["min-width", "min-height", "grid-item", "flex-cross", "flex-min-percentage"]) {
+ const element = document.getElementById(id);
+ println(`${id}: ${element.offsetWidth}x${element.offsetHeight}`);
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-column.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-column.html
new file mode 100644
index 00000000000..6e66e937ac6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-column.html
@@ -0,0 +1,92 @@
+<!DOCTYPE html>
+<title>calc-size() on flex-basis</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#flex-basis-property">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<style>
+ #container {
+ display: flex;
+ flex-direction: column;
+ font-family: 'Ahem';
+ font-size: 20px;
+ height: 200px;
+ }
+ #target {
+ height: 50px;
+ flex-grow: 0;
+ flex-shrink: 0;
+ }
+</style>
+
+<div id="container">
+ <!--
+ Given the contents of #target (and the Ahem font), intrinsic height is 1ch or 20px
+ -->
+ <div id="target">hello</div>
+</div>
+
+<script>
+
+setup({explicit_done: true});
+document.fonts.ready.then(()=> {
+ let basic_tests = [
+ /* I think flex layout rules require expectations be >= 20px */
+ { value: "274px", expected: "274px" },
+ { value: "min-content", expected: "20px" },
+ { value: "fit-content", expected: "20px" },
+ { value: "max-content", expected: "20px" },
+ { value: "content", expected: "20px" },
+ { value: "auto", expected: "50px" },
+ { value: "calc-size(any, 357px)", expected: "357px" },
+ { value: "calc-size(any, 220%)", expected: "440px" },
+ { value: "calc-size(max-content, 350%)", expected: "700px" },
+ { value: "calc-size(fit-content, 172px)", expected: "172px" },
+ { value: "calc-size(37px, 193px)", expected: "193px" },
+ { value: "calc-size(83px, size * 3)", expected: "249px" },
+ { value: "calc-size(min-content, size / 0.25)", expected: "80px" },
+ { value: "calc-size(max-content, size * 5.2)", expected: "104px" },
+ { value: "calc-size(fit-content, size / 4 + 60px)", expected: "65px" },
+ { value: "calc-size(stretch, size * 2 - 10%)", expected: "380px" },
+ { value: "calc-size(30px, 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(any, 30px), 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(2in, 30px), 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(min-content, 30px), 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(min-content, size), size * 3)", expected: "60px" },
+ { value: "calc-size(auto, size)", expected: "50px" },
+ { value: "calc-size(auto, size * 1.6 + 23px)", expected: "103px" },
+ { value: "calc-size(content, size)", expected: "20px" },
+ { value: "calc-size(content, size * 1.6 + 23px)", expected: "55px" },
+ { value: "auto", height_value: "auto", expected: "20px" },
+ { value: "calc-size(auto, size * 3)", height_value: "auto", expected: "60px" },
+ { value: "auto", height_value: "calc-size(auto, size * 7)", expected: "140px" },
+ { value: "calc-size(auto, size * 7)", height_value: "calc-size(auto, size * 3)", expected: "420px" },
+ { value: "auto", height_value: "calc-size(max-content, size + 12px)", expected: "32px" },
+ { value: "calc-size(auto, size + 4px)", height_value: "calc-size(fit-content, size + 12px)", expected: "36px" },
+ { value: "372px", height_value: "calc-size(fit-content, size + 12px)", expected: "372px" },
+ { value: "calc-size(content, size * 7)", height_value: "321px", expected: "140px" },
+ ];
+ const container = document.getElementById("container");
+ const target = document.getElementById("target");
+ const target_cs = getComputedStyle(target);
+ for (const obj of basic_tests) {
+ test((t) => {
+ target.style.removeProperty("flex-basis");
+ target.style.flexBasis = obj.value;
+ assert_not_equals(target.style.flexBasis, "", "flex-basis value is accepted");
+
+ target.style.removeProperty("height");
+ if ("height_value" in obj) {
+ target.style.height = obj.height_value;
+ assert_not_equals(target.style.height, "", "height value is accepted");
+ }
+
+ assert_equals(target_cs.height, obj.expected, "resulting height is correct");
+ }, `resolved value for height resulting from flex-basis: ${obj.value}${obj.height_value ? ` and height: ${obj.height_value}` : ""}`);
+ }
+
+ done();
+});
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-row.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-row.html
new file mode 100644
index 00000000000..fe9fabf08bd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-flex-basis-on-row.html
@@ -0,0 +1,94 @@
+<!DOCTYPE html>
+<title>calc-size() on flex-basis</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#flex-basis-property">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<style>
+ #container {
+ display: flex;
+ flex-direction: row;
+ font-family: 'Ahem';
+ font-size: 20px;
+ width: 500px;
+ }
+ #target {
+ width: 125px;
+ flex-grow: 0;
+ flex-shrink: 0;
+ }
+</style>
+
+<div id="container">
+ <!--
+ Given the contents of #target (and the Ahem font):
+ max-content width is 30ch or 600px
+ min-content width is 20ch or 400px
+ -->
+ <div id="target">ninechars twenty_characters___</div>
+</div>
+
+<script>
+
+setup({explicit_done: true});
+document.fonts.ready.then(()=> {
+ let basic_tests = [
+ /* I think flex layout rules require expectations be greater than width or min-content width */
+ { value: "274px", expected: "274px" },
+ { value: "min-content", expected: "400px" },
+ { value: "fit-content", expected: "500px" },
+ { value: "max-content", expected: "600px" },
+ { value: "content", expected: "600px" },
+ { value: "auto", expected: "125px" },
+ { value: "calc-size(any, 357px)", expected: "357px" },
+ { value: "calc-size(any, 31%)", expected: "155px" },
+ { value: "calc-size(max-content, 31%)", expected: "155px" },
+ { value: "calc-size(fit-content, 172px)", expected: "172px" },
+ { value: "calc-size(37px, 193px)", expected: "193px" },
+ { value: "calc-size(83px, size * 3)", expected: "249px" },
+ { value: "calc-size(min-content, size / 2)", expected: "200px" },
+ { value: "calc-size(max-content, size * 1.2)", expected: "720px" },
+ { value: "calc-size(fit-content, size / 4 + 30px)", expected: "155px" },
+ { value: "calc-size(stretch, size / 2 - 10%)", expected: "200px" },
+ { value: "calc-size(30px, 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(any, 30px), 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(2in, 30px), 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(min-content, 30px), 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(min-content, size), size)", expected: "400px" },
+ { value: "calc-size(auto, size)", expected: "125px" },
+ { value: "calc-size(auto, size * 1.6 + 23px)", expected: "223px" },
+ { value: "calc-size(content, size)", expected: "600px" },
+ { value: "calc-size(content, size / 2)", expected: "300px" },
+ { value: "auto", width_value: "auto", expected: "600px" },
+ { value: "calc-size(auto, size * 2)", width_value: "auto", expected: "1200px" },
+ { value: "auto", width_value: "calc-size(auto, size * 1.5 + 5px)", expected: "905px" },
+ { value: "calc-size(auto, size + 14px)", width_value: "calc-size(auto, size * 1.5)", expected: "914px" },
+ { value: "auto", width_value: "calc-size(max-content, size + 12px)", expected: "612px" },
+ { value: "calc-size(auto, size + 4px)", width_value: "calc-size(fit-content, size + 12px)", expected: "516px" },
+ { value: "472px", width_value: "calc-size(fit-content, size + 12px)", expected: "472px" },
+ { value: "calc-size(content, size * 1.5 + 4px)", width_value: "321px", expected: "904px" },
+ ];
+ const container = document.getElementById("container");
+ const target = document.getElementById("target");
+ const target_cs = getComputedStyle(target);
+ for (const obj of basic_tests) {
+ test((t) => {
+ target.style.removeProperty("flex-basis");
+ target.style.flexBasis = obj.value;
+ assert_not_equals(target.style.flexBasis, "", "flex-basis value is accepted");
+
+ target.style.removeProperty("width");
+ if ("width_value" in obj) {
+ target.style.width = obj.width_value;
+ assert_not_equals(target.style.width, "", "width value is accepted");
+ }
+
+ assert_equals(target_cs.width, obj.expected, "resulting width is correct");
+ }, `resolved value for width resulting from flex-basis: ${obj.value}${obj.width_value ? ` and width: ${obj.width_value}` : ""}`);
+ }
+
+ done();
+});
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-list-box.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-list-box.html
new file mode 100644
index 00000000000..23f331ea1ba
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-list-box.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>calc-size() with an intrinsic basis on a select</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<style>
+ .shrink { display: inline-block; }
+</style>
+
+<div id="log"></div>
+<div id="container"></div>
+
+<script>
+// A select sizes itself from its options and its own chrome, which differs between UAs, so every
+// expectation is derived from what this UA reports for the basis keyword on an identical control.
+const cases = [
+ { basis: "max-content", width: "calc-size(max-content, size)", expected: ref => ref },
+ { basis: "max-content", width: "calc-size(max-content, size * 2)", expected: ref => ref * 2 },
+ { basis: "max-content", width: "calc-size(max-content, size + 25px)", expected: ref => ref + 25 },
+ { basis: "min-content", width: "calc-size(min-content, size)", expected: ref => ref },
+ { basis: "min-content", width: "calc-size(min-content, size + 25px)", expected: ref => ref + 25 },
+];
+
+// A list box and a drop-down box are laid out by different code, and a UA is free to render either
+// one as the other.
+const controls = [
+ { name: "list box", markup: "<select size='2'><option>aaaaaaaaaa</option><option>bb</option></select>" },
+ { name: "drop-down box", markup: "<select><option>aaaaaaaaaa</option><option>bb</option></select>" },
+];
+
+const container = document.getElementById("container");
+
+function addSelect(markup, width) {
+ const shrink = document.createElement("div");
+ shrink.className = "shrink";
+ shrink.innerHTML = markup;
+ shrink.firstChild.style.width = width;
+ container.appendChild(shrink);
+ return shrink;
+}
+
+for (const control of controls) {
+ control.references = {
+ "max-content": addSelect(control.markup, "max-content"),
+ "min-content": addSelect(control.markup, "min-content"),
+ };
+ control.cases = cases.map(testCase => ({ ...testCase, shrink: addSelect(control.markup, testCase.width) }));
+}
+
+for (const control of controls) {
+ for (const testCase of control.cases) {
+ test(() => {
+ const reference = control.references[testCase.basis].getBoundingClientRect().width;
+ assert_greater_than(reference, 0, "the " + testCase.basis + " reference should be measurable");
+ assert_equals(testCase.shrink.getBoundingClientRect().width, testCase.expected(reference),
+ "against a " + testCase.basis + " reference of " + reference + "px");
+ }, "shrink-to-fit parent of a " + control.name + " with width: " + testCase.width);
+ }
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-replaced-element.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-replaced-element.html
new file mode 100644
index 00000000000..3df6550c572
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-replaced-element.html
@@ -0,0 +1,107 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>calc-size() with an intrinsic basis on a replaced element</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<style>
+ .shrink { display: inline-block; }
+ .shrink > img { display: block; }
+</style>
+
+<div id="log"></div>
+<div id="container"></div>
+
+<script>
+// A 60x20 image, so max-content and min-content are both 60px and every expectation below is exact.
+const SRC = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='20'%3E%3C/svg%3E";
+const INTRINSIC = 60;
+
+const cases = [
+ { width: "max-content", expected: 60, note: "keyword on its own, for comparison" },
+ { width: "calc-size(max-content, size)", expected: 60, note: "identity" },
+ { width: "calc-size(max-content, size * 2)", expected: 120 },
+ { width: "calc-size(min-content, size / 2)", expected: 30 },
+ { width: "calc-size(min-content, size + 15px)", expected: 75 },
+ { width: "calc-size(fit-content, size + 10px)", expected: 70 },
+ { width: "calc-size(max-content, 90px)", expected: 90, note: "calculation never mentions size" },
+ { width: "calc-size(calc-size(min-content, size * 2), size + 5px)", expected: 125, note: "nested: the outer size is the inner result" },
+ { width: "calc-size(auto, size)", expected: 60, note: "an auto basis is the intrinsic width" },
+ { width: "calc-size(auto, size * 2)", expected: 120 },
+ { width: "calc-size(auto, size / 2)", expected: 30 },
+ { width: "calc-size(auto, size + 15px)", expected: 75 },
+];
+
+// The image is 60x20, so a height written this way carries the width along through the 3:1 ratio.
+const blockAxisCases = [
+ { height: "calc-size(auto, size)", expectedHeight: 20, expectedWidth: 60 },
+ { height: "calc-size(auto, size * 2)", expectedHeight: 40, expectedWidth: 120 },
+ { height: "calc-size(max-content, size / 2)", expectedHeight: 10, expectedWidth: 30 },
+ { height: "calc-size(min-content, size + 10px)", expectedHeight: 30, expectedWidth: 90 },
+];
+
+const container = document.getElementById("container");
+
+function build() {
+ for (const testCase of cases) {
+ const shrink = document.createElement("div");
+ shrink.className = "shrink";
+ const img = document.createElement("img");
+ img.src = SRC;
+ img.style.width = testCase.width;
+ shrink.appendChild(img);
+ container.appendChild(shrink);
+ testCase.img = img;
+ testCase.shrink = shrink;
+ }
+
+ for (const testCase of blockAxisCases) {
+ const img = document.createElement("img");
+ img.src = SRC;
+ img.style.height = testCase.height;
+ img.style.display = "block";
+ container.appendChild(img);
+ testCase.img = img;
+ }
+}
+
+function run() {
+ for (const testCase of cases) {
+ const label = "width: " + testCase.width + (testCase.note ? " — " + testCase.note : "");
+
+ test(() => {
+ assert_equals(testCase.img.getBoundingClientRect().width, testCase.expected);
+ }, "used width, " + label);
+
+ // The width a replaced element contributes to a shrink-to-fit parent is the result of the
+ // calculation, not the size of its basis keyword.
+ test(() => {
+ assert_equals(testCase.shrink.getBoundingClientRect().width, testCase.expected);
+ }, "shrink-to-fit parent, " + label);
+ }
+
+ test(() => {
+ assert_equals(cases[0].img.getBoundingClientRect().width, INTRINSIC,
+ "the image is not the size this test assumes");
+ }, "the image has the intrinsic width the expectations are built on");
+
+ for (const testCase of blockAxisCases) {
+ test(() => {
+ assert_equals(testCase.img.getBoundingClientRect().height, testCase.expectedHeight);
+ }, "used height, height: " + testCase.height);
+
+ test(() => {
+ assert_equals(testCase.img.getBoundingClientRect().width, testCase.expectedWidth);
+ }, "width from the aspect ratio, height: " + testCase.height);
+ }
+}
+
+build();
+
+setup({ explicit_done: true });
+addEventListener("load", () => {
+ run();
+ done();
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-table.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-table.html
new file mode 100644
index 00000000000..34d5dffa96c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-table.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>calc-size() with an intrinsic basis on a table</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<style>
+ .shrink { display: inline-block; }
+ table { border-spacing: 0; }
+ td { padding: 0; width: 60px; height: 20px; background: #ccc; }
+</style>
+
+<div id="log"></div>
+<div id="container"></div>
+
+<script>
+// Every expectation is derived from what this UA reports for the basis keyword on the same table, so
+// nothing here depends on a particular intrinsic width.
+const cases = [
+ { basis: "max-content", width: "calc-size(max-content, size)", expected: ref => ref },
+ { basis: "max-content", width: "calc-size(max-content, size * 2)", expected: ref => ref * 2 },
+ { basis: "max-content", width: "calc-size(max-content, size + 25px)", expected: ref => ref + 25 },
+ { basis: "min-content", width: "calc-size(min-content, size)", expected: ref => ref },
+ { basis: "min-content", width: "calc-size(min-content, size + 25px)", expected: ref => ref + 25 },
+ { basis: "max-content", width: "calc-size(max-content, 90px)", expected: () => 90,
+ note: "calculation never mentions size" },
+];
+
+const container = document.getElementById("container");
+
+function addTable(width) {
+ const shrink = document.createElement("div");
+ shrink.className = "shrink";
+ shrink.innerHTML = "<table><tr><td></td></tr></table>";
+ shrink.firstChild.style.width = width;
+ container.appendChild(shrink);
+ return shrink;
+}
+
+// One reference per basis keyword, measured rather than assumed.
+const references = {
+ "max-content": addTable("max-content"),
+ "min-content": addTable("min-content"),
+};
+
+for (const testCase of cases)
+ testCase.shrink = addTable(testCase.width);
+
+for (const testCase of cases) {
+ const label = "width: " + testCase.width + (testCase.note ? " — " + testCase.note : "");
+
+ test(() => {
+ const reference = references[testCase.basis].getBoundingClientRect().width;
+ assert_equals(testCase.shrink.getBoundingClientRect().width, testCase.expected(reference),
+ "against a " + testCase.basis + " reference of " + reference + "px");
+ }, "shrink-to-fit parent of a table with " + label);
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-typed-om.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-typed-om.html
new file mode 100644
index 00000000000..7a5251753cd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-typed-om.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<title>calc-size() expressions</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script src="../../support/parsing-testcommon.js"></script>
+
+<div id="test"></div>
+
+<script>
+
+let e = document.getElementById("test");
+
+function test_typed_om_string(property, value, expected) {
+ test((t) => {
+ if (!expected) {
+ expected = value;
+ }
+ e.style.setProperty(property, value);
+ let actual = e.computedStyleMap().get(property).toString();
+ assert_equals(actual, expected);
+ e.style.setProperty(property, "");
+ }, `computedStyleMap serialization of ${property}: ${value}`);
+}
+
+test_typed_om_string("width", "calc-size(auto, size)");
+test_typed_om_string("width", "calc-size(fit-content, size)");
+test_typed_om_string("height", "calc-size(min-content, size * 2)", "calc-size(min-content, 2 * size)");
+test_typed_om_string("max-width", "calc-size(max-content, size / 2)", "calc-size(max-content, (0.5 * size))");
+test_typed_om_string("max-height", "calc-size(fit-content, 30px + size / 2)", "calc-size(fit-content, 30px + (0.5 * size))");
+test_typed_om_string("width", "calc-size(fit-content, 50% + size / 2)", "calc-size(fit-content, 50% + (0.5 * size))");
+test_typed_om_string("width", "calc-size(any, 30px)");
+test_typed_om_string("width", "calc-size(any, 25em)", "calc-size(any, 400px)");
+test_typed_om_string("width", "calc-size(any, 40%)");
+test_typed_om_string("width", "calc-size(any, 50px + 30%)", "calc-size(any, 30% + 50px)");
+test_typed_om_string("width", "calc-size(any, min(30px, 2em))", "calc-size(any, 30px)");
+test_typed_om_string("width", "calc-size(10px, sign(size) * size)");
+test_typed_om_string("width", "calc-size(30px, 25em)", "calc-size(30px, 400px)");
+test_typed_om_string("width", "calc-size(calc-size(any, 30px), size)");
+test_typed_om_string("width", "calc-size(calc-size(any, 30px), 25em)", "calc-size(calc-size(any, 30px), 400px)");
+test_typed_om_string("width", "calc-size(calc-size(2in, 30px), 25em)", "calc-size(calc-size(192px, 30px), 400px)");
+test_typed_om_string("width", "calc-size(calc-size(min-content, 30px), 25em)", "calc-size(calc-size(min-content, 30px), 400px)");
+test_typed_om_string("width", "calc-size(calc-size(min-content, size), size)");
+test_typed_om_string("width", "calc-size(calc-size(fit-content, size * 2), size)", "calc-size(calc-size(fit-content, 2 * size), size)");
+
+</script>