Skip to content

Commit

Permalink
LibWeb: Give CSSLoader a backpointer to its owner element
Browse files Browse the repository at this point in the history
This allows CSSLoader to set up the style sheet owner node internally,
and avoids an awkward weak link between CSSLoader and Document.
  • Loading branch information
awesomekling committed Mar 8, 2021
1 parent d07fcba commit bc116f3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
6 changes: 2 additions & 4 deletions Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/HTMLLinkElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class HTMLLinkElement final : public HTMLElement {
};
};

unsigned m_relationship { 0 };
CSSLoader m_css_loader;
unsigned m_relationship { 0 };
};

}
6 changes: 2 additions & 4 deletions Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down
14 changes: 9 additions & 5 deletions Userland/Libraries/LibWeb/Loader/CSSLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,32 @@
#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();
}

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);
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions Userland/Libraries/LibWeb/Loader/CSSLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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&);
Expand All @@ -51,8 +51,9 @@ class CSSLoader : public ResourceClient {
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;
};

}

0 comments on commit bc116f3

Please sign in to comment.