script: use Element::create instead of DOM struct constructors (#39325)

Creating elements by directly calling their interface constructors leads
to some state not being intialized correctly (see #39285). It is also
not in line with the specifications as many of them refer to the
[`create an element`][1] algorithm when an element needs to be created,
which directly maps to `Element::create` in the script crate.

So, switch all such places where elements are created by script to use
`Element::create`.

[1]: https://dom.spec.whatwg.org/#concept-create-element

Testing: Existing WPT tests.

Fixes: #39285

Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Mukilan Thiyagarajan 2025-09-16 14:56:42 +05:30 committed by GitHub
parent 2b261b02bf
commit 07b2ff5d60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 254 additions and 98 deletions

View file

@ -5,7 +5,7 @@
use std::cell::{Cell, Ref};
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name};
use html5ever::{LocalName, Prefix, QualName, local_name, ns};
use js::rust::HandleObject;
use crate::dom::attr::Attr;
@ -18,7 +18,7 @@ use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::document::Document;
use crate::dom::element::{AttributeMutation, Element};
use crate::dom::element::{AttributeMutation, CustomElementCreationMode, Element, ElementCreator};
use crate::dom::eventtarget::EventTarget;
use crate::dom::html::htmlelement::HTMLElement;
use crate::dom::html::htmlslotelement::HTMLSlotElement;
@ -105,13 +105,30 @@ impl HTMLDetailsElement {
.upcast::<Element>()
.attach_ua_shadow_root(false, can_gc);
let summary = HTMLSlotElement::new(local_name!("slot"), None, &document, None, can_gc);
let summary = Element::create(
QualName::new(None, ns!(html), local_name!("slot")),
None,
&document,
ElementCreator::ScriptCreated,
CustomElementCreationMode::Asynchronous,
None,
can_gc,
);
let summary = DomRoot::downcast::<HTMLSlotElement>(summary).unwrap();
root.upcast::<Node>()
.AppendChild(summary.upcast::<Node>(), can_gc)
.unwrap();
let fallback_summary =
HTMLElement::new(local_name!("summary"), None, &document, None, can_gc);
let fallback_summary = Element::create(
QualName::new(None, ns!(html), local_name!("summary")),
None,
&document,
ElementCreator::ScriptCreated,
CustomElementCreationMode::Asynchronous,
None,
can_gc,
);
let fallback_summary = DomRoot::downcast::<HTMLElement>(fallback_summary).unwrap();
fallback_summary
.upcast::<Node>()
.set_text_content_for_element(Some(DEFAULT_SUMMARY.into()), can_gc);
@ -120,7 +137,16 @@ impl HTMLDetailsElement {
.AppendChild(fallback_summary.upcast::<Node>(), can_gc)
.unwrap();
let descendants = HTMLSlotElement::new(local_name!("slot"), None, &document, None, can_gc);
let descendants = Element::create(
QualName::new(None, ns!(html), local_name!("slot")),
None,
&document,
ElementCreator::ScriptCreated,
CustomElementCreationMode::Asynchronous,
None,
can_gc,
);
let descendants = DomRoot::downcast::<HTMLSlotElement>(descendants).unwrap();
root.upcast::<Node>()
.AppendChild(descendants.upcast::<Node>(), can_gc)
.unwrap();