From cbdbe0c5a2b1153890209b6942d8902226f56bd2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 2 Feb 2022 15:34:13 +0000 Subject: LibWeb: Implement CalculatedStyleValue::to_string() --- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 3 +- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 80 +++++++++++++++++++++++++ Userland/Libraries/LibWeb/CSS/StyleValue.h | 20 +++++-- 3 files changed, 95 insertions(+), 8 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 79345fff14..1bfb4ef4e5 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2124,8 +2124,7 @@ RefPtr Parser::parse_calculated_value(Vector Parser::parse_dynamic_value(StyleComponentValueRule const& component_value) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 1996bcf71b..ffeb4c376c 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -398,6 +398,86 @@ void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& }); } +String CalculatedStyleValue::to_string() const +{ + return String::formatted("calc({})", m_expression->to_string()); +} + +String CalculatedStyleValue::CalcNumberValue::to_string() const +{ + return value.visit( + [](Number const& number) { return String::number(number.value); }, + [](NonnullOwnPtr const& sum) { return String::formatted("({})", sum->to_string()); }); +} + +String CalculatedStyleValue::CalcValue::to_string() const +{ + return value.visit( + [](Number const& number) { return String::number(number.value); }, + [](Length const& length) { return length.to_string(); }, + [](Percentage const& percentage) { return percentage.to_string(); }, + [](NonnullOwnPtr const& sum) { return String::formatted("({})", sum->to_string()); }); +} + +String CalculatedStyleValue::CalcSum::to_string() const +{ + StringBuilder builder; + builder.append(first_calc_product->to_string()); + for (auto const& item : zero_or_more_additional_calc_products) + builder.append(item.to_string()); + return builder.to_string(); +} + +String CalculatedStyleValue::CalcNumberSum::to_string() const +{ + StringBuilder builder; + builder.append(first_calc_number_product->to_string()); + for (auto const& item : zero_or_more_additional_calc_number_products) + builder.append(item.to_string()); + return builder.to_string(); +} + +String CalculatedStyleValue::CalcProduct::to_string() const +{ + StringBuilder builder; + builder.append(first_calc_value.to_string()); + for (auto const& item : zero_or_more_additional_calc_values) + builder.append(item.to_string()); + return builder.to_string(); +} + +String CalculatedStyleValue::CalcSumPartWithOperator::to_string() const +{ + return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string()); +} + +String CalculatedStyleValue::CalcProductPartWithOperator::to_string() const +{ + auto value_string = value.visit( + [](CalcValue const& v) { return v.to_string(); }, + [](CalcNumberValue const& v) { return v.to_string(); }); + return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string); +} + +String CalculatedStyleValue::CalcNumberProduct::to_string() const +{ + StringBuilder builder; + builder.append(first_calc_number_value.to_string()); + for (auto const& item : zero_or_more_additional_calc_number_values) + builder.append(item.to_string()); + return builder.to_string(); +} + +String CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const +{ + return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value.to_string()); +} + +String CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const +{ + return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string()); +} + Optional CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const { auto result = m_expression->resolve(&layout_node, {}); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 0651d9fe44..35f3954b22 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -707,12 +707,14 @@ public: struct CalcNumberValue { Variant> value; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; struct CalcValue { Variant> value; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -726,6 +728,7 @@ public: NonnullOwnPtr first_calc_product; NonnullOwnPtrVector zero_or_more_additional_calc_products; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -738,6 +741,7 @@ public: NonnullOwnPtr first_calc_number_product; NonnullOwnPtrVector zero_or_more_additional_calc_number_products; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -746,6 +750,7 @@ public: CalcValue first_calc_value; NonnullOwnPtrVector zero_or_more_additional_calc_values; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -758,6 +763,7 @@ public: SumOperation op; NonnullOwnPtr value; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -766,6 +772,7 @@ public: ProductOperation op; Variant value; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -774,6 +781,7 @@ public: CalcNumberValue first_calc_number_value; NonnullOwnPtrVector zero_or_more_additional_calc_number_values; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -782,6 +790,7 @@ public: ProductOperation op; CalcNumberValue value; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; @@ -794,16 +803,17 @@ public: SumOperation op; NonnullOwnPtr value; + String to_string() const; Optional resolved_type() const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; }; - static NonnullRefPtr create(String const& expression_string, NonnullOwnPtr calc_sum, ResolvedType resolved_type) + static NonnullRefPtr create(NonnullOwnPtr calc_sum, ResolvedType resolved_type) { - return adopt_ref(*new CalculatedStyleValue(expression_string, move(calc_sum), resolved_type)); + return adopt_ref(*new CalculatedStyleValue(move(calc_sum), resolved_type)); } - String to_string() const override { return m_expression_string; } + String to_string() const override; ResolvedType resolved_type() const { return m_resolved_type; } NonnullOwnPtr const& expression() const { return m_expression; } Optional resolve_length(Layout::Node const& layout_node) const; @@ -813,16 +823,14 @@ public: Optional resolve_integer(); private: - explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr calc_sum, ResolvedType resolved_type) + explicit CalculatedStyleValue(NonnullOwnPtr calc_sum, ResolvedType resolved_type) : StyleValue(Type::Calculated) , m_resolved_type(resolved_type) - , m_expression_string(expression_string) , m_expression(move(calc_sum)) { } ResolvedType m_resolved_type; - String m_expression_string; NonnullOwnPtr m_expression; }; -- cgit v1.2.3