mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-09 01:29:17 +00:00
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:
parent
04939d68f0
commit
ea164124de
Notes:
github-actions[bot]
2024-10-16 12:23:48 +00:00
Author: https://github.com/AtkinsSJ
Commit: ea164124de
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1795
5 changed files with 41 additions and 7 deletions
|
@ -36,10 +36,7 @@ bool ComponentValue::is_ident(StringView ident) const
|
||||||
|
|
||||||
String ComponentValue::to_string() const
|
String ComponentValue::to_string() const
|
||||||
{
|
{
|
||||||
return m_value.visit(
|
return m_value.visit([](auto const& it) { return it.to_string(); });
|
||||||
[](Token const& token) { return token.to_string(); },
|
|
||||||
[](SimpleBlock const& block) { return block.to_string(); },
|
|
||||||
[](Function const& function) { return function.to_string(); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String ComponentValue::to_debug_string() const
|
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(); });
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@ public:
|
||||||
|
|
||||||
String to_string() const;
|
String to_string() const;
|
||||||
String to_debug_string() const;
|
String to_debug_string() const;
|
||||||
|
String original_source_text() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Variant<Token, Function, SimpleBlock> m_value;
|
Variant<Token, Function, SimpleBlock> m_value;
|
||||||
|
|
|
@ -788,7 +788,8 @@ SimpleBlock Parser::consume_a_simple_block(TokenStream<T>& input)
|
||||||
// ending token
|
// ending token
|
||||||
if (token.is(Token::Type::EndOfFile) || token.is(ending_token)) {
|
if (token.is(Token::Type::EndOfFile) || token.is(ending_token)) {
|
||||||
// Discard a token from input. Return block.
|
// 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;
|
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 token’s value,
|
// Consume a token from input, and let function be a new function with its name equal the returned token’s value,
|
||||||
// and a value set to an empty list.
|
// and a value set to an empty list.
|
||||||
|
auto name_token = ((Token)input.consume_a_token());
|
||||||
Function function {
|
Function function {
|
||||||
.name = ((Token)input.consume_a_token()).function(),
|
.name = name_token.function(),
|
||||||
.value = {},
|
.value = {},
|
||||||
|
.name_token = name_token,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Process input:
|
// Process input:
|
||||||
|
@ -824,7 +827,8 @@ Function Parser::consume_a_function(TokenStream<T>& input)
|
||||||
// <)-token>
|
// <)-token>
|
||||||
if (token.is(Token::Type::EndOfFile) || token.is(Token::Type::CloseParen)) {
|
if (token.is(Token::Type::EndOfFile) || token.is(Token::Type::CloseParen)) {
|
||||||
// Discard a token from input. Return function.
|
// 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;
|
return function;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,17 @@ String SimpleBlock::to_string() const
|
||||||
return builder.to_string_without_validation();
|
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
|
String Function::to_string() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
@ -53,6 +64,17 @@ String Function::to_string() const
|
||||||
return builder.to_string_without_validation();
|
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
|
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) {
|
for (auto const& child : child_rules_and_lists_of_declarations) {
|
||||||
|
|
|
@ -63,20 +63,25 @@ struct Declaration {
|
||||||
struct SimpleBlock {
|
struct SimpleBlock {
|
||||||
Token token;
|
Token token;
|
||||||
Vector<ComponentValue> value;
|
Vector<ComponentValue> value;
|
||||||
|
Token end_token = {};
|
||||||
|
|
||||||
bool is_curly() const { return token.is(Token::Type::OpenCurly); }
|
bool is_curly() const { return token.is(Token::Type::OpenCurly); }
|
||||||
bool is_paren() const { return token.is(Token::Type::OpenParen); }
|
bool is_paren() const { return token.is(Token::Type::OpenParen); }
|
||||||
bool is_square() const { return token.is(Token::Type::OpenSquare); }
|
bool is_square() const { return token.is(Token::Type::OpenSquare); }
|
||||||
|
|
||||||
String to_string() const;
|
String to_string() const;
|
||||||
|
String original_source_text() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://drafts.csswg.org/css-syntax/#function
|
// https://drafts.csswg.org/css-syntax/#function
|
||||||
struct Function {
|
struct Function {
|
||||||
FlyString name;
|
FlyString name;
|
||||||
Vector<ComponentValue> value;
|
Vector<ComponentValue> value;
|
||||||
|
Token name_token = {};
|
||||||
|
Token end_token = {};
|
||||||
|
|
||||||
String to_string() const;
|
String to_string() const;
|
||||||
|
String original_source_text() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue