Bläddra i källkod

LibWeb: Resolve cyclic dependency between StyleSheet and ImportRule

Previously: CSSImportRule::loaded_style_sheet() (and others) depend on
the definition of class CSSStyleSheet. Meanwhile,
CSSStyleSheet::template for_each_effective_style_rule (and others)
depend on the definition of class CSSImportRule.

This hasn't caused any problems so far because CSSStyleSheet.h happened
to be always included after CSSImportRule.h (in part due to alphabetical
ordering).

However, a compilation unit that (for example) only contains
    #include <Userland/Libraries/LibWeb/CSSImportRule.h>
would fail to compile.

This patch resolves this issue by pushing the inline definition of
Web::CSS::CSSStyleSheet::for_each_effective_style_rule and
for_first_not_loaded_import_rule into a different file, and adding the
missing headers.
Ben Wiederhake 3 år sedan
förälder
incheckning
0db6ca4065

+ 1 - 0
Userland/Libraries/LibWeb/CSS/CSSImportRule.h

@@ -8,6 +8,7 @@
 
 #include <AK/URL.h>
 #include <LibWeb/CSS/CSSRule.h>
+#include <LibWeb/CSS/CSSStyleSheet.h>
 
 namespace Web::CSS {
 

+ 31 - 0
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp

@@ -4,6 +4,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <LibWeb/CSS/CSSImportRule.h>
 #include <LibWeb/CSS/CSSStyleSheet.h>
 #include <LibWeb/CSS/Parser/Parser.h>
 #include <LibWeb/DOM/ExceptionOr.h>
@@ -57,4 +58,34 @@ DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
     return delete_rule(index);
 }
 
+void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
+{
+    for (auto& rule : *m_rules)
+        if (rule.type() == CSSRule::Type::Style) {
+            callback(verify_cast<CSSStyleRule>(rule));
+        } else if (rule.type() == CSSRule::Type::Import) {
+            const auto& import_rule = verify_cast<CSSImportRule>(rule);
+            if (import_rule.has_import_result())
+                import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
+        }
+}
+
+bool CSSStyleSheet::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback)
+{
+    for (auto& rule : *m_rules)
+        if (rule.type() == CSSRule::Type::Import) {
+            auto& import_rule = verify_cast<CSSImportRule>(rule);
+            if (!import_rule.has_import_result()) {
+                callback(import_rule);
+                return true;
+            }
+
+            if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
+                return true;
+            }
+        }
+
+    return false;
+}
+
 }

+ 6 - 32
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h

@@ -6,16 +6,19 @@
 
 #pragma once
 
+#include <AK/Function.h>
 #include <AK/NonnullRefPtrVector.h>
 #include <AK/TypeCasts.h>
-#include <LibWeb/CSS/CSSImportRule.h>
 #include <LibWeb/CSS/CSSRule.h>
 #include <LibWeb/CSS/CSSRuleList.h>
+#include <LibWeb/CSS/CSSStyleRule.h>
 #include <LibWeb/CSS/StyleSheet.h>
 #include <LibWeb/Loader/Resource.h>
 
 namespace Web::CSS {
 
+class CSSImportRule;
+
 class CSSStyleSheet final : public StyleSheet {
 public:
     using WrapperType = Bindings::CSSStyleSheetWrapper;
@@ -42,37 +45,8 @@ public:
     DOM::ExceptionOr<void> remove_rule(unsigned index);
     DOM::ExceptionOr<void> delete_rule(unsigned index);
 
-    template<typename Callback>
-    void for_each_effective_style_rule(Callback callback) const
-    {
-        for (auto& rule : *m_rules)
-            if (rule.type() == CSSRule::Type::Style) {
-                callback(verify_cast<CSSStyleRule>(rule));
-            } else if (rule.type() == CSSRule::Type::Import) {
-                const auto& import_rule = verify_cast<CSSImportRule>(rule);
-                if (import_rule.has_import_result())
-                    import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
-            }
-    }
-
-    template<typename Callback>
-    bool for_first_not_loaded_import_rule(Callback callback)
-    {
-        for (auto& rule : *m_rules)
-            if (rule.type() == CSSRule::Type::Import) {
-                auto& import_rule = verify_cast<CSSImportRule>(rule);
-                if (!import_rule.has_import_result()) {
-                    callback(import_rule);
-                    return true;
-                }
-
-                if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
-                    return true;
-                }
-            }
-
-        return false;
-    }
+    void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
+    bool for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback);
 
 private:
     explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>);

+ 1 - 0
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -11,6 +11,7 @@
 #include <AK/Debug.h>
 #include <AK/NonnullRefPtrVector.h>
 #include <AK/SourceLocation.h>
+#include <LibWeb/CSS/CSSImportRule.h>
 #include <LibWeb/CSS/CSSMediaRule.h>
 #include <LibWeb/CSS/CSSStyleDeclaration.h>
 #include <LibWeb/CSS/CSSStyleRule.h>

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

@@ -77,7 +77,7 @@ Vector<MatchingRule> StyleComputer::collect_matching_rules(DOM::Element const& e
     size_t style_sheet_index = 0;
     for_each_stylesheet(declaration_type, [&](auto& sheet) {
         size_t rule_index = 0;
-        static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto& rule) {
+        static_cast<CSSStyleSheet const&>(sheet).for_each_effective_style_rule([&](auto const& rule) {
             size_t selector_index = 0;
             for (auto& selector : rule.selectors()) {
                 if (SelectorEngine::matches(selector, element)) {

+ 1 - 0
Userland/Libraries/LibWeb/Loader/CSSLoader.cpp

@@ -6,6 +6,7 @@
 
 #include <AK/Debug.h>
 #include <AK/URL.h>
+#include <LibWeb/CSS/CSSImportRule.h>
 #include <LibWeb/CSS/Parser/Parser.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Element.h>