1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

LibWeb: Add CSSNumericType::matches_dimension()

This isn't a spec algorithm, but is useful when type-checking parameters
to math functions.
This commit is contained in:
Sam Atkins 2023-07-08 17:47:45 +01:00 committed by Andreas Kling
parent 136dc7a1c3
commit 780998b3d5
2 changed files with 28 additions and 0 deletions

View file

@ -413,6 +413,32 @@ bool CSSNumericType::matches_number_percentage() const
return true;
}
bool CSSNumericType::matches_dimension() const
{
// This isn't a spec algorithm.
// A type should match `<dimension>` if there are no non-zero entries,
// or it has a single non-zero entry (other than percent) which is equal to 1.
auto number_of_one_exponents = 0;
for (auto i = 0; i < to_underlying(BaseType::__Count); ++i) {
auto base_type = static_cast<BaseType>(i);
auto type_exponent = exponent(base_type);
if (!type_exponent.has_value())
continue;
if (type_exponent == 1) {
if (base_type == BaseType::Percent)
return false;
number_of_one_exponents++;
} else if (type_exponent != 0) {
return false;
}
}
return number_of_one_exponents == 0 || number_of_one_exponents == 1;
}
ErrorOr<String> CSSNumericType::dump() const
{
StringBuilder builder;

View file

@ -79,6 +79,8 @@ public:
bool matches_time() const { return matches_dimension(BaseType::Time); }
bool matches_time_percentage() const { return matches_dimension_percentage(BaseType::Time); }
bool matches_dimension() const;
Optional<i32> const& exponent(BaseType type) const { return m_type_exponents[to_underlying(type)]; }
void set_exponent(BaseType type, i32 exponent) { m_type_exponents[to_underlying(type)] = exponent; }