Jelajahi Sumber

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.
Sam Atkins 4 tahun lalu
induk
melakukan
caff7fff94

+ 47 - 20
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -7,6 +7,9 @@
 
 #include <AK/NonnullRefPtrVector.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/DeclarationOrAtRule.h>
 #include <LibWeb/CSS/Parser/Parser.h>
@@ -63,13 +66,14 @@ Token Parser::current_token()
     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:");
 
-    for (auto& rule : rules) {
+    for (auto& rule : parser_rules) {
         dbgln("PRE:");
         for (auto& pre : rule.m_prelude) {
             dbgln("{}", pre.to_string());
@@ -78,12 +82,12 @@ NonnullRefPtrVector<QualifiedStyleRule> Parser::parse_as_stylesheet()
         dbgln("{}", rule.block().to_string());
         dbgln("");
 
-        auto selectors = parse_selectors(rule.m_prelude);
-        CSS::Selector selector = Selector(move(selectors));
-        dump_selector(selector);
+        auto css_rule = convert_rule(rule);
+        if (css_rule)
+            rules.append(*css_rule);
     }
 
-    return rules;
+    return CSSStyleSheet::create(rules);
 }
 
 Vector<CSS::Selector::ComplexSelector> Parser::parse_selectors(Vector<StyleComponentValueRule> parts)
@@ -696,9 +700,9 @@ Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations()
     return list;
 }
 
-RefPtr<QualifiedStyleRule> Parser::parse_as_rule()
+RefPtr<CSSRule> Parser::parse_as_rule()
 {
-    RefPtr<QualifiedStyleRule> rule;
+    RefPtr<CSSRule> rule;
 
     for (;;) {
         auto maybe_whitespace = peek_token();
@@ -712,12 +716,15 @@ RefPtr<QualifiedStyleRule> Parser::parse_as_rule()
 
     if (token.is_eof()) {
         return {};
-    }
-
-    if (token.is_at()) {
-        rule = consume_an_at_rule();
+    } else if (token.is_at()) {
+        auto at_rule = consume_an_at_rule();
+        rule = convert_rule(at_rule);
     } else {
-        rule = consume_a_qualified_rule();
+        auto qualified_rule = consume_a_qualified_rule();
+        if (!qualified_rule)
+            return {};
+
+        rule = convert_rule(*qualified_rule);
     }
 
     for (;;) {
@@ -736,12 +743,21 @@ RefPtr<QualifiedStyleRule> Parser::parse_as_rule()
     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 (;;) {
         auto maybe_whitespace = peek_token();
@@ -757,11 +773,16 @@ Optional<StyleDeclarationRule> Parser::parse_as_declaration()
         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()
@@ -827,4 +848,10 @@ Vector<StyleComponentValueRule> Parser::parse_as_list_of_comma_separated_compone
 
     return rules;
 }
+
+RefPtr<CSSRule> Parser::convert_rule(NonnullRefPtr<QualifiedStyleRule>)
+{
+    return {};
+}
+
 }

+ 12 - 5
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -21,21 +21,26 @@
 
 namespace Web::CSS {
 
+class CSSStyleSheet;
+class CSSRule;
+class CSSStyleRule;
+struct StyleProperty;
+
 class Parser {
 public:
     Parser(const StringView& input, const String& encoding = "utf-8");
     ~Parser();
 
     // 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>.
-    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.
-    RefPtr<QualifiedStyleRule> parse_as_rule();
+    RefPtr<CSSRule> parse_as_rule();
     // 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.
-    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().
     Optional<StyleComponentValueRule> parse_as_component_value();
     // For the contents of presentational attributes, which parse text into a single declaration’s 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<StyleFunctionRule> consume_a_function();
 
+    RefPtr<CSSRule> convert_rule(NonnullRefPtr<QualifiedStyleRule>);
+
     Tokenizer m_tokenizer;
     Vector<Token> m_tokens;
     int m_iterator_offset { -1 };