diff options
author | Sviatoslav Peleshko <speles@mail.ua> | 2021-02-21 18:44:17 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-28 10:27:32 +0100 |
commit | 183ebaee91a6a5b7ca2b8a91395e562070630c48 (patch) | |
tree | 7509b85619e44dcbe7d6330b3e4fc19c52a9e7a4 /Userland/Libraries | |
parent | 54617e1a9109b28b00599a247de2f9607152c129 (diff) | |
download | serenity-183ebaee91a6a5b7ca2b8a91395e562070630c48.zip |
LibWeb: Add actual document loading for the CSS (at)import rule
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSImportRule.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleSheet.h | 24 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp | 45 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h | 15 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLStyleElement.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.cpp | 111 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.h | 58 |
9 files changed, 224 insertions, 56 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index d9590baf0f..a1aed67271 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -176,6 +176,7 @@ set(SOURCES Layout/TreeBuilder.cpp LayoutTreeModel.cpp Loader/ContentFilter.cpp + Loader/CSSLoader.cpp Loader/FrameLoader.cpp Loader/ImageLoader.cpp Loader/ImageResource.cpp diff --git a/Userland/Libraries/LibWeb/CSS/CSSImportRule.h b/Userland/Libraries/LibWeb/CSS/CSSImportRule.h index 7607239528..0fc32e60f3 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSImportRule.h +++ b/Userland/Libraries/LibWeb/CSS/CSSImportRule.h @@ -45,6 +45,11 @@ 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; } + 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; }; @@ -52,6 +57,7 @@ private: CSSImportRule(URL); URL m_url; + RefPtr<StyleSheet> m_style_sheet; }; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.h b/Userland/Libraries/LibWeb/CSS/StyleSheet.h index 146a6a172d..a6ee3a1013 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.h @@ -29,6 +29,7 @@ #include <AK/NonnullRefPtrVector.h> #include <AK/TypeCasts.h> +#include <LibWeb/CSS/CSSImportRule.h> #include <LibWeb/CSS/CSSRule.h> #include <LibWeb/Loader/Resource.h> @@ -52,9 +53,32 @@ public: for (auto& rule : m_rules) if (rule.type() == CSSRule::Type::Style) { callback(downcast<StyleRule>(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>&&); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp index 8fcf9edaff..bf2d45e1d6 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,7 +36,11 @@ namespace Web::HTML { HTMLLinkElement::HTMLLinkElement(DOM::Document& document, QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) + , m_css_loader(document) { + m_css_loader.on_load = [&] { + document.update_style(); + }; } HTMLLinkElement::~HTMLLinkElement() @@ -46,44 +51,10 @@ void HTMLLinkElement::inserted_into(Node& node) { HTMLElement::inserted_into(node); - if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) - load_stylesheet(document().complete_url(href())); -} - -void HTMLLinkElement::resource_did_fail() -{ -} - -void HTMLLinkElement::resource_did_load() -{ - VERIFY(resource()); - if (!resource()->has_encoded_data()) - return; - - dbgln("HTMLLinkElement: Resource did load, looks good! {}", href()); - - auto sheet = parse_css(CSS::ParsingContext(document()), resource()->encoded_data()); - if (!sheet) { - dbgln("HTMLLinkElement: Failed to parse stylesheet: {}", href()); - return; + if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) { + m_css_loader.load_from_url(document().complete_url(href())); + document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull()); } - - // Transfer the rules from the successfully parsed sheet into the sheet we've already inserted. - m_style_sheet->rules() = sheet->rules(); - - document().update_style(); -} - -void HTMLLinkElement::load_stylesheet(const URL& url) -{ - // First insert an empty style sheet in the document sheet list. - // There's probably a nicer way to do this, but this ensures that sheets are in document order. - m_style_sheet = CSS::StyleSheet::create({}); - document().style_sheets().add_sheet(*m_style_sheet); - - LoadRequest request; - request.set_url(url); - set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request)); } void HTMLLinkElement::parse_attribute(const FlyString& name, const String& value) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h index d225cf8bbd..f9d4434cd0 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,13 +28,11 @@ #pragma once #include <LibWeb/HTML/HTMLElement.h> -#include <LibWeb/Loader/Resource.h> +#include <LibWeb/Loader/CSSLoader.h> namespace Web::HTML { -class HTMLLinkElement final - : public HTMLElement - , public ResourceClient { +class HTMLLinkElement final : public HTMLElement { public: using WrapperType = Bindings::HTMLLinkElementWrapper; @@ -47,14 +46,8 @@ public: String href() const { return attribute(HTML::AttributeNames::href); } private: - // ^ResourceClient - virtual void resource_did_fail() override; - virtual void resource_did_load() override; - void parse_attribute(const FlyString&, const String&) override; - void load_stylesheet(const URL&); - struct Relationship { enum { Alternate = 1 << 0, @@ -63,7 +56,7 @@ private: }; unsigned m_relationship { 0 }; - RefPtr<CSS::StyleSheet> m_style_sheet; + CSSLoader m_css_loader; }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp index dbe14f487d..f158205806 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,7 +26,6 @@ */ #include <AK/StringBuilder.h> -#include <LibWeb/CSS/Parser/CSSParser.h> #include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Text.h> #include <LibWeb/HTML/HTMLStyleElement.h> @@ -34,7 +34,11 @@ namespace Web::HTML { HTMLStyleElement::HTMLStyleElement(DOM::Document& document, QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) + , m_css_loader(document) { + m_css_loader.on_load = [&] { + document.update_style(); + }; } HTMLStyleElement::~HTMLStyleElement() @@ -48,17 +52,15 @@ void HTMLStyleElement::children_changed() if (is<DOM::Text>(child)) builder.append(downcast<DOM::Text>(child).text_content()); }); - m_stylesheet = parse_css(CSS::ParsingContext(document()), builder.to_string()); - if (m_stylesheet) - document().style_sheets().add_sheet(*m_stylesheet); - else - document().style_sheets().add_sheet(CSS::StyleSheet::create({})); + m_css_loader.load_from_text(builder.to_string()); + document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull()); + HTMLElement::children_changed(); } void HTMLStyleElement::removed_from(Node& old_parent) { - if (m_stylesheet) { + if (m_css_loader.style_sheet()) { // FIXME: Remove the sheet from the document } return HTMLElement::removed_from(old_parent); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.h b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.h index 7349c66ed0..93e59720d5 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,6 +28,7 @@ #pragma once #include <LibWeb/HTML/HTMLElement.h> +#include <LibWeb/Loader/CSSLoader.h> namespace Web::HTML { @@ -41,7 +43,7 @@ public: virtual void removed_from(Node&) override; private: - RefPtr<CSS::StyleSheet> m_stylesheet; + CSSLoader m_css_loader; }; } diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp new file mode 100644 index 0000000000..f73465b60a --- /dev/null +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2021, the SerenityOS developers. + * 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 <AK/Debug.h> +#include <AK/URL.h> +#include <LibWeb/CSS/CSSImportRule.h> +#include <LibWeb/CSS/Parser/CSSParser.h> +#include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/Loader/CSSLoader.h> +#include <LibWeb/Loader/ResourceLoader.h> + +namespace Web { + +CSSLoader::CSSLoader(DOM::Document& document) + : m_document(&document) +{ +} + +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({}); + + load_next_import_if_needed(); +} + +void CSSLoader::load_from_url(const URL& url) +{ + m_style_sheet = CSS::StyleSheet::create({}); + + LoadRequest request; + request.set_url(url); + set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request)); +} + +void CSSLoader::resource_did_load() +{ + VERIFY(resource()); + + if (!resource()->has_encoded_data()) { + dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, no encoded data. URL: {}", resource()->url()); + } else { + dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, has encoded data. URL: {}", resource()->url()); + } + + auto sheet = parse_css(CSS::ParsingContext(*m_document), resource()->encoded_data()); + if (!sheet) { + dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Failed to parse stylesheet: {}", resource()->url()); + return; + } + + bool was_imported = m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) { + rule.set_style_sheet(sheet); + }); + + // Transfer the rules from the successfully parsed sheet into the sheet we've already inserted. + if (!was_imported) { + m_style_sheet->rules() = sheet->rules(); + } + + if (on_load) + on_load(); + + load_next_import_if_needed(); +} + +void CSSLoader::resource_did_fail() +{ + dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did fail. URL: {}", resource()->url()); + + load_next_import_if_needed(); +} + +void CSSLoader::load_next_import_if_needed() +{ + // Create load request for the first import which isn't loaded. + // TODO: We need to somehow handle infinite cycles in imports. + m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) { + dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Loading @import {}", rule.url()); + + LoadRequest request; + request.set_url(rule.url()); + set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request)); + }); +} + +} diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.h b/Userland/Libraries/LibWeb/Loader/CSSLoader.h new file mode 100644 index 0000000000..0263182938 --- /dev/null +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2020, 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/Function.h> +#include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/Loader/Resource.h> + +namespace Web { + +class CSSLoader : public ResourceClient { +public: + CSSLoader(DOM::Document& document); + + void load_from_text(const String&); + void load_from_url(const URL&); + + void load_next_import_if_needed(); + + RefPtr<CSS::StyleSheet> style_sheet() const { return m_style_sheet; }; + + Function<void()> on_load; + Function<void()> on_fail; + +private: + // ^ResourceClient + virtual void resource_did_load() override; + virtual void resource_did_fail() override; + + RefPtr<CSS::StyleSheet> m_style_sheet; + const DOM::Document* m_document; +}; + +} |