LibWeb/CSS: Support converting CSSUnparsedValue to a StyleValue

Unfortunately this doesn't pass a lot of tests, because we strip out
whitespace when parsing property values. In particular, the WPT suite
tests with this:

```js
new CSSUnparsedValue([' ', new CSSVariableReferenceValue('--A')])
```

...which gets the whitespace stripped from the string, meaning when we
convert the value back to JS, we get the equivalent of this:

```js
new CSSUnparsedValue(['', new CSSVariableReferenceValue('--A')])
```

...and that's not the same so the test fails.
This commit is contained in:
Sam Atkins 2025-10-03 13:53:10 +01:00 committed by Andreas Kling
commit 0015ee310c
Notes: github-actions[bot] 2025-10-09 14:16:52 +00:00
4 changed files with 32 additions and 6 deletions

View file

@ -8,6 +8,8 @@
#include <LibWeb/Bindings/CSSUnparsedValuePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/CSSVariableReferenceValue.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::CSS {
@ -165,4 +167,27 @@ WebIDL::ExceptionOr<String> CSSUnparsedValue::to_string() const
return s.to_string_without_validation();
}
// https://drafts.css-houdini.org/css-typed-om-1/#create-an-internal-representation
WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> CSSUnparsedValue::create_an_internal_representation(PropertyNameAndID const&) const
{
// If value is a CSSStyleValue subclass,
// If value does not match the grammar of a list-valued property iteration of property, throw a TypeError.
//
// If any component of propertys CSS grammar has a limited numeric range, and the corresponding part of value
// is a CSSUnitValue that is outside of that range, replace that value with the result of wrapping it in a
// fresh CSSMathSum whose values internal slot contains only that part of value.
//
// Return the value.
// https://drafts.css-houdini.org/css-typed-om-1/#cssstylevalue-match-a-grammar
// A CSSUnparsedValue matches any grammar.
// NB: CSSUnparsedValue stores a list of strings, each of which may contain any number of tokens. So the simplest
// way to convert it to ComponentValues is to serialize and then parse it.
auto string = TRY(to_string());
auto parser = Parser::Parser::create(Parser::ParsingParams {}, string);
auto component_values = parser.parse_as_list_of_component_values();
return UnresolvedStyleValue::create(move(component_values));
}
}