Skip to content

Truly independent web browser

LibWeb: Implement calc-size() for sizing properties

Andreas Klingbdd416cb4962
diff --git a/Libraries/LibWeb/CSS/Keywords.json b/Libraries/LibWeb/CSS/Keywords.json
index d764a4d1010..d3b79b44ac2 100644
--- a/Libraries/LibWeb/CSS/Keywords.json
+++ b/Libraries/LibWeb/CSS/Keywords.json
@@ -47,6 +47,7 @@
"ancient-tamil",
"anonymous",
"antialiased",
+ "any",
"anywhere",
"appworkspace",
"arabic-abjad",
diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rs
index 91b43e1f60e..76cc5faff43 100644
--- a/Libraries/LibWeb/Rust/src/css/calc.rs
+++ b/Libraries/LibWeb/Rust/src/css/calc.rs
@@ -335,6 +335,7 @@ pub(crate) fn simplify_parsed_calculation(
let callbacks = CalcSimplifyCallbacks {
resolve_non_math_function: &|_| None,
resolve_channel_keyword: &|_| None,
+ resolve_size_keyword: &|| None,
absolutize_random_sharing: &|_| None,
};
let simplified = root.simplify(&evaluation_context, &callbacks);
@@ -355,6 +356,7 @@ pub(crate) fn serialize_context_free_calculation(
property: 0,
random_function_index: None,
allowed_color_channels: 0,
+ allow_size_keyword: false,
allow_random_functions: false,
parse_context: None,
},
@@ -580,6 +582,9 @@ pub enum CalcNode {
/// https://drafts.csswg.org/css-color-5/#relative-color
/// A color channel keyword inside a relative color, opaque code.
ChannelKeyword(u8),
+ /// https://drafts.csswg.org/css-values-5/#valdef-calc-size-size
+ /// The size keyword inside a calc-size() calculation, a <length> resolved at used-value time.
+ SizeKeyword,
Sum(Vec<Arc<CalcNode>>),
Product(Vec<Arc<CalcNode>>),
Negate(Arc<CalcNode>),
@@ -656,7 +661,10 @@ impl CalcNode {
/// node kind.
pub(crate) fn for_each_child(&self, f: &mut impl FnMut(&Arc<CalcNode>)) {
match self {
- CalcNode::Numeric(..) | CalcNode::ChannelKeyword(..) | CalcNode::NonMathFunction { .. } => {}
+ CalcNode::Numeric(..)
+ | CalcNode::ChannelKeyword(..)
+ | CalcNode::SizeKeyword
+ | CalcNode::NonMathFunction { .. } => {}
CalcNode::Sum(children)
| CalcNode::Product(children)
| CalcNode::Min(children)
@@ -755,6 +763,7 @@ impl CalcNode {
CalcNumericValue::Percentage(..) => Some(*percentage_leaf_type),
},
CalcNode::ChannelKeyword(..) => Some(CalcNumericType::default()),
+ CalcNode::SizeKeyword => single(0),
CalcNode::Sum(children) | CalcNode::Min(children) | CalcNode::Max(children) | CalcNode::Hypot(children) => {
add_children(&children.iter().collect::<Vec<_>>())
}
@@ -811,7 +820,7 @@ impl CalcNode {
) -> bool {
let leaf_independent = match self {
CalcNode::Numeric(CalcNumericValue::Length { unit, .. }) => length_is_independent(*unit),
- CalcNode::Numeric(..) | CalcNode::ChannelKeyword(..) => true,
+ CalcNode::Numeric(..) | CalcNode::ChannelKeyword(..) | CalcNode::SizeKeyword => true,
CalcNode::Random { sharing, .. } => style_value_is_independent(sharing),
CalcNode::NonMathFunction { value, .. } => style_value_is_independent(value),
_ => true,
@@ -1018,6 +1027,7 @@ fn combine_calculations(
let callbacks = CalcSimplifyCallbacks {
resolve_non_math_function: &|_| None,
resolve_channel_keyword: &|_| None,
+ resolve_size_keyword: &|| None,
absolutize_random_sharing: &|_| None,
};
let simplified = drop_zero_length_terms(root.simplify(&evaluation_context, &callbacks));
@@ -1214,6 +1224,8 @@ pub(crate) type RandomBaseValueResolver<'a> = &'a dyn Fn(&RetainedStyleValueData
pub(crate) struct CalcSimplifyCallbacks<'a> {
pub resolve_non_math_function: &'a dyn Fn(&RetainedStyleValueData) -> Option<Arc<CalcNode>>,
pub resolve_channel_keyword: &'a dyn Fn(u8) -> Option<f64>,
+ /// The used size a calc-size() calculation's size keyword stands for, in pixels.
+ pub resolve_size_keyword: &'a dyn Fn() -> Option<f64>,
/// Absolutizes a random() node's sharing options at computed-value time,
/// fixing its per-element random base value; None keeps the original.
pub absolutize_random_sharing: &'a dyn Fn(&RetainedStyleValueData) -> Option<RetainedStyleValueData>,
@@ -2013,6 +2025,16 @@ impl CalcNode {
}
return self.clone();
}
+ // https://drafts.csswg.org/css-values-5/#resolving-calc-size
+ if matches!(&**self, CalcNode::SizeKeyword) {
+ if let Some(resolved) = (callbacks.resolve_size_keyword)() {
+ return shared(CalcNode::Numeric(CalcNumericValue::Length {
+ value: resolved,
+ unit: canonical_unit_code(&crate::css::style_compute::LENGTH_UNIT_CANONICAL_PX_RATIOS),
+ }));
+ }
+ return self.clone();
+ }
// 3. At this point, root is an operator node. Simplify all the calculation children of root.
let root = self.with_simplified_children(context, callbacks);
@@ -2393,7 +2415,10 @@ impl CalcNode {
sharing: simplified_sharing,
}
}
- CalcNode::Numeric(..) | CalcNode::ChannelKeyword(..) | CalcNode::NonMathFunction { .. } => {
+ CalcNode::Numeric(..)
+ | CalcNode::ChannelKeyword(..)
+ | CalcNode::SizeKeyword
+ | CalcNode::NonMathFunction { .. } => {
return self.clone();
}
};
@@ -2803,6 +2828,7 @@ fn with_ffi_evaluation<R>(
},
// Relative color channels resolve through the color resolver, which supplies their values.
resolve_channel_keyword: &|_| None,
+ resolve_size_keyword: &|| None,
};
f(&evaluation_context, &callbacks)
}
@@ -2897,7 +2923,7 @@ fn resolve_calculated_with_channels(
length_resolution: LengthResolution,
channels: Option<&crate::css::color_resolution::RelativeColorContext>,
) -> Option<(f64, CalcNumericType, Option<ResolveAs>)> {
- resolve_calculated_with_channels_options(calculated, percentage_basis, length_resolution, channels, true)
+ resolve_calculated_with_channels_options(calculated, percentage_basis, length_resolution, channels, None, true)
}
fn resolve_calculated_with_channels_options(
@@ -2905,6 +2931,7 @@ fn resolve_calculated_with_channels_options(
percentage_basis: Option<CalcNumericValue>,
length_resolution: LengthResolution,
channels: Option<&crate::css::color_resolution::RelativeColorContext>,
+ size_keyword_px: Option<f64>,
apply_censoring_and_clamping: bool,
) -> Option<(f64, CalcNumericType, Option<ResolveAs>)> {
use crate::css::style_value::StyleValueData;
@@ -2943,6 +2970,7 @@ fn resolve_calculated_with_channels_options(
absolutize_random_sharing: &|_| None,
resolve_non_math_function: &|_| None,
resolve_channel_keyword: &|channel| channels.and_then(|map| map.get(channel as usize).copied().flatten()),
+ resolve_size_keyword: &|| size_keyword_px,
};
let simplified = root.simplify(&evaluation_context, &callbacks);
let (value, numeric_type) = resolve_simplified_calculation(
@@ -3287,10 +3315,33 @@ pub(crate) fn resolve_calculated_raw_length_without_context(
calculated: &crate::css::style_value::StyleValueData,
) -> Option<f64> {
let (value, numeric_type, resolve_as) =
- resolve_calculated_with_channels_options(calculated, None, LengthResolution::default(), None, false)?;
+ resolve_calculated_with_channels_options(calculated, None, LengthResolution::default(), None, None, false)?;
numeric_type.matches_dimension(0, resolve_as).then_some(value)
}
+/// Resolves a calc-size() calculation to pixels at used-value time, with its size keyword standing for
+/// `size_px`. Percentages resolve against `percentage_basis_px`, or against zero when that is indefinite.
+/// https://drafts.csswg.org/css-values-5/#resolving-calc-size
+pub(crate) fn resolve_calc_size_calculation(
+ calculated: &crate::css::style_value::StyleValueData,
+ size_px: f64,
+ percentage_basis_px: Option<f64>,
+) -> Option<f64> {
+ let basis = CalcNumericValue::Length {
+ value: percentage_basis_px.unwrap_or(0.0),
+ unit: canonical_unit_code(&crate::css::style_compute::LENGTH_UNIT_CANONICAL_PX_RATIOS),
+ };
+ let (value, numeric_type, resolve_as) = resolve_calculated_with_channels_options(
+ calculated,
+ Some(basis),
+ LengthResolution::default(),
+ None,
+ Some(size_px),
+ true,
+ )?;
+ (numeric_type.matches_dimension(0, resolve_as) || numeric_type.matches_percentage()).then_some(value)
+}
+
/// Resolves a calculated value that must produce a length using the immutable
/// per-element length metrics supplied to animation evaluation.
pub(crate) fn resolve_calculated_length_with_context(
@@ -3710,7 +3761,11 @@ impl CalcSerializer<'_> {
// children by commas.
// AD-HOC: ChannelKeyword nodes, used for relative-color syntax, serialize directly as the
// keyword name.
- if matches!(&**node, CalcNode::Numeric(..) | CalcNode::ChannelKeyword(..)) || is_operator {
+ if matches!(
+ &**node,
+ CalcNode::Numeric(..) | CalcNode::ChannelKeyword(..) | CalcNode::SizeKeyword
+ ) || is_operator
+ {
self.literal("calc(");
self.serialize_tree(node, false);
self.literal(")");
@@ -3811,6 +3866,7 @@ impl CalcSerializer<'_> {
// AD-HOC: ChannelKeyword nodes, used for relative-color syntax, serialize directly as
// the keyword name.
CalcNode::ChannelKeyword(channel) => self.channel(*channel),
+ CalcNode::SizeKeyword => self.literal("size"),
// 4. If root is a Negate node, let s be a string initially containing "(-1 * ".
CalcNode::Negate(child) => {
if emit_outer_parentheses {
@@ -4254,6 +4310,32 @@ pub(crate) fn serialize_math_function_pieces(
serializer.pieces
}
+/// Serializes a Calculated style value's calculation into the piece batch without a calc() around it, as
+/// calc-size() serializes its arguments.
+pub(crate) fn serialize_bare_calculation_pieces(
+ calculated: &crate::css::style_value::StyleValueData,
+ resolved_mode: bool,
+) -> Vec<FfiCalcSerializationPiece> {
+ let crate::css::style_value::StyleValueData::Calculated {
+ rust_calculation,
+ resolve_numbers_as_integers,
+ accepted_ranges,
+ ..
+ } = calculated
+ else {
+ unreachable!("calc serialization requires calculated value data");
+ };
+ let root = rust_calculation.node_arc();
+ let mut serializer = CalcSerializer {
+ pieces: Vec::new(),
+ resolved_mode,
+ resolve_numbers_as_integers: *resolve_numbers_as_integers,
+ accepted_ranges: accepted_ranges.as_slice(),
+ };
+ serializer.serialize_tree(&root, false);
+ serializer.pieces
+}
+
impl CalcNode {
/// Structural equality over two calculation trees, mirroring the C++ node
/// equals implementations: kinds, leaf values and child structures must match.
@@ -4264,6 +4346,7 @@ impl CalcNode {
match (self, other) {
(CalcNode::Numeric(a), CalcNode::Numeric(b)) => a == b,
(CalcNode::ChannelKeyword(a), CalcNode::ChannelKeyword(b)) => a == b,
+ (CalcNode::SizeKeyword, CalcNode::SizeKeyword) => true,
(CalcNode::Sum(a), CalcNode::Sum(b))
| (CalcNode::Product(a), CalcNode::Product(b))
| (CalcNode::Min(a), CalcNode::Min(b))
@@ -4468,6 +4551,7 @@ fn node_kind_code(node: &CalcNode) -> u8 {
CalcNode::Rem { .. } => 26,
CalcNode::Random { .. } => 27,
CalcNode::NonMathFunction { .. } => 28,
+ CalcNode::SizeKeyword => 29,
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/calc_size.rs b/Libraries/LibWeb/Rust/src/css/calc_size.rs
new file mode 100644
index 00000000000..9f8a67cc103
--- /dev/null
+++ b/Libraries/LibWeb/Rust/src/css/calc_size.rs
@@ -0,0 +1,181 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The calc-size() function.
+//! https://drafts.csswg.org/css-values-5/#calc-size
+
+use crate::css::css_enums::{keyword, keyword_from_ascii_case_insensitive};
+use crate::css::parser::component_value::{ComponentValue, trim_whitespace};
+use crate::css::parser::value_parser::{
+ NumericRange, ParseContext, equals_ascii_case_insensitive, parse_calculated_numeric_value_allowing_size_keyword,
+ value_list,
+};
+use crate::css::property_metadata::{property_accepted_keywords, property_accepted_value_types};
+use crate::css::serialize::{StringUnits, with_fly_string_units};
+use crate::css::style_value::{CssString, RetainedStyleValueData, StyleValueData};
+use crate::css::value_codes::{VALUE_TYPE_FIT_CONTENT, VALUE_TYPE_LENGTH};
+
+const COMMA: u8 = 1;
+
+pub(crate) fn is_calc_size_function_name(name: &CssString) -> bool {
+ with_fly_string_units(name, |units| match units {
+ StringUnits::Ascii(bytes) => bytes == b"calc-size",
+ StringUnits::Utf16(units) => units.iter().copied().eq(b"calc-size".iter().copied().map(u16::from)),
+ })
+}
+
+/// Parses `value` as a calc-size() function of the sizing property `property`.
+pub(crate) fn parse_calc_size(context: &ParseContext, property: u16, value: &ComponentValue) -> Option<StyleValueData> {
+ // <calc-size()> = calc-size( <calc-size-basis>, <calc-sum> )
+ let (name, arguments) = value.function()?;
+ if !equals_ascii_case_insensitive(name, b"calc-size") {
+ return None;
+ }
+ let mut arguments = arguments.split(ComponentValue::is_comma);
+ let basis = trim_whitespace(arguments.next()?);
+ let calculation = trim_whitespace(arguments.next()?);
+ if arguments.next().is_some() || basis.is_empty() || calculation.is_empty() {
+ return None;
+ }
+ let basis = parse_basis(context, property, basis)?;
+ // If the basis is any, it is a syntax error for size to appear in the calc-size calculation.
+ let basis_is_any = matches!(basis, StyleValueData::Keyword { keyword: keyword::ANY });
+ let calculation = parse_calc_sum(context, property, calculation, !basis_is_any)?;
+ Some(calc_size_function(basis, calculation))
+}
+
+/// The calc-size() function value with the given basis and calculation.
+pub(crate) fn calc_size_function(basis: StyleValueData, calculation: StyleValueData) -> StyleValueData {
+ StyleValueData::Function {
+ name: CssString::from_utf16(&"calc-size".encode_utf16().collect::<Vec<_>>()),
+ value: RetainedStyleValueData::from_owned(value_list(vec![basis, calculation], COMMA, false)),
+ }
+}
+
+// <calc-size-basis> = [ <size-keyword> | <calc-sum> | <calc-size()> | any ]
+fn parse_basis(context: &ParseContext, property: u16, values: &[ComponentValue]) -> Option<StyleValueData> {
+ if let [value] = values {
+ if let Some(identifier) = value.ident() {
+ let keyword = keyword_from_ascii_case_insensitive(identifier)?;
+ // <size-keyword> represents any sizing keywords allowed in the context where the function is used.
+ let is_size_keyword = match keyword {
+ keyword::ANY => true,
+ keyword::AUTO | keyword::CONTENT | keyword::MIN_CONTENT | keyword::MAX_CONTENT => {
+ property_accepted_keywords(property).contains(&keyword)
+ }
+ keyword::FIT_CONTENT => property_accepted_value_types(property).contains(&VALUE_TYPE_FIT_CONTENT),
+ _ => false,
+ };
+ return is_size_keyword.then_some(StyleValueData::Keyword { keyword });
+ }
+ if value
+ .function()
+ .is_some_and(|(name, _)| equals_ascii_case_insensitive(name, b"calc-size"))
+ {
+ return parse_calc_size(context, property, value);
+ }
+ }
+ parse_calc_sum(context, property, values, false)
+}
+
+/// A <calc-sum> given without a calc() around it, which must match <length-percentage>.
+fn parse_calc_sum(
+ context: &ParseContext,
+ property: u16,
+ values: &[ComponentValue],
+ allow_size_keyword: bool,
+) -> Option<StyleValueData> {
+ let name = "calc".encode_utf16().collect::<Vec<_>>();
+ parse_calculated_numeric_value_allowing_size_keyword(
+ context,
+ property,
+ VALUE_TYPE_LENGTH,
+ Some(VALUE_TYPE_LENGTH),
+ NumericRange::INFINITE,
+ &name,
+ values,
+ allow_size_keyword,
+ )
+}
+
+/// The basis and calculation of a calc-size() function value.
+pub(crate) fn calc_size_arguments(value: &StyleValueData) -> Option<(&StyleValueData, &StyleValueData)> {
+ let StyleValueData::Function { name, value } = value else {
+ return None;
+ };
+ if !is_calc_size_function_name(name) {
+ return None;
+ }
+ let StyleValueData::ValueList { values, .. } = value.data() else {
+ return None;
+ };
+ match values.as_slice() {
+ [basis, calculation] => Some((basis.data(), calculation.data())),
+ _ => None,
+ }
+}
+
+/// The sizing keyword a calc-size() acts as, after following nested calc-size() bases: auto, min-content,
+/// max-content or fit-content, or none when its basis is any or a calculation.
+pub(crate) fn calc_size_basis_keyword(value: &StyleValueData) -> Option<u16> {
+ let (basis, _) = calc_size_arguments(value)?;
+ match basis {
+ StyleValueData::Keyword { keyword: keyword::ANY } => None,
+ StyleValueData::Keyword { keyword } => Some(*keyword),
+ basis @ StyleValueData::Function { .. } => calc_size_basis_keyword(basis),
+ _ => None,
+ }
+}
+
+/// Resolves a calc-size() to pixels at used-value time.
+/// `keyword_size` gives the used size of a sizing keyword basis, and percentages resolve against
+/// `percentage_basis`, or against zero when that is indefinite.
+/// https://drafts.csswg.org/css-values-5/#resolving-calc-size
+pub(crate) fn resolve_calc_size(
+ value: &StyleValueData,
+ percentage_basis: Option<f64>,
+ keyword_size: &dyn Fn(u16) -> f64,
+) -> Option<f64> {
+ resolve_calc_size_with_basis_size(value, percentage_basis, None, keyword_size)
+}
+
+/// Resolves a calc-size() like resolve_calc_size(), with its size keyword standing for `basis_size` when given.
+/// That is the used size of a basis that behaves as auto, like a percentage of an indefinite size.
+pub(crate) fn resolve_calc_size_with_basis_size(
+ value: &StyleValueData,
+ percentage_basis: Option<f64>,
+ basis_size: Option<f64>,
+ keyword_size: &dyn Fn(u16) -> f64,
+) -> Option<f64> {
+ let (basis, calculation) = calc_size_arguments(value)?;
+ let size = if let Some(basis_size) = basis_size {
+ basis_size
+ } else {
+ match basis {
+ StyleValueData::Keyword { keyword: keyword::ANY } => 0.0,
+ StyleValueData::Keyword { keyword } => keyword_size(*keyword),
+ basis @ StyleValueData::Function { .. } => resolve_calc_size(basis, percentage_basis, keyword_size)?,
+ basis => resolve_length_percentage(basis, percentage_basis)?,
+ }
+ };
+ match calculation {
+ calculation @ StyleValueData::Calculated { .. } => {
+ crate::css::calc::resolve_calc_size_calculation(calculation, size, percentage_basis)
+ }
+ calculation => resolve_length_percentage(calculation, percentage_basis),
+ }
+}
+
+fn resolve_length_percentage(value: &StyleValueData, percentage_basis: Option<f64>) -> Option<f64> {
+ match value {
+ StyleValueData::Length { value, .. } => Some(*value),
+ StyleValueData::Percentage { value } => Some(percentage_basis.unwrap_or(0.0) * value / 100.0),
+ value @ StyleValueData::Calculated { .. } => {
+ crate::css::calc::resolve_calc_size_calculation(value, 0.0, percentage_basis)
+ }
+ _ => None,
+ }
+}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
index bade1c65748..77c44010edf 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
@@ -100,6 +100,15 @@ impl LengthPercentageRef<'_> {
StyleValueData::Length { .. } => false,
StyleValueData::Percentage { .. } => true,
StyleValueData::Calculated { rust_calculation, .. } => rust_calculation.node().contains_percentage(),
+ // A calc-size() whose basis is any or a calculation acts like a calculation.
+ value @ StyleValueData::Function { .. } => {
+ crate::css::calc_size::calc_size_arguments(value).is_some_and(|(basis, calculation)| {
+ [basis, calculation].into_iter().any(|argument| {
+ !matches!(argument, StyleValueData::Keyword { .. })
+ && LengthPercentageRef::over(argument).contains_percentage()
+ })
+ })
+ }
_ => unreachable!("computed length-percentage holds a non-length-percentage style value"),
}
}
@@ -115,6 +124,10 @@ impl LengthPercentageRef<'_> {
CssPixels::truncated_value_for(reference.to_double() * self.as_fraction())
}
StyleValueData::Calculated { .. } => resolve_calc_to_px(self.calculated_pointer(), reference),
+ // NB: A calc-size() that does not resolve is invalid at computed-value time, and resolves to zero.
+ value @ StyleValueData::Function { .. } => CssPixels::nearest_value_for(
+ crate::css::calc_size::resolve_calc_size(value, Some(reference.to_double()), &|_| 0.0).unwrap_or(0.0),
+ ),
_ => unreachable!("computed length-percentage holds a non-length-percentage style value"),
}
}
@@ -171,6 +184,15 @@ impl ComputedSize {
self.kind == ComputedSizeKind::None
}
+ /// The calc-size() function this size was given as, if any. It acts as its basis for all purposes other
+ /// than resolving the size.
+ /// https://drafts.csswg.org/css-values-5/#calc-size
+ pub(crate) fn calc_size(&self) -> Option<&StyleValueData> {
+ self.value
+ .style_value()
+ .filter(|value| crate::css::calc_size::calc_size_arguments(value).is_some())
+ }
+
pub(crate) fn is_length_percentage(&self) -> bool {
matches!(
self.kind,
@@ -196,6 +218,9 @@ impl ComputedSize {
/// The fit-content argument; None for the keyword-only form.
pub(crate) fn fit_content_available_space(&self) -> Option<LengthPercentageRef<'_>> {
debug_assert!(self.is_fit_content());
+ if self.calc_size().is_some() {
+ return None;
+ }
self.value.length_percentage()
}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index 7fa8e0872c0..7e0c549fba1 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -2682,6 +2682,19 @@ impl ComputedSize {
Some(StyleValueData::Keyword { keyword: value }) if *value == keyword::NONE => {
Self::keyword(ComputedSizeKind::None)
}
+ Some(value @ StyleValueData::Function { .. })
+ if crate::css::calc_size::calc_size_arguments(value).is_some() =>
+ {
+ // NB: A calc-size() acts like its basis keyword, or like a calculation for any other basis.
+ let kind = match crate::css::calc_size::calc_size_basis_keyword(value) {
+ Some(keyword::AUTO) => ComputedSizeKind::Auto,
+ Some(keyword::MIN_CONTENT) => ComputedSizeKind::MinContent,
+ Some(keyword::MAX_CONTENT) => ComputedSizeKind::MaxContent,
+ Some(keyword::FIT_CONTENT) => ComputedSizeKind::FitContent,
+ _ => ComputedSizeKind::Calculated,
+ };
+ Self::retained(kind, data)
+ }
Some(StyleValueData::Function { value, .. }) => Self::retained(ComputedSizeKind::FitContent, value),
Some(StyleValueData::Calculated { .. }) => Self::retained(ComputedSizeKind::Calculated, data),
Some(StyleValueData::Percentage { .. }) => Self::retained(ComputedSizeKind::Percentage, data),
diff --git a/Libraries/LibWeb/Rust/src/css/mod.rs b/Libraries/LibWeb/Rust/src/css/mod.rs
index 2f3dd4fb90f..2c2d4c85038 100644
--- a/Libraries/LibWeb/Rust/src/css/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/mod.rs
@@ -8,6 +8,7 @@ pub(crate) mod absolutize;
pub mod animated_overlay;
pub mod animation;
pub mod calc;
+pub(crate) mod calc_size;
pub mod cascaded_properties;
pub(crate) mod color_conversion;
pub mod color_interpolation;
diff --git a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
index 8ed6561d8f9..5b69c549342 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs
@@ -44,6 +44,8 @@ pub(crate) struct CalcParserContext<'a> {
pub property: u16,
pub random_function_index: Option<&'a RandomFunctionIndex>,
pub allowed_color_channels: u64,
+ /// Whether the calc-size() size keyword may appear, as in a calc-size() calculation.
+ pub allow_size_keyword: bool,
pub allow_random_functions: bool,
pub parse_context: Option<&'a ParseContext<'a>>,
}
@@ -56,6 +58,7 @@ impl CalcParserContext<'_> {
property: 0,
random_function_index: None,
allowed_color_channels: 0,
+ allow_size_keyword: false,
allow_random_functions: false,
parse_context: None,
}
@@ -241,6 +244,9 @@ fn parse_calc_keyword(identifier: &[u16], context: CalcParserContext) -> Result<
{
return Ok(Arc::new(CalcNode::ChannelKeyword(channel)));
}
+ if context.allow_size_keyword && equals_ascii_case_insensitive(identifier, b"size") {
+ return Ok(Arc::new(CalcNode::SizeKeyword));
+ }
let value = if equals_ascii_case_insensitive(identifier, b"e") {
std::f64::consts::E
} else if equals_ascii_case_insensitive(identifier, b"pi") {
@@ -783,6 +789,7 @@ mod tests {
property: 1,
random_function_index: Some(&random_function_index),
allowed_color_channels: 0,
+ allow_size_keyword: false,
allow_random_functions: true,
parse_context: None,
};
diff --git a/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs
index c25479c3e51..668411f1d96 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs
@@ -373,6 +373,7 @@ fn parse_calculated_numeric(
property,
random_function_index: context.random_function_index,
allowed_color_channels,
+ allow_size_keyword: false,
allow_random_functions: context_allows_random_functions(context),
parse_context: Some(context),
},
diff --git a/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs
index f9153e0711b..b2e1eba52e7 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/positions_shapes_parser.rs
@@ -7,7 +7,7 @@
// Parsed values use the thread-confined shared graph owned by the C++ style objects.
#![allow(clippy::arc_with_non_send_sync)]
-use super::component_value::{ComponentKind, ComponentValue};
+use super::component_value::{ComponentKind, ComponentValue, trim_whitespace};
use super::token_stream::TokenStream;
use super::value_parser::{
NumericRange, ParseContext, ParseOutcome, equals_ascii_case_insensitive, parse_length_from_stream,
@@ -1833,6 +1833,13 @@ pub(crate) fn parse_anchor_fit_property(
} else {
parsed
}
+ } else if property_accepted_value_types(property).contains(&VALUE_TYPE_FIT_CONTENT)
+ && let [value] = trim_whitespace(values)
+ && value
+ .function()
+ .is_some_and(|(name, _)| equals_ascii_case_insensitive(name, b"calc-size"))
+ {
+ crate::css::calc_size::parse_calc_size(context, property, value)
} else if property_accepted_value_types(property).contains(&VALUE_TYPE_FIT_CONTENT) {
let is_fit_or_anchor_size = values.iter().find(|value| !value.is_whitespace()).is_some_and(|value| {
value
diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
index 590a78f5ab0..e4a5ffed808 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
@@ -1199,6 +1199,31 @@ pub(crate) fn parse_calculated_numeric_value_with_ranges(
range: NumericRange,
name: &[u16],
values: &[ComponentValue],
+) -> Option<StyleValueData> {
+ parse_calculated_numeric_value_allowing_size_keyword(
+ context,
+ property,
+ value_type,
+ percentages_resolve_as,
+ range,
+ name,
+ values,
+ false,
+ )
+}
+
+/// Parses a math function like parse_calculated_numeric_value_with_ranges(), where `allow_size_keyword` lets the
+/// calculation read the size keyword of a calc-size() calculation.
+#[allow(clippy::too_many_arguments)]
+pub(crate) fn parse_calculated_numeric_value_allowing_size_keyword(
+ context: &ParseContext,
+ property: u16,
+ value_type: u8,
+ percentages_resolve_as: Option<u8>,
+ range: NumericRange,
+ name: &[u16],
+ values: &[ComponentValue],
+ allow_size_keyword: bool,
) -> Option<StyleValueData> {
if equals_ascii_case_insensitive(name, b"calc")
&& let Some(tree_value) = single_non_whitespace_value(values)
@@ -1228,6 +1253,7 @@ pub(crate) fn parse_calculated_numeric_value_with_ranges(
property,
random_function_index: context.random_function_index,
allowed_color_channels: 0,
+ allow_size_keyword,
allow_random_functions: context_allows_random_functions(context),
parse_context: Some(context),
},
diff --git a/Libraries/LibWeb/Rust/src/css/serialize.rs b/Libraries/LibWeb/Rust/src/css/serialize.rs
index 4a549996a4d..d6171d611c0 100644
--- a/Libraries/LibWeb/Rust/src/css/serialize.rs
+++ b/Libraries/LibWeb/Rust/src/css/serialize.rs
@@ -219,6 +219,11 @@ pub(crate) fn serialize_computed_size(size: &crate::css::computed_value_types::C
use crate::css::computed_value_types::ComputedSizeKind;
let mut sink = TextSink::new();
+ // NB: A calc-size() keeps its whole function, whatever keyword its basis is.
+ if let Some(calc_size) = size.calc_size() {
+ serialize_style_value(&mut sink, calc_size, SerializationMode::Normal);
+ return sink.into_string();
+ }
match size.kind {
ComputedSizeKind::Auto => sink.push_ascii("auto"),
ComputedSizeKind::Calculated | ComputedSizeKind::Length | ComputedSizeKind::Percentage => {
@@ -962,6 +967,25 @@ pub(crate) fn serialize_style_value(sink: &mut TextSink, value: &StyleValueData,
serialize_style_value(sink, value, mode);
}
}
+ StyleValueData::Function { name, value } if crate::css::calc_size::is_calc_size_function_name(name) => {
+ // https://drafts.csswg.org/css-values-5/#calc-size
+ // Both arguments are calculations given without a calc() around them.
+ sink.push_ascii("calc-size(");
+ if let StyleValueData::ValueList { values, .. } = value.data() {
+ for (index, argument) in values.as_slice().iter().enumerate() {
+ if index > 0 {
+ sink.push_ascii(", ");
+ }
+ match argument.data() {
+ argument @ StyleValueData::Calculated { .. } => {
+ serialize_calculated_pieces(sink, argument, mode, true);
+ }
+ argument => serialize_style_value(sink, argument, mode),
+ }
+ }
+ }
+ sink.push_ascii(")");
+ }
StyleValueData::Function { name, value } => {
with_fly_string_units(name, |units| push_units_raw(sink, &units));
sink.push_ascii("(");
@@ -2214,9 +2238,18 @@ fn serialize_number_with_type(sink: &mut TextSink, value: f64, number_type: u8,
/// batch the calc serializer in calc.rs produces; mirrors the consumer loop in
/// CalculatedStyleValue.cpp.
fn serialize_calculated(sink: &mut TextSink, value: &StyleValueData, mode: SerializationMode) {
+ serialize_calculated_pieces(sink, value, mode, false);
+}
+
+/// Serializes a calculation, as a whole math function, or as a bare calculation without a calc() around it.
+fn serialize_calculated_pieces(sink: &mut TextSink, value: &StyleValueData, mode: SerializationMode, bare: bool) {
use crate::css::css_enums::channel_keyword;
- let pieces = crate::css::calc::serialize_math_function_pieces(value, mode.is_resolved());
+ let pieces = if bare {
+ crate::css::calc::serialize_bare_calculation_pieces(value, mode.is_resolved())
+ } else {
+ crate::css::calc::serialize_math_function_pieces(value, mode.is_resolved())
+ };
let mut previous_piece_appended = false;
for piece in &pieces {
let start_length = sink.len();
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 2471b72447d..217d70e3552 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -1294,6 +1294,26 @@ impl AbsposEngine<'_> {
resolved_anchor_insets,
);
+ // NB: A calc-size() with an auto basis resolves against the inline size auto gives the box.
+ if style.width().is_auto() && style.width().calc_size().is_some() {
+ let inline_size = sizing.resolve_calc_size(
+ node,
+ SizingAxis::Inline,
+ style.width(),
+ auto_px_value(used_inline_size),
+ constraints.percentage_basis_inline_size,
+ );
+ (used_inline_size, left, right, margin_left, margin_right) = self.solve_non_replaced_inline_once(
+ node,
+ containing_block_inline_size,
+ available_space,
+ constraints,
+ static_position_rect,
+ Some(inline_size),
+ resolved_anchor_insets,
+ );
+ }
+
if !sizing.should_treat_max_inline_size_as_none(node, available_space.inline_size, constraints) {
let max_inline_size =
sizing.calculate_inner_inline_size(node, available_space.inline_size, style.max_width(), constraints);
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 2ec1b199362..fc8304cd57e 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -762,6 +762,27 @@ impl<'pass> BlockFormattingContext<'pass> {
&mut margin_right_is_auto,
);
+ // NB: A calc-size() with an auto basis resolves against the inline size auto gives the box.
+ if style.width().is_auto()
+ && style.width().calc_size().is_some()
+ && let Some(auto_inline_size) = used_inline_size
+ {
+ let inline_size = sizing.resolve_calc_size(
+ node,
+ SizingAxis::Inline,
+ style.width(),
+ auto_inline_size,
+ constraints.percentage_basis_inline_size,
+ );
+ used_inline_size = compute(
+ Some(inline_size),
+ &mut margin_left,
+ &mut margin_right,
+ &mut margin_left_is_auto,
+ &mut margin_right_is_auto,
+ );
+ }
+
// 2. The tentative used width is greater than 'max-width', the rules above are applied again,
// but this time using the computed value of 'max-width' as the computed value for 'width'.
if !sizing.should_treat_max_inline_size_as_none(node, available_space.inline_size, constraints) {
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 6e5bd2915d7..59338caf72c 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -1297,7 +1297,9 @@ impl<'pass> SizingContext<'pass> {
constraints: ContainingBlockConstraints,
) -> bool {
let size = self.style(node).max_width();
- if size.is_none() || (available == AvailableSize::MaxContent && size.is_max_content()) {
+ // 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) {
return true;
}
// https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
@@ -1320,9 +1322,10 @@ impl<'pass> SizingContext<'pass> {
return true;
}
}
- (size.is_fit_content() && available.is_intrinsic_sizing_constraint())
- || (size.is_max_content() && available == AvailableSize::MaxContent)
- || (size.is_min_content() && available == AvailableSize::MinContent)
+ size.calc_size().is_none()
+ && ((size.is_fit_content() && available.is_intrinsic_sizing_constraint())
+ || (size.is_max_content() && available == AvailableSize::MaxContent)
+ || (size.is_min_content() && available == AvailableSize::MinContent))
}
pub(crate) fn should_treat_max_block_size_as_none(
@@ -1347,9 +1350,10 @@ impl<'pass> SizingContext<'pass> {
return true;
}
}
- (size.is_fit_content() && available.is_intrinsic_sizing_constraint())
- || (size.is_max_content() && available == AvailableSize::MaxContent)
- || (size.is_min_content() && available == AvailableSize::MinContent)
+ size.calc_size().is_none()
+ && ((size.is_fit_content() && available.is_intrinsic_sizing_constraint())
+ || (size.is_max_content() && available == AvailableSize::MaxContent)
+ || (size.is_min_content() && available == AvailableSize::MinContent))
}
// https://quirks.spec.whatwg.org/#the-percentage-height-calculation-quirk
@@ -1616,6 +1620,13 @@ impl<'pass> SizingContext<'pass> {
} else {
child_automatic_block_size.unwrap_or_else(automatic_block_size_fallback)
};
+ 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() {
@@ -1917,6 +1928,17 @@ impl<'pass> SizingContext<'pass> {
self.calculate_inner_inline_size(node, available_space.inline_size, style.width(), constraints)
};
+ let unconstrained_inline_size = if width_was_treated_as_auto {
+ self.resolve_calc_size(
+ node,
+ SizingAxis::Inline,
+ style.width(),
+ unconstrained_inline_size,
+ constraints.percentage_basis_inline_size,
+ )
+ } else {
+ unconstrained_inline_size
+ };
let mut inline_size = unconstrained_inline_size;
if !self.should_treat_max_inline_size_as_none(node, available_space.inline_size, constraints) {
inline_size = inline_size.min(self.calculate_inner_inline_size(
@@ -3181,6 +3203,55 @@ impl<'pass> SizingContext<'pass> {
}
}
+ /// The used content size of a box whose size property is `size`, given the content size its basis resolved
+ /// to. A calc-size() resolves its calculation against that size; any other size keeps it.
+ /// https://drafts.csswg.org/css-values-5/#resolving-calc-size
+ pub(crate) fn resolve_calc_size(
+ &self,
+ node: Node,
+ axis: SizingAxis,
+ size: &ComputedSize,
+ content_size: CssPixels,
+ percentage_basis: Option<CssPixels>,
+ ) -> CssPixels {
+ let Some(calc_size) = size.calc_size() else {
+ return content_size;
+ };
+ // The size is that of the box the box-sizing property names.
+ let style = self.style(node);
+ let edges = if style.box_sizing() == box_sizing::BORDER_BOX {
+ let used = self.used(node);
+ match axis {
+ SizingAxis::Inline => {
+ style.border_left_width()
+ + used.padding_left.get()
+ + used.padding_right.get()
+ + style.border_right_width()
+ }
+ SizingAxis::Block => {
+ style.border_top_width()
+ + used.padding_top.get()
+ + used.padding_bottom.get()
+ + style.border_bottom_width()
+ }
+ }
+ } else {
+ CssPixels::default()
+ };
+ let basis_size = (content_size + edges).to_double();
+ // A size given as a calculation reaches here when it behaves as auto, and auto's size then stands for it.
+ let behaves_as_auto = size.is_length_percentage();
+ let Some(resolved) = crate::css::calc_size::resolve_calc_size_with_basis_size(
+ calc_size,
+ percentage_basis.map(CssPixels::to_double),
+ behaves_as_auto.then_some(basis_size),
+ &|_| basis_size,
+ ) else {
+ return content_size;
+ };
+ (CssPixels::nearest_value_for(resolved) - edges).max(CssPixels::default())
+ }
+
pub(crate) fn calculate_inner_inline_size(
&self,
node: Node,
@@ -3198,8 +3269,8 @@ impl<'pass> SizingContext<'pass> {
} else {
available.to_px_or_zero()
};
- if preferred_size.is_fit_content() {
- return self.calculate_fit_content_size(
+ let keyword_size = if preferred_size.is_fit_content() {
+ Some(self.calculate_fit_content_size(
node,
SizingAxis::Inline,
AvailableSpace {
@@ -3207,14 +3278,23 @@ impl<'pass> SizingContext<'pass> {
block_size: AvailableSize::Indefinite,
},
constraints,
+ ))
+ } else if preferred_size.is_max_content() {
+ Some(self.calculate_max_content_inline_size(node, constraints))
+ } else if preferred_size.is_min_content() {
+ Some(self.calculate_min_content_inline_size(node, constraints))
+ } else {
+ None
+ };
+ if let Some(keyword_size) = keyword_size {
+ return self.resolve_calc_size(
+ node,
+ SizingAxis::Inline,
+ preferred_size,
+ keyword_size,
+ constraints.percentage_basis_inline_size,
);
}
- if preferred_size.is_max_content() {
- return self.calculate_max_content_inline_size(node, constraints);
- }
- if preferred_size.is_min_content() {
- return self.calculate_min_content_inline_size(node, constraints);
- }
let value = preferred_size.to_px(basis);
let style = self.style(node);
if style.box_sizing() == box_sizing::BORDER_BOX {
@@ -3241,21 +3321,22 @@ impl<'pass> SizingContext<'pass> {
return self.content_block_size_from_aspect_ratio(node, self.used(node).content_inline_size.get());
}
assert!(!preferred_size.is_auto());
- if preferred_size.is_fit_content() {
- return self.calculate_fit_content_size(node, SizingAxis::Block, available_space, constraints);
- }
- if preferred_size.is_max_content() {
- return self.calculate_max_content_block_size(
- node,
- available_space.inline_size.to_px_or_zero(),
- constraints,
- );
- }
- if preferred_size.is_min_content() {
- return self.calculate_min_content_block_size(
+ let keyword_size = if preferred_size.is_fit_content() {
+ Some(self.calculate_fit_content_size(node, SizingAxis::Block, available_space, constraints))
+ } else if preferred_size.is_max_content() {
+ Some(self.calculate_max_content_block_size(node, available_space.inline_size.to_px_or_zero(), constraints))
+ } else if preferred_size.is_min_content() {
+ Some(self.calculate_min_content_block_size(node, available_space.inline_size.to_px_or_zero(), constraints))
+ } else {
+ None
+ };
+ if let Some(keyword_size) = keyword_size {
+ return self.resolve_calc_size(
node,
- available_space.inline_size.to_px_or_zero(),
- constraints,
+ SizingAxis::Block,
+ preferred_size,
+ keyword_size,
+ constraints.percentage_basis_block_size,
);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
index ee346e8b2ca..65096e60017 100644
--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs
@@ -1432,8 +1432,13 @@ impl<'pass> TableFormattingContext<'pass> {
// those values, or if the computed value of the table-layout property is auto, then the table-root is said to be laid out in auto mode.
let style = self.style(self.run.box_);
let width = style.width();
+ // NB: A calc-size() width counts as well, whatever its basis.
+ // https://drafts.csswg.org/css-values-5/#calc-size
style.table_layout() == TABLE_LAYOUT_FIXED
- && (width.is_length() || width.is_percentage() || width.is_min_content() || width.is_fit_content())
+ && (width.is_length_percentage()
+ || width.is_min_content()
+ || width.is_fit_content()
+ || width.calc_size().is_some())
}
/// Whether a cell takes part in measuring the tracks of an axis. https://www.w3.org/TR/css-tables-3/#computing-column-measures:
diff --git a/Tests/LibWeb/Crash/wpt-import/css/css-values/calc-size/calc-size-svg-001-crash.html b/Tests/LibWeb/Crash/wpt-import/css/css-values/calc-size/calc-size-svg-001-crash.html
new file mode 100644
index 00000000000..ec1e09a4315
--- /dev/null
+++ b/Tests/LibWeb/Crash/wpt-import/css/css-values/calc-size/calc-size-svg-001-crash.html
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML>
+<style>
+
+image {
+ height: calc-size(auto, size * 0.5);
+}
+
+</style>
+
+<svg><image></image></svg>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-004.html
new file mode 100644
index 00000000000..52a1d3e21dd
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-004.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(fit-content, 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-005.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-005.html
new file mode 100644
index 00000000000..2891009ce88
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-005.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(fit-content, 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-006.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-006.html
new file mode 100644
index 00000000000..13481c1d1fd
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-values/calc-size/calc-size-min-max-sizes-006.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(fit-content, 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/Text/expected/wpt-import/css/css-tables/fixed-layout-2.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/fixed-layout-2.txt
index 45815668d08..2855ebc47fd 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/fixed-layout-2.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-tables/fixed-layout-2.txt
@@ -1,14 +1,22 @@
Harness status: OK
-Found 8 tests
+Found 17 tests
-7 Pass
-1 Fail
+17 Pass
Pass 10px
+Pass calc-size(10px, size)
Pass 100%
-Fail calc(10px + 100%)
+Pass calc-size(100%, size)
+Pass calc(10px + 100%)
+Pass calc-size(calc(10px + 100%), size)
Pass auto
+Pass calc-size(auto, size)
Pass min-content
+Pass calc-size(min-content, size)
Pass max-content
+Pass calc-size(max-content, size)
Pass fit-content
+Pass calc-size(fit-content, size)
+Pass calc-size(any, 10px + 100%)
+Pass calc-size(calc-size(any, 10px + 100%), size)
Pass fit-content(0)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-height-box-sizing.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-height-box-sizing.txt
new file mode 100644
index 00000000000..54bd5ec60fa
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-height-box-sizing.txt
@@ -0,0 +1,20 @@
+Harness status: OK
+
+Found 15 tests
+
+15 Pass
+Pass resolved height with styles {"height":"auto"}
+Pass resolved height with styles {"height":"auto","box-sizing":"border-box"}
+Pass resolved height with styles {"height":"calc-size(auto, size)"}
+Pass resolved height with styles {"height":"calc-size(auto, size)","box-sizing":"border-box"}
+Pass resolved height with styles {"height":"calc-size(auto, size * 2)"}
+Pass resolved height with styles {"height":"calc-size(auto, size * 2)","box-sizing":"content-box"}
+Pass resolved height with styles {"height":"calc-size(auto, size * 2)","box-sizing":"border-box"}
+Pass resolved height with styles {"height":"calc-size(min-content, size * 2)"}
+Pass resolved height with styles {"height":"calc-size(fit-content, size * 2)","box-sizing":"border-box"}
+Pass resolved height with styles {"min-height":"calc-size(min-content, size * 2)"}
+Pass resolved height with styles {"min-height":"calc-size(fit-content, size * 2)","box-sizing":"border-box"}
+Pass resolved height with styles {"height":"200px","max-height":"calc-size(min-content, size * 2)"}
+Pass resolved height with styles {"height":"200px","max-height":"calc-size(fit-content, size * 2)","box-sizing":"border-box"}
+Pass resolved height with styles {"height":"calc-size(11px, size * 2)"}
+Pass resolved height with styles {"height":"calc-size(11px, size * 2)","box-sizing":"border-box"}
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-height.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-height.txt
new file mode 100644
index 00000000000..d12434191e2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-height.txt
@@ -0,0 +1,42 @@
+Harness status: OK
+
+Found 37 tests
+
+37 Pass
+Pass resolved height for height in auto height container: calc-size(any, 357px)
+Pass resolved height for height in definite height container: calc-size(any, 357px)
+Pass resolved height for height in auto height container: calc-size(any, 31%)
+Pass resolved height for height in definite height container: calc-size(any, 31%)
+Pass resolved height for height in auto height container: calc-size(31%, size)
+Pass resolved height for height in definite height container: calc-size(31%, size)
+Pass resolved height for height in auto height container: calc-size(max-content, 31%)
+Pass resolved height for height in definite height container: calc-size(max-content, 31%)
+Pass resolved height for height in auto height container: calc-size(fit-content, 72px)
+Pass resolved height for height in definite height container: calc-size(fit-content, 72px)
+Pass resolved height for height in auto height container: calc-size(37px, 93px)
+Pass resolved height for height in definite height container: calc-size(37px, 93px)
+Pass resolved height for height in auto height container: calc-size(83px, size * 3)
+Pass resolved height for height in definite height container: calc-size(83px, size * 3)
+Pass resolved height for height in auto height container: calc-size(min-content, size / 2)
+Pass resolved height for height in definite height container: calc-size(min-content, size / 2)
+Pass resolved height for height in auto height container: calc-size(max-content, size * 1.2)
+Pass resolved height for height in definite height container: calc-size(max-content, size * 1.2)
+Pass resolved height for height in auto height container: calc-size(fit-content, size / 2 + 30px)
+Pass resolved height for height in definite height container: calc-size(fit-content, size / 2 + 30px)
+Pass resolved height for height in auto height container: calc-size(30px, 15em)
+Pass resolved height for height in definite height container: calc-size(30px, 15em)
+Pass resolved height for height in auto height container: calc-size(calc-size(any, 30px), 15em)
+Pass resolved height for height in definite height container: calc-size(calc-size(any, 30px), 15em)
+Pass resolved height for height in auto height container: calc-size(calc-size(2in, 30px), 15em)
+Pass resolved height for height in definite height container: calc-size(calc-size(2in, 30px), 15em)
+Pass resolved height for height in auto height container: calc-size(calc-size(any, 300px), size * 2)
+Pass resolved height for height in definite height container: calc-size(calc-size(any, 300px), size * 2)
+Pass resolved height for height in auto height container: calc-size(calc-size(min-content, 30px), 15em)
+Pass resolved height for height in definite height container: calc-size(calc-size(min-content, 30px), 15em)
+Pass resolved height for height in auto height container: calc-size(calc-size(min-content, size), size)
+Pass resolved height for height in definite height container: calc-size(calc-size(min-content, size), size)
+Pass resolved height for height in auto height container: calc-size(any, 31% + 12px)
+Pass resolved height for height in definite height container: calc-size(any, 31% + 12px)
+Pass resolved height for height in auto height container: calc-size(auto, size * 1.5)
+Pass resolved height for height in definite height container: calc-size(auto, size * 1.5)
+Pass calc-size() is indefinite when the intrinsic size is indefinite
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-out-of-flow.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-out-of-flow.txt
new file mode 100644
index 00000000000..02e424a0b71
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-out-of-flow.txt
@@ -0,0 +1,17 @@
+Harness status: OK
+
+Found 12 tests
+
+12 Pass
+Pass used width, width: max-content — keyword on its own, for comparison
+Pass used width, width: calc-size(max-content, size) — identity
+Pass used width, width: calc-size(max-content, size + 20px)
+Pass used width, width: calc-size(min-content, size * 2)
+Pass used width, width: calc-size(fit-content, size + 10px)
+Pass used width, width: calc-size(stretch, size / 4) — stretch is the containing block
+Pass used width, width: calc-size(auto, size / 2); left: 0; right: 0 — an auto basis with both insets set
+Pass used width, width: calc-size(max-content, 30%) — a percentage resolves against the containing block
+Pass used width, minWidth: calc-size(max-content, size + 30px)
+Pass used width, width: 300px; maxWidth: calc-size(max-content, size + 20px)
+Pass used width, width: calc-size(calc-size(min-content, size * 2), size + 5px) — nested: the outer size is the inner result
+Pass the content and the containing block have the widths the expectations are built on
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-parsing.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-parsing.txt
new file mode 100644
index 00000000000..6d965bf5233
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-parsing.txt
@@ -0,0 +1,99 @@
+Harness status: OK
+
+Found 94 tests
+
+94 Pass
+Pass e.style['width'] = "calc-size(auto, size)" should set the property value
+Pass e.style['min-width'] = "calc-size(auto, size)" should set the property value
+Pass e.style['height'] = "calc-size(auto, size)" should set the property value
+Pass e.style['min-height'] = "calc-size(auto, size)" should set the property value
+Pass e.style['max-height'] = "calc-size(auto, size)" should not set the property value
+Pass e.style['max-width'] = "calc-size(auto, size)" should not set the property value
+Pass e.style['height'] = "calc-size(none, size)" should not set the property value
+Pass e.style['width'] = "calc-size(none, size)" should not set the property value
+Pass e.style['min-height'] = "calc-size(none, size)" should not set the property value
+Pass e.style['min-width'] = "calc-size(none, size)" should not set the property value
+Pass e.style['max-height'] = "calc-size(none, size)" should not set the property value
+Pass e.style['max-width'] = "calc-size(none, size)" should not set the property value
+Pass e.style['width'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['height'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['max-width'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['max-height'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['min-width'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['min-height'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['block-size'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['inline-size'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['max-block-size'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['max-inline-size'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['min-block-size'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['min-inline-size'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['width'] = "calc-size(fit-content, size)" should set the property value
+Pass e.style['height'] = "calc-size(min-content, size * 2)" should set the property value
+Pass e.style['max-width'] = "calc-size(max-content, size / 2)" should set the property value
+Pass e.style['max-height'] = "calc-size(fit-content, 30px + size / 2)" should set the property value
+Pass e.style['width'] = "calc-size(fit-content, 50% + size / 2)" should set the property value
+Pass e.style['width'] = "calc-size(any, 25em)" should set the property value
+Pass e.style['width'] = "calc-size(any, 40%)" should set the property value
+Pass e.style['width'] = "calc-size(any, 50px + 30%)" should set the property value
+Pass e.style['width'] = "calc-size(calc-size(any, 30px), size)" should set the property value
+Pass e.style['width'] = "calc-size(any, size)" should not set the property value
+Pass e.style['width'] = "calc-size(any, fit-content)" should not set the property value
+Pass e.style['width'] = "calc-size(any, max-content)" should not set the property value
+Pass e.style['width'] = "calc-size(any, min-content)" should not set the property value
+Pass e.style['width'] = "calc-size(10px, sign(size) * size)" should set the property value
+Pass e.style['width'] = "size" should not set the property value
+Pass e.style['width'] = "sign(size)" should not set the property value
+Pass e.style['width'] = "calc(50px * sign(size))" should not set the property value
+Pass e.style['width'] = "calc-size(any, calc-size(10px, sign(size) * size))" should not set the property value
+Pass e.style['width'] = "calc-size(any, calc-size(any, 10px))" should not set the property value
+Pass e.style['width'] = "calc(calc-size(auto, size))" should not set the property value
+Pass e.style['width'] = "calc(50px * sign(calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc(calc-size(auto, size) + calc-size(auto, size))" should not set the property value
+Pass e.style['width'] = "calc(abs(calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc(100px * progress(calc-size(auto, size) from calc-size(auto, 0px) to calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc(100px * progress(calc-size(auto, size) from 0px to 100px))" should not set the property value
+Pass e.style['width'] = "calc(100px * progress(50px from calc-size(auto, 0px) to calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "min(calc-size(auto, 0px), calc-size(auto, size))" should not set the property value
+Pass e.style['width'] = "calc(min(calc-size(auto, 0px), calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "max(calc-size(auto, 0px), calc-size(auto, size))" should not set the property value
+Pass e.style['width'] = "calc(max(calc-size(auto, 0px), calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "clamp(calc-size(auto, 0px), calc-size(auto, 30px), calc-size(auto, size))" should not set the property value
+Pass e.style['width'] = "calc(clamp(calc-size(auto, 0px), calc-size(auto, 30px), calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc(cos(calc-size(auto, 0px)))" should not set the property value
+Pass e.style['width'] = "calc(atan2(calc-size(auto, size), calc-size(auto, 50px)))" should not set the property value
+Pass e.style['width'] = "calc(sqrt(calc-size(auto, 0px)))" should not set the property value
+Pass e.style['width'] = "calc(hypot(calc-size(auto, size * 0.5), calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc(round(calc-size(auto, size * 0.5), calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc(round(calc-size(auto, size * 0.5)))" should not set the property value
+Pass e.style['width'] = "calc(mod(calc-size(auto, size * 0.5), calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc(rem(calc-size(auto, size * 0.5), calc-size(auto, size)))" should not set the property value
+Pass e.style['width'] = "calc-size(30px, 25em)" should set the property value
+Pass e.style['width'] = "calc-size(calc-size(any, 30px), 25em)" should set the property value
+Pass e.style['width'] = "calc-size(calc-size(2in, 30px), 25em)" should set the property value
+Pass e.style['width'] = "calc-size(calc-size(min-content, 30px), 25em)" should set the property value
+Pass e.style['width'] = "calc-size(calc-size(min-content, size), size)" should set the property value
+Pass e.style['height'] = "calc(12% + calc-size(any, 31%))" should not set the property value
+Pass e.style['width'] = "calc-size(30px)" should not set the property value
+Pass e.style['width'] = "calc-size(min(30px, 2em))" should not set the property value
+Pass e.style['width'] = "calc-size(any)" should not set the property value
+Pass e.style['width'] = "calc-size(calc-size(any, 30px))" should not set the property value
+Pass e.style['width'] = "calc-size(size)" should not set the property value
+Pass e.style['width'] = "calc-size(fit-content)" should not set the property value
+Pass e.style['width'] = "calc-size(calc-size(fit-content, size * 2))" should not set the property value
+Pass e.style['width'] = "calc-size(calc-size(30px))" should not set the property value
+Pass e.style['flex-basis'] = "calc-size(any, 50px)" should set the property value
+Pass e.style['flex-basis'] = "calc-size(auto, size)" should set the property value
+Pass e.style['flex-basis'] = "calc-size(min-content, size)" should set the property value
+Pass e.style['flex-basis'] = "calc-size(max-content, size)" should set the property value
+Pass e.style['flex-basis'] = "calc-size(fit-content, size)" should set the property value
+Pass e.style['flex-basis'] = "calc-size(content, size)" should set the property value
+Pass e.style['width'] = "calc-size(content, size)" should not set the property value
+Pass e.style['height'] = "calc-size(content, size)" should not set the property value
+Pass e.style['max-width'] = "calc-size(content, size)" should not set the property value
+Pass e.style['max-height'] = "calc-size(content, size)" should not set the property value
+Pass e.style['min-width'] = "calc-size(content, size)" should not set the property value
+Pass e.style['min-height'] = "calc-size(content, size)" should not set the property value
+Pass e.style['width'] = "calc-size(0px, 0px)" should set the property value
+Pass e.style['width'] = "calc-size(0, 0px)" should not set the property value
+Pass e.style['width'] = "calc-size(0px, 0)" should not set the property value
+Pass e.style['width'] = "calc-size(0, 0)" should not set the property value
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-width-box-sizing.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-width-box-sizing.txt
new file mode 100644
index 00000000000..e7f63669d15
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-width-box-sizing.txt
@@ -0,0 +1,20 @@
+Harness status: OK
+
+Found 15 tests
+
+15 Pass
+Pass resolved width with styles {"width":"auto"}
+Pass resolved width with styles {"width":"auto","box-sizing":"border-box"}
+Pass resolved width with styles {"width":"calc-size(auto, size)"}
+Pass resolved width with styles {"width":"calc-size(auto, size)","box-sizing":"border-box"}
+Pass resolved width with styles {"width":"calc-size(auto, size * 2)"}
+Pass resolved width with styles {"width":"calc-size(auto, size * 2)","box-sizing":"content-box"}
+Pass resolved width with styles {"width":"calc-size(auto, size * 2)","box-sizing":"border-box"}
+Pass resolved width with styles {"width":"calc-size(min-content, size * 2)"}
+Pass resolved width with styles {"width":"calc-size(fit-content, size * 2)","box-sizing":"border-box"}
+Pass resolved width with styles {"min-width":"calc-size(min-content, size * 2)"}
+Pass resolved width with styles {"min-width":"calc-size(max-content, size * 2)","box-sizing":"border-box"}
+Pass resolved width with styles {"width":"200px","max-width":"calc-size(max-content, size * 2)"}
+Pass resolved width with styles {"width":"200px","max-width":"calc-size(min-content, size * 2)","box-sizing":"border-box"}
+Pass resolved width with styles {"width":"calc-size(11px, size * 2)"}
+Pass resolved width with styles {"width":"calc-size(11px, size * 2)","box-sizing":"border-box"}
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-width.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-width.txt
new file mode 100644
index 00000000000..1d355125c0d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/calc-size-width.txt
@@ -0,0 +1,26 @@
+Harness status: OK
+
+Found 20 tests
+
+19 Pass
+1 Fail
+Pass resolved width for width: calc-size(any, 357px)
+Pass resolved width for width: calc-size(any, 31%)
+Pass resolved width for width: calc-size(max-content, 31%)
+Pass resolved width for width: calc-size(fit-content, 72px)
+Pass resolved width for width: calc-size(37px, 93px)
+Pass resolved width for width: calc-size(83px, size * 3)
+Pass resolved width for width: calc-size(min-content, size / 2)
+Pass resolved width for width: calc-size(max-content, size * 1.2)
+Pass resolved width for width: calc-size(fit-content, size / 4 + 30px)
+Fail resolved width for width: calc-size(stretch, size / 2 - 10%)
+Pass resolved width for width: calc-size(30px, 15em)
+Pass resolved width for width: calc-size(calc-size(any, 30px), 15em)
+Pass resolved width for width: calc-size(calc-size(2in, 30px), 15em)
+Pass resolved width for width: calc-size(calc-size(min-content, 30px), 15em)
+Pass resolved width for width: calc-size(calc-size(min-content, size), size)
+Pass resolved width for width: calc-size(auto, size)
+Pass resolved width for width: calc-size(auto, size * 0.6 + 23px)
+Pass resolved width for width: calc-size(fit-content, 0.5 * size) with container width 300px
+Pass resolved width for width: calc-size(fit-content, 0.5 * size) with container width 500px
+Pass resolved width for width: calc-size(fit-content, 0.5 * size) with container width 700px
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-height-box-sizing.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-height-box-sizing.html
new file mode 100644
index 00000000000..c274660c7ee
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-height-box-sizing.html
@@ -0,0 +1,189 @@
+<!DOCTYPE html>
+<title>calc-size() on height, with border/padding/margin/box-sizing</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>
+ #container {
+ display: inline-block;
+ font-size: 20px;
+ }
+ #target {
+ width: 123px;
+ border-style: solid;
+ border-width: 1px 20px 3px 20px;
+ padding: 3px 20px 5px 20px;
+ margin: 2px 20px 1px 20px;
+ }
+ #child {
+ width: 20px;
+ height: 7px;
+ }
+</style>
+
+<div id="container">
+ <div id="target">
+ <div id="child"></div>
+ </div>
+</div>
+
+<script>
+
+let tests = [
+ {
+ styles: {
+ "height": "auto",
+ },
+ expected_target_computed: "7px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "height": "auto",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "19px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "height": "calc-size(auto, size)",
+ },
+ expected_target_computed: "7px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "height": "calc-size(auto, size)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "19px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "height": "calc-size(auto, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "height": "calc-size(auto, size * 2)",
+ "box-sizing": "content-box", /* just one test with explicit initial value */
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "height": "calc-size(auto, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "height": "calc-size(min-content, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "height": "calc-size(fit-content, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "min-height": "calc-size(min-content, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "min-height": "calc-size(fit-content, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "height": "200px",
+ "max-height": "calc-size(min-content, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "height": "200px",
+ "max-height": "calc-size(fit-content, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "height": "calc-size(11px, size * 2)",
+ },
+ expected_target_computed: "22px",
+ expected_target_border_box: 34,
+ expected_container: 37,
+ },
+ {
+ styles: {
+ "height": "calc-size(11px, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "22px",
+ expected_target_border_box: 22,
+ expected_container: 25,
+ },
+];
+const container = document.getElementById("container");
+const container_cs = getComputedStyle(container);
+const target = document.getElementById("target");
+const target_cs = getComputedStyle(target);
+for (const obj of tests) {
+ test((t) => {
+ for (const prop in obj.styles) {
+ target.style.setProperty(prop, obj.styles[prop]);
+ }
+ t.add_cleanup(() => {
+ for (const prop in obj.styles) {
+ target.style.removeProperty(prop);
+ }
+ });
+ assert_equals(target_cs.height, obj.expected_target_computed,
+ "getComputedStyle(target).height");
+ assert_equals(target.getBoundingClientRect().height, obj.expected_target_border_box,
+ "target.getBoundingClientRect().height");
+ assert_equals(container_cs.height, `${obj.expected_container}px`,
+ "getComputedStyle(container).height");
+ assert_equals(container.getBoundingClientRect().height, obj.expected_container,
+ "container.getBoundingClientRect().height");
+ }, `resolved height with styles ${JSON.stringify(obj.styles)}`);
+}
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-height.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-height.html
new file mode 100644
index 00000000000..0e920f74f81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-height.html
@@ -0,0 +1,78 @@
+<!DOCTYPE html>
+<title>calc-size() on height</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>
+ #container {
+ font-size: 20px;
+ }
+ #child {
+ width: 123px;
+ height: 10px;
+ }
+</style>
+
+<div id="container">
+ <div id="target">
+ <div id="child"></div>
+ </div>
+</div>
+
+<script>
+
+let basic_tests = [
+ { value: "calc-size(any, 357px)", expected: "357px" },
+ { value: "calc-size(any, 31%)", expected_auto: "0px", expected_definite: "31px" },
+ { value: "calc-size(31%, size)", expected_auto: "10px", expected_definite: "31px" },
+ { value: "calc-size(max-content, 31%)", expected_auto: "0px", expected_definite: "31px" },
+ { value: "calc-size(fit-content, 72px)", expected: "72px" },
+ { value: "calc-size(37px, 93px)", expected: "93px" },
+ { value: "calc-size(83px, size * 3)", expected: "249px" },
+ { value: "calc-size(min-content, size / 2)", expected: "5px" },
+ { value: "calc-size(max-content, size * 1.2)", expected: "12px" },
+ { value: "calc-size(fit-content, size / 2 + 30px)", expected: "35px" },
+ { 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(any, 300px), size * 2)", expected: "600px" },
+ { value: "calc-size(calc-size(min-content, 30px), 15em)", expected: "300px" },
+ { value: "calc-size(calc-size(min-content, size), size)", expected: "10px" },
+ { value: "calc-size(any, 31% + 12px)", expected_auto: "12px", expected_definite: "43px" },
+ { value: "calc-size(auto, size * 1.5)", expected: "15px" },
+];
+const container = document.getElementById("container");
+const target = document.getElementById("target");
+const target_cs = getComputedStyle(target);
+const child = document.getElementById("child");
+const child_cs = getComputedStyle(child);
+for (const obj of basic_tests) {
+ test((t) => {
+ target.style.removeProperty("height");
+ target.style.height = obj.value;
+ container.style.height = "auto";
+ let expected = "expected" in obj ? obj.expected : obj.expected_auto;
+ assert_equals(target_cs.height, expected);
+ }, `resolved height for height in auto height container: ${obj.value}`);
+ test((t) => {
+ target.style.removeProperty("height");
+ target.style.height = obj.value;
+ container.style.height = "100px";
+ let expected = "expected" in obj ? obj.expected : obj.expected_definite;
+ assert_equals(target_cs.height, expected);
+ }, `resolved height for height in definite height container: ${obj.value}`);
+}
+
+test((t) => {
+ t.add_cleanup(() => {
+ child.style.height = "";
+ child.innerHTML = "";
+ });
+ container.style.height = "auto";
+ target.style.height = "calc-size(min-content, size + 23px)";
+ child.style.height = "100%";
+ child.innerHTML = "<div style='height: 7px'></div>";
+ assert_equals(child_cs.height, "7px");
+}, "calc-size() is indefinite when the intrinsic size is indefinite");
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-out-of-flow.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-out-of-flow.html
new file mode 100644
index 00000000000..0d25a4776fd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-out-of-flow.html
@@ -0,0 +1,71 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>calc-size() with an intrinsic basis on an out-of-flow positioned box</title>
+<link rel="help" href="https://drafts.csswg.org/css-values-5/#calc-size">
+<link rel="help" href="https://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<style>
+ #container {
+ position: relative;
+ width: 400px;
+ height: 300px;
+ }
+ #container > div {
+ position: absolute;
+ top: 0;
+ }
+ /* The content is 100px wide, so min-content and max-content are both 100px. */
+ #container > div > div {
+ width: 100px;
+ height: 20px;
+ }
+</style>
+
+<div id="log"></div>
+<div id="container"></div>
+
+<script>
+const CONTENT = 100;
+const CONTAINER = 400;
+
+const cases = [
+ { styles: { width: "max-content" }, expected: 100, note: "keyword on its own, for comparison" },
+ { styles: { width: "calc-size(max-content, size)" }, expected: 100, note: "identity" },
+ { styles: { width: "calc-size(max-content, size + 20px)" }, expected: 120 },
+ { styles: { width: "calc-size(min-content, size * 2)" }, expected: 200 },
+ { styles: { width: "calc-size(fit-content, size + 10px)" }, expected: 110 },
+ { styles: { width: "calc-size(stretch, size / 4)" }, expected: 100, note: "stretch is the containing block" },
+ { styles: { width: "calc-size(auto, size / 2)", left: "0", right: "0" }, expected: 200, note: "an auto basis with both insets set" },
+ { styles: { width: "calc-size(max-content, 30%)" }, expected: 120, note: "a percentage resolves against the containing block" },
+ { styles: { minWidth: "calc-size(max-content, size + 30px)" }, expected: 130 },
+ { styles: { width: "300px", maxWidth: "calc-size(max-content, size + 20px)" }, expected: 120 },
+ { styles: { width: "calc-size(calc-size(min-content, size * 2), size + 5px)" }, expected: 205, note: "nested: the outer size is the inner result" },
+];
+
+const container = document.getElementById("container");
+
+for (const testCase of cases) {
+ const box = document.createElement("div");
+ Object.assign(box.style, testCase.styles);
+ box.appendChild(document.createElement("div"));
+ container.appendChild(box);
+ testCase.box = box;
+}
+
+for (const testCase of cases) {
+ const written = Object.entries(testCase.styles).map(([property, value]) => `${property}: ${value}`).join("; ");
+
+ test(() => {
+ assert_equals(testCase.box.getBoundingClientRect().width, testCase.expected);
+ }, "used width, " + written + (testCase.note ? " — " + testCase.note : ""));
+}
+
+test(() => {
+ assert_equals(cases[0].box.getBoundingClientRect().width, CONTENT,
+ "the content is not the width this test assumes");
+ assert_equals(container.getBoundingClientRect().width, CONTAINER,
+ "the containing block is not the width this test assumes");
+}, "the content and the containing block have the widths the expectations are built on");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-parsing.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-parsing.html
new file mode 100644
index 00000000000..1d0d62554da
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-parsing.html
@@ -0,0 +1,115 @@
+<!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>
+
+<script>
+
+test_valid_value("width", "calc-size(auto, size)");
+test_valid_value("min-width", "calc-size(auto, size)");
+test_valid_value("height", "calc-size(auto, size)");
+test_valid_value("min-height", "calc-size(auto, size)");
+test_invalid_value("max-height", "calc-size(auto, size)");
+test_invalid_value("max-width", "calc-size(auto, size)");
+
+test_invalid_value("height", "calc-size(none, size)");
+test_invalid_value("width", "calc-size(none, size)");
+test_invalid_value("min-height", "calc-size(none, size)");
+test_invalid_value("min-width", "calc-size(none, size)");
+test_invalid_value("max-height", "calc-size(none, size)");
+test_invalid_value("max-width", "calc-size(none, size)");
+
+test_valid_value("width", "calc-size(max-content, size)");
+test_valid_value("height", "calc-size(max-content, size)");
+test_valid_value("max-width", "calc-size(max-content, size)");
+test_valid_value("max-height", "calc-size(max-content, size)");
+test_valid_value("min-width", "calc-size(max-content, size)");
+test_valid_value("min-height", "calc-size(max-content, size)");
+test_valid_value("block-size", "calc-size(max-content, size)");
+test_valid_value("inline-size", "calc-size(max-content, size)");
+test_valid_value("max-block-size", "calc-size(max-content, size)");
+test_valid_value("max-inline-size", "calc-size(max-content, size)");
+test_valid_value("min-block-size", "calc-size(max-content, size)");
+test_valid_value("min-inline-size", "calc-size(max-content, size)");
+
+test_valid_value("width", "calc-size(fit-content, size)");
+test_valid_value("height", "calc-size(min-content, size * 2)", "calc-size(min-content, 2 * size)");
+test_valid_value("max-width", "calc-size(max-content, size / 2)", "calc-size(max-content, 0.5 * size)");
+test_valid_value("max-height", "calc-size(fit-content, 30px + size / 2)", "calc-size(fit-content, 30px + (0.5 * size))");
+test_valid_value("width", "calc-size(fit-content, 50% + size / 2)", "calc-size(fit-content, 50% + (0.5 * size))");
+
+test_valid_value("width", "calc-size(any, 25em)");
+test_valid_value("width", "calc-size(any, 40%)");
+test_valid_value("width", "calc-size(any, 50px + 30%)", "calc-size(any, 30% + 50px)");
+test_valid_value("width", "calc-size(calc-size(any, 30px), size)");
+test_invalid_value("width", "calc-size(any, size)");
+test_invalid_value("width", "calc-size(any, fit-content)");
+test_invalid_value("width", "calc-size(any, max-content)");
+test_invalid_value("width", "calc-size(any, min-content)");
+test_valid_value("width", "calc-size(10px, sign(size) * size)");
+test_invalid_value("width", "size");
+test_invalid_value("width", "sign(size)");
+test_invalid_value("width", "calc(50px * sign(size))");
+test_invalid_value("width", "calc-size(any, calc-size(10px, sign(size) * size))");
+test_invalid_value("width", "calc-size(any, calc-size(any, 10px))");
+test_invalid_value("width", "calc(calc-size(auto, size))");
+test_invalid_value("width", "calc(50px * sign(calc-size(auto, size)))");
+test_invalid_value("width", "calc(calc-size(auto, size) + calc-size(auto, size))");
+test_invalid_value("width", "calc(abs(calc-size(auto, size)))");
+test_invalid_value("width", "calc(100px * progress(calc-size(auto, size) from calc-size(auto, 0px) to calc-size(auto, size)))");
+test_invalid_value("width", "calc(100px * progress(calc-size(auto, size) from 0px to 100px))");
+test_invalid_value("width", "calc(100px * progress(50px from calc-size(auto, 0px) to calc-size(auto, size)))");
+test_invalid_value("width", "min(calc-size(auto, 0px), calc-size(auto, size))");
+test_invalid_value("width", "calc(min(calc-size(auto, 0px), calc-size(auto, size)))");
+test_invalid_value("width", "max(calc-size(auto, 0px), calc-size(auto, size))");
+test_invalid_value("width", "calc(max(calc-size(auto, 0px), calc-size(auto, size)))");
+test_invalid_value("width", "clamp(calc-size(auto, 0px), calc-size(auto, 30px), calc-size(auto, size))");
+test_invalid_value("width", "calc(clamp(calc-size(auto, 0px), calc-size(auto, 30px), calc-size(auto, size)))");
+test_invalid_value("width", "calc(cos(calc-size(auto, 0px)))");
+test_invalid_value("width", "calc(atan2(calc-size(auto, size), calc-size(auto, 50px)))");
+test_invalid_value("width", "calc(sqrt(calc-size(auto, 0px)))");
+test_invalid_value("width", "calc(hypot(calc-size(auto, size * 0.5), calc-size(auto, size)))");
+test_invalid_value("width", "calc(round(calc-size(auto, size * 0.5), calc-size(auto, size)))");
+test_invalid_value("width", "calc(round(calc-size(auto, size * 0.5)))");
+test_invalid_value("width", "calc(mod(calc-size(auto, size * 0.5), calc-size(auto, size)))");
+test_invalid_value("width", "calc(rem(calc-size(auto, size * 0.5), calc-size(auto, size)))");
+
+test_valid_value("width", "calc-size(30px, 25em)");
+test_valid_value("width", "calc-size(calc-size(any, 30px), 25em)");
+test_valid_value("width", "calc-size(calc-size(2in, 30px), 25em)", "calc-size(calc-size(192px, 30px), 25em)");
+test_valid_value("width", "calc-size(calc-size(min-content, 30px), 25em)");
+test_valid_value("width", "calc-size(calc-size(min-content, size), size)");
+
+test_invalid_value("height", "calc(12% + calc-size(any, 31%))");
+
+test_invalid_value("width", "calc-size(30px)");
+test_invalid_value("width", "calc-size(min(30px, 2em))");
+test_invalid_value("width", "calc-size(any)");
+test_invalid_value("width", "calc-size(calc-size(any, 30px))");
+test_invalid_value("width", "calc-size(size)");
+test_invalid_value("width", "calc-size(fit-content)");
+test_invalid_value("width", "calc-size(calc-size(fit-content, size * 2))");
+test_invalid_value("width", "calc-size(calc-size(30px))");
+
+test_valid_value("flex-basis", "calc-size(any, 50px)");
+test_valid_value("flex-basis", "calc-size(auto, size)");
+test_valid_value("flex-basis", "calc-size(min-content, size)");
+test_valid_value("flex-basis", "calc-size(max-content, size)");
+test_valid_value("flex-basis", "calc-size(fit-content, size)");
+test_valid_value("flex-basis", "calc-size(content, size)");
+
+test_invalid_value("width", "calc-size(content, size)");
+test_invalid_value("height", "calc-size(content, size)");
+test_invalid_value("max-width", "calc-size(content, size)");
+test_invalid_value("max-height", "calc-size(content, size)");
+test_invalid_value("min-width", "calc-size(content, size)");
+test_invalid_value("min-height", "calc-size(content, size)");
+
+test_valid_value("width", "calc-size(0px, 0px)");
+test_invalid_value("width", "calc-size(0, 0px)");
+test_invalid_value("width", "calc-size(0px, 0)");
+test_invalid_value("width", "calc-size(0, 0)");
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-width-box-sizing.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-width-box-sizing.html
new file mode 100644
index 00000000000..902ce8b948e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-width-box-sizing.html
@@ -0,0 +1,190 @@
+<!DOCTYPE html>
+<title>calc-size() on width, with border/padding/margin/box-sizing</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>
+ #container {
+ display: inline-block;
+ font-size: 20px;
+ }
+ #target {
+ display: inline-block;
+ height: 123px;
+ border-style: solid;
+ border-width: 20px 1px 20px 3px;
+ padding: 20px 3px 20px 5px;
+ margin: 20px 2px 20px 1px;
+ }
+ #child {
+ height: 20px;
+ width: 7px;
+ }
+</style>
+
+<div id="container">
+ <div id="target">
+ <div id="child"></div>
+ </div>
+</div>
+
+<script>
+
+let tests = [
+ {
+ styles: {
+ "width": "auto",
+ },
+ expected_target_computed: "7px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "width": "auto",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "19px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "width": "calc-size(auto, size)",
+ },
+ expected_target_computed: "7px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "width": "calc-size(auto, size)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "19px",
+ expected_target_border_box: 19,
+ expected_container: 22,
+ },
+ {
+ styles: {
+ "width": "calc-size(auto, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "width": "calc-size(auto, size * 2)",
+ "box-sizing": "content-box", /* just one test with explicit initial value */
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "width": "calc-size(auto, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "width": "calc-size(min-content, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "width": "calc-size(fit-content, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "min-width": "calc-size(min-content, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "min-width": "calc-size(max-content, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "width": "200px",
+ "max-width": "calc-size(max-content, size * 2)",
+ },
+ expected_target_computed: "14px",
+ expected_target_border_box: 26,
+ expected_container: 29,
+ },
+ {
+ styles: {
+ "width": "200px",
+ "max-width": "calc-size(min-content, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "38px",
+ expected_target_border_box: 38,
+ expected_container: 41,
+ },
+ {
+ styles: {
+ "width": "calc-size(11px, size * 2)",
+ },
+ expected_target_computed: "22px",
+ expected_target_border_box: 34,
+ expected_container: 37,
+ },
+ {
+ styles: {
+ "width": "calc-size(11px, size * 2)",
+ "box-sizing": "border-box",
+ },
+ expected_target_computed: "22px",
+ expected_target_border_box: 22,
+ expected_container: 25,
+ },
+];
+const container = document.getElementById("container");
+const container_cs = getComputedStyle(container);
+const target = document.getElementById("target");
+const target_cs = getComputedStyle(target);
+for (const obj of tests) {
+ test((t) => {
+ for (const prop in obj.styles) {
+ target.style.setProperty(prop, obj.styles[prop]);
+ }
+ t.add_cleanup(() => {
+ for (const prop in obj.styles) {
+ target.style.removeProperty(prop);
+ }
+ });
+ assert_equals(target_cs.width, obj.expected_target_computed,
+ "getComputedStyle(target).width");
+ assert_equals(target.getBoundingClientRect().width, obj.expected_target_border_box,
+ "target.getBoundingClientRect().width");
+ assert_equals(container_cs.width, `${obj.expected_container}px`,
+ "getComputedStyle(container).width");
+ assert_equals(container.getBoundingClientRect().width, obj.expected_container,
+ "container.getBoundingClientRect().width");
+ }, `resolved width with styles ${JSON.stringify(obj.styles)}`);
+}
+
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-width.html b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-width.html
new file mode 100644
index 00000000000..d467ba7918a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-values/calc-size/calc-size-width.html
@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<title>calc-size() on width</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>
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<style>
+ #container {
+ font-family: 'Ahem';
+ font-size: 20px;
+ width: 500px;
+ }
+ #target {
+ }
+</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 = [
+ { 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, 72px)", expected: "72px" },
+ { value: "calc-size(37px, 93px)", expected: "93px" },
+ { 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: "500px" },
+ { value: "calc-size(auto, size * 0.6 + 23px)", expected: "323px" },
+ ];
+ 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("width");
+ target.style.width = obj.value;
+ assert_equals(target_cs.width, obj.expected);
+ }, `resolved width for width: ${obj.value}`);
+ }
+
+ target.style.width = "calc-size(fit-content, size / 2)";
+ let container_size_tests = [
+ { container_width: "300px", expected: "200px" },
+ { container_width: "500px", expected: "250px" },
+ { container_width: "700px", expected: "300px" },
+ ];
+ for (const obj of container_size_tests) {
+ test((t) => {
+ container.style.width = obj.container_width;
+ assert_equals(target_cs.width, obj.expected);
+ }, `resolved width for width: ${target.style.width} with container width ${obj.container_width}`);
+ }
+ container.style.width = "";
+
+ done();
+});
+
+</script>