ladybird/Libraries/LibWeb/CSS/Parser/ArbitrarySubstitutionFunctions.h
Sam Atkins b6032b0fcd LibWeb/CSS: Reimplement var()/attr() as arbitrary substitution functions
"Arbitrary substitution functions" are a family of functions that
includes var() and attr(). All of them resolve to an arbitrary set of
component values that are not known at parse-time, so they have to be
substituted at computed-value time.

Besides it being nice to follow the spec closely, this means we'll be
able to implement the others (such as `if()` and `inherit()`) more
easily.

The main omission here is the new "spread syntax", which can be
implemented in the future.
2025-07-09 16:44:20 +01:00

54 lines
1.6 KiB
C++

/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/Parser/Parser.h>
namespace Web::CSS::Parser {
// https://drafts.csswg.org/css-values-5/#substitution-context
struct SubstitutionContext {
enum class DependencyType : u8 {
Property,
Attribute,
Function,
};
DependencyType dependency_type;
String first;
Optional<String> second {};
bool is_cyclic { false };
bool operator==(SubstitutionContext const&) const;
String to_string() const;
};
class GuardedSubstitutionContexts {
public:
void guard(SubstitutionContext&);
void unguard(SubstitutionContext const&);
private:
Vector<SubstitutionContext&> m_contexts;
};
enum class ArbitrarySubstitutionFunction : u8 {
Attr,
Var,
};
[[nodiscard]] Optional<ArbitrarySubstitutionFunction> to_arbitrary_substitution_function(FlyString const& name);
bool contains_guaranteed_invalid_value(Vector<ComponentValue> const&);
[[nodiscard]] Vector<ComponentValue> substitute_arbitrary_substitution_functions(DOM::AbstractElement&, GuardedSubstitutionContexts&, Vector<ComponentValue> const&, Optional<SubstitutionContext> = {});
using ArbitrarySubstitutionFunctionArguments = Vector<Vector<ComponentValue>>;
[[nodiscard]] Optional<ArbitrarySubstitutionFunctionArguments> parse_according_to_argument_grammar(ArbitrarySubstitutionFunction, Vector<ComponentValue> const&);
[[nodiscard]] Vector<ComponentValue> replace_an_arbitrary_substitution_function(DOM::AbstractElement&, GuardedSubstitutionContexts&, ArbitrarySubstitutionFunction, ArbitrarySubstitutionFunctionArguments const&);
}