mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:57:45 +00:00
LibWeb: Use StateTransaction for supports-query parsing
This commit is contained in:
parent
dd5927edad
commit
f1bb3e5ce5
1 changed files with 35 additions and 44 deletions
|
@ -1347,8 +1347,8 @@ RefPtr<Supports> Parser::parse_a_supports(TokenStream<T>& tokens)
|
||||||
|
|
||||||
OwnPtr<Supports::Condition> Parser::parse_supports_condition(TokenStream<ComponentValue>& tokens)
|
OwnPtr<Supports::Condition> Parser::parse_supports_condition(TokenStream<ComponentValue>& tokens)
|
||||||
{
|
{
|
||||||
|
auto transaction = tokens.begin_transaction();
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
auto start_position = tokens.position();
|
|
||||||
|
|
||||||
auto& peeked_token = tokens.peek_token();
|
auto& peeked_token = tokens.peek_token();
|
||||||
// `not <supports-in-parens>`
|
// `not <supports-in-parens>`
|
||||||
|
@ -1356,15 +1356,14 @@ OwnPtr<Supports::Condition> Parser::parse_supports_condition(TokenStream<Compone
|
||||||
tokens.next_token();
|
tokens.next_token();
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
auto child = parse_supports_in_parens(tokens);
|
auto child = parse_supports_in_parens(tokens);
|
||||||
if (child.has_value()) {
|
if (!child.has_value())
|
||||||
auto* condition = new Supports::Condition;
|
return {};
|
||||||
condition->type = Supports::Condition::Type::Not;
|
|
||||||
condition->children.append(child.release_value());
|
|
||||||
return adopt_own(*condition);
|
|
||||||
}
|
|
||||||
|
|
||||||
tokens.rewind_to_position(start_position);
|
transaction.commit();
|
||||||
return {};
|
auto condition = make<Supports::Condition>();
|
||||||
|
condition->type = Supports::Condition::Type::Not;
|
||||||
|
condition->children.append(child.release_value());
|
||||||
|
return condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ` <supports-in-parens> [ and <supports-in-parens> ]*
|
// ` <supports-in-parens> [ and <supports-in-parens> ]*
|
||||||
|
@ -1382,20 +1381,16 @@ OwnPtr<Supports::Condition> Parser::parse_supports_condition(TokenStream<Compone
|
||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
bool is_invalid = false;
|
|
||||||
while (tokens.has_next_token()) {
|
while (tokens.has_next_token()) {
|
||||||
if (!children.is_empty()) {
|
if (!children.is_empty()) {
|
||||||
// Expect `and` or `or` here
|
// Expect `and` or `or` here
|
||||||
auto maybe_combination = as_condition_type(tokens.next_token());
|
auto maybe_combination = as_condition_type(tokens.next_token());
|
||||||
if (!maybe_combination.has_value()) {
|
if (!maybe_combination.has_value())
|
||||||
is_invalid = true;
|
return {};
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!condition_type.has_value()) {
|
if (!condition_type.has_value()) {
|
||||||
condition_type = maybe_combination.value();
|
condition_type = maybe_combination.value();
|
||||||
} else if (maybe_combination != condition_type) {
|
} else if (maybe_combination != condition_type) {
|
||||||
is_invalid = true;
|
return {};
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1404,47 +1399,40 @@ OwnPtr<Supports::Condition> Parser::parse_supports_condition(TokenStream<Compone
|
||||||
if (auto in_parens = parse_supports_in_parens(tokens); in_parens.has_value()) {
|
if (auto in_parens = parse_supports_in_parens(tokens); in_parens.has_value()) {
|
||||||
children.append(in_parens.release_value());
|
children.append(in_parens.release_value());
|
||||||
} else {
|
} else {
|
||||||
is_invalid = true;
|
return {};
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_invalid && !children.is_empty()) {
|
if (children.is_empty())
|
||||||
auto* condition = new Supports::Condition;
|
return {};
|
||||||
condition->type = condition_type.value_or(Supports::Condition::Type::Or);
|
|
||||||
condition->children = move(children);
|
|
||||||
return adopt_own(*condition);
|
|
||||||
}
|
|
||||||
|
|
||||||
tokens.rewind_to_position(start_position);
|
transaction.commit();
|
||||||
return {};
|
auto condition = make<Supports::Condition>();
|
||||||
|
condition->type = condition_type.value_or(Supports::Condition::Type::Or);
|
||||||
|
condition->children = move(children);
|
||||||
|
return condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Supports::InParens> Parser::parse_supports_in_parens(TokenStream<ComponentValue>& tokens)
|
Optional<Supports::InParens> Parser::parse_supports_in_parens(TokenStream<ComponentValue>& tokens)
|
||||||
{
|
{
|
||||||
tokens.skip_whitespace();
|
|
||||||
auto start_position = tokens.position();
|
|
||||||
|
|
||||||
auto& first_token = tokens.peek_token();
|
|
||||||
// `( <supports-condition> )`
|
// `( <supports-condition> )`
|
||||||
|
auto& first_token = tokens.peek_token();
|
||||||
if (first_token.is_block() && first_token.block().is_paren()) {
|
if (first_token.is_block() && first_token.block().is_paren()) {
|
||||||
|
auto transaction = tokens.begin_transaction();
|
||||||
tokens.next_token();
|
tokens.next_token();
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
|
|
||||||
TokenStream child_tokens { first_token.block().values() };
|
TokenStream child_tokens { first_token.block().values() };
|
||||||
if (auto condition = parse_supports_condition(child_tokens)) {
|
if (auto condition = parse_supports_condition(child_tokens)) {
|
||||||
if (child_tokens.has_next_token()) {
|
if (child_tokens.has_next_token())
|
||||||
tokens.rewind_to_position(start_position);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
transaction.commit();
|
||||||
return Supports::InParens {
|
return Supports::InParens {
|
||||||
.value = { condition.release_nonnull() }
|
.value = { condition.release_nonnull() }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens.rewind_to_position(start_position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// `<supports-feature>`
|
// `<supports-feature>`
|
||||||
|
@ -1461,58 +1449,61 @@ Optional<Supports::InParens> Parser::parse_supports_in_parens(TokenStream<Compon
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens.rewind_to_position(start_position);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Supports::Feature> Parser::parse_supports_feature(TokenStream<ComponentValue>& tokens)
|
Optional<Supports::Feature> Parser::parse_supports_feature(TokenStream<ComponentValue>& tokens)
|
||||||
{
|
{
|
||||||
|
auto transaction = tokens.begin_transaction();
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
auto start_position = tokens.position();
|
|
||||||
|
|
||||||
auto& first_token = tokens.next_token();
|
auto& first_token = tokens.next_token();
|
||||||
|
|
||||||
// `<supports-decl>`
|
// `<supports-decl>`
|
||||||
if (first_token.is_block() && first_token.block().is_paren()) {
|
if (first_token.is_block() && first_token.block().is_paren()) {
|
||||||
TokenStream block_tokens { first_token.block().values() };
|
TokenStream block_tokens { first_token.block().values() };
|
||||||
// FIXME: Parsing and then converting back to a string is weird.
|
// FIXME: Parsing and then converting back to a string is weird.
|
||||||
if (auto declaration = consume_a_declaration(block_tokens); declaration.has_value()) {
|
if (auto declaration = consume_a_declaration(block_tokens); declaration.has_value()) {
|
||||||
|
transaction.commit();
|
||||||
return Supports::Feature {
|
return Supports::Feature {
|
||||||
Supports::Declaration { declaration->to_string() }
|
Supports::Declaration { declaration->to_string() }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// `<supports-selector-fn>`
|
// `<supports-selector-fn>`
|
||||||
if (first_token.is_function() && first_token.function().name().equals_ignoring_case("selector"sv)) {
|
if (first_token.is_function() && first_token.function().name().equals_ignoring_case("selector"sv)) {
|
||||||
// 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());
|
builder.append(item.to_string());
|
||||||
|
transaction.commit();
|
||||||
return Supports::Feature {
|
return Supports::Feature {
|
||||||
Supports::Selector { builder.to_string() }
|
Supports::Selector { builder.to_string() }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens.rewind_to_position(start_position);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.w3.org/TR/mediaqueries-4/#typedef-general-enclosed
|
// https://www.w3.org/TR/mediaqueries-4/#typedef-general-enclosed
|
||||||
Optional<GeneralEnclosed> Parser::parse_general_enclosed(TokenStream<ComponentValue>& tokens)
|
Optional<GeneralEnclosed> Parser::parse_general_enclosed(TokenStream<ComponentValue>& tokens)
|
||||||
{
|
{
|
||||||
|
auto transaction = tokens.begin_transaction();
|
||||||
tokens.skip_whitespace();
|
tokens.skip_whitespace();
|
||||||
auto start_position = tokens.position();
|
|
||||||
|
|
||||||
auto& first_token = tokens.next_token();
|
auto& first_token = tokens.next_token();
|
||||||
|
|
||||||
// `[ <function-token> <any-value>? ) ]`
|
// `[ <function-token> <any-value>? ) ]`
|
||||||
if (first_token.is_function())
|
if (first_token.is_function()) {
|
||||||
|
transaction.commit();
|
||||||
return GeneralEnclosed { first_token.to_string() };
|
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();
|
||||||
return GeneralEnclosed { first_token.to_string() };
|
return GeneralEnclosed { first_token.to_string() };
|
||||||
|
}
|
||||||
|
|
||||||
tokens.rewind_to_position(start_position);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue