From d07fcba69b7b437fc3d8cf88d330f0983c005b6c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 8 Mar 2021 13:40:53 +0100 Subject: [PATCH] LibWeb: Implement StyleSheet.ownerNode :^) --- Userland/Libraries/LibWeb/CSS/StyleSheet.cpp | 11 ++++++++++- Userland/Libraries/LibWeb/CSS/StyleSheet.h | 8 +++++++- Userland/Libraries/LibWeb/CSS/StyleSheet.idl | 5 ++++- Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp | 7 +++++-- Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp | 8 ++++++-- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp index 964f3badcf..473def3bc1 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * @@ -26,7 +26,16 @@ */ #include +#include namespace Web::CSS { +void StyleSheet::set_owner_node(DOM::Element* element) +{ + if (element) + m_owner_node = element->make_weak_ptr(); + else + m_owner_node = nullptr; +} + } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.h b/Userland/Libraries/LibWeb/CSS/StyleSheet.h index df6fb735ff..7f8b3dec13 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * @@ -41,8 +41,14 @@ public: virtual ~StyleSheet() = default; + DOM::Element* owner_node() { return m_owner_node; } + void set_owner_node(DOM::Element*); + protected: StyleSheet() = default; + +private: + WeakPtr m_owner_node; }; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.idl b/Userland/Libraries/LibWeb/CSS/StyleSheet.idl index 3972052b36..315a0939ce 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.idl +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.idl @@ -1,9 +1,12 @@ interface StyleSheet { + + readonly attribute Element? ownerNode; + // readonly attribute CSSOMString type; // readonly attribute USVString? href; - // readonly attribute (Element or ProcessingInstruction)? ownerNode; // readonly attribute CSSStyleSheet? parentStyleSheet; // readonly attribute DOMString? title; // [SameObject, PutForwards=mediaText] readonly attribute MediaList media; // attribute boolean disabled; + }; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp index bf2d45e1d6..2ba2f5fab7 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLLinkElement.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * @@ -53,7 +53,10 @@ 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())); - document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull()); + if (auto sheet = m_css_loader.style_sheet()) { + sheet->set_owner_node(this); + document().style_sheets().add_sheet(sheet.release_nonnull()); + } } } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp index f158205806..05f8632987 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLStyleElement.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, the SerenityOS developers. * All rights reserved. * @@ -53,7 +53,11 @@ void HTMLStyleElement::children_changed() builder.append(downcast(child).text_content()); }); m_css_loader.load_from_text(builder.to_string()); - document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull()); + + if (auto sheet = m_css_loader.style_sheet()) { + sheet->set_owner_node(this); + document().style_sheets().add_sheet(sheet.release_nonnull()); + } HTMLElement::children_changed(); }