LibWeb: Implement CSS::Token::to_string()

This outputs valid CSS, as opposed to to_debug_string().
This commit is contained in:
Sam Atkins 2021-11-24 12:57:21 +00:00 committed by Andreas Kling
parent 3e7e503dee
commit cf07da082e
2 changed files with 63 additions and 0 deletions

View file

@ -6,9 +6,71 @@
#include <AK/String.h>
#include <LibWeb/CSS/Parser/Token.h>
#include <LibWeb/CSS/Serialize.h>
namespace Web::CSS {
String Token::to_string() const
{
StringBuilder builder;
switch (m_type) {
case Type::EndOfFile:
return "";
case Type::Ident:
return serialize_an_identifier(ident());
case Type::Function:
return String::formatted("{}(", serialize_an_identifier(function()));
case Type::AtKeyword:
return String::formatted("@{}", serialize_an_identifier(at_keyword()));
case Type::Hash:
return String::formatted("#{}", serialize_an_identifier(hash_value()));
case Type::String:
return serialize_a_string(string());
case Type::BadString:
return "";
case Type::Url:
return serialize_a_url(url());
case Type::BadUrl:
return "url()";
case Type::Delim:
return m_value;
case Type::Number:
return String::number(m_number_value);
case Type::Percentage:
return String::formatted("{}%", m_number_value);
case Type::Dimension:
return String::formatted("{}{}", m_number_value, m_unit);
case Type::Whitespace:
return " ";
case Type::CDO:
return "<!--";
case Type::CDC:
return "-->";
case Type::Colon:
return ":";
case Type::Semicolon:
return ";";
case Type::Comma:
return ",";
case Type::OpenSquare:
return "[";
case Type::CloseSquare:
return "]";
case Type::OpenParen:
return "(";
case Type::CloseParen:
return ")";
case Type::OpenCurly:
return "{";
case Type::CloseCurly:
return "}";
case Type::Invalid:
default:
VERIFY_NOT_REACHED();
}
}
String Token::to_debug_string() const
{
StringBuilder builder;

View file

@ -157,6 +157,7 @@ public:
String bracket_string() const;
String bracket_mirror_string() const;
String to_string() const;
String to_debug_string() const;
Position const& start_position() const { return m_start_position; }