From 60867703c0a450787c486353f4b819e0dc5b3a68 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 18 Nov 2021 19:22:59 +0000 Subject: LibWeb: Make HTMLLinkElement responsible for its own loading This is the last use of CSSLoader, which can now be deleted. --- Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp | 43 ++++++++++++++++++---- Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h | 13 +++++-- 2 files changed, 46 insertions(+), 10 deletions(-) (limited to 'Userland/Libraries/LibWeb') 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 * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #include +#include #include #include #include @@ -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 * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include #include -#include 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 m_preload_resource; - CSSLoader m_css_loader; + Optional m_document_load_event_delayer; unsigned m_relationship { 0 }; }; -- cgit v1.2.3