summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-02-02 15:34:13 +0000
committerAndreas Kling <kling@serenityos.org>2022-02-04 13:52:02 +0100
commitcbdbe0c5a2b1153890209b6942d8902226f56bd2 (patch)
tree597a5dd345052eca3abd7915dd3c1600ac6a9d51 /Userland/Libraries/LibWeb
parent714832e70544011c00be3cf0bd9fa8ba18ac7486 (diff)
downloadserenity-cbdbe0c5a2b1153890209b6942d8902226f56bd2.zip
LibWeb: Implement CalculatedStyleValue::to_string()
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp3
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValue.cpp80
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValue.h20
3 files changed, 95 insertions, 8 deletions
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<StyleValue> Parser::parse_calculated_value(Vector<StyleComponentValueRule
};
dbgln_if(CSS_PARSER_DEBUG, "Deduced calc() resolved type as: {}", to_string(calc_type.value()));
- // FIXME: Either produce a string value of calc() here, or do so in CalculatedStyleValue::to_string().
- return CalculatedStyleValue::create("(FIXME:calc to string)", calc_expression.release_nonnull(), calc_type.release_value());
+ return CalculatedStyleValue::create(calc_expression.release_nonnull(), calc_type.release_value());
}
RefPtr<StyleValue> 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<CalcNumberSum> 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<CalcSum> 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<Length> 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<Number, NonnullOwnPtr<CalcNumberSum>> value;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
struct CalcValue {
Variant<Number, Length, Percentage, NonnullOwnPtr<CalcSum>> value;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
@@ -726,6 +728,7 @@ public:
NonnullOwnPtr<CalcProduct> first_calc_product;
NonnullOwnPtrVector<CalcSumPartWithOperator> zero_or_more_additional_calc_products;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
@@ -738,6 +741,7 @@ public:
NonnullOwnPtr<CalcNumberProduct> first_calc_number_product;
NonnullOwnPtrVector<CalcNumberSumPartWithOperator> zero_or_more_additional_calc_number_products;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
@@ -746,6 +750,7 @@ public:
CalcValue first_calc_value;
NonnullOwnPtrVector<CalcProductPartWithOperator> zero_or_more_additional_calc_values;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
@@ -758,6 +763,7 @@ public:
SumOperation op;
NonnullOwnPtr<CalcProduct> value;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
@@ -766,6 +772,7 @@ public:
ProductOperation op;
Variant<CalcValue, CalcNumberValue> value;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
@@ -774,6 +781,7 @@ public:
CalcNumberValue first_calc_number_value;
NonnullOwnPtrVector<CalcNumberProductPartWithOperator> zero_or_more_additional_calc_number_values;
+ String to_string() const;
Optional<ResolvedType> 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<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
@@ -794,16 +803,17 @@ public:
SumOperation op;
NonnullOwnPtr<CalcNumberProduct> value;
+ String to_string() const;
Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
};
- static NonnullRefPtr<CalculatedStyleValue> create(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum, ResolvedType resolved_type)
+ static NonnullRefPtr<CalculatedStyleValue> create(NonnullOwnPtr<CalcSum> 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<CalcSum> const& expression() const { return m_expression; }
Optional<Length> resolve_length(Layout::Node const& layout_node) const;
@@ -813,16 +823,14 @@ public:
Optional<i64> resolve_integer();
private:
- explicit CalculatedStyleValue(String const& expression_string, NonnullOwnPtr<CalcSum> calc_sum, ResolvedType resolved_type)
+ explicit CalculatedStyleValue(NonnullOwnPtr<CalcSum> 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<CalcSum> m_expression;
};