improvement: body element check (#37442)

Created a new method `HTMLElement::is_body_element` that replaces
`HTMLBodyElement::is_the_html_body_element`.

Testing: Existing WPT tests should pass.
Fixes: https://github.com/servo/servo/issues/37429

---------

Signed-off-by: iamlockon <xdddxyyyxzzz123@gmail.com>
This commit is contained in:
Jay Wang 2025-06-15 13:11:32 +09:00 committed by GitHub
parent c74a422e4c
commit 96adb1e959
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 28 deletions

View file

@ -439,7 +439,7 @@ impl HTMLElementMethods<crate::DomTypeHolder> for HTMLElement {
/// <https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetparent>
fn GetOffsetParent(&self, can_gc: CanGc) -> Option<DomRoot<Element>> {
if self.is::<HTMLBodyElement>() || self.is::<HTMLHtmlElement>() {
if self.is_body_element() || self.is::<HTMLHtmlElement>() {
return None;
}
@ -452,7 +452,7 @@ impl HTMLElementMethods<crate::DomTypeHolder> for HTMLElement {
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsettop
fn OffsetTop(&self, can_gc: CanGc) -> i32 {
if self.is::<HTMLBodyElement>() {
if self.is_body_element() {
return 0;
}
@ -465,7 +465,7 @@ impl HTMLElementMethods<crate::DomTypeHolder> for HTMLElement {
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetleft
fn OffsetLeft(&self, can_gc: CanGc) -> i32 {
if self.is::<HTMLBodyElement>() {
if self.is_body_element() {
return 0;
}
@ -817,6 +817,19 @@ impl HTMLElement {
}
}
/// <https://html.spec.whatwg.org/multipage/#the-body-element-2>
pub(crate) fn is_body_element(&self) -> bool {
let self_node = self.upcast::<Node>();
self_node.GetParentNode().is_some_and(|parent| {
let parent_node = parent.upcast::<Node>();
(self_node.is::<HTMLBodyElement>() || self_node.is::<HTMLFrameSetElement>()) &&
parent_node.is::<HTMLHtmlElement>() &&
self_node
.preceding_siblings()
.all(|n| !n.is::<HTMLBodyElement>() && !n.is::<HTMLFrameSetElement>())
})
}
/// <https://html.spec.whatwg.org/multipage/#category-submit>
pub(crate) fn is_submittable_element(&self) -> bool {
match self.upcast::<Node>().type_id() {