Skip to content

Truly independent web browser

LibWeb: Make calc-size() with a calculation basis act as its basis

Andreas Kling10546610d218
diff --git a/Libraries/LibWeb/Rust/src/css/calc_size.rs b/Libraries/LibWeb/Rust/src/css/calc_size.rs
index 9f8a67cc103..f86338644b1 100644
--- a/Libraries/LibWeb/Rust/src/css/calc_size.rs
+++ b/Libraries/LibWeb/Rust/src/css/calc_size.rs
@@ -130,6 +130,15 @@ pub(crate) fn calc_size_basis_keyword(value: &StyleValueData) -> Option<u16> {
}
}
+/// Whether a calc-size() has a <calc-sum> basis, after following nested calc-size() bases.
+pub(crate) fn calc_size_has_calc_sum_basis(value: &StyleValueData) -> bool {
+ match calc_size_arguments(value) {
+ Some((StyleValueData::Keyword { .. }, _)) | None => false,
+ Some((basis @ StyleValueData::Function { .. }, _)) => calc_size_has_calc_sum_basis(basis),
+ Some(_) => true,
+ }
+}
+
/// 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.
@@ -138,28 +147,13 @@ 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)?,
- }
+ let size = 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 { .. } => {
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 328b9fb30db..bef2ed8fdd4 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -3305,6 +3305,13 @@ impl<'pass> SizingContext<'pass> {
let Some(calc_size) = size.calc_size() else {
return content_size;
};
+ // https://drafts.csswg.org/css-values-5/#valdef-calc-size-calc-sum
+ // For all purposes other than actually resolving the size, the calc-size() acts like this calculation wrapped
+ // in a calc() (including, for example, whether it represents a definite or indefinite size).
+ // NB: A size given as a calculation reaches here when it behaves as auto, so the calc-size() does too.
+ if crate::css::calc_size::calc_size_has_calc_sum_basis(calc_size) {
+ 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 {
@@ -3327,14 +3334,11 @@ impl<'pass> SizingContext<'pass> {
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 {
+ let Some(resolved) =
+ crate::css::calc_size::resolve_calc_size(calc_size, percentage_basis.map(CssPixels::to_double), &|_| {
+ basis_size
+ })
+ else {
return content_size;
};
(CssPixels::nearest_value_for(resolved) - edges).max(CssPixels::default())
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/animation/calc-size-height-interpolation.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/animation/calc-size-height-interpolation.txt
index 505f4351a6f..9ad681739a2 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/animation/calc-size-height-interpolation.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/calc-size/animation/calc-size-height-interpolation.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 1156 tests
-968 Pass
-188 Fail
+992 Pass
+164 Fail
Pass CSS Transitions: property <height> from [calc-size(auto, size)] to [calc-size(auto, size * 2)] at (-0.25) should be [75px]
Pass CSS Transitions: property <height> from [calc-size(auto, size)] to [calc-size(auto, size * 2)] at (0) should be [100px]
Pass CSS Transitions: property <height> from [calc-size(auto, size)] to [calc-size(auto, size * 2)] at (0.25) should be [125px]
@@ -1070,30 +1070,30 @@ Pass CSS Animations: property <height> from [calc-size(20%, size)] to [calc-size
Pass Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [60px]
Pass Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [120px]
Pass Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [180px]
-Fail CSS Transitions: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail CSS Transitions: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail CSS Transitions: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
-Fail CSS Transitions with transition: all: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail CSS Transitions with transition: all: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail CSS Transitions with transition: all: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
-Fail CSS Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail CSS Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail CSS Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
-Fail Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
-Fail CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
-Fail CSS Transitions with transition: all: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail CSS Transitions with transition: all: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail CSS Transitions with transition: all: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
-Fail CSS Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail CSS Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail CSS Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
-Fail Web Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
-Fail Web Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
-Fail Web Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass CSS Transitions: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass CSS Transitions: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass CSS Transitions: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass CSS Transitions with transition: all: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass CSS Transitions with transition: all: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass CSS Transitions with transition: all: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass CSS Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass CSS Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass CSS Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass Web Animations: property <height> from [calc-size(20%, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass CSS Transitions with transition: all: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass CSS Transitions with transition: all: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass CSS Transitions with transition: all: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass CSS Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass CSS Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass CSS Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
+Pass Web Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0) should be [100px]
+Pass Web Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (0.5) should be [100px]
+Pass Web Animations: property <height> from [calc-size(50px, size)] to [calc-size(60%, size)] at (1) should be [100px]
Pass CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(200px, size)] at (0) should be [50px]
Pass CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(200px, size)] at (0.5) should be [125px]
Pass CSS Transitions: property <height> from [calc-size(50px, size)] to [calc-size(200px, size)] at (1) should be [200px]