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/LibWeb/Loader | |
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/LibWeb/Loader')
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Loader/CSSLoader.h | 5 |
2 files changed, 12 insertions, 7 deletions
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; }; } |