1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 04:27:40 +00:00

LibWeb: Convert some CSS parser *Rule classes to using pointers

Previously these were all passed around by value, but some of them
(StyleComponentValueRule and StyleBlockRule) want to include each
other as fields, so this had to change.
This commit is contained in:
Sam Atkins 2021-07-01 14:13:48 +01:00 committed by Andreas Kling
parent a558916e1f
commit 89bfde29dc
7 changed files with 47 additions and 29 deletions

View file

@ -74,7 +74,7 @@ Vector<QualifiedStyleRule> Parser::parse_as_stylesheet()
dbgln("{}", pre.to_string()); dbgln("{}", pre.to_string());
} }
dbgln("BLOCK:"); dbgln("BLOCK:");
dbgln("{}", rule.m_block.to_string()); dbgln("{}", rule.block().to_string());
dbgln(""); dbgln("");
auto selectors = parse_selectors(rule.m_prelude); auto selectors = parse_selectors(rule.m_prelude);
@ -95,7 +95,7 @@ Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleCompo
if (index >= parts.size()) if (index >= parts.size())
return {}; return {};
auto current_value = parts.at(index); auto& current_value = parts.at(index);
index++; index++;
CSS::Selector::SimpleSelector::Type type; CSS::Selector::SimpleSelector::Type type;
@ -246,7 +246,7 @@ Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleCompo
return simple_selector; return simple_selector;
} }
} else if (current_value.is_function()) { } else if (current_value.is_function()) {
auto pseudo_function = current_value.function(); auto& pseudo_function = current_value.function();
if (pseudo_function.name().equals_ignoring_case("nth-child")) { if (pseudo_function.name().equals_ignoring_case("nth-child")) {
simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthChild; simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthChild;
simple_selector.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(pseudo_function.values_as_string()); simple_selector.nth_child_pattern = CSS::Selector::SimpleSelector::NthChildPattern::parse(pseudo_function.values_as_string());
@ -469,12 +469,12 @@ StyleComponentValueRule Parser::consume_a_component_value()
return component; return component;
} }
StyleBlockRule Parser::consume_a_simple_block() NonnullRefPtr<StyleBlockRule> Parser::consume_a_simple_block()
{ {
auto ending_token = current_token().mirror_variant(); auto ending_token = current_token().mirror_variant();
StyleBlockRule block; NonnullRefPtr<StyleBlockRule> block = create<StyleBlockRule>();
block.m_token = current_token(); block->m_token = current_token();
for (;;) { for (;;) {
auto token = next_token(); auto token = next_token();
@ -495,14 +495,13 @@ StyleBlockRule Parser::consume_a_simple_block()
continue; continue;
} }
} }
block.m_values.append(value.to_string()); block->m_values.append(value.to_string());
} }
} }
StyleFunctionRule Parser::consume_a_function() NonnullRefPtr<StyleFunctionRule> Parser::consume_a_function()
{ {
StyleFunctionRule function; NonnullRefPtr<StyleFunctionRule> function = create<StyleFunctionRule>(current_token().m_value.to_string());
function.m_name = current_token().m_value.to_string();
for (;;) { for (;;) {
auto token = next_token(); auto token = next_token();
@ -522,7 +521,7 @@ StyleFunctionRule Parser::consume_a_function()
continue; continue;
} }
} }
function.m_values.append(value.to_string()); function->m_values.append(value.to_string());
} }
return function; return function;

View file

@ -78,8 +78,8 @@ private:
Optional<StyleDeclarationRule> consume_a_declaration(Vector<StyleComponentValueRule>); Optional<StyleDeclarationRule> consume_a_declaration(Vector<StyleComponentValueRule>);
Optional<StyleDeclarationRule> consume_a_declaration(); Optional<StyleDeclarationRule> consume_a_declaration();
StyleComponentValueRule consume_a_component_value(); StyleComponentValueRule consume_a_component_value();
StyleBlockRule consume_a_simple_block(); NonnullRefPtr<StyleBlockRule> consume_a_simple_block();
StyleFunctionRule consume_a_function(); NonnullRefPtr<StyleFunctionRule> consume_a_function();
Tokenizer m_tokenizer; Tokenizer m_tokenizer;
Vector<Token> m_tokens; Vector<Token> m_tokens;

View file

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibWeb/CSS/Parser/StyleBlockRule.h>
#include <LibWeb/CSS/Parser/StyleComponentValueRule.h> #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
namespace Web::CSS { namespace Web::CSS {
@ -18,11 +19,15 @@ class QualifiedStyleRule {
public: public:
QualifiedStyleRule(); QualifiedStyleRule();
~QualifiedStyleRule(); ~QualifiedStyleRule();
Vector<StyleComponentValueRule> const& prelude() const { return m_prelude; }
StyleBlockRule const& block() const { return *m_block; }
String to_string() const; String to_string() const;
private: private:
Vector<StyleComponentValueRule> m_prelude; Vector<StyleComponentValueRule> m_prelude;
StyleBlockRule m_block; RefPtr<StyleBlockRule> m_block;
}; };
} }

View file

@ -7,12 +7,13 @@
#pragma once #pragma once
#include <AK/RefCounted.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibWeb/CSS/Parser/Token.h> #include <LibWeb/CSS/Parser/Token.h>
namespace Web::CSS { namespace Web::CSS {
class StyleBlockRule { class StyleBlockRule : public RefCounted<StyleBlockRule> {
friend class Parser; friend class Parser;
public: public:
@ -31,5 +32,4 @@ private:
Token m_token; Token m_token;
Vector<String> m_values; Vector<String> m_values;
}; };
} }

View file

@ -7,12 +7,15 @@
#pragma once #pragma once
#include <LibWeb/CSS/Parser/StyleBlockRule.h> #include <AK/NonnullRefPtr.h>
#include <LibWeb/CSS/Parser/StyleFunctionRule.h> #include <AK/RefPtr.h>
#include <LibWeb/CSS/Parser/Token.h> #include <LibWeb/CSS/Parser/Token.h>
namespace Web::CSS { namespace Web::CSS {
class StyleBlockRule;
class StyleFunctionRule;
class StyleComponentValueRule { class StyleComponentValueRule {
friend class Parser; friend class Parser;
@ -27,10 +30,18 @@ public:
~StyleComponentValueRule(); ~StyleComponentValueRule();
bool is_block() const { return m_type == ComponentType::Block; } bool is_block() const { return m_type == ComponentType::Block; }
StyleBlockRule const& block() const { return m_block; } StyleBlockRule const& block() const
{
VERIFY(is_block());
return *m_block;
}
bool is_function() const { return m_type == ComponentType::Function; } bool is_function() const { return m_type == ComponentType::Function; }
StyleFunctionRule const& function() const { return m_function; } StyleFunctionRule const& function() const
{
VERIFY(is_function());
return *m_function;
}
bool is(Token::TokenType type) const bool is(Token::TokenType type) const
{ {
@ -44,7 +55,7 @@ public:
private: private:
ComponentType m_type; ComponentType m_type;
Token m_token; Token m_token;
StyleFunctionRule m_function; RefPtr<StyleFunctionRule> m_function;
StyleBlockRule m_block; RefPtr<StyleBlockRule> m_block;
}; };
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/RefCounted.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
@ -14,11 +15,11 @@ namespace Web::CSS {
class StyleComponentValueRule; class StyleComponentValueRule;
class StyleFunctionRule { class StyleFunctionRule : public RefCounted<StyleFunctionRule> {
friend class Parser; friend class Parser;
public: public:
StyleFunctionRule(); StyleFunctionRule(String name);
~StyleFunctionRule(); ~StyleFunctionRule();
String const& name() const { return m_name; } String const& name() const { return m_name; }
@ -38,5 +39,4 @@ private:
String m_name; String m_name;
Vector<String> m_values; Vector<String> m_values;
}; };
} }

View file

@ -45,7 +45,10 @@ StyleComponentValueRule::~StyleComponentValueRule() { }
StyleDeclarationRule::StyleDeclarationRule() { } StyleDeclarationRule::StyleDeclarationRule() { }
StyleDeclarationRule::~StyleDeclarationRule() { } StyleDeclarationRule::~StyleDeclarationRule() { }
StyleFunctionRule::StyleFunctionRule() { } StyleFunctionRule::StyleFunctionRule(String name)
: m_name(name)
{
}
StyleFunctionRule::~StyleFunctionRule() { } StyleFunctionRule::~StyleFunctionRule() { }
template<class SeparatorType, class CollectionType> template<class SeparatorType, class CollectionType>
@ -106,7 +109,7 @@ String QualifiedStyleRule::to_string() const
StringBuilder builder; StringBuilder builder;
append_with_to_string(builder, " ", m_prelude); append_with_to_string(builder, " ", m_prelude);
builder.append(m_block.to_string()); builder.append(m_block->to_string());
return builder.to_string(); return builder.to_string();
} }
@ -131,10 +134,10 @@ String StyleComponentValueRule::to_string() const
builder.append(m_token.to_string()); builder.append(m_token.to_string());
break; break;
case ComponentType::Function: case ComponentType::Function:
builder.append(m_function.to_string()); builder.append(m_function->to_string());
break; break;
case ComponentType::Block: case ComponentType::Block:
builder.append(m_block.to_string()); builder.append(m_block->to_string());
break; break;
} }