From 9cbd08a330d87657954eeb91df376471820f8c87 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 12 Dec 2024 14:38:15 +0000 Subject: [PATCH] LibWeb/CSS: Implement "consistent type" concept on CSSNumericType --- Libraries/LibWeb/CSS/CSSNumericType.cpp | 35 ++++++++++++++++++++++++- Libraries/LibWeb/CSS/CSSNumericType.h | 6 ++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Libraries/LibWeb/CSS/CSSNumericType.cpp b/Libraries/LibWeb/CSS/CSSNumericType.cpp index 53e603ae4c2..b568e5192cd 100644 --- a/Libraries/LibWeb/CSS/CSSNumericType.cpp +++ b/Libraries/LibWeb/CSS/CSSNumericType.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Sam Atkins + * Copyright (c) 2023-2024, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -263,6 +263,39 @@ CSSNumericType CSSNumericType::inverted() const return result; } +// https://drafts.csswg.org/css-values-4/#css-consistent-typec +bool CSSNumericType::has_consistent_type_with(CSSNumericType const& other) const +{ + // Two or more calculations have a consistent type if adding the types doesn’t result in failure. + return added_to(other).has_value(); +} + +// https://drafts.csswg.org/css-values-4/#css-consistent-typec +Optional CSSNumericType::consistent_type(CSSNumericType const& other) const +{ + // The consistent type is the result of the type addition. + return added_to(other); +} + +// https://drafts.csswg.org/css-values-4/#css-make-a-type-consistent +Optional CSSNumericType::made_consistent_with(CSSNumericType const& input) const +{ + auto base = *this; + + // 1. If both base and input have different non-null percent hints, they can’t be made consistent. Return failure. + auto base_percent_hint = base.percent_hint(); + auto input_percent_hint = input.percent_hint(); + if (base_percent_hint.has_value() && input_percent_hint.has_value() && base_percent_hint != input_percent_hint) + return {}; + + // 2. If base has a null percent hint set base’s percent hint to input’s percent hint. + if (!base_percent_hint.has_value()) + base.set_percent_hint(input_percent_hint); + + // 3. Return base. + return base; +} + // https://drafts.css-houdini.org/css-typed-om-1/#apply-the-percent-hint void CSSNumericType::apply_percent_hint(BaseType hint) { diff --git a/Libraries/LibWeb/CSS/CSSNumericType.h b/Libraries/LibWeb/CSS/CSSNumericType.h index 09ab31d1bc1..b00a0de9c8a 100644 --- a/Libraries/LibWeb/CSS/CSSNumericType.h +++ b/Libraries/LibWeb/CSS/CSSNumericType.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Sam Atkins + * Copyright (c) 2023-2024, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -63,6 +63,10 @@ public: Optional multiplied_by(CSSNumericType const& other) const; CSSNumericType inverted() const; + bool has_consistent_type_with(CSSNumericType const& other) const; + Optional consistent_type(CSSNumericType const& other) const; + Optional made_consistent_with(CSSNumericType const& other) const; + bool matches_angle() const { return matches_dimension(BaseType::Angle); } bool matches_angle_percentage() const { return matches_dimension_percentage(BaseType::Angle); } bool matches_flex() const { return matches_dimension(BaseType::Flex); }