diff --git a/components/script/dom/html/htmlstyleelement.rs b/components/script/dom/html/htmlstyleelement.rs
index dbc0bafef81..7e2659b4460 100644
--- a/components/script/dom/html/htmlstyleelement.rs
+++ b/components/script/dom/html/htmlstyleelement.rs
@@ -97,7 +97,10 @@ impl HTMLStyleElement {
pub(crate) fn parse_own_css(&self) {
let node = self.upcast::();
- assert!(node.is_connected());
+ assert!(
+ node.is_in_a_document_tree() || node.is_in_a_shadow_tree(),
+ "This stylesheet does not have an owner, so there's no reason to parse its contents"
+ );
// Step 4. of
let mut type_attribute = self.Type();
diff --git a/tests/wpt/meta/MANIFEST.json b/tests/wpt/meta/MANIFEST.json
index 4f24ac911ef..334f4296c54 100644
--- a/tests/wpt/meta/MANIFEST.json
+++ b/tests/wpt/meta/MANIFEST.json
@@ -10380,6 +10380,13 @@
{}
]
],
+ "modify-style-element-in-disconnected-shadow-crash.html": [
+ "3808642a97451eef40573214fbe1d1c6273c557f",
+ [
+ null,
+ {}
+ ]
+ ],
"move-to-new-tree-1343016.html": [
"853884b99301e22383e0fd0936cf154beab5e93e",
[
diff --git a/tests/wpt/tests/shadow-dom/crashtests/modify-style-element-in-disconnected-shadow-crash.html b/tests/wpt/tests/shadow-dom/crashtests/modify-style-element-in-disconnected-shadow-crash.html
new file mode 100644
index 00000000000..3808642a974
--- /dev/null
+++ b/tests/wpt/tests/shadow-dom/crashtests/modify-style-element-in-disconnected-shadow-crash.html
@@ -0,0 +1,16 @@
+
+
+
+ Modifying a style element in a disconnected shadow tree should not crash
+
+
+
+
+
+