diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp index 381820a5622..d5242517c4c 100644 --- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp @@ -56,7 +56,14 @@ void HTMLLinkElement::removed_from(Node* old_parent, Node& old_root) { Base::removed_from(old_parent, old_root); if (m_loaded_style_sheet) { - document_or_shadow_root_style_sheets().remove_a_css_style_sheet(*m_loaded_style_sheet); + auto& style_sheet_list = [&old_root] -> CSS::StyleSheetList& { + if (auto* shadow_root = as_if(old_root); shadow_root) + return shadow_root->style_sheets(); + + return as(old_root).style_sheets(); + }(); + + style_sheet_list.remove_a_css_style_sheet(*m_loaded_style_sheet); m_loaded_style_sheet = nullptr; } } @@ -487,7 +494,7 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru if (!response.url_list().is_empty()) location = response.url_list().first().to_string(); - document().style_sheets().create_a_css_style_sheet( + document_or_shadow_root_style_sheets().create_a_css_style_sheet( "text/css"_string, this, attribute(HTML::AttributeNames::media).value_or({}), diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLLinkElement-appends-and-removes-from-ShadowRoot-styleSheets.txt b/Tests/LibWeb/Text/expected/HTML/HTMLLinkElement-appends-and-removes-from-ShadowRoot-styleSheets.txt new file mode 100644 index 00000000000..7184d2c3889 --- /dev/null +++ b/Tests/LibWeb/Text/expected/HTML/HTMLLinkElement-appends-and-removes-from-ShadowRoot-styleSheets.txt @@ -0,0 +1,13 @@ +== before appending +is linkEl.sheet null? true +- document stylesheets (length = 0) +- shadow root stylesheets (length = 0) +== link loaded +is linkEl.sheet null? false +- document stylesheets (length = 0) +- shadow root stylesheets (length = 1) +-- shadow root linkEl.sheet === sheet: true +== link removed +is linkEl.sheet null? true +- document stylesheets (length = 0) +- shadow root stylesheets (length = 0) diff --git a/Tests/LibWeb/Text/input/HTML/HTMLLinkElement-appends-and-removes-from-ShadowRoot-styleSheets.html b/Tests/LibWeb/Text/input/HTML/HTMLLinkElement-appends-and-removes-from-ShadowRoot-styleSheets.html new file mode 100644 index 00000000000..ef290e73986 --- /dev/null +++ b/Tests/LibWeb/Text/input/HTML/HTMLLinkElement-appends-and-removes-from-ShadowRoot-styleSheets.html @@ -0,0 +1,40 @@ + +
+ +