Skip to content

Commit fe7bac7

Browse files
committed
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.
1 parent 987d510 commit fe7bac7

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Libraries/LibWeb/CSS/Serialize.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
2+
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

77
#include <AK/StringBuilder.h>
88
#include <AK/Utf8View.h>
9+
#include <LibWeb/CSS/Parser/ComponentValue.h>
910
#include <LibWeb/CSS/Serialize.h>
1011

1112
namespace Web::CSS {
@@ -241,4 +242,13 @@ String serialize_a_css_declaration(StringView property, StringView value, Import
241242
return MUST(builder.to_string());
242243
}
243244

245+
// https://drafts.csswg.org/css-syntax/#serialization
246+
String serialize_a_series_of_component_values(ReadonlySpan<Parser::ComponentValue> component_values, InsertWhitespace insert_whitespace)
247+
{
248+
// FIXME: There are special rules here where we should insert a comment between certain tokens. Do that!
249+
if (insert_whitespace == InsertWhitespace::Yes)
250+
return MUST(String::join(' ', component_values));
251+
return MUST(String::join(""sv, component_values));
252+
}
253+
244254
}

Libraries/LibWeb/CSS/Serialize.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
2+
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
@@ -46,4 +46,11 @@ void serialize_a_comma_separated_list(StringBuilder& builder, Vector<T> const& i
4646

4747
String serialize_a_css_declaration(StringView property, StringView value, Important = Important::No);
4848

49+
enum class InsertWhitespace : u8 {
50+
No,
51+
Yes,
52+
};
53+
// FIXME: Remove InsertWhitespace param once style value parsing stops discarding whitespace tokens.
54+
String serialize_a_series_of_component_values(ReadonlySpan<Parser::ComponentValue>, InsertWhitespace = InsertWhitespace::No);
55+
4956
}

Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
* SPDX-License-Identifier: BSD-2-Clause
88
*/
99

10-
#include "UnresolvedStyleValue.h"
1110
#include <AK/StringBuilder.h>
11+
#include <LibWeb/CSS/Serialize.h>
12+
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
1213

1314
namespace Web::CSS {
1415

@@ -17,7 +18,7 @@ String UnresolvedStyleValue::to_string(SerializationMode) const
1718
if (m_original_source_text.has_value())
1819
return *m_original_source_text;
1920

20-
return MUST(String::join(' ', m_values));
21+
return serialize_a_series_of_component_values(m_values, InsertWhitespace::Yes);
2122
}
2223

2324
bool UnresolvedStyleValue::equals(CSSStyleValue const& other) const

0 commit comments

Comments
 (0)