LibWeb/CSS: Implement "consistent type" concept on CSSNumericType

This commit is contained in:
Sam Atkins 2024-12-12 14:38:15 +00:00 committed by Andreas Kling
parent 2192868a0e
commit 9cbd08a330
Notes: github-actions[bot] 2024-12-21 17:15:39 +00:00
2 changed files with 39 additions and 2 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023-2024, Sam Atkins <sam@ladybird.org>
*
* 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 doesnt result in failure.
return added_to(other).has_value();
}
// https://drafts.csswg.org/css-values-4/#css-consistent-typec
Optional<CSSNumericType> 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> CSSNumericType::made_consistent_with(CSSNumericType const& input) const
{
auto base = *this;
// 1. If both base and input have different non-null percent hints, they cant 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 bases percent hint to inputs 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)
{

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023-2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -63,6 +63,10 @@ public:
Optional<CSSNumericType> multiplied_by(CSSNumericType const& other) const;
CSSNumericType inverted() const;
bool has_consistent_type_with(CSSNumericType const& other) const;
Optional<CSSNumericType> consistent_type(CSSNumericType const& other) const;
Optional<CSSNumericType> 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); }