diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2021-11-18 19:22:59 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-18 21:11:19 +0100 |
commit | 60867703c0a450787c486353f4b819e0dc5b3a68 (patch) | |
tree | 61b5c4e2d7b8b3b56e5080edfe74cb548c21bdd5 /Userland/Libraries/LibWeb | |
parent | 6fc1810190f05fddc296a5b56e15ee7cf0a2863c (diff) | |
download | serenity-60867703c0a450787c486353f4b819e0dc5b3a68.zip |
LibWeb: Make HTMLLinkElement responsible for its own loading
This is the last use of CSSLoader, which can now be deleted.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp | 43 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h | 13 |
2 files changed, 46 insertions, 10 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp index 47f40c798f..a67989527d 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp @@ -1,11 +1,13 @@ /* * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ #include <AK/ByteBuffer.h> +#include <AK/Debug.h> #include <AK/URL.h> #include <LibWeb/CSS/Parser/Parser.h> #include <LibWeb/DOM/Document.h> @@ -16,11 +18,7 @@ namespace Web::HTML { HTMLLinkElement::HTMLLinkElement(DOM::Document& document, QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) - , m_css_loader(*this) { - m_css_loader.on_load = [&] { - document.update_style(); - }; } HTMLLinkElement::~HTMLLinkElement() @@ -32,9 +30,11 @@ void HTMLLinkElement::inserted() HTMLElement::inserted(); if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) { - m_css_loader.load_from_url(document().parse_url(href())); - if (auto sheet = m_css_loader.style_sheet()) - document().style_sheets().add_sheet(sheet.release_nonnull()); + auto url = document().parse_url(href()); + dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Loading import URL: {}", url); + auto request = LoadRequest::create_for_url_on_page(url, document().page()); + set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request)); + m_document_load_event_delayer.emplace(document()); } if (m_relationship & Relationship::Preload) { @@ -69,4 +69,33 @@ void HTMLLinkElement::parse_attribute(const FlyString& name, const String& value } } +void HTMLLinkElement::resource_did_fail() +{ + dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did fail. URL: {}", resource()->url()); + + m_document_load_event_delayer.clear(); +} + +void HTMLLinkElement::resource_did_load() +{ + VERIFY(resource()); + + m_document_load_event_delayer.clear(); + + if (!resource()->has_encoded_data()) { + dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, no encoded data. URL: {}", resource()->url()); + } else { + dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, has encoded data. URL: {}", resource()->url()); + } + + auto sheet = parse_css(CSS::ParsingContext(document()), resource()->encoded_data()); + if (!sheet) { + dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url()); + return; + } + + sheet->set_owner_node(this); + document().style_sheets().add_sheet(sheet.release_nonnull()); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h index 3405c92a99..c95298bc9e 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h @@ -1,18 +1,21 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include <LibWeb/DOM/DocumentLoadEventDelayer.h> #include <LibWeb/HTML/HTMLElement.h> -#include <LibWeb/Loader/CSSLoader.h> namespace Web::HTML { -class HTMLLinkElement final : public HTMLElement { +class HTMLLinkElement final + : public HTMLElement + , public ResourceClient { public: using WrapperType = Bindings::HTMLLinkElementWrapper; @@ -28,6 +31,10 @@ public: private: void parse_attribute(const FlyString&, const String&) override; + // ^ResourceClient + virtual void resource_did_fail() override; + virtual void resource_did_load() override; + struct Relationship { enum { Alternate = 1 << 0, @@ -40,7 +47,7 @@ private: RefPtr<Resource> m_preload_resource; - CSSLoader m_css_loader; + Optional<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer; unsigned m_relationship { 0 }; }; |