浏览代码

LibWeb: Move CSS Parser into new Web::CSS::Parser namespace

The goal here is to move the parser-internal classes into this namespace
so they can have more convenient names without causing collisions. The
Parser itself won't collide, and would be more convenient to just
remain `CSS::Parser`, but having a namespace and a class with the same
name makes C++ unhappy.
Sam Atkins 3 年之前
父节点
当前提交
c449cabae3

+ 2 - 2
Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp

@@ -312,7 +312,7 @@ NonnullRefPtr<StyleValue> property_initial_value(PropertyID property_id)
     static bool initialized = false;
     if (!initialized) {
         initialized = true;
-        ParsingContext parsing_context;
+        Parser::ParsingContext parsing_context;
 )~~~");
 
     // NOTE: Parsing a shorthand property requires that its longhands are already available here.
@@ -334,7 +334,7 @@ NonnullRefPtr<StyleValue> property_initial_value(PropertyID property_id)
         member_generator.set("initial_value_string", initial_value_string);
         member_generator.append(R"~~~(
         {
-            auto parsed_value = Parser(parsing_context, "@initial_value_string@").parse_as_css_value(PropertyID::@name:titlecase@);
+            auto parsed_value = parse_css_value(parsing_context, "@initial_value_string@", PropertyID::@name:titlecase@);
             VERIFY(!parsed_value.is_null());
             initial_values[to_underlying(PropertyID::@name:titlecase@)] = parsed_value.release_nonnull();
         }

+ 1 - 1
Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp

@@ -73,7 +73,7 @@ void CSSImportRule::resource_did_load()
         dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
     }
 
-    auto sheet = parse_css_stylesheet(CSS::ParsingContext(*m_document, resource()->url()), resource()->encoded_data());
+    auto sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), resource()->encoded_data());
     if (!sheet) {
         dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
         return;

+ 1 - 1
Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp

@@ -65,7 +65,7 @@ DOM::ExceptionOr<void> PropertyOwningCSSStyleDeclaration::set_property(PropertyI
         return {};
 
     // 5. Let component value list be the result of parsing value for property property.
-    auto component_value_list = parse_css_value(CSS::ParsingContext {}, value, property_id);
+    auto component_value_list = parse_css_value(CSS::Parser::ParsingContext {}, value, property_id);
 
     // 6. If component value list is null, then return.
     if (!component_value_list)

+ 1 - 1
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp

@@ -27,7 +27,7 @@ DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned
     // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
 
     // 3. Let parsed rule be the return value of invoking parse a rule with rule.
-    auto parsed_rule = parse_css_rule(CSS::ParsingContext {}, rule);
+    auto parsed_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule);
 
     // 4. If parsed rule is a syntax error, return parsed rule.
     if (!parsed_rule)

+ 1 - 1
Userland/Libraries/LibWeb/CSS/MediaQuery.h

@@ -213,7 +213,7 @@ private:
 };
 
 class MediaQuery : public RefCounted<MediaQuery> {
-    friend class Parser;
+    friend class Parser::Parser;
 
 public:
     ~MediaQuery() = default;

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Parser/Declaration.h

@@ -14,7 +14,7 @@
 namespace Web::CSS {
 
 class Declaration {
-    friend class Parser;
+    friend class Parser::Parser;
 
 public:
     Declaration();

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Parser/DeclarationOrAtRule.h

@@ -12,7 +12,7 @@
 namespace Web::CSS {
 
 class DeclarationOrAtRule {
-    friend class Parser;
+    friend class Parser::Parser;
 
 public:
     explicit DeclarationOrAtRule(RefPtr<StyleRule> at);

+ 19 - 19
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -34,7 +34,7 @@ static void log_parse_error(SourceLocation const& location = SourceLocation::cur
     dbgln_if(CSS_PARSER_DEBUG, "Parse error (CSS) {}", location);
 }
 
-namespace Web::CSS {
+namespace Web::CSS::Parser {
 
 ParsingContext::ParsingContext(DOM::Document const& document, Optional<AK::URL> const url)
     : m_document(&document)
@@ -5741,8 +5741,8 @@ RefPtr<StyleValue> Parser::parse_css_value(Badge<StyleComputer>, ParsingContext
     if (tokens.is_empty() || property_id == CSS::PropertyID::Invalid || property_id == CSS::PropertyID::Custom)
         return {};
 
-    CSS::Parser parser(context, "");
-    CSS::TokenStream<CSS::ComponentValue> token_stream { tokens };
+    Parser parser(context, "");
+    TokenStream<ComponentValue> token_stream { tokens };
     auto result = parser.parse_css_value(property_id, token_stream);
     if (result.is_error())
         return {};
@@ -5868,59 +5868,59 @@ TimePercentage Parser::Dimension::time_percentage() const
 
 namespace Web {
 
-RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const& context, StringView css, Optional<AK::URL> location)
+RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::Parser::ParsingContext const& context, StringView css, Optional<AK::URL> location)
 {
     if (css.is_empty())
         return CSS::CSSStyleSheet::create({}, location);
-    CSS::Parser parser(context, css);
+    CSS::Parser::Parser parser(context, css);
     return parser.parse_as_css_stylesheet(location);
 }
 
-RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const& context, StringView css, DOM::Element& element)
+RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::Parser::ParsingContext const& context, StringView css, DOM::Element& element)
 {
     if (css.is_empty())
         return CSS::ElementInlineCSSStyleDeclaration::create(element, {}, {});
-    CSS::Parser parser(context, css);
+    CSS::Parser::Parser parser(context, css);
     return parser.parse_as_style_attribute(element);
 }
 
-RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const& context, StringView string, CSS::PropertyID property_id)
+RefPtr<CSS::StyleValue> parse_css_value(CSS::Parser::ParsingContext const& context, StringView string, CSS::PropertyID property_id)
 {
     if (string.is_empty())
         return {};
-    CSS::Parser parser(context, string);
+    CSS::Parser::Parser parser(context, string);
     return parser.parse_as_css_value(property_id);
 }
 
-RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const& context, StringView css_text)
+RefPtr<CSS::CSSRule> parse_css_rule(CSS::Parser::ParsingContext const& context, StringView css_text)
 {
-    CSS::Parser parser(context, css_text);
+    CSS::Parser::Parser parser(context, css_text);
     return parser.parse_as_css_rule();
 }
 
-Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const& context, StringView selector_text)
+Optional<CSS::SelectorList> parse_selector(CSS::Parser::ParsingContext const& context, StringView selector_text)
 {
-    CSS::Parser parser(context, selector_text);
+    CSS::Parser::Parser parser(context, selector_text);
     return parser.parse_as_selector();
 }
 
-RefPtr<CSS::MediaQuery> parse_media_query(CSS::ParsingContext const& context, StringView string)
+RefPtr<CSS::MediaQuery> parse_media_query(CSS::Parser::ParsingContext const& context, StringView string)
 {
-    CSS::Parser parser(context, string);
+    CSS::Parser::Parser parser(context, string);
     return parser.parse_as_media_query();
 }
 
-NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::ParsingContext const& context, StringView string)
+NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::Parser::ParsingContext const& context, StringView string)
 {
-    CSS::Parser parser(context, string);
+    CSS::Parser::Parser parser(context, string);
     return parser.parse_as_media_query_list();
 }
 
-RefPtr<CSS::Supports> parse_css_supports(CSS::ParsingContext const& context, StringView string)
+RefPtr<CSS::Supports> parse_css_supports(CSS::Parser::ParsingContext const& context, StringView string)
 {
     if (string.is_empty())
         return {};
-    CSS::Parser parser(context, string);
+    CSS::Parser::Parser parser(context, string);
     return parser.parse_as_supports();
 }
 

+ 9 - 9
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -30,7 +30,7 @@
 #include <LibWeb/CSS/UnicodeRange.h>
 #include <LibWeb/Forward.h>
 
-namespace Web::CSS {
+namespace Web::CSS::Parser {
 
 class ParsingContext {
 public:
@@ -375,13 +375,13 @@ private:
 
 namespace Web {
 
-RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const&, StringView, Optional<AK::URL> location = {});
-RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const&, StringView, DOM::Element&);
-RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
-Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView);
-RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const&, StringView);
-RefPtr<CSS::MediaQuery> parse_media_query(CSS::ParsingContext const&, StringView);
-NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::ParsingContext const&, StringView);
-RefPtr<CSS::Supports> parse_css_supports(CSS::ParsingContext const&, StringView);
+RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::Parser::ParsingContext const&, StringView, Optional<AK::URL> location = {});
+RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::Parser::ParsingContext const&, StringView, DOM::Element&);
+RefPtr<CSS::StyleValue> parse_css_value(CSS::Parser::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
+Optional<CSS::SelectorList> parse_selector(CSS::Parser::ParsingContext const&, StringView);
+RefPtr<CSS::CSSRule> parse_css_rule(CSS::Parser::ParsingContext const&, StringView);
+RefPtr<CSS::MediaQuery> parse_media_query(CSS::Parser::ParsingContext const&, StringView);
+NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::Parser::ParsingContext const&, StringView);
+RefPtr<CSS::Supports> parse_css_supports(CSS::Parser::ParsingContext const&, StringView);
 
 }

+ 2 - 1
Userland/Libraries/LibWeb/CSS/Parser/StyleBlockRule.h

@@ -11,11 +11,12 @@
 #include <AK/Vector.h>
 #include <LibWeb/CSS/Parser/ComponentValue.h>
 #include <LibWeb/CSS/Parser/Token.h>
+#include <LibWeb/Forward.h>
 
 namespace Web::CSS {
 
 class StyleBlockRule : public RefCounted<StyleBlockRule> {
-    friend class Parser;
+    friend class Parser::Parser;
 
 public:
     StyleBlockRule();

+ 2 - 3
Userland/Libraries/LibWeb/CSS/Parser/StyleFunctionRule.h

@@ -11,13 +11,12 @@
 #include <AK/String.h>
 #include <AK/Vector.h>
 #include <LibWeb/CSS/Parser/ComponentValue.h>
+#include <LibWeb/Forward.h>
 
 namespace Web::CSS {
 
-class ComponentValue;
-
 class StyleFunctionRule : public RefCounted<StyleFunctionRule> {
-    friend class Parser;
+    friend class Parser::Parser;
 
 public:
     explicit StyleFunctionRule(String name);

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Parser/StyleRule.h

@@ -15,7 +15,7 @@
 namespace Web::CSS {
 
 class StyleRule : public RefCounted<StyleRule> {
-    friend class Parser;
+    friend class Parser::Parser;
 
 public:
     enum class Type {

+ 3 - 3
Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -114,7 +114,7 @@ static StyleSheet& default_stylesheet()
     if (!sheet) {
         extern char const default_stylesheet_source[];
         String css = default_stylesheet_source;
-        sheet = parse_css_stylesheet(CSS::ParsingContext(), css).leak_ref();
+        sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(), css).leak_ref();
     }
     return *sheet;
 }
@@ -125,7 +125,7 @@ static StyleSheet& quirks_mode_stylesheet()
     if (!sheet) {
         extern char const quirks_mode_stylesheet_source[];
         String css = quirks_mode_stylesheet_source;
-        sheet = parse_css_stylesheet(CSS::ParsingContext(), css).leak_ref();
+        sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(), css).leak_ref();
     }
     return *sheet;
 }
@@ -652,7 +652,7 @@ RefPtr<StyleValue> StyleComputer::resolve_unresolved_style_value(DOM::Element& e
     if (!expand_unresolved_values(element, string_from_property_id(property_id), dependencies, unresolved.values(), expanded_values, 0))
         return {};
 
-    if (auto parsed_value = Parser::parse_css_value({}, ParsingContext { document() }, property_id, expanded_values))
+    if (auto parsed_value = Parser::Parser::parse_css_value({}, Parser::ParsingContext { document() }, property_id, expanded_values))
         return parsed_value.release_nonnull();
 
     return {};

+ 2 - 2
Userland/Libraries/LibWeb/CSS/Supports.cpp

@@ -52,13 +52,13 @@ bool Supports::InParens::evaluate() const
 
 bool Supports::Declaration::evaluate() const
 {
-    auto style_property = Parser({}, declaration).parse_as_supports_condition();
+    auto style_property = Parser::Parser({}, declaration).parse_as_supports_condition();
     return style_property.has_value();
 }
 
 bool Supports::Selector::evaluate() const
 {
-    auto style_property = Parser({}, selector).parse_as_selector();
+    auto style_property = Parser::Parser({}, selector).parse_as_selector();
     return style_property.has_value();
 }
 

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Supports.h

@@ -18,7 +18,7 @@ namespace Web::CSS {
 
 // https://www.w3.org/TR/css-conditional-4/#at-supports
 class Supports final : public RefCounted<Supports> {
-    friend class Parser;
+    friend class Parser::Parser;
 
 public:
     struct Declaration {

+ 3 - 3
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -313,7 +313,7 @@ void Element::parse_attribute(FlyString const& name, String const& value)
         // https://drafts.csswg.org/cssom/#ref-for-cssstyledeclaration-updating-flag
         if (m_inline_style && m_inline_style->is_updating())
             return;
-        m_inline_style = parse_css_style_attribute(CSS::ParsingContext(document()), value, *this);
+        m_inline_style = parse_css_style_attribute(CSS::Parser::ParsingContext(document()), value, *this);
         set_needs_style_update(true);
     }
 }
@@ -422,7 +422,7 @@ RefPtr<DOMTokenList> const& Element::class_list()
 // https://dom.spec.whatwg.org/#dom-element-matches
 DOM::ExceptionOr<bool> Element::matches(StringView selectors) const
 {
-    auto maybe_selectors = parse_selector(CSS::ParsingContext(static_cast<ParentNode&>(const_cast<Element&>(*this))), selectors);
+    auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(static_cast<ParentNode&>(const_cast<Element&>(*this))), selectors);
     if (!maybe_selectors.has_value())
         return DOM::SyntaxError::create("Failed to parse selector");
 
@@ -437,7 +437,7 @@ DOM::ExceptionOr<bool> Element::matches(StringView selectors) const
 // https://dom.spec.whatwg.org/#dom-element-closest
 DOM::ExceptionOr<DOM::Element const*> Element::closest(StringView selectors) const
 {
-    auto maybe_selectors = parse_selector(CSS::ParsingContext(static_cast<ParentNode&>(const_cast<Element&>(*this))), selectors);
+    auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(static_cast<ParentNode&>(const_cast<Element&>(*this))), selectors);
     if (!maybe_selectors.has_value())
         return DOM::SyntaxError::create("Failed to parse selector");
 

+ 2 - 2
Userland/Libraries/LibWeb/DOM/ParentNode.cpp

@@ -17,7 +17,7 @@ namespace Web::DOM {
 
 ExceptionOr<RefPtr<Element>> ParentNode::query_selector(StringView selector_text)
 {
-    auto maybe_selectors = parse_selector(CSS::ParsingContext(*this), selector_text);
+    auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
     if (!maybe_selectors.has_value())
         return DOM::SyntaxError::create("Failed to parse selector");
 
@@ -40,7 +40,7 @@ ExceptionOr<RefPtr<Element>> ParentNode::query_selector(StringView selector_text
 
 ExceptionOr<NonnullRefPtr<NodeList>> ParentNode::query_selector_all(StringView selector_text)
 {
-    auto maybe_selectors = parse_selector(CSS::ParsingContext(*this), selector_text);
+    auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
     if (!maybe_selectors.has_value())
         return DOM::SyntaxError::create("Failed to parse selector");
 

+ 4 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -98,6 +98,10 @@ enum class PropertyID;
 enum class ValueID;
 }
 
+namespace Web::CSS::Parser {
+class Parser;
+}
+
 namespace Web::DOM {
 class AbstractRange;
 class AbortController;

+ 1 - 1
Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp

@@ -126,7 +126,7 @@ void HTMLLinkElement::resource_did_load_stylesheet()
         }
     }
 
-    auto sheet = parse_css_stylesheet(CSS::ParsingContext(document(), resource()->url()), resource()->encoded_data());
+    auto sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(document(), resource()->url()), resource()->encoded_data());
     if (!sheet) {
         dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url());
         return;

+ 1 - 1
Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp

@@ -126,7 +126,7 @@ void HTMLStyleElement::update_a_style_block()
 
     // FIXME: This is a bit awkward, as the spec doesn't actually tell us when to parse the CSS text,
     //        so we just do it here and pass the parsed sheet to create_a_css_style_sheet().
-    auto sheet = parse_css_stylesheet(CSS::ParsingContext(document()), text_content());
+    auto sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(document()), text_content());
     if (!sheet)
         return;
 

+ 1 - 2
Userland/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp

@@ -30,8 +30,7 @@ void HTMLTableCellElement::apply_presentational_hints(CSS::StyleProperties& styl
             if (value.equals_ignoring_case("center"sv) || value.equals_ignoring_case("middle"sv)) {
                 style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::LibwebCenter));
             } else {
-                CSS::Parser parser(CSS::ParsingContext(document()), value.view());
-                if (auto parsed_value = parser.parse_as_css_value(CSS::PropertyID::TextAlign))
+                if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value.view(), CSS::PropertyID::TextAlign))
                     style.set_property(CSS::PropertyID::TextAlign, parsed_value.release_nonnull());
             }
             return;

+ 1 - 1
Userland/Libraries/LibWeb/HTML/Window.cpp

@@ -390,7 +390,7 @@ NonnullRefPtr<CSS::CSSStyleDeclaration> Window::get_computed_style(DOM::Element&
 
 NonnullRefPtr<CSS::MediaQueryList> Window::match_media(String media)
 {
-    auto media_query_list = CSS::MediaQueryList::create(associated_document(), parse_media_query_list(CSS::ParsingContext(associated_document()), media));
+    auto media_query_list = CSS::MediaQueryList::create(associated_document(), parse_media_query_list(CSS::Parser::ParsingContext(associated_document()), media));
     associated_document().add_media_query_list(media_query_list);
     return media_query_list;
 }

+ 1 - 1
Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp

@@ -19,7 +19,7 @@ SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, DOM::QualifiedNa
 
 void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) const
 {
-    CSS::ParsingContext parsing_context { document() };
+    CSS::Parser::ParsingContext parsing_context { document() };
     for_each_attribute([&](auto& name, auto& value) {
         if (name.equals_ignoring_case("fill")) {
             // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now.