LibWeb/CSS: Preserve original source text for ComponentValues

This requires a little bit of ad-hoc tracking of start/end Tokens for
Function and SimpleBlock.
This commit is contained in:
Sam Atkins 2024-10-14 16:16:42 +01:00 committed by Andreas Kling
parent 04939d68f0
commit ea164124de
Notes: github-actions[bot] 2024-10-16 12:23:48 +00:00
5 changed files with 41 additions and 7 deletions

View file

@ -36,10 +36,7 @@ bool ComponentValue::is_ident(StringView ident) const
String ComponentValue::to_string() const
{
return m_value.visit(
[](Token const& token) { return token.to_string(); },
[](SimpleBlock const& block) { return block.to_string(); },
[](Function const& function) { return function.to_string(); });
return m_value.visit([](auto const& it) { return it.to_string(); });
}
String ComponentValue::to_debug_string() const
@ -56,4 +53,9 @@ String ComponentValue::to_debug_string() const
});
}
String ComponentValue::original_source_text() const
{
return m_value.visit([](auto const& it) { return it.original_source_text(); });
}
}

View file

@ -41,6 +41,7 @@ public:
String to_string() const;
String to_debug_string() const;
String original_source_text() const;
private:
Variant<Token, Function, SimpleBlock> m_value;

View file

@ -788,7 +788,8 @@ SimpleBlock Parser::consume_a_simple_block(TokenStream<T>& input)
// ending token
if (token.is(Token::Type::EndOfFile) || token.is(ending_token)) {
// Discard a token from input. Return block.
input.discard_a_token();
// AD-HOC: Store the token instead as the "end token"
block.end_token = input.consume_a_token();
return block;
}
@ -811,9 +812,11 @@ Function Parser::consume_a_function(TokenStream<T>& input)
// Consume a token from input, and let function be a new function with its name equal the returned tokens value,
// and a value set to an empty list.
auto name_token = ((Token)input.consume_a_token());
Function function {
.name = ((Token)input.consume_a_token()).function(),
.name = name_token.function(),
.value = {},
.name_token = name_token,
};
// Process input:
@ -824,7 +827,8 @@ Function Parser::consume_a_function(TokenStream<T>& input)
// <)-token>
if (token.is(Token::Type::EndOfFile) || token.is(Token::Type::CloseParen)) {
// Discard a token from input. Return function.
input.discard_a_token();
// AD-HOC: Store the token instead as the "end token"
function.end_token = input.consume_a_token();
return function;
}

View file

@ -40,6 +40,17 @@ String SimpleBlock::to_string() const
return builder.to_string_without_validation();
}
String SimpleBlock::original_source_text() const
{
StringBuilder builder;
builder.append(token.original_source_text());
for (auto const& component_value : value) {
builder.append(component_value.original_source_text());
}
builder.append(end_token.original_source_text());
return builder.to_string_without_validation();
}
String Function::to_string() const
{
StringBuilder builder;
@ -53,6 +64,17 @@ String Function::to_string() const
return builder.to_string_without_validation();
}
String Function::original_source_text() const
{
StringBuilder builder;
builder.append(name_token.original_source_text());
for (auto const& component_value : value) {
builder.append(component_value.original_source_text());
}
builder.append(end_token.original_source_text());
return builder.to_string_without_validation();
}
void AtRule::for_each(AtRuleVisitor&& visit_at_rule, QualifiedRuleVisitor&& visit_qualified_rule, DeclarationVisitor&& visit_declaration) const
{
for (auto const& child : child_rules_and_lists_of_declarations) {

View file

@ -63,20 +63,25 @@ struct Declaration {
struct SimpleBlock {
Token token;
Vector<ComponentValue> value;
Token end_token = {};
bool is_curly() const { return token.is(Token::Type::OpenCurly); }
bool is_paren() const { return token.is(Token::Type::OpenParen); }
bool is_square() const { return token.is(Token::Type::OpenSquare); }
String to_string() const;
String original_source_text() const;
};
// https://drafts.csswg.org/css-syntax/#function
struct Function {
FlyString name;
Vector<ComponentValue> value;
Token name_token = {};
Token end_token = {};
String to_string() const;
String original_source_text() const;
};
}