diff --git a/Libraries/LibWeb/CSS/Serialize.cpp b/Libraries/LibWeb/CSS/Serialize.cpp index 165730de2e1..fddc2dcc68b 100644 --- a/Libraries/LibWeb/CSS/Serialize.cpp +++ b/Libraries/LibWeb/CSS/Serialize.cpp @@ -1,11 +1,12 @@ /* - * Copyright (c) 2021-2023, Sam Atkins + * Copyright (c) 2021-2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #include #include +#include #include namespace Web::CSS { @@ -241,4 +242,13 @@ String serialize_a_css_declaration(StringView property, StringView value, Import return MUST(builder.to_string()); } +// https://drafts.csswg.org/css-syntax/#serialization +String serialize_a_series_of_component_values(ReadonlySpan 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)); +} + } diff --git a/Libraries/LibWeb/CSS/Serialize.h b/Libraries/LibWeb/CSS/Serialize.h index 03c206ef893..ec8050686a8 100644 --- a/Libraries/LibWeb/CSS/Serialize.h +++ b/Libraries/LibWeb/CSS/Serialize.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023, Sam Atkins + * Copyright (c) 2021-2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -46,4 +46,11 @@ void serialize_a_comma_separated_list(StringBuilder& builder, Vector const& i 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, InsertWhitespace = InsertWhitespace::No); + } diff --git a/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp index aee35f548e2..062cc6e3d61 100644 --- a/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp @@ -7,8 +7,9 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "UnresolvedStyleValue.h" #include +#include +#include namespace Web::CSS { @@ -17,7 +18,7 @@ String UnresolvedStyleValue::to_string(SerializationMode) const if (m_original_source_text.has_value()) 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