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:");
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)
@ -249,7 +249,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
if (current_value.is(Token::Type::Hash)) {
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 {};
}
type = Selector::SimpleSelector::Type::Id;
@ -260,12 +260,12 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
return {};
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 {};
}
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() == "*") {
type = Selector::SimpleSelector::Type::Universal;
} 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.
auto& attribute_part = attribute_parts.first();
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 {};
}
@ -315,7 +315,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
size_t attribute_index = 1;
auto& delim_part = attribute_parts.at(attribute_index);
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 {};
}
@ -331,7 +331,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
auto& delim_second_part = attribute_parts.at(attribute_index);
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 {};
}
@ -360,7 +360,7 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
auto& value_part = attribute_parts.at(attribute_index);
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 {};
}
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;
}
} else {
dbgln("Unexpected Block in pseudo-class name, expected a function or identifier. '{}'", current_value.to_string());
return simple_selector;
dbgln("Unexpected Block in pseudo-class name, expected a function or identifier. '{}'", current_value.to_debug_string());
return {};
}
}
@ -487,8 +487,6 @@ Optional<Selector> Parser::parse_single_selector(TokenStream<T>& tokens, bool is
relation = Selector::ComplexSelector::Relation::Column;
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();
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;
@ -1328,7 +1326,7 @@ RefPtr<StyleValue> Parser::parse_css_value(PropertyID property_id, TokenStream<S
// 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,
// 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())
return color;

View file

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

View file

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

View file

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

View file

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