From 6a10821bfdf3d5471e03900e9b685078c9757efa Mon Sep 17 00:00:00 2001 From: stelar7 Date: Fri, 26 May 2023 11:21:49 +0200 Subject: [PATCH] LibWeb: Parse `min()` css math function --- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 42 +++++++++ Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 1 + .../CSS/StyleValues/CalculatedStyleValue.cpp | 89 +++++++++++++++++++ .../CSS/StyleValues/CalculatedStyleValue.h | 25 +++++- 4 files changed, 155 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 80ee476c55..b4de4a4a32 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -3434,6 +3434,45 @@ ErrorOr> Parser::parse_calculated_value(Vector> Parser::parse_min_function(Function const& function) +{ + TokenStream stream { function.values() }; + auto parameters = parse_a_comma_separated_list_of_component_values(stream); + + if (parameters.size() == 0) { + dbgln_if(CSS_PARSER_DEBUG, "min() must have at least 1 parameter"sv); + return nullptr; + } + + Vector> calculated_parameters; + calculated_parameters.ensure_capacity(parameters.size()); + + CalculatedStyleValue::ResolvedType type; + bool first = true; + for (auto& parameter : parameters) { + auto calculation_node = TRY(parse_a_calculation(parameter)); + + if (!calculation_node) { + dbgln_if(CSS_PARSER_DEBUG, "min() parameters must be valid calculations"sv); + return nullptr; + } + + if (first) { + type = calculation_node->resolved_type().value(); + first = false; + } + + if (calculation_node->resolved_type().value() != type) { + dbgln_if(CSS_PARSER_DEBUG, "min() parameters must all be of the same type"sv); + return nullptr; + } + + calculated_parameters.append(calculation_node.release_nonnull()); + } + + return TRY(MinCalculationNode::create(move(calculated_parameters))); +} + ErrorOr> Parser::parse_dynamic_value(ComponentValue const& component_value) { if (component_value.is_function()) { @@ -3460,6 +3499,9 @@ ErrorOr> Parser::parse_a_calc_function_node(Function con if (function.name().equals_ignoring_ascii_case("calc"sv)) return TRY(parse_a_calculation(function.values())); + if (function.name().equals_ignoring_ascii_case("min"sv)) + return TRY(parse_min_function(function)); + dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name()); return nullptr; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 48991b36bf..6ae4906356 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -290,6 +290,7 @@ private: ErrorOr> parse_dynamic_value(ComponentValue const&); ErrorOr> parse_calculated_value(Vector const&); ErrorOr> parse_a_calc_function_node(Function const&); + ErrorOr> parse_min_function(Function const&); ErrorOr> parse_dimension_value(ComponentValue const&); ErrorOr> parse_integer_value(TokenStream&); ErrorOr> parse_number_value(TokenStream&); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp index 6cda1a98db..bcabffd06a 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp @@ -24,6 +24,17 @@ static bool is_dimension(CalculatedStyleValue::ResolvedType type) && type != CalculatedStyleValue::ResolvedType::Percentage; } +static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Layout::Node const* layout_node) +{ + return value.visit( + [](Number const& number) { return number.value(); }, + [](Angle const& angle) { return angle.to_degrees(); }, + [](Frequency const& frequency) { return frequency.to_hertz(); }, + [layout_node](Length const& length) { return length.to_px(*layout_node).value(); }, + [](Percentage const& percentage) { return percentage.value(); }, + [](Time const& time) { return time.to_seconds(); }); +}; + CalculationNode::CalculationNode(Type type) : m_type(type) { @@ -390,6 +401,84 @@ ErrorOr InvertCalculationNode::dump(StringBuilder& builder, int indent) co return {}; } +ErrorOr> MinCalculationNode::create(Vector> values) +{ + return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values))); +} + +MinCalculationNode::MinCalculationNode(Vector> values) + : CalculationNode(Type::Min) + , m_values(move(values)) +{ +} + +MinCalculationNode::~MinCalculationNode() = default; + +ErrorOr MinCalculationNode::to_string() const +{ + StringBuilder builder; + TRY(builder.try_append("min("sv)); + for (size_t i = 0; i < m_values.size(); ++i) { + if (i != 0) + TRY(builder.try_append(", "sv)); + TRY(builder.try_append(TRY(m_values[i]->to_string()))); + } + TRY(builder.try_append(")"sv)); + return builder.to_string(); +} + +Optional MinCalculationNode::resolved_type() const +{ + // NOTE: We check during parsing that all values have the same type. + return m_values[0]->resolved_type(); +} + +bool MinCalculationNode::contains_percentage() const +{ + for (auto const& value : m_values) { + if (value->contains_percentage()) + return true; + } + + return false; +} + +CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Layout::Node const* layout_node, CalculatedStyleValue::PercentageBasis const& percentage_basis) const +{ + CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(layout_node, percentage_basis); + auto smallest_value = resolve_value(smallest_node.value(), layout_node); + + for (size_t i = 1; i < m_values.size(); i++) { + auto child_resolved = m_values[i]->resolve(layout_node, percentage_basis); + auto child_value = resolve_value(child_resolved.value(), layout_node); + + if (child_value < smallest_value) { + smallest_value = child_value; + smallest_node = child_resolved; + } + } + + return smallest_node; +} + +ErrorOr MinCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) +{ + for (auto& value : m_values) { + TRY(value->for_each_child_node(callback)); + TRY(callback(value)); + } + + return {}; +} + +ErrorOr MinCalculationNode::dump(StringBuilder& builder, int indent) const +{ + TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent)); + for (auto const& value : m_values) + TRY(value->dump(builder, indent + 2)); + return {}; +} + void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis) { add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h index c339e26344..3c30dd8921 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h @@ -119,8 +119,11 @@ public: // NOTE: Currently, any value with a `var()` or `attr()` function in it is always an // UnresolvedStyleValue so we do not have to implement a NonMathFunction type here. - // Operator nodes - // https://www.w3.org/TR/css-values-4/#calculation-tree-operator-nodes + // Comparison function nodes, a sub-type of operator node + // https://drafts.csswg.org/css-values-4/#comp-func + Min, + Max, + Clamp, // Calc-operator nodes, a sub-type of operator node // https://www.w3.org/TR/css-values-4/#calculation-tree-calc-operator-nodes @@ -253,4 +256,22 @@ private: NonnullOwnPtr m_value; }; +class MinCalculationNode final : public CalculationNode { +public: + static ErrorOr> create(Vector>); + ~MinCalculationNode(); + + virtual ErrorOr to_string() const override; + virtual Optional resolved_type() const override; + virtual bool contains_percentage() const override; + virtual CalculatedStyleValue::CalculationResult resolve(Layout::Node const*, CalculatedStyleValue::PercentageBasis const&) const override; + virtual ErrorOr for_each_child_node(Function(NonnullOwnPtr&)> const&) override; + + virtual ErrorOr dump(StringBuilder&, int indent) const override; + +private: + explicit MinCalculationNode(Vector>); + Vector> m_values; +}; + }