diff options
-rw-r--r-- | Userland/Libraries/LibWeb/CMakeLists.txt | 1 | ||||
-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 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Dump.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Forward.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.h | 6 |
17 files changed, 162 insertions, 82 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 23d18de374..795c833c16 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -12,6 +12,7 @@ set(SOURCES CSS/CSSImportRule.cpp CSS/CSSRule.cpp CSS/CSSStyleRule.cpp + CSS/CSSStyleSheet.cpp CSS/DefaultStyleSheetSource.cpp CSS/Length.cpp CSS/Parser/CSSParser.cpp 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 { diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index b124965d9e..47f097075b 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -37,8 +37,8 @@ #include <LibJS/Forward.h> #include <LibWeb/Bindings/ScriptExecutionContext.h> #include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/CSS/CSSStyleSheet.h> #include <LibWeb/CSS/StyleResolver.h> -#include <LibWeb/CSS/StyleSheet.h> #include <LibWeb/CSS/StyleSheetList.h> #include <LibWeb/DOM/DOMImplementation.h> #include <LibWeb/DOM/ExceptionOr.h> diff --git a/Userland/Libraries/LibWeb/Dump.cpp b/Userland/Libraries/LibWeb/Dump.cpp index f844a9a329..1be93503a7 100644 --- a/Userland/Libraries/LibWeb/Dump.cpp +++ b/Userland/Libraries/LibWeb/Dump.cpp @@ -31,8 +31,8 @@ #include <LibWeb/CSS/CSSImportRule.h> #include <LibWeb/CSS/CSSRule.h> #include <LibWeb/CSS/CSSStyleRule.h> +#include <LibWeb/CSS/CSSStyleSheet.h> #include <LibWeb/CSS/PropertyID.h> -#include <LibWeb/CSS/StyleSheet.h> #include <LibWeb/DOM/Comment.h> #include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Element.h> @@ -440,9 +440,11 @@ void dump_sheet(const CSS::StyleSheet& sheet) void dump_sheet(StringBuilder& builder, const CSS::StyleSheet& sheet) { - builder.appendff("StyleSheet{{{}}}: {} rule(s)\n", &sheet, sheet.rules().size()); + VERIFY(is<CSS::CSSStyleSheet>(sheet)); - for (auto& rule : sheet.rules()) { + builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, static_cast<const CSS::CSSStyleSheet&>(sheet).rules().size()); + + for (auto& rule : static_cast<const CSS::CSSStyleSheet&>(sheet).rules()) { dump_rule(builder, rule); } } diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index b5ccb3712d..20585682e8 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -31,6 +31,7 @@ namespace Web::CSS { class CSSRule; class CSSImportRule; class CSSStyleRule; +class CSSStyleSheet; class Length; class Selector; class StyleDeclaration; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp index 4560bdadb8..0a0bb989f7 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp @@ -25,7 +25,6 @@ */ #include <LibGfx/Bitmap.h> -#include <LibGfx/ImageDecoder.h> #include <LibWeb/CSS/Parser/CSSParser.h> #include <LibWeb/CSS/StyleResolver.h> #include <LibWeb/DOM/Document.h> diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp index f73465b60a..ec49234d30 100644 --- a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp @@ -43,14 +43,14 @@ void CSSLoader::load_from_text(const String& text) { m_style_sheet = parse_css(CSS::ParsingContext(*m_document), text); if (!m_style_sheet) - m_style_sheet = CSS::StyleSheet::create({}); + m_style_sheet = CSS::CSSStyleSheet::create({}); load_next_import_if_needed(); } void CSSLoader::load_from_url(const URL& url) { - m_style_sheet = CSS::StyleSheet::create({}); + m_style_sheet = CSS::CSSStyleSheet::create({}); LoadRequest request; request.set_url(url); diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.h b/Userland/Libraries/LibWeb/Loader/CSSLoader.h index 0263182938..01eb265433 100644 --- a/Userland/Libraries/LibWeb/Loader/CSSLoader.h +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.h @@ -27,7 +27,7 @@ #pragma once #include <AK/Function.h> -#include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/CSS/CSSStyleSheet.h> #include <LibWeb/Loader/Resource.h> namespace Web { @@ -41,7 +41,7 @@ public: void load_next_import_if_needed(); - RefPtr<CSS::StyleSheet> style_sheet() const { return m_style_sheet; }; + RefPtr<CSS::CSSStyleSheet> style_sheet() const { return m_style_sheet; }; Function<void()> on_load; Function<void()> on_fail; @@ -51,7 +51,7 @@ private: virtual void resource_did_load() override; virtual void resource_did_fail() override; - RefPtr<CSS::StyleSheet> m_style_sheet; + RefPtr<CSS::CSSStyleSheet> m_style_sheet; const DOM::Document* m_document; }; |