diff --git a/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index 399996d48ed..7cab90e9839 100644 --- a/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -333,6 +333,22 @@ void CSSStyleSheet::invalidate_owners(DOM::StyleInvalidationReason reason) } } +GC::Ptr CSSStyleSheet::owning_document() const +{ + if (!m_owning_documents_or_shadow_roots.is_empty()) + return (*m_owning_documents_or_shadow_roots.begin())->document(); + + if (m_owner_css_rule && m_owner_css_rule->parent_style_sheet()) { + if (auto document = m_owner_css_rule->parent_style_sheet()->owning_document()) + return document; + } + + if (auto* element = const_cast(this)->owner_node()) + return element->document(); + + return nullptr; +} + bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window) { bool any_media_queries_changed_match_state = false; @@ -425,8 +441,8 @@ bool CSSStyleSheet::has_associated_font_loader(FontLoader& font_loader) const Parser::ParsingParams CSSStyleSheet::make_parsing_params() const { - if (!m_owning_documents_or_shadow_roots.is_empty()) - return Parser::ParsingParams { (*m_owning_documents_or_shadow_roots.begin())->document() }; + if (auto document = owning_document()) + return Parser::ParsingParams { *document }; return Parser::ParsingParams { realm() }; } diff --git a/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Libraries/LibWeb/CSS/CSSStyleSheet.h index ecb4ece8e77..699d41b3e62 100644 --- a/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -68,6 +68,7 @@ public: void add_owning_document_or_shadow_root(DOM::Node& document_or_shadow_root); void remove_owning_document_or_shadow_root(DOM::Node& document_or_shadow_root); void invalidate_owners(DOM::StyleInvalidationReason); + GC::Ptr owning_document() const; Optional default_namespace() const; GC::Ptr default_namespace_rule() const { return m_default_namespace_rule; }