Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp (C++)

Code before:
        return MaxCalculationNode::create(move(simplified_children));
    }

    // 6. If root is a Negate node:
    if (root->type() == CalculationNode::Type::Negate) {
        auto const& root_negate = as<NegateCalculationNode>(*root);
        auto const& child = root_negate.child();
        // 1. If root’s child is a numeric value, return an equivalent numeric value, but with the value negated (0 - value).
        if (child.type() == CalculationNode::Type::Numeric)
            return as<NumericCalculationNode>(child).negated(context);

        // 2. If root’s child is a Negate node, return the child’s child.
        if (child.type() == CalculationNode::Type::Negate)
            return as<NegateCalculationNode>(child).child();

        // AD-HOC: Convert negated sums into sums of negated nodes - see https://github.com/w3c/csswg-drafts/issues/13020
        if (child.type() == CalculationNode::Type::Sum) {
            Vector<NonnullRefPtr<CalculationNode const>> negated_sum_components;

            for (auto const& sum_child : child.children()) {
                if (sum_child->type() == CalculationNode::Type::Numeric)
                    negated_sum_components.append(as<NumericCalculationNode>(*sum_child).negated(context));
                else
                    negated_sum_components.append(NegateCalculationNode::create(sum_child));
            }

            return SumCalculationNode::create(negated_sum_components);
        }

        // 3. Return root.
        // NOTE: Because our root is immutable, we have to return a new node if the child was modified.
        if (&child == &root_negate.child())
            return root;
        return NegateCalculationNode::create(move(child));
    }

    // 7. If root is an Invert node:
    if (root->type() == CalculationNode::Type::Invert) {
        auto const& root_invert = as<InvertCalculationNode>(*root);
        auto const& child = root_invert.child();

        // 1. If root’s child is a number (not a percentage or dimension) return the reciprocal of the child’s value.
        if (child.type() == CalculationNode::Type::Numeric) {
            if (auto const* number = as<NumericCalculationNode>(child).value().get_pointer<Number>()) {
                // TODO: Ensure we're doing the right thing for weird divisions.
                return NumericCalculationNode::create(Number(Number::Type::Number, 1.0 / number->value()), context);
            }
        }

        // 2. If root’s child is an Invert node, return the child’s child.
        if (child.type() == CalculationNode::Type::Invert)

Review comment (suggestion):
Hmm... If `sum_child` is already a negate node that couldn't be simplified earlier, we'll wrap a negate with a negate here I think? I'm not sure if that can happen or not.

Code after:
        return MaxCalculationNode::create(move(simplified_children));
    }

    // 6. If root is a Negate node:
    if (root->type() == CalculationNode::Type::Negate) {
        auto const& root_negate = as<NegateCalculationNode>(*root);
        auto const& child = root_negate.child();
        // 1. If root’s child is a numeric value, return an equivalent numeric value, but with the value negated (0 - value).
        if (child.type() == CalculationNode::Type::Numeric)
            return as<NumericCalculationNode>(child).negated(context);

        // 2. If root’s child is a Negate node, return the child’s child.
        if (child.type() == CalculationNode::Type::Negate)
            return as<NegateCalculationNode>(child).child();

        // AD-HOC: Convert negated sums into sums of negated nodes - see https://github.com/w3c/csswg-drafts/issues/13020
        if (child.type() == CalculationNode::Type::Sum) {
            Vector<NonnullRefPtr<CalculationNode const>> negated_sum_components;

            for (auto const& sum_child : child.children()) {
                if (sum_child->type() == CalculationNode::Type::Numeric)
                    negated_sum_components.append(as<NumericCalculationNode>(*sum_child).negated(context));
                else if (sum_child->type() == CalculationNode::Type::Negate)
                    negated_sum_components.append(as<NegateCalculationNode>(*sum_child).child());
                else
                    negated_sum_components.append(NegateCalculationNode::create(sum_child));
            }

            return SumCalculationNode::create(negated_sum_components);
        }

        // 3. Return root.
        // NOTE: Because our root is immutable, we have to return a new node if the child was modified.
        if (&child == &root_negate.child())
            return root;
        return NegateCalculationNode::create(move(child));
    }

    // 7. If root is an Invert node:
    if (root->type() == CalculationNode::Type::Invert) {
        auto const& root_invert = as<InvertCalculationNode>(*root);
        auto const& child = root_invert.child();

        // 1. If root’s child is a number (not a percentage or dimension) return the reciprocal of the child’s value.
        if (child.type() == CalculationNode::Type::Numeric) {
            if (auto const* number = as<NumericCalculationNode>(child).value().get_pointer<Number>()) {
                // TODO: Ensure we're doing the right thing for weird divisions.
                return NumericCalculationNode::create(Number(Number::Type::Number, 1.0 / number->value()), context);
            }
        }