From 2407a03fd9caa0f1fd625f7344304f794b6946db Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 27 Jan 2022 15:46:24 +0000 Subject: [PATCH] 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. --- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 24 ++++++++++++++++++++ Userland/Libraries/LibWeb/CSS/StyleValue.h | 3 +++ 2 files changed, 27 insertions(+) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index cd91c4ef4a..0012131917 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -416,6 +416,30 @@ Optional CalculatedStyleValue::resolve_length_percentage(Layou }); } +Optional CalculatedStyleValue::resolve_percentage() const +{ + auto result = m_expression->resolve(nullptr, {}); + if (result.value().has()) + return result.value().get(); + return {}; +} + +Optional CalculatedStyleValue::resolve_number() +{ + auto result = m_expression->resolve(nullptr, {}); + if (result.value().has()) + return result.value().get(); + return {}; +} + +Optional CalculatedStyleValue::resolve_integer() +{ + auto result = m_expression->resolve(nullptr, {}); + if (result.value().has()) + return lroundf(result.value().get()); + return {}; +} + static bool is_number(CalculatedStyleValue::ResolvedType type) { return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer; diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index be0c3bee44..8a87c6240e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -803,6 +803,9 @@ public: NonnullOwnPtr const& expression() const { return m_expression; } Optional resolve_length(Layout::Node const& layout_node) const; Optional resolve_length_percentage(Layout::Node const&, Length const& percentage_basis) const; + Optional resolve_percentage() const; + Optional resolve_number(); + Optional resolve_integer(); private: explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr calc_sum, ResolvedType resolved_type)