mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 18:32:28 -05: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;
|
||||
|
||||
ErrorOr<String> Block::to_string() const
|
||||
String Block::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
TRY(builder.try_append(m_token.bracket_string()));
|
||||
TRY(builder.try_join(' ', m_values));
|
||||
TRY(builder.try_append(m_token.bracket_mirror_string()));
|
||||
builder.append(m_token.bracket_string());
|
||||
builder.join(' ', m_values);
|
||||
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; }
|
||||
|
||||
ErrorOr<String> to_string() const;
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
Block(Token, Vector<ComponentValue>&&);
|
||||
|
|
|
@ -26,7 +26,7 @@ ComponentValue::ComponentValue(NonnullRefPtr<Block> block)
|
|||
|
||||
ComponentValue::~ComponentValue() = default;
|
||||
|
||||
ErrorOr<String> ComponentValue::to_string() const
|
||||
String ComponentValue::to_string() const
|
||||
{
|
||||
return m_value.visit(
|
||||
[](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(); });
|
||||
}
|
||||
|
||||
ErrorOr<String> ComponentValue::to_debug_string() const
|
||||
String ComponentValue::to_debug_string() const
|
||||
{
|
||||
return m_value.visit(
|
||||
[](Token const& token) -> ErrorOr<String> {
|
||||
return String::formatted("Token: {}", TRY(token.to_debug_string()));
|
||||
[](Token const& token) {
|
||||
return MUST(String::formatted("Token: {}", token.to_debug_string()));
|
||||
},
|
||||
[](NonnullRefPtr<Block> const& block) -> ErrorOr<String> {
|
||||
return String::formatted("Block: {}", TRY(block->to_string()));
|
||||
[](NonnullRefPtr<Block> const& block) {
|
||||
return MUST(String::formatted("Block: {}", block->to_string()));
|
||||
},
|
||||
[](NonnullRefPtr<Function> const& function) -> ErrorOr<String> {
|
||||
return String::formatted("Function: {}", TRY(function->to_string()));
|
||||
[](NonnullRefPtr<Function> const& function) {
|
||||
return MUST(String::formatted("Function: {}", function->to_string()));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -35,8 +35,8 @@ public:
|
|||
Token const& token() const { return m_value.get<Token>(); }
|
||||
operator Token() const { return m_value.get<Token>(); }
|
||||
|
||||
ErrorOr<String> to_string() const;
|
||||
ErrorOr<String> to_debug_string() const;
|
||||
String to_string() const;
|
||||
String to_debug_string() const;
|
||||
|
||||
private:
|
||||
Variant<Token, NonnullRefPtr<Function>, NonnullRefPtr<Block>> m_value;
|
||||
|
@ -47,6 +47,6 @@ template<>
|
|||
struct AK::Formatter<Web::CSS::Parser::ComponentValue> : Formatter<StringView> {
|
||||
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;
|
||||
|
||||
ErrorOr<String> Declaration::to_string() const
|
||||
String Declaration::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
serialize_an_identifier(builder, m_name);
|
||||
TRY(builder.try_append(": "sv));
|
||||
TRY(builder.try_join(' ', m_values));
|
||||
builder.append(": "sv);
|
||||
builder.join(' ', m_values);
|
||||
|
||||
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; }
|
||||
Important importance() const { return m_important; }
|
||||
|
||||
ErrorOr<String> to_string() const;
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
FlyString m_name;
|
||||
|
|
|
@ -18,16 +18,16 @@ Function::Function(FlyString name, Vector<ComponentValue>&& values)
|
|||
|
||||
Function::~Function() = default;
|
||||
|
||||
ErrorOr<String> Function::to_string() const
|
||||
String Function::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
serialize_an_identifier(builder, m_name);
|
||||
TRY(builder.try_append('('));
|
||||
TRY(builder.try_join(' ', m_values));
|
||||
TRY(builder.try_append(')'));
|
||||
builder.append('(');
|
||||
builder.join(' ', m_values);
|
||||
builder.append(')');
|
||||
|
||||
return builder.to_string();
|
||||
return MUST(builder.to_string());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public:
|
|||
StringView name() const { return m_name; }
|
||||
Vector<ComponentValue> const& values() const { return m_values; }
|
||||
|
||||
ErrorOr<String> to_string() const;
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
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()) {
|
||||
transaction.commit();
|
||||
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.
|
||||
StringBuilder builder;
|
||||
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();
|
||||
return Supports::Feature {
|
||||
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>? ) ]`
|
||||
if (first_token.is_function()) {
|
||||
transaction.commit();
|
||||
return GeneralEnclosed { first_token.to_string().release_value_but_fixme_should_propagate_errors() };
|
||||
return GeneralEnclosed { first_token.to_string() };
|
||||
}
|
||||
|
||||
// `( <any-value>? )`
|
||||
if (first_token.is_block() && first_token.block().is_paren()) {
|
||||
transaction.commit();
|
||||
return GeneralEnclosed { first_token.to_string().release_value_but_fixme_should_propagate_errors() };
|
||||
return GeneralEnclosed { first_token.to_string() };
|
||||
}
|
||||
|
||||
return {};
|
||||
|
@ -1498,7 +1498,7 @@ CSSRule* Parser::convert_to_rule(NonnullRefPtr<Rule> rule)
|
|||
return {};
|
||||
}
|
||||
|
||||
auto name = name_token.to_string().release_value_but_fixme_should_propagate_errors();
|
||||
auto name = name_token.to_string();
|
||||
|
||||
if (!rule->block())
|
||||
return {};
|
||||
|
@ -6247,7 +6247,7 @@ public:
|
|||
|
||||
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:
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
namespace Web::CSS::Parser {
|
||||
|
||||
ErrorOr<String> Token::to_string() const
|
||||
String Token::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
|
@ -20,15 +20,15 @@ ErrorOr<String> Token::to_string() const
|
|||
case Type::Ident:
|
||||
return serialize_an_identifier(ident());
|
||||
case Type::Function:
|
||||
return String::formatted("{}(", serialize_an_identifier(function()));
|
||||
return MUST(String::formatted("{}(", serialize_an_identifier(function())));
|
||||
case Type::AtKeyword:
|
||||
return String::formatted("@{}", serialize_an_identifier(at_keyword()));
|
||||
return MUST(String::formatted("@{}", serialize_an_identifier(at_keyword())));
|
||||
case Type::Hash: {
|
||||
switch (m_hash_type) {
|
||||
case HashType::Id:
|
||||
return String::formatted("#{}", serialize_an_identifier(hash_value()));
|
||||
return MUST(String::formatted("#{}", serialize_an_identifier(hash_value())));
|
||||
case HashType::Unrestricted:
|
||||
return String::formatted("#{}", hash_value());
|
||||
return MUST(String::formatted("#{}", hash_value()));
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -43,11 +43,11 @@ ErrorOr<String> Token::to_string() const
|
|||
case Type::Delim:
|
||||
return String { m_value };
|
||||
case Type::Number:
|
||||
return String::number(m_number_value.value());
|
||||
return MUST(String::number(m_number_value.value()));
|
||||
case Type::Percentage:
|
||||
return String::formatted("{}%", m_number_value.value());
|
||||
return MUST(String::formatted("{}%", m_number_value.value()));
|
||||
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:
|
||||
return " "_string;
|
||||
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) {
|
||||
case Type::Invalid:
|
||||
|
@ -87,29 +87,29 @@ ErrorOr<String> Token::to_debug_string() const
|
|||
case Type::EndOfFile:
|
||||
return "__EOF__"_string;
|
||||
case Type::Ident:
|
||||
return String::formatted("Ident: {}", ident());
|
||||
return MUST(String::formatted("Ident: {}", ident()));
|
||||
case Type::Function:
|
||||
return String::formatted("Function: {}", function());
|
||||
return MUST(String::formatted("Function: {}", function()));
|
||||
case Type::AtKeyword:
|
||||
return String::formatted("AtKeyword: {}", at_keyword());
|
||||
return MUST(String::formatted("AtKeyword: {}", at_keyword()));
|
||||
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:
|
||||
return String::formatted("String: {}", string());
|
||||
return MUST(String::formatted("String: {}", string()));
|
||||
case Type::BadString:
|
||||
return "BadString"_string;
|
||||
case Type::Url:
|
||||
return String::formatted("Url: {}", url());
|
||||
return MUST(String::formatted("Url: {}", url()));
|
||||
case Type::BadUrl:
|
||||
return "BadUrl"_string;
|
||||
case Type::Delim:
|
||||
return String::formatted("Delim: {}", m_value);
|
||||
return MUST(String::formatted("Delim: {}", m_value));
|
||||
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:
|
||||
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:
|
||||
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:
|
||||
return "Whitespace"_string;
|
||||
case Type::CDO:
|
||||
|
|
|
@ -144,8 +144,8 @@ public:
|
|||
StringView bracket_string() const;
|
||||
StringView bracket_mirror_string() const;
|
||||
|
||||
ErrorOr<String> to_string() const;
|
||||
ErrorOr<String> to_debug_string() const;
|
||||
String to_string() const;
|
||||
String to_debug_string() const;
|
||||
|
||||
String const& representation() const { return m_representation; }
|
||||
Position const& start_position() const { return m_start_position; }
|
||||
|
|
|
@ -16,7 +16,7 @@ ErrorOr<String> UnresolvedStyleValue::to_string() const
|
|||
{
|
||||
StringBuilder builder;
|
||||
for (auto& value : m_values)
|
||||
TRY(builder.try_append(TRY(value.to_string())));
|
||||
TRY(builder.try_append(value.to_string()));
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue