LibWeb/CSS: Stub out a function for serializing ComponentValue sequences

This is very hacky and wrong, but it means there's one place to fix,
instead of one for UnresolvedStyleValue, and one for invalid
MediaFeatureValues which I'm about to implement.
This commit is contained in:
Sam Atkins 2025-05-21 16:56:49 +01:00
parent 987d510dbb
commit fe7bac73f0
Notes: github-actions[bot] 2025-05-23 09:19:41 +00:00
3 changed files with 22 additions and 4 deletions

View file

@ -1,11 +1,12 @@
/* /*
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
#include <LibWeb/CSS/Parser/ComponentValue.h>
#include <LibWeb/CSS/Serialize.h> #include <LibWeb/CSS/Serialize.h>
namespace Web::CSS { namespace Web::CSS {
@ -241,4 +242,13 @@ String serialize_a_css_declaration(StringView property, StringView value, Import
return MUST(builder.to_string()); return MUST(builder.to_string());
} }
// https://drafts.csswg.org/css-syntax/#serialization
String serialize_a_series_of_component_values(ReadonlySpan<Parser::ComponentValue> component_values, InsertWhitespace insert_whitespace)
{
// FIXME: There are special rules here where we should insert a comment between certain tokens. Do that!
if (insert_whitespace == InsertWhitespace::Yes)
return MUST(String::join(' ', component_values));
return MUST(String::join(""sv, component_values));
}
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -46,4 +46,11 @@ void serialize_a_comma_separated_list(StringBuilder& builder, Vector<T> const& i
String serialize_a_css_declaration(StringView property, StringView value, Important = Important::No); String serialize_a_css_declaration(StringView property, StringView value, Important = Important::No);
enum class InsertWhitespace : u8 {
No,
Yes,
};
// FIXME: Remove InsertWhitespace param once style value parsing stops discarding whitespace tokens.
String serialize_a_series_of_component_values(ReadonlySpan<Parser::ComponentValue>, InsertWhitespace = InsertWhitespace::No);
} }

View file

@ -7,8 +7,9 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "UnresolvedStyleValue.h"
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibWeb/CSS/Serialize.h>
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
namespace Web::CSS { namespace Web::CSS {
@ -17,7 +18,7 @@ String UnresolvedStyleValue::to_string(SerializationMode) const
if (m_original_source_text.has_value()) if (m_original_source_text.has_value())
return *m_original_source_text; return *m_original_source_text;
return MUST(String::join(' ', m_values)); return serialize_a_series_of_component_values(m_values, InsertWhitespace::Yes);
} }
bool UnresolvedStyleValue::equals(CSSStyleValue const& other) const bool UnresolvedStyleValue::equals(CSSStyleValue const& other) const