LibWeb: Convert QualifiedStyleRule to a RefPtr type in new Parser

This commit is contained in:
Sam Atkins 2021-07-01 16:49:33 +01:00 committed by Andreas Kling
parent f690259a42
commit 06cd418770
Notes: sideshowbarker 2024-07-18 22:57:59 +09:00
5 changed files with 28 additions and 26 deletions

View File

@ -15,7 +15,7 @@ class DeclarationOrAtRule {
friend class Parser;
public:
explicit DeclarationOrAtRule(AtStyleRule at);
explicit DeclarationOrAtRule(RefPtr<AtStyleRule> at);
explicit DeclarationOrAtRule(StyleDeclarationRule declaration);
~DeclarationOrAtRule();
@ -28,7 +28,7 @@ public:
private:
DeclarationType m_type;
AtStyleRule m_at;
RefPtr<AtStyleRule> m_at;
StyleDeclarationRule m_declaration;
};

View File

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NonnullRefPtrVector.h>
#include <AK/SourceLocation.h>
#include <LibWeb/CSS/Parser/AtStyleRule.h>
#include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
@ -62,7 +63,7 @@ Token Parser::current_token()
return m_tokens.at(m_iterator_offset);
}
Vector<QualifiedStyleRule> Parser::parse_as_stylesheet()
NonnullRefPtrVector<QualifiedStyleRule> Parser::parse_as_stylesheet()
{
auto rules = consume_a_list_of_rules(true);
@ -390,9 +391,9 @@ bool Parser::is_combinator(String input)
return input == ">" || input == "+" || input == "~" || input == "||";
}
Vector<QualifiedStyleRule> Parser::consume_a_list_of_rules(bool top_level)
NonnullRefPtrVector<QualifiedStyleRule> Parser::consume_a_list_of_rules(bool top_level)
{
Vector<QualifiedStyleRule> rules;
NonnullRefPtrVector<QualifiedStyleRule> rules;
for (;;) {
auto token = next_token();
@ -412,8 +413,8 @@ Vector<QualifiedStyleRule> Parser::consume_a_list_of_rules(bool top_level)
reconsume_current_input_token();
auto maybe_qualified = consume_a_qualified_rule();
if (maybe_qualified.has_value()) {
rules.append(maybe_qualified.value());
if (maybe_qualified) {
rules.append(maybe_qualified.release_nonnull());
}
continue;
@ -427,15 +428,15 @@ Vector<QualifiedStyleRule> Parser::consume_a_list_of_rules(bool top_level)
reconsume_current_input_token();
auto maybe_qualified = consume_a_qualified_rule();
if (maybe_qualified.has_value()) {
rules.append(maybe_qualified.value());
if (maybe_qualified) {
rules.append(maybe_qualified.release_nonnull());
}
}
return rules;
}
AtStyleRule Parser::consume_an_at_rule()
NonnullRefPtr<AtStyleRule> Parser::consume_an_at_rule()
{
auto initial = next_token();
@ -466,9 +467,9 @@ AtStyleRule Parser::consume_an_at_rule()
}
}
Optional<QualifiedStyleRule> Parser::consume_a_qualified_rule()
RefPtr<QualifiedStyleRule> Parser::consume_a_qualified_rule()
{
QualifiedStyleRule rule;
NonnullRefPtr<QualifiedStyleRule> rule = create<QualifiedStyleRule>();
for (;;) {
auto token = next_token();
@ -479,7 +480,7 @@ Optional<QualifiedStyleRule> Parser::consume_a_qualified_rule()
}
if (token.is_open_curly()) {
rule.m_block = consume_a_simple_block();
rule->m_block = consume_a_simple_block();
return rule;
}
@ -487,7 +488,7 @@ Optional<QualifiedStyleRule> Parser::consume_a_qualified_rule()
reconsume_current_input_token();
auto value = consume_a_component_value();
rule.m_prelude.append(value);
rule->m_prelude.append(value);
}
return rule;
@ -690,9 +691,9 @@ Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations()
return list;
}
Optional<QualifiedStyleRule> Parser::parse_as_rule()
RefPtr<QualifiedStyleRule> Parser::parse_as_rule()
{
Optional<QualifiedStyleRule> rule;
RefPtr<QualifiedStyleRule> rule;
for (;;) {
auto maybe_whitespace = peek_token();
@ -730,7 +731,7 @@ Optional<QualifiedStyleRule> Parser::parse_as_rule()
return {};
}
Vector<QualifiedStyleRule> Parser::parse_as_list_of_rules()
NonnullRefPtrVector<QualifiedStyleRule> Parser::parse_as_list_of_rules()
{
return consume_a_list_of_rules(false);
}

View File

@ -27,11 +27,11 @@ public:
~Parser();
// The normal parser entry point, for parsing stylesheets.
Vector<QualifiedStyleRule> parse_as_stylesheet();
NonnullRefPtrVector<QualifiedStyleRule> parse_as_stylesheet();
// For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
Vector<QualifiedStyleRule> parse_as_list_of_rules();
NonnullRefPtrVector<QualifiedStyleRule> parse_as_list_of_rules();
// For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
Optional<QualifiedStyleRule> parse_as_rule();
RefPtr<QualifiedStyleRule> parse_as_rule();
// Used in @supports conditions. [CSS3-CONDITIONAL]
Optional<StyleDeclarationRule> parse_as_declaration();
// For the contents of a style attribute, which parses text into the contents of a single style rule.
@ -71,9 +71,9 @@ private:
void reconsume_current_input_token();
bool is_combinator(String);
Vector<QualifiedStyleRule> consume_a_list_of_rules(bool top_level);
AtStyleRule consume_an_at_rule();
Optional<QualifiedStyleRule> consume_a_qualified_rule();
NonnullRefPtrVector<QualifiedStyleRule> consume_a_list_of_rules(bool top_level);
NonnullRefPtr<AtStyleRule> consume_an_at_rule();
RefPtr<QualifiedStyleRule> consume_a_qualified_rule();
Vector<DeclarationOrAtRule> consume_a_list_of_declarations();
Optional<StyleDeclarationRule> consume_a_declaration(Vector<StyleComponentValueRule>);
Optional<StyleDeclarationRule> consume_a_declaration();

View File

@ -7,13 +7,14 @@
#pragma once
#include <AK/RefCounted.h>
#include <AK/Vector.h>
#include <LibWeb/CSS/Parser/StyleBlockRule.h>
#include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
namespace Web::CSS {
class QualifiedStyleRule {
class QualifiedStyleRule : public RefCounted<QualifiedStyleRule> {
friend class Parser;
public:

View File

@ -18,7 +18,7 @@ namespace Web::CSS {
AtStyleRule::AtStyleRule() { }
AtStyleRule::~AtStyleRule() { }
DeclarationOrAtRule::DeclarationOrAtRule(AtStyleRule at)
DeclarationOrAtRule::DeclarationOrAtRule(RefPtr<AtStyleRule> at)
: m_type(DeclarationType::At)
, m_at(move(at))
{
@ -94,7 +94,7 @@ String DeclarationOrAtRule::to_string() const
switch (m_type) {
default:
case DeclarationType::At:
builder.append(m_at.to_string());
builder.append(m_at->to_string());
break;
case DeclarationType::Declaration:
builder.append(m_declaration.to_string());