diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 24ffdba7ba..ba7e886e43 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -4071,6 +4071,46 @@ ErrorOr> Parser::parse_mod_function(Function const& func return TRY(ModCalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull())); } +ErrorOr> Parser::parse_rem_function(Function const& function) +{ + TokenStream stream { function.values() }; + auto parameters = parse_a_comma_separated_list_of_component_values(stream); + + if (parameters.size() != 2) { + dbgln_if(CSS_PARSER_DEBUG, "rem() must have exactly two parameters"sv); + return nullptr; + } + + auto node_a = TRY(parse_a_calculation(parameters[0])); + auto node_b = TRY(parse_a_calculation(parameters[1])); + + if (!node_a || !node_b) { + dbgln_if(CSS_PARSER_DEBUG, "rem() parameters must be valid calculations"sv); + return nullptr; + } + + auto node_a_maybe_parameter_type = node_a->resolved_type(); + if (!node_a_maybe_parameter_type.has_value()) { + dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for rem() parameter 1"sv); + return nullptr; + } + + auto node_b_maybe_parameter_type = node_b->resolved_type(); + if (!node_b_maybe_parameter_type.has_value()) { + dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for rem() parameter 2"sv); + return nullptr; + } + + auto node_a_resolved_type = node_a_maybe_parameter_type.value(); + auto node_b_resolved_type = node_b_maybe_parameter_type.value(); + if (node_a_resolved_type != node_b_resolved_type) { + dbgln_if(CSS_PARSER_DEBUG, "rem() parameters must all be of the same type"sv); + return nullptr; + } + + return TRY(RemCalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull())); +} + ErrorOr> Parser::parse_dynamic_value(ComponentValue const& component_value) { if (component_value.is_function()) { @@ -4157,6 +4197,9 @@ ErrorOr> Parser::parse_a_calc_function_node(Function con if (function.name().equals_ignoring_ascii_case("mod"sv)) return TRY(parse_mod_function(function)); + if (function.name().equals_ignoring_ascii_case("rem"sv)) + return TRY(parse_rem_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 baa0192b3e..3e99382f0b 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -309,6 +309,7 @@ private: ErrorOr> parse_exp_function(Function const&); ErrorOr> parse_round_function(Function const&); ErrorOr> parse_mod_function(Function const&); + ErrorOr> parse_rem_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 3f0b527e0d..a896230e63 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp @@ -1696,6 +1696,70 @@ ErrorOr ModCalculationNode::dump(StringBuilder& builder, int indent) const return {}; } +ErrorOr> RemCalculationNode::create(NonnullOwnPtr x, NonnullOwnPtr y) +{ + return adopt_nonnull_own_or_enomem(new (nothrow) RemCalculationNode(move(x), move(y))); +} + +RemCalculationNode::RemCalculationNode(NonnullOwnPtr x, NonnullOwnPtr y) + : CalculationNode(Type::Rem) + , m_x(move(x)) + , m_y(move(y)) +{ +} + +RemCalculationNode::~RemCalculationNode() = default; + +ErrorOr RemCalculationNode::to_string() const +{ + StringBuilder builder; + builder.append("rem("sv); + builder.append(TRY(m_x->to_string())); + builder.append(", "sv); + builder.append(TRY(m_y->to_string())); + builder.append(")"sv); + return builder.to_string(); +} + +Optional RemCalculationNode::resolved_type() const +{ + // Note: We check during parsing that all values have the same type + return m_x->resolved_type(); +} + +bool RemCalculationNode::contains_percentage() const +{ + return m_x->contains_percentage() || m_y->contains_percentage(); +} + +CalculatedStyleValue::CalculationResult RemCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const +{ + auto resolved_type = m_x->resolved_type().value(); + auto node_a = m_x->resolve(context, percentage_basis); + auto node_b = m_y->resolve(context, percentage_basis); + + auto node_a_value = resolve_value(node_a.value(), context); + auto node_b_value = resolve_value(node_b.value(), context); + + auto value = fmod(node_a_value, node_b_value); + return to_resolved_type(resolved_type, value); +} + +ErrorOr RemCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) +{ + TRY(m_x->for_each_child_node(callback)); + TRY(m_y->for_each_child_node(callback)); + TRY(callback(m_x)); + TRY(callback(m_y)); + return {}; +} + +ErrorOr RemCalculationNode::dump(StringBuilder& builder, int indent) const +{ + TRY(builder.try_appendff("{: >{}}REM: {}\n", "", indent, TRY(to_string()))); + return {}; +} + void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional context, PercentageBasis const& percentage_basis) { add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h index 899f6de775..3694ba1444 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h @@ -676,4 +676,23 @@ private: NonnullOwnPtr m_y; }; +class RemCalculationNode final : public CalculationNode { +public: + static ErrorOr> create(NonnullOwnPtr, NonnullOwnPtr); + ~RemCalculationNode(); + + virtual ErrorOr to_string() const override; + virtual Optional resolved_type() const override; + virtual bool contains_percentage() const override; + virtual CalculatedStyleValue::CalculationResult resolve(Optional, CalculatedStyleValue::PercentageBasis const&) const override; + virtual ErrorOr for_each_child_node(Function(NonnullOwnPtr&)> const&) override; + + virtual ErrorOr dump(StringBuilder&, int indent) const override; + +private: + RemCalculationNode(NonnullOwnPtr, NonnullOwnPtr); + NonnullOwnPtr m_x; + NonnullOwnPtr m_y; +}; + }