diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 441993a8e8e..b85366a27d2 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -398,7 +398,7 @@ impl Document { #[inline] pub fn browsing_context(&self) -> Option> { if self.has_browsing_context { - Some(self.window.browsing_context()) + self.window.maybe_browsing_context() } else { None } diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index d9095d2c6d5..440b188646e 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -319,10 +319,15 @@ impl Window { &self.image_cache_thread } + /// This can panic if it is called after the browsing context has been discarded pub fn browsing_context(&self) -> Root { self.browsing_context.get().unwrap() } + pub fn maybe_browsing_context(&self) -> Option> { + self.browsing_context.get() + } + pub fn bluetooth_thread(&self) -> IpcSender { self.bluetooth_thread.clone() } @@ -627,7 +632,7 @@ impl WindowMethods for Window { // https://html.spec.whatwg.org/multipage/#dom-parent fn GetParent(&self) -> Option> { // Steps 1. and 2. - if self.browsing_context().is_discarded() { + if self.maybe_browsing_context().map_or(true, |c| c.is_discarded()) { return None; } match self.parent() { @@ -641,7 +646,7 @@ impl WindowMethods for Window { // https://html.spec.whatwg.org/multipage/#dom-top fn GetTop(&self) -> Option> { // Steps 1. and 2. - if self.browsing_context().is_discarded() { + if self.maybe_browsing_context().map_or(true, |c| c.is_discarded()) { return None; } // Step 5.