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

LibWeb: Implement CalculatedStyleValue::to_string()

This commit is contained in:
Sam Atkins 2022-02-02 15:34:13 +00:00 committed by Andreas Kling
parent 714832e705
commit cbdbe0c5a2
3 changed files with 95 additions and 8 deletions

View file

@ -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())); 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(calc_expression.release_nonnull(), calc_type.release_value());
return CalculatedStyleValue::create("(FIXME:calc to string)", calc_expression.release_nonnull(), calc_type.release_value());
} }
RefPtr<StyleValue> Parser::parse_dynamic_value(StyleComponentValueRule const& component_value) RefPtr<StyleValue> Parser::parse_dynamic_value(StyleComponentValueRule const& component_value)

View file

@ -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 Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
{ {
auto result = m_expression->resolve(&layout_node, {}); auto result = m_expression->resolve(&layout_node, {});

View file

@ -707,12 +707,14 @@ public:
struct CalcNumberValue { struct CalcNumberValue {
Variant<Number, NonnullOwnPtr<CalcNumberSum>> value; Variant<Number, NonnullOwnPtr<CalcNumberSum>> value;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
struct CalcValue { struct CalcValue {
Variant<Number, Length, Percentage, NonnullOwnPtr<CalcSum>> value; Variant<Number, Length, Percentage, NonnullOwnPtr<CalcSum>> value;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -726,6 +728,7 @@ public:
NonnullOwnPtr<CalcProduct> first_calc_product; NonnullOwnPtr<CalcProduct> first_calc_product;
NonnullOwnPtrVector<CalcSumPartWithOperator> zero_or_more_additional_calc_products; NonnullOwnPtrVector<CalcSumPartWithOperator> zero_or_more_additional_calc_products;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -738,6 +741,7 @@ public:
NonnullOwnPtr<CalcNumberProduct> first_calc_number_product; NonnullOwnPtr<CalcNumberProduct> first_calc_number_product;
NonnullOwnPtrVector<CalcNumberSumPartWithOperator> zero_or_more_additional_calc_number_products; NonnullOwnPtrVector<CalcNumberSumPartWithOperator> zero_or_more_additional_calc_number_products;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -746,6 +750,7 @@ public:
CalcValue first_calc_value; CalcValue first_calc_value;
NonnullOwnPtrVector<CalcProductPartWithOperator> zero_or_more_additional_calc_values; NonnullOwnPtrVector<CalcProductPartWithOperator> zero_or_more_additional_calc_values;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -758,6 +763,7 @@ public:
SumOperation op; SumOperation op;
NonnullOwnPtr<CalcProduct> value; NonnullOwnPtr<CalcProduct> value;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -766,6 +772,7 @@ public:
ProductOperation op; ProductOperation op;
Variant<CalcValue, CalcNumberValue> value; Variant<CalcValue, CalcNumberValue> value;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -774,6 +781,7 @@ public:
CalcNumberValue first_calc_number_value; CalcNumberValue first_calc_number_value;
NonnullOwnPtrVector<CalcNumberProductPartWithOperator> zero_or_more_additional_calc_number_values; NonnullOwnPtrVector<CalcNumberProductPartWithOperator> zero_or_more_additional_calc_number_values;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -782,6 +790,7 @@ public:
ProductOperation op; ProductOperation op;
CalcNumberValue value; CalcNumberValue value;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const; CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) const;
}; };
@ -794,16 +803,17 @@ public:
SumOperation op; SumOperation op;
NonnullOwnPtr<CalcNumberProduct> value; NonnullOwnPtr<CalcNumberProduct> value;
String to_string() const;
Optional<ResolvedType> resolved_type() const; Optional<ResolvedType> resolved_type() const;
CalculationResult resolve(Layout::Node const*, Length const& percentage_basis) 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; } ResolvedType resolved_type() const { return m_resolved_type; }
NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; } NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; }
Optional<Length> resolve_length(Layout::Node const& layout_node) const; Optional<Length> resolve_length(Layout::Node const& layout_node) const;
@ -813,16 +823,14 @@ public:
Optional<i64> resolve_integer(); Optional<i64> resolve_integer();
private: 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) : StyleValue(Type::Calculated)
, m_resolved_type(resolved_type) , m_resolved_type(resolved_type)
, m_expression_string(expression_string)
, m_expression(move(calc_sum)) , m_expression(move(calc_sum))
{ {
} }
ResolvedType m_resolved_type; ResolvedType m_resolved_type;
String m_expression_string;
NonnullOwnPtr<CalcSum> m_expression; NonnullOwnPtr<CalcSum> m_expression;
}; };