1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:57:44 +00:00

LibWeb: Convert CSS::Parser methods to return desired types

This is very much stubbed out for now. Most notably is
Parser::convert_rule() where most of the conversion will happen
from the parser's internal rule classes to CSSRule and its children.
This commit is contained in:
Sam Atkins 2021-07-09 16:34:29 +01:00 committed by Andreas Kling
parent f9ffa34622
commit caff7fff94
2 changed files with 59 additions and 25 deletions

View file

@ -7,6 +7,9 @@
#include <AK/NonnullRefPtrVector.h> #include <AK/NonnullRefPtrVector.h>
#include <AK/SourceLocation.h> #include <AK/SourceLocation.h>
#include <LibWeb/CSS/CSSStyleDeclaration.h>
#include <LibWeb/CSS/CSSStyleRule.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/Parser/AtStyleRule.h> #include <LibWeb/CSS/Parser/AtStyleRule.h>
#include <LibWeb/CSS/Parser/DeclarationOrAtRule.h> #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
#include <LibWeb/CSS/Parser/Parser.h> #include <LibWeb/CSS/Parser/Parser.h>
@ -63,13 +66,14 @@ Token Parser::current_token()
return m_tokens.at(m_iterator_offset); return m_tokens.at(m_iterator_offset);
} }
NonnullRefPtrVector<QualifiedStyleRule> Parser::parse_as_stylesheet() NonnullRefPtr<CSSStyleSheet> Parser::parse_as_stylesheet()
{ {
auto rules = consume_a_list_of_rules(true); auto parser_rules = consume_a_list_of_rules(true);
NonnullRefPtrVector<CSSRule> rules;
dbgln("Printing rules:"); dbgln("Printing rules:");
for (auto& rule : rules) { for (auto& rule : parser_rules) {
dbgln("PRE:"); dbgln("PRE:");
for (auto& pre : rule.m_prelude) { for (auto& pre : rule.m_prelude) {
dbgln("{}", pre.to_string()); dbgln("{}", pre.to_string());
@ -78,12 +82,12 @@ NonnullRefPtrVector<QualifiedStyleRule> Parser::parse_as_stylesheet()
dbgln("{}", rule.block().to_string()); dbgln("{}", rule.block().to_string());
dbgln(""); dbgln("");
auto selectors = parse_selectors(rule.m_prelude); auto css_rule = convert_rule(rule);
CSS::Selector selector = Selector(move(selectors)); if (css_rule)
dump_selector(selector); rules.append(*css_rule);
} }
return rules; return CSSStyleSheet::create(rules);
} }
Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleComponentValueRule> parts) Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleComponentValueRule> parts)
@ -696,9 +700,9 @@ Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations()
return list; return list;
} }
RefPtr<QualifiedStyleRule> Parser::parse_as_rule() RefPtr<CSSRule> Parser::parse_as_rule()
{ {
RefPtr<QualifiedStyleRule> rule; RefPtr<CSSRule> rule;
for (;;) { for (;;) {
auto maybe_whitespace = peek_token(); auto maybe_whitespace = peek_token();
@ -712,12 +716,15 @@ RefPtr<QualifiedStyleRule> Parser::parse_as_rule()
if (token.is_eof()) { if (token.is_eof()) {
return {}; return {};
} } else if (token.is_at()) {
auto at_rule = consume_an_at_rule();
if (token.is_at()) { rule = convert_rule(at_rule);
rule = consume_an_at_rule();
} else { } else {
rule = consume_a_qualified_rule(); auto qualified_rule = consume_a_qualified_rule();
if (!qualified_rule)
return {};
rule = convert_rule(*qualified_rule);
} }
for (;;) { for (;;) {
@ -736,12 +743,21 @@ RefPtr<QualifiedStyleRule> Parser::parse_as_rule()
return {}; return {};
} }
NonnullRefPtrVector<QualifiedStyleRule> Parser::parse_as_list_of_rules() NonnullRefPtrVector<CSSRule> Parser::parse_as_list_of_rules()
{ {
return consume_a_list_of_rules(false); auto parsed_rules = consume_a_list_of_rules(false);
NonnullRefPtrVector<CSSRule> rules;
for (auto& rule : parsed_rules) {
auto converted_rule = convert_rule(rule);
if (converted_rule)
rules.append(*converted_rule);
}
return rules;
} }
Optional<StyleDeclarationRule> Parser::parse_as_declaration() Optional<StyleProperty> Parser::parse_as_declaration()
{ {
for (;;) { for (;;) {
auto maybe_whitespace = peek_token(); auto maybe_whitespace = peek_token();
@ -757,11 +773,16 @@ Optional<StyleDeclarationRule> Parser::parse_as_declaration()
return {}; return {};
} }
return consume_a_declaration(); auto declaration = consume_a_declaration();
// FIXME: Return the declaration.
return {};
} }
Vector<DeclarationOrAtRule> Parser::parse_as_list_of_declarations() Vector<StyleProperty> Parser::parse_as_list_of_declarations()
{ {
return consume_a_list_of_declarations(); auto declarations = consume_a_list_of_declarations();
// FIXME: Return the declarations.
return {};
} }
Optional<StyleComponentValueRule> Parser::parse_as_component_value() Optional<StyleComponentValueRule> Parser::parse_as_component_value()
@ -827,4 +848,10 @@ Vector<StyleComponentValueRule> Parser::parse_as_list_of_comma_separated_compone
return rules; return rules;
} }
RefPtr<CSSRule> Parser::convert_rule(NonnullRefPtr<QualifiedStyleRule>)
{
return {};
}
} }

View file

@ -21,21 +21,26 @@
namespace Web::CSS { namespace Web::CSS {
class CSSStyleSheet;
class CSSRule;
class CSSStyleRule;
struct StyleProperty;
class Parser { class Parser {
public: public:
Parser(const StringView& input, const String& encoding = "utf-8"); Parser(const StringView& input, const String& encoding = "utf-8");
~Parser(); ~Parser();
// The normal parser entry point, for parsing stylesheets. // The normal parser entry point, for parsing stylesheets.
NonnullRefPtrVector<QualifiedStyleRule> parse_as_stylesheet(); NonnullRefPtr<CSSStyleSheet> 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>. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
NonnullRefPtrVector<QualifiedStyleRule> parse_as_list_of_rules(); NonnullRefPtrVector<CSSRule> 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. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
RefPtr<QualifiedStyleRule> parse_as_rule(); RefPtr<CSSRule> parse_as_rule();
// Used in @supports conditions. [CSS3-CONDITIONAL] // Used in @supports conditions. [CSS3-CONDITIONAL]
Optional<StyleDeclarationRule> parse_as_declaration(); Optional<StyleProperty> parse_as_declaration();
// For the contents of a style attribute, which parses text into the contents of a single style rule. // For the contents of a style attribute, which parses text into the contents of a single style rule.
Vector<DeclarationOrAtRule> parse_as_list_of_declarations(); Vector<StyleProperty> parse_as_list_of_declarations();
// For things that need to consume a single value, like the parsing rules for attr(). // For things that need to consume a single value, like the parsing rules for attr().
Optional<StyleComponentValueRule> parse_as_component_value(); Optional<StyleComponentValueRule> parse_as_component_value();
// For the contents of presentational attributes, which parse text into a single declarations value, or for parsing a stand-alone selector [SELECT] or list of Media Queries [MEDIAQ], as in Selectors API or the media HTML attribute. // For the contents of presentational attributes, which parse text into a single declarations value, or for parsing a stand-alone selector [SELECT] or list of Media Queries [MEDIAQ], as in Selectors API or the media HTML attribute.
@ -80,6 +85,8 @@ private:
NonnullRefPtr<StyleBlockRule> consume_a_simple_block(); NonnullRefPtr<StyleBlockRule> consume_a_simple_block();
NonnullRefPtr<StyleFunctionRule> consume_a_function(); NonnullRefPtr<StyleFunctionRule> consume_a_function();
RefPtr<CSSRule> convert_rule(NonnullRefPtr<QualifiedStyleRule>);
Tokenizer m_tokenizer; Tokenizer m_tokenizer;
Vector<Token> m_tokens; Vector<Token> m_tokens;
int m_iterator_offset { -1 }; int m_iterator_offset { -1 };