diff options
author | Andreas Kling <kling@serenityos.org> | 2021-03-07 16:14:04 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-07 17:45:15 +0100 |
commit | fefb05f6f3caa7b531c3981fb1c539e9396f74d4 (patch) | |
tree | 4e44e2c3ace9818d0c354da2cd96380388709d9c /Userland/Libraries/LibWeb/CSS | |
parent | 0af476266200a707770959173c9e7e2028235119 (diff) | |
download | serenity-fefb05f6f3caa7b531c3981fb1c539e9396f74d4.zip |
LibWeb: Split CSS::StyleSheet into StyleSheet and CSSStyleSheet
This is a little convoluted but matches the CSSOM specification.
Diffstat (limited to 'Userland/Libraries/LibWeb/CSS')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSImportRule.h | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp | 40 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h | 88 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/CSSParser.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/CSSParser.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleResolver.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleSheet.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleSheet.h | 54 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleSheetList.h | 2 |
10 files changed, 149 insertions, 72 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp index 0bdf55000d..e8b05b864d 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp @@ -26,7 +26,7 @@ #include <AK/URL.h> #include <LibWeb/CSS/CSSImportRule.h> -#include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/CSS/CSSStyleSheet.h> namespace Web::CSS { diff --git a/Userland/Libraries/LibWeb/CSS/CSSImportRule.h b/Userland/Libraries/LibWeb/CSS/CSSImportRule.h index 0fc32e60f3..2c752daab7 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSImportRule.h +++ b/Userland/Libraries/LibWeb/CSS/CSSImportRule.h @@ -46,18 +46,18 @@ public: const URL& url() const { return m_url; } bool has_import_result() const { return !m_style_sheet.is_null(); } - RefPtr<StyleSheet> loaded_style_sheet() { return m_style_sheet; } - const RefPtr<StyleSheet> loaded_style_sheet() const { return m_style_sheet; } + RefPtr<CSSStyleSheet> loaded_style_sheet() { return m_style_sheet; } + const RefPtr<CSSStyleSheet> loaded_style_sheet() const { return m_style_sheet; } void set_style_sheet(const RefPtr<StyleSheet>& style_sheet) { m_style_sheet = style_sheet; } virtual StringView class_name() const { return "CSSImportRule"; }; virtual Type type() const { return Type::Import; }; private: - CSSImportRule(URL); + explicit CSSImportRule(URL); URL m_url; - RefPtr<StyleSheet> m_style_sheet; + RefPtr<CSSStyleSheet> m_style_sheet; }; } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp new file mode 100644 index 0000000000..01bae6ee67 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <LibWeb/CSS/CSSStyleSheet.h> + +namespace Web::CSS { + +CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules) + : m_rules(move(rules)) +{ +} + +CSSStyleSheet::~CSSStyleSheet() +{ +} + +} diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h new file mode 100644 index 0000000000..a1b3e9f784 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/NonnullRefPtrVector.h> +#include <AK/TypeCasts.h> +#include <LibWeb/CSS/CSSImportRule.h> +#include <LibWeb/CSS/CSSRule.h> +#include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/Loader/Resource.h> + +namespace Web::CSS { + +class CSSStyleSheet final : public StyleSheet { +public: + static NonnullRefPtr<CSSStyleSheet> create(NonnullRefPtrVector<CSSRule> rules) + { + return adopt(*new CSSStyleSheet(move(rules))); + } + + virtual ~CSSStyleSheet(); + + const NonnullRefPtrVector<CSSRule>& rules() const { return m_rules; } + NonnullRefPtrVector<CSSRule>& rules() { return m_rules; } + + template<typename Callback> + void for_each_effective_style_rule(Callback callback) const + { + for (auto& rule : m_rules) + if (rule.type() == CSSRule::Type::Style) { + callback(downcast<CSSStyleRule>(rule)); + } else if (rule.type() == CSSRule::Type::Import) { + const auto& import_rule = downcast<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 = downcast<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; + } + +private: + explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>); + + NonnullRefPtrVector<CSSRule> m_rules; +}; + +} diff --git a/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.cpp index 57966db36b..ba7b7815f8 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.cpp @@ -934,7 +934,7 @@ public: consume_whitespace_or_comments(); } - RefPtr<CSS::StyleSheet> parse_sheet() + RefPtr<CSS::CSSStyleSheet> parse_sheet() { if (peek(0) == (char)0xef && peek(1) == (char)0xbb && peek(2) == (char)0xbf) { // HACK: Skip UTF-8 BOM. @@ -945,7 +945,7 @@ public: parse_rule(); } - return CSS::StyleSheet::create(move(rules)); + return CSS::CSSStyleSheet::create(move(rules)); } RefPtr<CSS::StyleDeclaration> parse_standalone_declaration() @@ -986,10 +986,10 @@ Optional<CSS::Selector> parse_selector(const CSS::ParsingContext& context, const return parser.parse_individual_selector(); } -RefPtr<CSS::StyleSheet> parse_css(const CSS::ParsingContext& context, const StringView& css) +RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext& context, const StringView& css) { if (css.is_empty()) - return CSS::StyleSheet::create({}); + return CSS::CSSStyleSheet::create({}); CSSParser parser(context, css); return parser.parse_sheet(); } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.h b/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.h index 80421fce8e..bf1da4229e 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/CSSParser.h @@ -28,7 +28,7 @@ #include <AK/NonnullRefPtr.h> #include <AK/String.h> -#include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/CSS/CSSStyleSheet.h> namespace Web::CSS { class ParsingContext { @@ -48,7 +48,7 @@ private: namespace Web { -RefPtr<CSS::StyleSheet> parse_css(const CSS::ParsingContext&, const StringView&); +RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext&, const StringView&); RefPtr<CSS::StyleDeclaration> parse_css_declaration(const CSS::ParsingContext&, const StringView&); RefPtr<CSS::StyleValue> parse_css_value(const CSS::ParsingContext&, const StringView&, CSS::PropertyID property_id = CSS::PropertyID::Invalid); Optional<CSS::Selector> parse_selector(const CSS::ParsingContext&, const StringView&); diff --git a/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp b/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp index 520108d9e7..f3ba206ac4 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleResolver.cpp @@ -87,8 +87,10 @@ Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& e size_t style_sheet_index = 0; for_each_stylesheet([&](auto& sheet) { + if (!is<CSSStyleSheet>(sheet)) + return; size_t rule_index = 0; - sheet.for_each_effective_style_rule([&](auto& rule) { + static_cast<const CSSStyleSheet&>(sheet).for_each_effective_style_rule([&](auto& rule) { size_t selector_index = 0; for (auto& selector : rule.selectors()) { if (SelectorEngine::matches(selector, element)) { @@ -100,7 +102,7 @@ Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& e ++rule_index; }); ++style_sheet_index; - }); + }); return matching_rules; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp index fd664327b7..964f3badcf 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp @@ -29,13 +29,4 @@ namespace Web::CSS { -StyleSheet::StyleSheet(NonnullRefPtrVector<CSSRule>&& rules) - : m_rules(move(rules)) -{ -} - -StyleSheet::~StyleSheet() -{ -} - } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.h b/Userland/Libraries/LibWeb/CSS/StyleSheet.h index 31004a4e35..1bca84210c 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.h @@ -27,62 +27,16 @@ #pragma once -#include <AK/NonnullRefPtrVector.h> -#include <AK/TypeCasts.h> -#include <LibWeb/CSS/CSSImportRule.h> -#include <LibWeb/CSS/CSSRule.h> -#include <LibWeb/Loader/Resource.h> +#include <AK/RefCounted.h> namespace Web::CSS { class StyleSheet : public RefCounted<StyleSheet> { public: - static NonnullRefPtr<StyleSheet> create(NonnullRefPtrVector<CSSRule>&& rules) - { - return adopt(*new StyleSheet(move(rules))); - } + virtual ~StyleSheet() = default; - ~StyleSheet(); - - const NonnullRefPtrVector<CSSRule>& rules() const { return m_rules; } - NonnullRefPtrVector<CSSRule>& rules() { return m_rules; } - - template<typename Callback> - void for_each_effective_style_rule(Callback callback) const - { - for (auto& rule : m_rules) - if (rule.type() == CSSRule::Type::Style) { - callback(downcast<CSSStyleRule>(rule)); - } else if (rule.type() == CSSRule::Type::Import) { - const CSSImportRule& import_rule = downcast<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) { - CSSImportRule& import_rule = downcast<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; - } - -private: - explicit StyleSheet(NonnullRefPtrVector<CSSRule>&&); - - NonnullRefPtrVector<CSSRule> m_rules; +protected: + StyleSheet() = default; }; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.h b/Userland/Libraries/LibWeb/CSS/StyleSheetList.h index 636e030f39..df2b993812 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheetList.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.h @@ -26,8 +26,10 @@ #pragma once +#include <AK/NonnullRefPtrVector.h> #include <AK/RefCounted.h> #include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/Forward.h> namespace Web::CSS { |