LibWeb: Set up initial infrastructure for non-math functions in calc

There are some non-math functions (such as tree counting functions)
which we should allow within `calc()`s . This commit implements the
initial infrastructure for this.

We don't yet parse any of these non-math functions in
`parse_a_calculation` so there is no functional change.
This commit is contained in:
Callum Law
2025-09-30 16:39:23 +13:00
committed by Tim Ledbetter
parent 831e471444
commit 55bcdcf824
Notes: github-actions[bot] 2025-10-20 15:13:37 +00:00
5 changed files with 89 additions and 9 deletions

View File

@@ -19,13 +19,12 @@
#include <LibWeb/CSS/NumericType.h>
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/Resolution.h>
#include <LibWeb/CSS/StyleValues/AbstractNonMathCalcFunctionStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValue.h>
#include <LibWeb/CSS/Time.h>
namespace Web::CSS {
class CalculationNode;
// https://drafts.csswg.org/css-values-4/#calc-context
// Contains the context available at parse-time.
struct CalculationContext {
@@ -146,7 +145,7 @@ public:
enum class Type {
Numeric,
// NOTE: Currently, any value with a `var()` or `attr()` function in it is always an
// UnresolvedStyleValue so we do not have to implement a NonMathFunction type here.
// UnresolvedStyleValue so we do not have to implement them as CalculationNodes.
// Comparison function nodes, a sub-type of operator node
// https://drafts.csswg.org/css-values-4/#comp-func
@@ -189,6 +188,9 @@ public:
Round,
Mod,
Rem,
// Non-math functions
NonMathFunction
};
using NumericValue = CalculatedStyleValue::CalculationResult::Value;
@@ -764,6 +766,25 @@ private:
NonnullRefPtr<CalculationNode const> m_y;
};
class NonMathFunctionCalculationNode final : public CalculationNode {
public:
static NonnullRefPtr<NonMathFunctionCalculationNode const> create(AbstractNonMathCalcFunctionStyleValue const&, NumericType);
~NonMathFunctionCalculationNode();
virtual bool contains_percentage() const override { return false; }
virtual NonnullRefPtr<CalculationNode const> with_simplified_children(CalculationContext const&, CalculationResolutionContext const&) const override { return *this; }
virtual Vector<NonnullRefPtr<CalculationNode const>> children() const override { return {}; }
virtual void dump(StringBuilder&, int indent) const override;
virtual bool equals(CalculationNode const&) const override;
ValueComparingNonnullRefPtr<AbstractNonMathCalcFunctionStyleValue const> function() const { return m_function; }
private:
NonMathFunctionCalculationNode(AbstractNonMathCalcFunctionStyleValue const& function, NumericType);
ValueComparingNonnullRefPtr<AbstractNonMathCalcFunctionStyleValue const> m_function;
};
// https://drafts.csswg.org/css-values-4/#calc-simplification
NonnullRefPtr<CalculationNode const> simplify_a_calculation_tree(CalculationNode const& root, CalculationContext const& context, CalculationResolutionContext const& resolution_context);