diff options
author | Andreas Kling <kling@serenityos.org> | 2021-03-08 15:43:03 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-08 16:03:14 +0100 |
commit | bc116f3b13c1fc1a199eee9dc401b1ee0bbbe098 (patch) | |
tree | aaa1711402751c36825c6c30b13f279fdd18c749 /Userland/Libraries | |
parent | d07fcba69b7b437fc3d8cf88d330f0983c005b6c (diff) | |
download | serenity-bc116f3b13c1fc1a199eee9dc401b1ee0bbbe098.zip |
LibWeb: Give CSSLoader a backpointer to its owner element
This allows CSSLoader to set up the style sheet owner node internally,
and avoids an awkward weak link between CSSLoader and Document.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.h | 5 |
5 files changed, 17 insertions, 16 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp index 2ba2f5fab7..478d569ac9 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp @@ -36,7 +36,7 @@ namespace Web::HTML { HTMLLinkElement::HTMLLinkElement(DOM::Document& document, QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) - , m_css_loader(document) + , m_css_loader(*this) { m_css_loader.on_load = [&] { document.update_style(); @@ -53,10 +53,8 @@ void HTMLLinkElement::inserted_into(Node& node) if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) { m_css_loader.load_from_url(document().complete_url(href())); - if (auto sheet = m_css_loader.style_sheet()) { - sheet->set_owner_node(this); + if (auto sheet = m_css_loader.style_sheet()) 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 f9d4434cd0..fc0df34bad 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h @@ -55,8 +55,8 @@ private: }; }; - unsigned m_relationship { 0 }; CSSLoader m_css_loader; + unsigned m_relationship { 0 }; }; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp index 05f8632987..9dece219dc 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp @@ -34,7 +34,7 @@ namespace Web::HTML { HTMLStyleElement::HTMLStyleElement(DOM::Document& document, QualifiedName qualified_name) : HTMLElement(document, move(qualified_name)) - , m_css_loader(document) + , m_css_loader(*this) { m_css_loader.on_load = [&] { document.update_style(); @@ -54,10 +54,8 @@ void HTMLStyleElement::children_changed() }); m_css_loader.load_from_text(builder.to_string()); - if (auto sheet = m_css_loader.style_sheet()) { - sheet->set_owner_node(this); + if (auto sheet = m_css_loader.style_sheet()) document().style_sheets().add_sheet(sheet.release_nonnull()); - } HTMLElement::children_changed(); } diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp index ec49234d30..c029373ced 100644 --- a/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.cpp @@ -29,21 +29,24 @@ #include <LibWeb/CSS/CSSImportRule.h> #include <LibWeb/CSS/Parser/CSSParser.h> #include <LibWeb/CSS/StyleSheet.h> +#include <LibWeb/DOM/Element.h> #include <LibWeb/Loader/CSSLoader.h> #include <LibWeb/Loader/ResourceLoader.h> namespace Web { -CSSLoader::CSSLoader(DOM::Document& document) - : m_document(&document) +CSSLoader::CSSLoader(DOM::Element& owner_element) + : m_owner_element(owner_element) { } 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 = parse_css(CSS::ParsingContext(m_owner_element.document()), text); + if (!m_style_sheet) { m_style_sheet = CSS::CSSStyleSheet::create({}); + m_style_sheet->set_owner_node(&m_owner_element); + } load_next_import_if_needed(); } @@ -51,6 +54,7 @@ void CSSLoader::load_from_text(const String& text) void CSSLoader::load_from_url(const URL& url) { m_style_sheet = CSS::CSSStyleSheet::create({}); + m_style_sheet->set_owner_node(&m_owner_element); LoadRequest request; request.set_url(url); @@ -67,7 +71,7 @@ void CSSLoader::resource_did_load() 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()); + auto sheet = parse_css(CSS::ParsingContext(m_owner_element.document()), resource()->encoded_data()); if (!sheet) { dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Failed to parse stylesheet: {}", resource()->url()); return; diff --git a/Userland/Libraries/LibWeb/Loader/CSSLoader.h b/Userland/Libraries/LibWeb/Loader/CSSLoader.h index 01eb265433..fe2166b8e0 100644 --- a/Userland/Libraries/LibWeb/Loader/CSSLoader.h +++ b/Userland/Libraries/LibWeb/Loader/CSSLoader.h @@ -34,7 +34,7 @@ namespace Web { class CSSLoader : public ResourceClient { public: - CSSLoader(DOM::Document& document); + explicit CSSLoader(DOM::Element& owner_element); void load_from_text(const String&); void load_from_url(const URL&); @@ -51,8 +51,9 @@ private: virtual void resource_did_load() override; virtual void resource_did_fail() override; + DOM::Element& m_owner_element; + RefPtr<CSS::CSSStyleSheet> m_style_sheet; - const DOM::Document* m_document; }; } |