diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 4d235d7eb15..6f777d8985b 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -145,7 +145,6 @@ use crate::dom::hashchangeevent::HashChangeEvent; use crate::dom::htmlanchorelement::HTMLAnchorElement; use crate::dom::htmlareaelement::HTMLAreaElement; use crate::dom::htmlbaseelement::HTMLBaseElement; -use crate::dom::htmlbodyelement::HTMLBodyElement; use crate::dom::htmlcollection::{CollectionFilter, HTMLCollection}; use crate::dom::htmlelement::HTMLElement; use crate::dom::htmlembedelement::HTMLEmbedElement; @@ -2490,12 +2489,11 @@ impl Document { } pub(crate) fn get_body_attribute(&self, local_name: &LocalName) -> DOMString { - match self - .GetBody() - .and_then(DomRoot::downcast::) - { - Some(ref body) => body.upcast::().get_string_attribute(local_name), - None => DOMString::new(), + match self.GetBody() { + Some(ref body) if body.is_body_element() => { + body.upcast::().get_string_attribute(local_name) + }, + _ => DOMString::new(), } } @@ -2505,10 +2503,7 @@ impl Document { value: DOMString, can_gc: CanGc, ) { - if let Some(ref body) = self - .GetBody() - .and_then(DomRoot::downcast::) - { + if let Some(ref body) = self.GetBody().filter(|elem| elem.is_body_element()) { let body = body.upcast::(); let value = body.parse_attribute(&ns!(), local_name, value); body.set_attribute(local_name, value, can_gc); diff --git a/components/script/dom/htmlbodyelement.rs b/components/script/dom/htmlbodyelement.rs index 19b0ab4efce..1c824236096 100644 --- a/components/script/dom/htmlbodyelement.rs +++ b/components/script/dom/htmlbodyelement.rs @@ -55,17 +55,6 @@ impl HTMLBodyElement { can_gc, ) } - - /// - pub(crate) fn is_the_html_body_element(&self) -> bool { - let self_node = self.upcast::(); - let root_elem = self.upcast::().root_element(); - let root_node = root_elem.upcast::(); - root_node.is_parent_of(self_node) && - self_node - .preceding_siblings() - .all(|n| !n.is::()) - } } impl HTMLBodyElementMethods for HTMLBodyElement { diff --git a/components/script/dom/htmlelement.rs b/components/script/dom/htmlelement.rs index f47a40d3cdb..c85fc85d000 100644 --- a/components/script/dom/htmlelement.rs +++ b/components/script/dom/htmlelement.rs @@ -439,7 +439,7 @@ impl HTMLElementMethods for HTMLElement { /// fn GetOffsetParent(&self, can_gc: CanGc) -> Option> { - if self.is::() || self.is::() { + if self.is_body_element() || self.is::() { return None; } @@ -452,7 +452,7 @@ impl HTMLElementMethods for HTMLElement { // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsettop fn OffsetTop(&self, can_gc: CanGc) -> i32 { - if self.is::() { + if self.is_body_element() { return 0; } @@ -465,7 +465,7 @@ impl HTMLElementMethods for HTMLElement { // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetleft fn OffsetLeft(&self, can_gc: CanGc) -> i32 { - if self.is::() { + if self.is_body_element() { return 0; } @@ -817,6 +817,19 @@ impl HTMLElement { } } + /// + pub(crate) fn is_body_element(&self) -> bool { + let self_node = self.upcast::(); + self_node.GetParentNode().is_some_and(|parent| { + let parent_node = parent.upcast::(); + (self_node.is::() || self_node.is::()) && + parent_node.is::() && + self_node + .preceding_siblings() + .all(|n| !n.is::() && !n.is::()) + }) + } + /// pub(crate) fn is_submittable_element(&self) -> bool { match self.upcast::().type_id() { diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index e61f3c45c2a..88bb74f1978 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -92,7 +92,6 @@ use crate::dom::element::{CustomElementCreationMode, Element, ElementCreator, Se use crate::dom::event::{Event, EventBubbles, EventCancelable}; use crate::dom::eventtarget::EventTarget; use crate::dom::globalscope::GlobalScope; -use crate::dom::htmlbodyelement::HTMLBodyElement; use crate::dom::htmlcanvaselement::{HTMLCanvasElement, LayoutHTMLCanvasElementHelpers}; use crate::dom::htmlcollection::HTMLCollection; use crate::dom::htmlelement::HTMLElement; @@ -958,8 +957,8 @@ impl Node { let in_quirks_mode = document.quirks_mode() == QuirksMode::Quirks; let is_root = self.downcast::().is_some_and(|e| e.is_root()); let is_body_element = self - .downcast::() - .is_some_and(|e| e.is_the_html_body_element()); + .downcast::() + .is_some_and(|e| e.is_body_element()); // "4. If the element is the root element and document is not in quirks mode // return max(viewport scrolling area width/height, viewport width/height)."