1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 05: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("BLOCK:");
dbgln("{}", rule.m_block.to_string());
dbgln("{}", rule.block().to_string());
dbgln("");
auto selectors = parse_selectors(rule.m_prelude);
@ -95,7 +95,7 @@ Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleCompo
if (index >= parts.size())
return {};
auto current_value = parts.at(index);
auto& current_value = parts.at(index);
index++;
CSS::Selector::SimpleSelector::Type type;
@ -246,7 +246,7 @@ Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleCompo
return simple_selector;
}
} 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")) {
simple_selector.pseudo_class = CSS::Selector::SimpleSelector::PseudoClass::NthChild;
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;
}
StyleBlockRule Parser::consume_a_simple_block()
NonnullRefPtr<StyleBlockRule> Parser::consume_a_simple_block()
{
auto ending_token = current_token().mirror_variant();
StyleBlockRule block;
block.m_token = current_token();
NonnullRefPtr<StyleBlockRule> block = create<StyleBlockRule>();
block->m_token = current_token();
for (;;) {
auto token = next_token();
@ -495,14 +495,13 @@ StyleBlockRule Parser::consume_a_simple_block()
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;
function.m_name = current_token().m_value.to_string();
NonnullRefPtr<StyleFunctionRule> function = create<StyleFunctionRule>(current_token().m_value.to_string());
for (;;) {
auto token = next_token();
@ -522,7 +521,7 @@ StyleFunctionRule Parser::consume_a_function()
continue;
}
}
function.m_values.append(value.to_string());
function->m_values.append(value.to_string());
}
return function;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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