mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 17:40:27 +00:00
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.
This commit is contained in:
parent
d07fcba69b
commit
bc116f3b13
Notes:
sideshowbarker
2024-07-18 21:37:03 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/bc116f3b13c
5 changed files with 17 additions and 16 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -55,8 +55,8 @@ private:
|
|||
};
|
||||
};
|
||||
|
||||
unsigned m_relationship { 0 };
|
||||
CSSLoader m_css_loader;
|
||||
unsigned m_relationship { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue