Skip to content

Commit db910c6

Browse files
committed
LibWeb/CSS: Add option to retain a Declaration's full source text
This will be needed by Supports, which expects declarations to serialize verbatim.
1 parent a8d0be4 commit db910c6

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

Libraries/LibWeb/CSS/Parser/Parser.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ void Parser::consume_a_function_and_do_nothing(TokenStream<Token>& input)
10421042

10431043
// https://drafts.csswg.org/css-syntax/#consume-declaration
10441044
template<typename T>
1045-
Optional<Declaration> Parser::consume_a_declaration(TokenStream<T>& input, Nested nested)
1045+
Optional<Declaration> Parser::consume_a_declaration(TokenStream<T>& input, Nested nested, SaveOriginalText save_full_text)
10461046
{
10471047
// To consume a declaration from a token stream input, given an optional bool nested (default false):
10481048

@@ -1054,6 +1054,7 @@ Optional<Declaration> Parser::consume_a_declaration(TokenStream<T>& input, Neste
10541054
.name {},
10551055
.value {},
10561056
};
1057+
auto start_token_index = input.current_index();
10571058

10581059
// 1. If the next token is an <ident-token>, consume a token from input and set decl’s name to the token’s value.
10591060
if (input.next_token().is(Token::Type::Ident)) {
@@ -1177,8 +1178,17 @@ Optional<Declaration> Parser::consume_a_declaration(TokenStream<T>& input, Neste
11771178
}
11781179

11791180
// 9. If decl is valid in the current context, return it; otherwise return nothing.
1180-
if (is_valid_in_the_current_context(declaration))
1181+
if (is_valid_in_the_current_context(declaration)) {
1182+
// AD-HOC: Assemble source tokens.
1183+
if (save_full_text == SaveOriginalText::Yes) {
1184+
StringBuilder original_full_text;
1185+
for (auto& token : input.tokens_since(start_token_index))
1186+
original_full_text.append(token.to_string());
1187+
1188+
declaration.original_full_text = original_full_text.to_string_without_validation();
1189+
}
11811190
return declaration;
1191+
}
11821192
return {};
11831193
}
11841194

@@ -1929,8 +1939,8 @@ template Vector<RuleOrListOfDeclarations> Parser::consume_a_blocks_contents(Toke
19291939
template Vector<ComponentValue> Parser::consume_a_list_of_component_values(TokenStream<ComponentValue>&, Optional<Token::Type>, Nested);
19301940
template Vector<ComponentValue> Parser::consume_a_list_of_component_values(TokenStream<Token>&, Optional<Token::Type>, Nested);
19311941

1932-
template Optional<Declaration> Parser::consume_a_declaration(TokenStream<Token>&, Nested);
1933-
template Optional<Declaration> Parser::consume_a_declaration(TokenStream<ComponentValue>&, Nested);
1942+
template Optional<Declaration> Parser::consume_a_declaration(TokenStream<Token>&, Nested, SaveOriginalText);
1943+
template Optional<Declaration> Parser::consume_a_declaration(TokenStream<ComponentValue>&, Nested, SaveOriginalText);
19341944

19351945
template void Parser::consume_the_remnants_of_a_bad_declaration(TokenStream<Token>&, Nested);
19361946
template void Parser::consume_the_remnants_of_a_bad_declaration(TokenStream<ComponentValue>&, Nested);

Libraries/LibWeb/CSS/Parser/Parser.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,12 @@ class Parser {
250250
Vector<RuleOrListOfDeclarations> consume_a_block(TokenStream<T>&);
251251
template<typename T>
252252
Vector<RuleOrListOfDeclarations> consume_a_blocks_contents(TokenStream<T>&);
253+
enum class SaveOriginalText : u8 {
254+
No,
255+
Yes,
256+
};
253257
template<typename T>
254-
Optional<Declaration> consume_a_declaration(TokenStream<T>&, Nested = Nested::No);
258+
Optional<Declaration> consume_a_declaration(TokenStream<T>&, Nested = Nested::No, SaveOriginalText = SaveOriginalText::No);
255259
template<typename T>
256260
void consume_the_remnants_of_a_bad_declaration(TokenStream<T>&, Nested);
257261
template<typename T>

Libraries/LibWeb/CSS/Parser/Types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
2+
* Copyright (c) 2024-2025, Sam Atkins <sam@ladybird.org>
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
@@ -8,6 +8,7 @@
88

99
#include <AK/FlyString.h>
1010
#include <AK/Function.h>
11+
#include <AK/OwnPtr.h>
1112
#include <AK/Variant.h>
1213
#include <AK/Vector.h>
1314
#include <LibWeb/CSS/Parser/Token.h>
@@ -56,6 +57,7 @@ struct Declaration {
5657
Vector<ComponentValue> value;
5758
Important important = Important::No;
5859
Optional<String> original_value_text = {};
60+
Optional<String> original_full_text = {};
5961

6062
// FIXME: Only needed by our janky @supports re-serialization-re-parse code.
6163
String to_string() const;

0 commit comments

Comments
 (0)