LibWeb/CSS: Include guaranteed-invalid value in ComponentValue

Treating these like any other ComponentValue means not having to convert
between different types of Vector, and that we will be able to use
TokenStream to parse the "argument grammars" of arbitrary substitution
functions.
This commit is contained in:
Sam Atkins 2025-06-18 17:24:19 +01:00 committed by Tim Ledbetter
commit 9079be850b
Notes: github-actions[bot] 2025-07-09 15:46:20 +00:00
4 changed files with 44 additions and 4 deletions

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,7 +10,7 @@
namespace Web::CSS::Parser {
ComponentValue::ComponentValue(Token token)
: m_value(token)
: m_value(move(token))
{
}
ComponentValue::ComponentValue(Function&& function)
@ -21,6 +21,10 @@ ComponentValue::ComponentValue(SimpleBlock&& block)
: m_value(move(block))
{
}
ComponentValue::ComponentValue(GuaranteedInvalidValue&& invalid)
: m_value(move(invalid))
{
}
ComponentValue::~ComponentValue() = default;
@ -50,6 +54,9 @@ String ComponentValue::to_debug_string() const
},
[](Function const& function) {
return MUST(String::formatted("Function: {}", function.to_string()));
},
[](GuaranteedInvalidValue const&) {
return "Guaranteed-invalid value"_string;
});
}
@ -58,4 +65,25 @@ String ComponentValue::original_source_text() const
return m_value.visit([](auto const& it) { return it.original_source_text(); });
}
bool ComponentValue::contains_guaranteed_invalid_value() const
{
return m_value.visit(
[](Token const&) {
return false;
},
[](SimpleBlock const& block) {
return block.value
.first_matching([](auto const& it) { return it.contains_guaranteed_invalid_value(); })
.has_value();
},
[](Function const& function) {
return function.value
.first_matching([](auto const& it) { return it.contains_guaranteed_invalid_value(); })
.has_value();
},
[](GuaranteedInvalidValue const&) {
return true;
});
}
}

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -24,6 +24,7 @@ public:
ComponentValue(Token);
explicit ComponentValue(Function&&);
explicit ComponentValue(SimpleBlock&&);
explicit ComponentValue(GuaranteedInvalidValue&&);
~ComponentValue();
bool is_block() const { return m_value.has<SimpleBlock>(); }
@ -40,12 +41,15 @@ public:
Token const& token() const { return m_value.get<Token>(); }
operator Token() const { return m_value.get<Token>(); }
bool is_guaranteed_invalid() const { return m_value.has<GuaranteedInvalidValue>(); }
bool contains_guaranteed_invalid_value() const;
String to_string() const;
String to_debug_string() const;
String original_source_text() const;
private:
Variant<Token, Function, SimpleBlock> m_value;
Variant<Token, Function, SimpleBlock, GuaranteedInvalidValue> m_value;
};
}

View file

@ -88,4 +88,11 @@ struct Function {
bool contains_arbitrary_substitution_function() const;
};
// https://drafts.csswg.org/css-variables/#guaranteed-invalid-value
struct GuaranteedInvalidValue {
GuaranteedInvalidValue() = default;
String to_string() const { return {}; }
String original_source_text() const { return {}; }
};
}

View file

@ -342,6 +342,7 @@ class Tokenizer;
struct AtRule;
struct Declaration;
struct Function;
struct GuaranteedInvalidValue;
struct QualifiedRule;
struct SimpleBlock;