1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

LibWeb: Implement CalculationResult type for calc() results

calc() sub-expressions can return a variety of different types, which
then can be combined using the basic arithmetic operators. This class
should make that easier to deal with, instead of having to handle all
the possible combinations at each call site. :^)

We take the Layout::Node as a pointer not a reference, since later we'll
need to call these functions when resolving to `<number>` or `<integer>`
which don't use those, and we don't want to force users to pass them in
unnecessarily.
This commit is contained in:
Sam Atkins 2022-01-27 14:50:31 +00:00 committed by Andreas Kling
parent b69f6097de
commit 35f64af3a4
2 changed files with 135 additions and 9 deletions

View file

@ -664,6 +664,33 @@ public:
Time,
};
enum class SumOperation {
Add,
Subtract,
};
enum class ProductOperation {
Multiply,
Divide,
};
class CalculationResult {
public:
CalculationResult(Variant<float, Length, Percentage> value)
: m_value(move(value))
{
}
void add(CalculationResult const& other, Layout::Node const*, Length const& percentage_basis);
void subtract(CalculationResult const& other, Layout::Node const*, Length const& percentage_basis);
void multiply_by(CalculationResult const& other, Layout::Node const*);
void divide_by(CalculationResult const& other, Layout::Node const*);
Variant<float, Length, Percentage> const& value() const { return m_value; }
private:
void add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const*, Length const& percentage_basis);
Variant<float, Length, Percentage> m_value;
};
struct CalcSum;
struct CalcSumPartWithOperator;
struct CalcProduct;
@ -683,15 +710,6 @@ public:
Optional<ResolvedType> resolved_type() const;
};
enum class SumOperation {
Add,
Subtract,
};
enum class ProductOperation {
Multiply,
Divide,
};
// This represents that: https://www.w3.org/TR/css-values-3/#calc-syntax
struct CalcSum {
CalcSum(NonnullOwnPtr<CalcProduct> first_calc_product, NonnullOwnPtrVector<CalcSumPartWithOperator> additional)