LibWeb/CSS: Parse calc() as intermediate types instead of UnparsedCN

Previously we created a tree of CalculationNodes with dummy
UnparsedCalculationNode children, and then swapped those with the real
children. This matched the spec closely but had the unfortunate
downside that CalculationNodes couldn't be immutable, and couldn't know
their properties at construct-time. UnparsedCalculationNode is also a
footgun, as if it gets left in the tree accidentally we would VERIFY().

So instead, let's parse the calc() tree into an intermediate format, and
then convert each node in that tree, depth-first, into its
corresponding CalculationNode. This means each CalculationNode knows
what its children are when it is constructed, and they never change.

Apart from deleting UnparsedCalculationNode, we can also get rid of the
for_each_child_node() method that was only used by this "replace the
children" code.
This commit is contained in:
Sam Atkins 2024-12-17 14:00:41 +00:00
commit 6969d1eba3
Notes: github-actions[bot] 2024-12-18 12:22:26 +00:00
4 changed files with 166 additions and 336 deletions

View file

@ -8,6 +8,7 @@
#pragma once
#include <AK/Error.h>
#include <AK/NonnullRawPtr.h>
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <LibGfx/Font/UnicodeRange.h>
@ -34,6 +35,29 @@ namespace Web::CSS::Parser {
class PropertyDependencyNode;
namespace CalcParsing {
struct Operator {
char delim;
};
struct ProductNode;
struct SumNode;
struct InvertNode;
struct NegateNode;
using Node = Variant<Operator, Number, Dimension, CalculationNode::ConstantType, NonnullOwnPtr<ProductNode>, NonnullOwnPtr<SumNode>, NonnullOwnPtr<InvertNode>, NonnullOwnPtr<NegateNode>, NonnullRawPtr<ComponentValue const>>;
struct ProductNode {
Vector<Node> children;
};
struct SumNode {
Vector<Node> children;
};
struct InvertNode {
Node child;
};
struct NegateNode {
Node child;
};
}
class Parser {
public:
static Parser create(ParsingContext const&, StringView input, StringView encoding = "utf-8"sv);
@ -371,6 +395,7 @@ private:
RefPtr<CSSStyleValue> parse_grid_area_shorthand_value(TokenStream<ComponentValue>&);
RefPtr<CSSStyleValue> parse_grid_shorthand_value(TokenStream<ComponentValue>&);
OwnPtr<CalculationNode> convert_to_calculation_node(CalcParsing::Node const&);
OwnPtr<CalculationNode> parse_a_calculation(Vector<ComponentValue> const&);
ParseErrorOr<NonnullRefPtr<Selector>> parse_complex_selector(TokenStream<ComponentValue>&, SelectorType);