diff options
author | stelar7 <dudedbz@gmail.com> | 2023-05-26 15:00:22 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-06-02 05:22:12 +0200 |
commit | eaee0ecd88704cbfd747ec460ef433cfd6a4658b (patch) | |
tree | e1b19cc02a3d81c5e974510a7561f1c72fa0a24f | |
parent | 6a10821bfdf3d5471e03900e9b685078c9757efa (diff) | |
download | serenity-eaee0ecd88704cbfd747ec460ef433cfd6a4658b.zip |
LibWeb: Parse `max()` css math function
4 files changed, 139 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index b4de4a4a32..8d4f89bcc5 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -3473,6 +3473,45 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_min_function(Function const& func return TRY(MinCalculationNode::create(move(calculated_parameters))); } +ErrorOr<OwnPtr<CalculationNode>> Parser::parse_max_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, "max() must have at least 1 parameter"sv); + return nullptr; + } + + Vector<NonnullOwnPtr<CalculationNode>> 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, "max() 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, "max() parameters must all be of the same type"sv); + return nullptr; + } + + calculated_parameters.append(calculation_node.release_nonnull()); + } + + return TRY(MaxCalculationNode::create(move(calculated_parameters))); +} + ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value) { if (component_value.is_function()) { @@ -3502,6 +3541,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con if (function.name().equals_ignoring_ascii_case("min"sv)) return TRY(parse_min_function(function)); + if (function.name().equals_ignoring_ascii_case("max"sv)) + return TRY(parse_max_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 6ae4906356..5d5e15f0c7 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -291,6 +291,7 @@ private: ErrorOr<RefPtr<CalculatedStyleValue>> parse_calculated_value(Vector<ComponentValue> const&); ErrorOr<OwnPtr<CalculationNode>> parse_a_calc_function_node(Function const&); ErrorOr<OwnPtr<CalculationNode>> parse_min_function(Function const&); + ErrorOr<OwnPtr<CalculationNode>> parse_max_function(Function const&); ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&); ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&); ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp index bcabffd06a..a870b2af1b 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp @@ -479,6 +479,84 @@ ErrorOr<void> MinCalculationNode::dump(StringBuilder& builder, int indent) const return {}; } +ErrorOr<NonnullOwnPtr<MaxCalculationNode>> MaxCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values) +{ + return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values))); +} + +MaxCalculationNode::MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values) + : CalculationNode(Type::Max) + , m_values(move(values)) +{ +} + +MaxCalculationNode::~MaxCalculationNode() = default; + +ErrorOr<String> MaxCalculationNode::to_string() const +{ + StringBuilder builder; + TRY(builder.try_append("max("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<CalculatedStyleValue::ResolvedType> MaxCalculationNode::resolved_type() const +{ + // NOTE: We check during parsing that all values have the same type. + return m_values[0]->resolved_type(); +} + +bool MaxCalculationNode::contains_percentage() const +{ + for (auto const& value : m_values) { + if (value->contains_percentage()) + return true; + } + + return false; +} + +CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Layout::Node const* layout_node, CalculatedStyleValue::PercentageBasis const& percentage_basis) const +{ + CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(layout_node, percentage_basis); + auto largest_value = resolve_value(largest_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 > largest_value) { + largest_value = child_value; + largest_node = child_resolved; + } + } + + return largest_node; +} + +ErrorOr<void> MaxCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback) +{ + for (auto& value : m_values) { + TRY(value->for_each_child_node(callback)); + TRY(callback(value)); + } + + return {}; +} + +ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const +{ + TRY(builder.try_appendff("{: >{}}MAX:\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 3c30dd8921..e02c424d95 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h @@ -274,4 +274,22 @@ private: Vector<NonnullOwnPtr<CalculationNode>> m_values; }; +class MaxCalculationNode final : public CalculationNode { +public: + static ErrorOr<NonnullOwnPtr<MaxCalculationNode>> create(Vector<NonnullOwnPtr<CalculationNode>>); + ~MaxCalculationNode(); + + virtual ErrorOr<String> to_string() const override; + virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override; + virtual bool contains_percentage() const override; + virtual CalculatedStyleValue::CalculationResult resolve(Layout::Node const*, CalculatedStyleValue::PercentageBasis const&) const override; + virtual ErrorOr<void> for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const&) override; + + virtual ErrorOr<void> dump(StringBuilder&, int indent) const override; + +private: + explicit MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>>); + Vector<NonnullOwnPtr<CalculationNode>> m_values; +}; + } |