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

LibWeb: Add resolving calc() to a number/integer/percentage

None of these require any outside metrics, which is nice! I believe the
Values-4 spec would have us simplify them down into a single value at
parse time, but that's a yak for another day.
This commit is contained in:
Sam Atkins 2022-01-27 15:46:24 +00:00 committed by Andreas Kling
parent e111e8e44e
commit 2407a03fd9
2 changed files with 27 additions and 0 deletions

View file

@ -416,6 +416,30 @@ Optional<LengthPercentage> CalculatedStyleValue::resolve_length_percentage(Layou
});
}
Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
{
auto result = m_expression->resolve(nullptr, {});
if (result.value().has<Percentage>())
return result.value().get<Percentage>();
return {};
}
Optional<float> CalculatedStyleValue::resolve_number()
{
auto result = m_expression->resolve(nullptr, {});
if (result.value().has<float>())
return result.value().get<float>();
return {};
}
Optional<i64> CalculatedStyleValue::resolve_integer()
{
auto result = m_expression->resolve(nullptr, {});
if (result.value().has<float>())
return lroundf(result.value().get<float>());
return {};
}
static bool is_number(CalculatedStyleValue::ResolvedType type)
{
return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;

View file

@ -803,6 +803,9 @@ public:
NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; }
Optional<Length> resolve_length(Layout::Node const& layout_node) const;
Optional<LengthPercentage> resolve_length_percentage(Layout::Node const&, Length const& percentage_basis) const;
Optional<Percentage> resolve_percentage() const;
Optional<float> resolve_number();
Optional<i64> resolve_integer();
private:
explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum, ResolvedType resolved_type)