From 0db6ca406547eb2f4fb6d584d4e17733ebaa02e8 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Thu, 30 Sep 2021 22:57:35 +0200 Subject: [PATCH] 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 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. --- Userland/Libraries/LibWeb/CSS/CSSImportRule.h | 1 + .../Libraries/LibWeb/CSS/CSSStyleSheet.cpp | 31 +++++++++++++++ Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h | 38 +++---------------- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 1 + .../Libraries/LibWeb/CSS/StyleComputer.cpp | 2 +- .../Libraries/LibWeb/Loader/CSSLoader.cpp | 1 + 6 files changed, 41 insertions(+), 33 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/CSSImportRule.h b/Userland/Libraries/LibWeb/CSS/CSSImportRule.h index bff07f2acb..f633701037 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSImportRule.h +++ b/Userland/Libraries/LibWeb/CSS/CSSImportRule.h @@ -8,6 +8,7 @@ #include #include +#include namespace Web::CSS { diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index b85bbe13ab..d4a3dcf065 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -57,4 +58,34 @@ DOM::ExceptionOr CSSStyleSheet::remove_rule(unsigned index) return delete_rule(index); } +void CSSStyleSheet::for_each_effective_style_rule(Function const& callback) const +{ + for (auto& rule : *m_rules) + if (rule.type() == CSSRule::Type::Style) { + callback(verify_cast(rule)); + } else if (rule.type() == CSSRule::Type::Import) { + const auto& import_rule = verify_cast(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 const& callback) +{ + for (auto& rule : *m_rules) + if (rule.type() == CSSRule::Type::Import) { + auto& import_rule = verify_cast(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; +} + } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index 0528ffb49a..576ee76fa0 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -6,16 +6,19 @@ #pragma once +#include #include #include -#include #include #include +#include #include #include namespace Web::CSS { +class CSSImportRule; + class CSSStyleSheet final : public StyleSheet { public: using WrapperType = Bindings::CSSStyleSheetWrapper; @@ -42,37 +45,8 @@ public: DOM::ExceptionOr remove_rule(unsigned index); DOM::ExceptionOr delete_rule(unsigned index); - template - void for_each_effective_style_rule(Callback callback) const - { - for (auto& rule : *m_rules) - if (rule.type() == CSSRule::Type::Style) { - callback(verify_cast(rule)); - } else if (rule.type() == CSSRule::Type::Import) { - const auto& import_rule = verify_cast(rule); - if (import_rule.has_import_result()) - import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback); - } - } - - template - 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(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 const& callback) const; + bool for_first_not_loaded_import_rule(Function const& callback); private: explicit CSSStyleSheet(NonnullRefPtrVector); diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 1ef678fd5d..cebf6816e3 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 994915fcb4..eb3ca4eafd 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -77,7 +77,7 @@ Vector 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(sheet).for_each_effective_style_rule([&](auto& rule) { + static_cast(sheet).for_each_effective_style_rule([&](auto const& rule) { size_t selector_index = 0; for (auto& selector : rule.selectors()) { if (SelectorEngine::matches(selector, element)) { diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp index 418ae70276..89a56d48da 100644 --- a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include