mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:37:35 +00:00
LibWeb: Make serializing CSS Parser types infallible
This commit is contained in:
parent
846c719e49
commit
ccfe197e5a
12 changed files with 57 additions and 57 deletions
|
@ -17,15 +17,15 @@ Block::Block(Token token, Vector<ComponentValue>&& values)
|
||||||
|
|
||||||
Block::~Block() = default;
|
Block::~Block() = default;
|
||||||
|
|
||||||
ErrorOr<String> Block::to_string() const
|
String Block::to_string() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
|
||||||
TRY(builder.try_append(m_token.bracket_string()));
|
builder.append(m_token.bracket_string());
|
||||||
TRY(builder.try_join(' ', m_values));
|
builder.join(' ', m_values);
|
||||||
TRY(builder.try_append(m_token.bracket_mirror_string()));
|
builder.append(m_token.bracket_mirror_string());
|
||||||
|
|
||||||
return builder.to_string();
|
return MUST(builder.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ public:
|
||||||
|
|
||||||
Vector<ComponentValue> const& values() const { return m_values; }
|
Vector<ComponentValue> const& values() const { return m_values; }
|
||||||
|
|
||||||
ErrorOr<String> to_string() const;
|
String to_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Block(Token, Vector<ComponentValue>&&);
|
Block(Token, Vector<ComponentValue>&&);
|
||||||
|
|
|
@ -26,7 +26,7 @@ ComponentValue::ComponentValue(NonnullRefPtr<Block> block)
|
||||||
|
|
||||||
ComponentValue::~ComponentValue() = default;
|
ComponentValue::~ComponentValue() = default;
|
||||||
|
|
||||||
ErrorOr<String> ComponentValue::to_string() const
|
String ComponentValue::to_string() const
|
||||||
{
|
{
|
||||||
return m_value.visit(
|
return m_value.visit(
|
||||||
[](Token const& token) { return token.to_string(); },
|
[](Token const& token) { return token.to_string(); },
|
||||||
|
@ -34,17 +34,17 @@ ErrorOr<String> ComponentValue::to_string() const
|
||||||
[](NonnullRefPtr<Function> const& function) { return function->to_string(); });
|
[](NonnullRefPtr<Function> const& function) { return function->to_string(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<String> ComponentValue::to_debug_string() const
|
String ComponentValue::to_debug_string() const
|
||||||
{
|
{
|
||||||
return m_value.visit(
|
return m_value.visit(
|
||||||
[](Token const& token) -> ErrorOr<String> {
|
[](Token const& token) {
|
||||||
return String::formatted("Token: {}", TRY(token.to_debug_string()));
|
return MUST(String::formatted("Token: {}", token.to_debug_string()));
|
||||||
},
|
},
|
||||||
[](NonnullRefPtr<Block> const& block) -> ErrorOr<String> {
|
[](NonnullRefPtr<Block> const& block) {
|
||||||
return String::formatted("Block: {}", TRY(block->to_string()));
|
return MUST(String::formatted("Block: {}", block->to_string()));
|
||||||
},
|
},
|
||||||
[](NonnullRefPtr<Function> const& function) -> ErrorOr<String> {
|
[](NonnullRefPtr<Function> const& function) {
|
||||||
return String::formatted("Function: {}", TRY(function->to_string()));
|
return MUST(String::formatted("Function: {}", function->to_string()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,8 +35,8 @@ public:
|
||||||
Token const& token() const { return m_value.get<Token>(); }
|
Token const& token() const { return m_value.get<Token>(); }
|
||||||
operator Token() const { return m_value.get<Token>(); }
|
operator Token() const { return m_value.get<Token>(); }
|
||||||
|
|
||||||
ErrorOr<String> to_string() const;
|
String to_string() const;
|
||||||
ErrorOr<String> to_debug_string() const;
|
String to_debug_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Variant<Token, NonnullRefPtr<Function>, NonnullRefPtr<Block>> m_value;
|
Variant<Token, NonnullRefPtr<Function>, NonnullRefPtr<Block>> m_value;
|
||||||
|
@ -47,6 +47,6 @@ template<>
|
||||||
struct AK::Formatter<Web::CSS::Parser::ComponentValue> : Formatter<StringView> {
|
struct AK::Formatter<Web::CSS::Parser::ComponentValue> : Formatter<StringView> {
|
||||||
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Parser::ComponentValue const& component_value)
|
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Parser::ComponentValue const& component_value)
|
||||||
{
|
{
|
||||||
return Formatter<StringView>::format(builder, TRY(component_value.to_string()));
|
return Formatter<StringView>::format(builder, component_value.to_string());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,18 +19,18 @@ Declaration::Declaration(FlyString name, Vector<ComponentValue> values, Importan
|
||||||
|
|
||||||
Declaration::~Declaration() = default;
|
Declaration::~Declaration() = default;
|
||||||
|
|
||||||
ErrorOr<String> Declaration::to_string() const
|
String Declaration::to_string() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
|
||||||
serialize_an_identifier(builder, m_name);
|
serialize_an_identifier(builder, m_name);
|
||||||
TRY(builder.try_append(": "sv));
|
builder.append(": "sv);
|
||||||
TRY(builder.try_join(' ', m_values));
|
builder.join(' ', m_values);
|
||||||
|
|
||||||
if (m_important == Important::Yes)
|
if (m_important == Important::Yes)
|
||||||
TRY(builder.try_append(" !important"sv));
|
builder.append(" !important"sv);
|
||||||
|
|
||||||
return builder.to_string();
|
return MUST(builder.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ public:
|
||||||
Vector<ComponentValue> const& values() const { return m_values; }
|
Vector<ComponentValue> const& values() const { return m_values; }
|
||||||
Important importance() const { return m_important; }
|
Important importance() const { return m_important; }
|
||||||
|
|
||||||
ErrorOr<String> to_string() const;
|
String to_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FlyString m_name;
|
FlyString m_name;
|
||||||
|
|
|
@ -18,16 +18,16 @@ Function::Function(FlyString name, Vector<ComponentValue>&& values)
|
||||||
|
|
||||||
Function::~Function() = default;
|
Function::~Function() = default;
|
||||||
|
|
||||||
ErrorOr<String> Function::to_string() const
|
String Function::to_string() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
|
||||||
serialize_an_identifier(builder, m_name);
|
serialize_an_identifier(builder, m_name);
|
||||||
TRY(builder.try_append('('));
|
builder.append('(');
|
||||||
TRY(builder.try_join(' ', m_values));
|
builder.join(' ', m_values);
|
||||||
TRY(builder.try_append(')'));
|
builder.append(')');
|
||||||
|
|
||||||
return builder.to_string();
|
return MUST(builder.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ public:
|
||||||
StringView name() const { return m_name; }
|
StringView name() const { return m_name; }
|
||||||
Vector<ComponentValue> const& values() const { return m_values; }
|
Vector<ComponentValue> const& values() const { return m_values; }
|
||||||
|
|
||||||
ErrorOr<String> to_string() const;
|
String to_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Function(FlyString name, Vector<ComponentValue>&& values);
|
Function(FlyString name, Vector<ComponentValue>&& values);
|
||||||
|
|
|
@ -301,7 +301,7 @@ Optional<Supports::Feature> Parser::parse_supports_feature(TokenStream<Component
|
||||||
if (auto declaration = consume_a_declaration(block_tokens); declaration.has_value()) {
|
if (auto declaration = consume_a_declaration(block_tokens); declaration.has_value()) {
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
return Supports::Feature {
|
return Supports::Feature {
|
||||||
Supports::Declaration { declaration->to_string().release_value_but_fixme_should_propagate_errors(), JS::make_handle(m_context.realm()) }
|
Supports::Declaration { declaration->to_string(), JS::make_handle(m_context.realm()) }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -311,7 +311,7 @@ Optional<Supports::Feature> Parser::parse_supports_feature(TokenStream<Component
|
||||||
// FIXME: Parsing and then converting back to a string is weird.
|
// FIXME: Parsing and then converting back to a string is weird.
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
for (auto const& item : first_token.function().values())
|
for (auto const& item : first_token.function().values())
|
||||||
builder.append(item.to_string().release_value_but_fixme_should_propagate_errors());
|
builder.append(item.to_string());
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
return Supports::Feature {
|
return Supports::Feature {
|
||||||
Supports::Selector { builder.to_string().release_value_but_fixme_should_propagate_errors(), JS::make_handle(m_context.realm()) }
|
Supports::Selector { builder.to_string().release_value_but_fixme_should_propagate_errors(), JS::make_handle(m_context.realm()) }
|
||||||
|
@ -331,13 +331,13 @@ Optional<GeneralEnclosed> Parser::parse_general_enclosed(TokenStream<ComponentVa
|
||||||
// `[ <function-token> <any-value>? ) ]`
|
// `[ <function-token> <any-value>? ) ]`
|
||||||
if (first_token.is_function()) {
|
if (first_token.is_function()) {
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
return GeneralEnclosed { first_token.to_string().release_value_but_fixme_should_propagate_errors() };
|
return GeneralEnclosed { first_token.to_string() };
|
||||||
}
|
}
|
||||||
|
|
||||||
// `( <any-value>? )`
|
// `( <any-value>? )`
|
||||||
if (first_token.is_block() && first_token.block().is_paren()) {
|
if (first_token.is_block() && first_token.block().is_paren()) {
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
return GeneralEnclosed { first_token.to_string().release_value_but_fixme_should_propagate_errors() };
|
return GeneralEnclosed { first_token.to_string() };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
@ -1498,7 +1498,7 @@ CSSRule* Parser::convert_to_rule(NonnullRefPtr<Rule> rule)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto name = name_token.to_string().release_value_but_fixme_should_propagate_errors();
|
auto name = name_token.to_string();
|
||||||
|
|
||||||
if (!rule->block())
|
if (!rule->block())
|
||||||
return {};
|
return {};
|
||||||
|
@ -6247,7 +6247,7 @@ public:
|
||||||
|
|
||||||
virtual ErrorOr<void> dump(StringBuilder& builder, int indent) const override
|
virtual ErrorOr<void> dump(StringBuilder& builder, int indent) const override
|
||||||
{
|
{
|
||||||
return builder.try_appendff("{: >{}}UNPARSED({})\n", "", indent, TRY(m_component_value.to_debug_string()));
|
return builder.try_appendff("{: >{}}UNPARSED({})\n", "", indent, m_component_value.to_debug_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
namespace Web::CSS::Parser {
|
namespace Web::CSS::Parser {
|
||||||
|
|
||||||
ErrorOr<String> Token::to_string() const
|
String Token::to_string() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
|
||||||
|
@ -20,15 +20,15 @@ ErrorOr<String> Token::to_string() const
|
||||||
case Type::Ident:
|
case Type::Ident:
|
||||||
return serialize_an_identifier(ident());
|
return serialize_an_identifier(ident());
|
||||||
case Type::Function:
|
case Type::Function:
|
||||||
return String::formatted("{}(", serialize_an_identifier(function()));
|
return MUST(String::formatted("{}(", serialize_an_identifier(function())));
|
||||||
case Type::AtKeyword:
|
case Type::AtKeyword:
|
||||||
return String::formatted("@{}", serialize_an_identifier(at_keyword()));
|
return MUST(String::formatted("@{}", serialize_an_identifier(at_keyword())));
|
||||||
case Type::Hash: {
|
case Type::Hash: {
|
||||||
switch (m_hash_type) {
|
switch (m_hash_type) {
|
||||||
case HashType::Id:
|
case HashType::Id:
|
||||||
return String::formatted("#{}", serialize_an_identifier(hash_value()));
|
return MUST(String::formatted("#{}", serialize_an_identifier(hash_value())));
|
||||||
case HashType::Unrestricted:
|
case HashType::Unrestricted:
|
||||||
return String::formatted("#{}", hash_value());
|
return MUST(String::formatted("#{}", hash_value()));
|
||||||
}
|
}
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
@ -43,11 +43,11 @@ ErrorOr<String> Token::to_string() const
|
||||||
case Type::Delim:
|
case Type::Delim:
|
||||||
return String { m_value };
|
return String { m_value };
|
||||||
case Type::Number:
|
case Type::Number:
|
||||||
return String::number(m_number_value.value());
|
return MUST(String::number(m_number_value.value()));
|
||||||
case Type::Percentage:
|
case Type::Percentage:
|
||||||
return String::formatted("{}%", m_number_value.value());
|
return MUST(String::formatted("{}%", m_number_value.value()));
|
||||||
case Type::Dimension:
|
case Type::Dimension:
|
||||||
return String::formatted("{}{}", m_number_value.value(), dimension_unit());
|
return MUST(String::formatted("{}{}", m_number_value.value(), dimension_unit()));
|
||||||
case Type::Whitespace:
|
case Type::Whitespace:
|
||||||
return " "_string;
|
return " "_string;
|
||||||
case Type::CDO:
|
case Type::CDO:
|
||||||
|
@ -78,7 +78,7 @@ ErrorOr<String> Token::to_string() const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<String> Token::to_debug_string() const
|
String Token::to_debug_string() const
|
||||||
{
|
{
|
||||||
switch (m_type) {
|
switch (m_type) {
|
||||||
case Type::Invalid:
|
case Type::Invalid:
|
||||||
|
@ -87,29 +87,29 @@ ErrorOr<String> Token::to_debug_string() const
|
||||||
case Type::EndOfFile:
|
case Type::EndOfFile:
|
||||||
return "__EOF__"_string;
|
return "__EOF__"_string;
|
||||||
case Type::Ident:
|
case Type::Ident:
|
||||||
return String::formatted("Ident: {}", ident());
|
return MUST(String::formatted("Ident: {}", ident()));
|
||||||
case Type::Function:
|
case Type::Function:
|
||||||
return String::formatted("Function: {}", function());
|
return MUST(String::formatted("Function: {}", function()));
|
||||||
case Type::AtKeyword:
|
case Type::AtKeyword:
|
||||||
return String::formatted("AtKeyword: {}", at_keyword());
|
return MUST(String::formatted("AtKeyword: {}", at_keyword()));
|
||||||
case Type::Hash:
|
case Type::Hash:
|
||||||
return String::formatted("Hash: {} (hash_type: {})", hash_value(), m_hash_type == HashType::Unrestricted ? "Unrestricted" : "Id");
|
return MUST(String::formatted("Hash: {} (hash_type: {})", hash_value(), m_hash_type == HashType::Unrestricted ? "Unrestricted" : "Id"));
|
||||||
case Type::String:
|
case Type::String:
|
||||||
return String::formatted("String: {}", string());
|
return MUST(String::formatted("String: {}", string()));
|
||||||
case Type::BadString:
|
case Type::BadString:
|
||||||
return "BadString"_string;
|
return "BadString"_string;
|
||||||
case Type::Url:
|
case Type::Url:
|
||||||
return String::formatted("Url: {}", url());
|
return MUST(String::formatted("Url: {}", url()));
|
||||||
case Type::BadUrl:
|
case Type::BadUrl:
|
||||||
return "BadUrl"_string;
|
return "BadUrl"_string;
|
||||||
case Type::Delim:
|
case Type::Delim:
|
||||||
return String::formatted("Delim: {}", m_value);
|
return MUST(String::formatted("Delim: {}", m_value));
|
||||||
case Type::Number:
|
case Type::Number:
|
||||||
return String::formatted("Number: {}{} (number_type: {})", m_number_value.value() > 0 && m_number_value.is_integer_with_explicit_sign() ? "+" : "", m_number_value.value(), m_number_value.is_integer() ? "Integer" : "Number");
|
return MUST(String::formatted("Number: {}{} (number_type: {})", m_number_value.value() > 0 && m_number_value.is_integer_with_explicit_sign() ? "+" : "", m_number_value.value(), m_number_value.is_integer() ? "Integer" : "Number"));
|
||||||
case Type::Percentage:
|
case Type::Percentage:
|
||||||
return String::formatted("Percentage: {}% (number_type: {})", percentage(), m_number_value.is_integer() ? "Integer" : "Number");
|
return MUST(String::formatted("Percentage: {}% (number_type: {})", percentage(), m_number_value.is_integer() ? "Integer" : "Number"));
|
||||||
case Type::Dimension:
|
case Type::Dimension:
|
||||||
return String::formatted("Dimension: {}{} (number_type: {})", dimension_value(), dimension_unit(), m_number_value.is_integer() ? "Integer" : "Number");
|
return MUST(String::formatted("Dimension: {}{} (number_type: {})", dimension_value(), dimension_unit(), m_number_value.is_integer() ? "Integer" : "Number"));
|
||||||
case Type::Whitespace:
|
case Type::Whitespace:
|
||||||
return "Whitespace"_string;
|
return "Whitespace"_string;
|
||||||
case Type::CDO:
|
case Type::CDO:
|
||||||
|
|
|
@ -144,8 +144,8 @@ public:
|
||||||
StringView bracket_string() const;
|
StringView bracket_string() const;
|
||||||
StringView bracket_mirror_string() const;
|
StringView bracket_mirror_string() const;
|
||||||
|
|
||||||
ErrorOr<String> to_string() const;
|
String to_string() const;
|
||||||
ErrorOr<String> to_debug_string() const;
|
String to_debug_string() const;
|
||||||
|
|
||||||
String const& representation() const { return m_representation; }
|
String const& representation() const { return m_representation; }
|
||||||
Position const& start_position() const { return m_start_position; }
|
Position const& start_position() const { return m_start_position; }
|
||||||
|
|
|
@ -16,7 +16,7 @@ ErrorOr<String> UnresolvedStyleValue::to_string() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
for (auto& value : m_values)
|
for (auto& value : m_values)
|
||||||
TRY(builder.try_append(TRY(value.to_string())));
|
TRY(builder.try_append(value.to_string()));
|
||||||
return builder.to_string();
|
return builder.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue