From 26fc01eecb18463d003f6d181c1c8852d2c08ccb Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Wed, 29 Jun 2022 23:32:05 +0100 Subject: [PATCH] LibWeb/CSS: Fix incorrect `calc( - )` computations When swapping both values to perform the actual calculation, we need to consider that `A + B == B + A`, but `A - B != B - A`, so turn it into `-B + A`. Co-Authored-By: Sam Atkins --- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index eecd4aa489..bdd3d163f2 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -478,10 +478,13 @@ void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperat // Other side isn't a percentage, so the easiest way to handle it without duplicating all the logic, is just to swap `this` and `other`. CalculationResult new_value = other; - if (op == SumOperation::Add) + if (op == SumOperation::Add) { new_value.add(*this, layout_node, percentage_basis); - else - new_value.subtract(*this, layout_node, percentage_basis); + } else { + // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A' + new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node); + new_value.add(*this, layout_node, percentage_basis); + } *this = new_value; });