1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-19 06:17:35 +00:00

LibWeb: Increase clarity with CSS token debug logging

Had to adjust some places that were using Token.to_string() for
non-debug-logging purposes. Changed its name to to_debug_string()
to make the usage clearer.
This commit is contained in:
Sam Atkins 2021-07-07 21:29:24 +01:00 committed by Andreas Kling
parent e5ac5e1fab
commit fabc09a593
5 changed files with 29 additions and 27 deletions

View file

@ -131,7 +131,7 @@ void TokenStream<T>::dump_all_tokens()
{ {
dbgln("Dumping all tokens:"); dbgln("Dumping all tokens:");
for (auto& token : m_tokens) for (auto& token : m_tokens)
dbgln("{}", token.to_string()); dbgln("{}", token.to_debug_string());
} }
Parser::Parser(ParsingContext const& context, StringView const& input, String const& encoding) Parser::Parser(ParsingContext const& context, StringView const& input, String const& encoding)
@ -249,7 +249,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
if (current_value.is(Token::Type::Hash)) { if (current_value.is(Token::Type::Hash)) {
if (((Token)current_value).m_hash_type != Token::HashType::Id) { if (((Token)current_value).m_hash_type != Token::HashType::Id) {
dbgln("Selector contains hash token that is not an id: {}", current_value.to_string()); dbgln("Selector contains hash token that is not an id: {}", current_value.to_debug_string());
return {}; return {};
} }
type = Selector::SimpleSelector::Type::Id; type = Selector::SimpleSelector::Type::Id;
@ -260,12 +260,12 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
return {}; return {};
if (!current_value.is(Token::Type::Ident)) { if (!current_value.is(Token::Type::Ident)) {
dbgln("Expected an ident after '.', got: {}", current_value.to_string()); dbgln("Expected an ident after '.', got: {}", current_value.to_debug_string());
return {}; return {};
} }
type = Selector::SimpleSelector::Type::Class; type = Selector::SimpleSelector::Type::Class;
value = current_value.to_string(); value = current_value.token().ident().to_lowercase_string();
} else if (current_value.is(Token::Type::Delim) && current_value.token().delim() == "*") { } else if (current_value.is(Token::Type::Delim) && current_value.token().delim() == "*") {
type = Selector::SimpleSelector::Type::Universal; type = Selector::SimpleSelector::Type::Universal;
} else if (current_value.is(Token::Type::Ident)) { } else if (current_value.is(Token::Type::Ident)) {
@ -302,7 +302,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
// FIXME: Handle namespace prefix for attribute name. // FIXME: Handle namespace prefix for attribute name.
auto& attribute_part = attribute_parts.first(); auto& attribute_part = attribute_parts.first();
if (!attribute_part.is(Token::Type::Ident)) { if (!attribute_part.is(Token::Type::Ident)) {
dbgln("Expected ident for attribute name, got: '{}'", attribute_part.to_string()); dbgln("Expected ident for attribute name, got: '{}'", attribute_part.to_debug_string());
return {}; return {};
} }
@ -315,7 +315,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
size_t attribute_index = 1; size_t attribute_index = 1;
auto& delim_part = attribute_parts.at(attribute_index); auto& delim_part = attribute_parts.at(attribute_index);
if (!delim_part.is(Token::Type::Delim)) { if (!delim_part.is(Token::Type::Delim)) {
dbgln("Expected a delim for attribute comparison, got: '{}'", delim_part.to_string()); dbgln("Expected a delim for attribute comparison, got: '{}'", delim_part.to_debug_string());
return {}; return {};
} }
@ -331,7 +331,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
auto& delim_second_part = attribute_parts.at(attribute_index); auto& delim_second_part = attribute_parts.at(attribute_index);
if (!(delim_second_part.is(Token::Type::Delim) && delim_second_part.token().delim() == "=")) { if (!(delim_second_part.is(Token::Type::Delim) && delim_second_part.token().delim() == "=")) {
dbgln("Expected a double delim for attribute comparison, got: '{}{}'", delim_part.to_string(), delim_second_part.to_string()); dbgln("Expected a double delim for attribute comparison, got: '{}{}'", delim_part.to_debug_string(), delim_second_part.to_debug_string());
return {}; return {};
} }
@ -360,7 +360,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
auto& value_part = attribute_parts.at(attribute_index); auto& value_part = attribute_parts.at(attribute_index);
if (!value_part.is(Token::Type::Ident) && !value_part.is(Token::Type::String)) { if (!value_part.is(Token::Type::Ident) && !value_part.is(Token::Type::String)) {
dbgln("Expected a string or ident for the value to match attribute against, got: '{}'", value_part.to_string()); dbgln("Expected a string or ident for the value to match attribute against, got: '{}'", value_part.to_debug_string());
return {}; return {};
} }
simple_selector.attribute_value = value_part.token().is(Token::Type::Ident) ? value_part.token().ident() : value_part.token().string(); simple_selector.attribute_value = value_part.token().is(Token::Type::Ident) ? value_part.token().ident() : value_part.token().string();
@ -449,8 +449,8 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
return simple_selector; return simple_selector;
} }
} else { } else {
dbgln("Unexpected Block in pseudo-class name, expected a function or identifier. '{}'", current_value.to_string()); dbgln("Unexpected Block in pseudo-class name, expected a function or identifier. '{}'", current_value.to_debug_string());
return simple_selector; return {};
} }
} }
@ -487,8 +487,6 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
relation = Selector::ComplexSelector::Relation::Column; relation = Selector::ComplexSelector::Relation::Column;
tokens.next_token(); tokens.next_token();
} }
} else {
dbgln("Unrecognized relation delimiter: '{}'", delim);
} }
} }
@ -736,7 +734,7 @@ NonnullRefPtr<StyleFunctionRule> Parser::consume_a_function(TokenStream<T>& toke
tokens.reconsume_current_input_token(); tokens.reconsume_current_input_token();
auto value = consume_a_component_value(tokens); auto value = consume_a_component_value(tokens);
function->m_values.append(value.to_string()); function->m_values.append(value.token().m_value.to_string());
} }
return function; return function;
@ -1328,7 +1326,7 @@ RefPtr<StyleValue> Parser::parse_css_value(PropertyID property_id, TokenStream<S
// https://www.w3.org/TR/css-color-3/ // https://www.w3.org/TR/css-color-3/
// Right now, this uses non-CSS-specific parsing, and assumes the whole color value is one token, // Right now, this uses non-CSS-specific parsing, and assumes the whole color value is one token,
// which is isn't if it's a function-style syntax. // which is isn't if it's a function-style syntax.
auto color = Color::from_string(token.to_string().to_lowercase()); auto color = Color::from_string(token.token().m_value.to_string().to_lowercase());
if (color.has_value()) if (color.has_value())
return color; return color;

View file

@ -52,7 +52,7 @@ public:
Token const& token() const { return m_token; } Token const& token() const { return m_token; }
operator Token() const { return m_token; } operator Token() const { return m_token; }
String to_string() const; String to_debug_string() const;
private: private:
ComponentType m_type; ComponentType m_type;

View file

@ -70,7 +70,7 @@ void append_with_to_string(StringBuilder& builder, SeparatorType& separator, Col
first = false; first = false;
else else
builder.append(separator); builder.append(separator);
builder.append(item.to_string()); builder.append(item.to_debug_string());
} }
} }
@ -133,18 +133,21 @@ String StyleBlockRule::to_string() const
return builder.to_string(); return builder.to_string();
} }
String StyleComponentValueRule::to_string() const String StyleComponentValueRule::to_debug_string() const
{ {
StringBuilder builder; StringBuilder builder;
switch (m_type) { switch (m_type) {
case ComponentType::Token: case ComponentType::Token:
builder.append(m_token.to_string()); builder.append("Token: ");
builder.append(m_token.to_debug_string());
break; break;
case ComponentType::Function: case ComponentType::Function:
builder.append("Function: ");
builder.append(m_function->to_string()); builder.append(m_function->to_string());
break; break;
case ComponentType::Block: case ComponentType::Block:
builder.append("Block: ");
builder.append(m_block->to_string()); builder.append(m_block->to_string());
break; break;
} }

View file

@ -9,7 +9,7 @@
namespace Web::CSS { namespace Web::CSS {
String Token::to_string() const String Token::to_debug_string() const
{ {
StringBuilder builder; StringBuilder builder;
@ -21,7 +21,7 @@ String Token::to_string() const
builder.append("__EOF__"); builder.append("__EOF__");
break; break;
case Type::Ident: case Type::Ident:
//builder.append("Identifier"); builder.append("Identifier: ");
builder.append(m_value.to_string()); builder.append(m_value.to_string());
return builder.to_string(); return builder.to_string();
case Type::Function: case Type::Function:
@ -31,11 +31,11 @@ String Token::to_string() const
builder.append("@"); builder.append("@");
break; break;
case Type::Hash: case Type::Hash:
builder.append("#"); builder.append("Hash: ");
builder.append(m_value.to_string()); builder.append(m_value.to_string());
return builder.to_string(); return builder.to_string();
case Type::String: case Type::String:
//builder.append("String"); builder.append("String: ");
builder.append(m_value.to_string()); builder.append(m_value.to_string());
return builder.to_string(); return builder.to_string();
case Type::BadString: case Type::BadString:
@ -48,21 +48,21 @@ String Token::to_string() const
builder.append("Invalid Url"); builder.append("Invalid Url");
break; break;
case Type::Delim: case Type::Delim:
//builder.append("Delimiter"); builder.append("Delimiter: ");
builder.append(m_value.to_string()); builder.append(m_value.to_string());
return builder.to_string(); return builder.to_string();
case Type::Number: case Type::Number:
//builder.append("Number"); builder.append("Number: ");
builder.append(m_value.to_string()); builder.append(m_value.to_string());
builder.append(m_unit.to_string()); builder.append(m_unit.to_string());
return builder.to_string(); return builder.to_string();
case Type::Percentage: case Type::Percentage:
//builder.append("Percentage"); builder.append("Percentage: ");
builder.append(m_value.to_string()); builder.append(m_value.to_string());
builder.append(m_unit.to_string()); builder.append(m_unit.to_string());
return builder.to_string(); return builder.to_string();
case Type::Dimension: case Type::Dimension:
//builder.append("Dimension"); builder.append("Dimension: ");
builder.append(m_value.to_string()); builder.append(m_value.to_string());
builder.append(m_unit.to_string()); builder.append(m_unit.to_string());
return builder.to_string(); return builder.to_string();

View file

@ -85,7 +85,8 @@ public:
Type mirror_variant() const; Type mirror_variant() const;
String bracket_string() const; String bracket_string() const;
String bracket_mirror_string() const; String bracket_mirror_string() const;
String to_string() const;
String to_debug_string() const;
private: private:
Type m_type { Type::Invalid }; Type m_type { Type::Invalid };