Skip to main content

script/dom/
create.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use html5ever::{LocalName, Prefix, QualName, local_name, ns};
6use js::context::JSContext;
7use js::rust::HandleObject;
8
9use crate::dom::bindings::error::{report_pending_exception, throw_dom_exception};
10use crate::dom::bindings::inheritance::Castable;
11use crate::dom::bindings::reflector::DomGlobal;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::customelementregistry::{
14    CustomElementRegistry, CustomElementState, is_valid_custom_element_name, upgrade_element,
15};
16use crate::dom::document::Document;
17use crate::dom::element::{CustomElementCreationMode, Element, ElementCreator};
18use crate::dom::globalscope::GlobalScope;
19use crate::dom::html::htmlanchorelement::HTMLAnchorElement;
20use crate::dom::html::htmlareaelement::HTMLAreaElement;
21use crate::dom::html::htmlaudioelement::HTMLAudioElement;
22use crate::dom::html::htmlbaseelement::HTMLBaseElement;
23use crate::dom::html::htmlbodyelement::HTMLBodyElement;
24use crate::dom::html::htmlbrelement::HTMLBRElement;
25use crate::dom::html::htmlbuttonelement::HTMLButtonElement;
26use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
27use crate::dom::html::htmldataelement::HTMLDataElement;
28use crate::dom::html::htmldatalistelement::HTMLDataListElement;
29use crate::dom::html::htmldetailselement::HTMLDetailsElement;
30use crate::dom::html::htmldialogelement::HTMLDialogElement;
31use crate::dom::html::htmldirectoryelement::HTMLDirectoryElement;
32use crate::dom::html::htmldivelement::HTMLDivElement;
33use crate::dom::html::htmldlistelement::HTMLDListElement;
34use crate::dom::html::htmlelement::HTMLElement;
35use crate::dom::html::htmlembedelement::HTMLEmbedElement;
36use crate::dom::html::htmlfieldsetelement::HTMLFieldSetElement;
37use crate::dom::html::htmlfontelement::HTMLFontElement;
38use crate::dom::html::htmlformelement::HTMLFormElement;
39use crate::dom::html::htmlframeelement::HTMLFrameElement;
40use crate::dom::html::htmlframesetelement::HTMLFrameSetElement;
41use crate::dom::html::htmlheadelement::HTMLHeadElement;
42use crate::dom::html::htmlheadingelement::{HTMLHeadingElement, HeadingLevel};
43use crate::dom::html::htmlhrelement::HTMLHRElement;
44use crate::dom::html::htmlhtmlelement::HTMLHtmlElement;
45use crate::dom::html::htmliframeelement::HTMLIFrameElement;
46use crate::dom::html::htmlimageelement::HTMLImageElement;
47use crate::dom::html::htmllabelelement::HTMLLabelElement;
48use crate::dom::html::htmllegendelement::HTMLLegendElement;
49use crate::dom::html::htmllielement::HTMLLIElement;
50use crate::dom::html::htmllinkelement::HTMLLinkElement;
51use crate::dom::html::htmlmapelement::HTMLMapElement;
52use crate::dom::html::htmlmenuelement::HTMLMenuElement;
53use crate::dom::html::htmlmetaelement::HTMLMetaElement;
54use crate::dom::html::htmlmeterelement::HTMLMeterElement;
55use crate::dom::html::htmlmodelement::HTMLModElement;
56use crate::dom::html::htmlobjectelement::HTMLObjectElement;
57use crate::dom::html::htmlolistelement::HTMLOListElement;
58use crate::dom::html::htmloptgroupelement::HTMLOptGroupElement;
59use crate::dom::html::htmloptionelement::HTMLOptionElement;
60use crate::dom::html::htmloutputelement::HTMLOutputElement;
61use crate::dom::html::htmlparagraphelement::HTMLParagraphElement;
62use crate::dom::html::htmlparamelement::HTMLParamElement;
63use crate::dom::html::htmlpictureelement::HTMLPictureElement;
64use crate::dom::html::htmlpreelement::HTMLPreElement;
65use crate::dom::html::htmlprogresselement::HTMLProgressElement;
66use crate::dom::html::htmlquoteelement::HTMLQuoteElement;
67use crate::dom::html::htmlscriptelement::HTMLScriptElement;
68use crate::dom::html::htmlselectelement::HTMLSelectElement;
69use crate::dom::html::htmlslotelement::HTMLSlotElement;
70use crate::dom::html::htmlsourceelement::HTMLSourceElement;
71use crate::dom::html::htmlspanelement::HTMLSpanElement;
72use crate::dom::html::htmlstyleelement::HTMLStyleElement;
73use crate::dom::html::htmltablecaptionelement::HTMLTableCaptionElement;
74use crate::dom::html::htmltablecellelement::HTMLTableCellElement;
75use crate::dom::html::htmltablecolelement::HTMLTableColElement;
76use crate::dom::html::htmltableelement::HTMLTableElement;
77use crate::dom::html::htmltablerowelement::HTMLTableRowElement;
78use crate::dom::html::htmltablesectionelement::HTMLTableSectionElement;
79use crate::dom::html::htmltemplateelement::HTMLTemplateElement;
80use crate::dom::html::htmltextareaelement::HTMLTextAreaElement;
81use crate::dom::html::htmltimeelement::HTMLTimeElement;
82use crate::dom::html::htmltitleelement::HTMLTitleElement;
83use crate::dom::html::htmltrackelement::HTMLTrackElement;
84use crate::dom::html::htmlulistelement::HTMLUListElement;
85use crate::dom::html::htmlunknownelement::HTMLUnknownElement;
86use crate::dom::html::htmlvideoelement::HTMLVideoElement;
87use crate::dom::html::input_element::HTMLInputElement;
88use crate::dom::htmlmarqueeelement::HTMLMarqueeElement;
89use crate::dom::svg::svgelement::SVGElement;
90use crate::dom::svg::svgimageelement::SVGImageElement;
91use crate::dom::svg::svgsvgelement::SVGSVGElement;
92use crate::realms::enter_auto_realm;
93use crate::script_runtime::CanGc;
94use crate::script_thread::ScriptThread;
95
96fn create_svg_element(
97    cx: &mut JSContext,
98    name: QualName,
99    prefix: Option<Prefix>,
100    document: &Document,
101    proto: Option<HandleObject>,
102) -> DomRoot<Element> {
103    assert_eq!(name.ns, ns!(svg));
104
105    macro_rules! make(
106        ($ctor:ident) => ({
107            let obj = $ctor::new(cx, name.local, prefix, document, proto);
108            DomRoot::upcast(obj)
109        });
110    );
111
112    match name.local {
113        local_name!("image") => make!(SVGImageElement),
114        local_name!("svg") => make!(SVGSVGElement),
115        _ => make!(SVGElement),
116    }
117}
118
119/// <https://dom.spec.whatwg.org/#concept-create-element>
120#[expect(clippy::too_many_arguments)]
121fn create_html_element(
122    cx: &mut JSContext,
123    name: QualName,
124    prefix: Option<Prefix>,
125    is: Option<LocalName>,
126    document: &Document,
127    creator: ElementCreator,
128    mode: CustomElementCreationMode,
129    proto: Option<HandleObject>,
130) -> DomRoot<Element> {
131    assert_eq!(name.ns, ns!(html));
132
133    // Step 2. If registry is "default", then set registry
134    // to the result of looking up a custom element registry given document.
135    // TODO: We don't pass in any other value than "default" atm
136    let registry = CustomElementRegistry::lookup_a_custom_element_registry(document.upcast());
137
138    // Step 3. Let definition be the result of looking up a custom element
139    // definition given document, namespace, localName, and is.
140    let definition = document.lookup_custom_element_definition(&name.ns, &name.local, is.as_ref());
141
142    // Step 4. If definition is non-null...
143    if let Some(definition) = definition {
144        // ...and definition’s name is not equal to its local name
145        // (i.e., definition represents a customized built-in element):
146        if !definition.is_autonomous() {
147            // Step 4.1. Let interface be the element interface for localName and the HTML namespace.
148            // Step 4.2. Set result to the result of creating an element internal given document,
149            // interface, localName, the HTML namespace, prefix, "undefined", is, and registry.
150            let element = create_native_html_element(cx, name, prefix, document, creator, proto);
151            element.set_is(definition.name.clone());
152            element.set_custom_element_state(CustomElementState::Undefined);
153            element.set_custom_element_registry(registry);
154
155            match mode {
156                // Step 4.3. If synchronousCustomElements is true, then run this step while catching any exceptions:
157                CustomElementCreationMode::Synchronous => {
158                    // Step 4.3.1. Upgrade result using definition.
159                    upgrade_element(cx, definition, &element);
160                    // TODO: "If this step threw an exception exception:" steps.
161                },
162                // Step 4.4. Otherwise, enqueue a custom element upgrade reaction given result and definition.
163                CustomElementCreationMode::Asynchronous => {
164                    ScriptThread::enqueue_upgrade_reaction(&element, definition)
165                },
166            }
167            return element;
168        } else {
169            // Step 5. Otherwise, if definition is non-null:
170            match mode {
171                // Step 5.1. If synchronousCustomElements is true, then run these
172                // steps while catching any exceptions:
173                CustomElementCreationMode::Synchronous => {
174                    let local_name = name.local;
175                    // TODO(jdm) Pass proto to create_element?
176                    // Steps 4.1.1-4.1.11
177                    return match definition.create_element(
178                        document,
179                        prefix.clone(),
180                        registry.clone(),
181                        CanGc::from_cx(cx),
182                    ) {
183                        Ok(element) => {
184                            element.set_custom_element_definition(definition.clone());
185                            element
186                        },
187                        Err(error) => {
188                            // If any of these steps threw an exception exception:
189                            let global =
190                                GlobalScope::current().unwrap_or_else(|| document.global());
191
192                            let mut realm = enter_auto_realm(cx, &*global);
193                            let cx = &mut realm.current_realm();
194
195                            // Substep 1. Report exception for definition’s constructor’s corresponding
196                            // JavaScript object’s associated realm’s global object.
197                            throw_dom_exception(cx.into(), &global, error, CanGc::from_cx(cx));
198                            report_pending_exception(cx);
199
200                            // Substep 2. Set result to the result of creating an element internal given document,
201                            // HTMLUnknownElement, localName, the HTML namespace, prefix, "failed", null, and registry.
202                            let element = DomRoot::upcast::<Element>(HTMLUnknownElement::new(
203                                cx, local_name, prefix, document, proto,
204                            ));
205                            element.set_custom_element_state(CustomElementState::Failed);
206                            element.set_custom_element_registry(registry);
207                            element
208                        },
209                    };
210                },
211                // Step 4.2. Otherwise:
212                CustomElementCreationMode::Asynchronous => {
213                    // Step 4.2.1. Set result to a new element that implements the HTMLElement interface,
214                    // with no attributes, namespace set to the HTML namespace, namespace prefix set to
215                    // prefix, local name set to localName, custom element state set to "undefined",
216                    // custom element definition set to null, is value set to null, and node document
217                    // set to document.
218                    let result = DomRoot::upcast::<Element>(HTMLElement::new(
219                        cx, name.local, prefix, document, proto,
220                    ));
221                    result.set_custom_element_state(CustomElementState::Undefined);
222                    result.set_custom_element_registry(registry);
223                    // Step 4.2.2. Enqueue a custom element upgrade reaction given result and definition.
224                    ScriptThread::enqueue_upgrade_reaction(&result, definition);
225                    return result;
226                },
227            }
228        }
229    }
230
231    // Step 5. Otherwise:
232    // Step 5.1. Let interface be the element interface for localName and namespace.
233    // Step 5.2. Set result to a new element that implements interface, with no attributes,
234    // namespace set to namespace, namespace prefix set to prefix, local name set to localName,
235    // custom element state set to "uncustomized", custom element definition set to null,
236    // is value set to is, and node document set to document.
237    let result = create_native_html_element(cx, name.clone(), prefix, document, creator, proto);
238    // Step 5.3. If namespace is the HTML namespace, and either localName is a valid custom element name or
239    // is is non-null, then set result’s custom element state to "undefined".
240    match is {
241        Some(is) => {
242            result.set_is(is);
243            result.set_custom_element_state(CustomElementState::Undefined);
244            result.set_custom_element_registry(registry);
245        },
246        None => {
247            if is_valid_custom_element_name(&name.local) {
248                result.set_custom_element_state(CustomElementState::Undefined);
249                result.set_custom_element_registry(registry);
250            } else {
251                // Note: This is a performance optimization. See the doc comment of the method for
252                // more information.
253                result.set_initial_custom_element_state_to_uncustomized();
254            }
255        },
256    };
257
258    // Step 6. Return result.
259    result
260}
261
262pub(crate) fn create_native_html_element(
263    cx: &mut JSContext,
264    name: QualName,
265    prefix: Option<Prefix>,
266    document: &Document,
267    creator: ElementCreator,
268    proto: Option<HandleObject>,
269) -> DomRoot<Element> {
270    assert_eq!(name.ns, ns!(html));
271
272    macro_rules! make(
273        ($ctor:ident) => ({
274            let obj = $ctor::new(cx, name.local, prefix, document, proto);
275            DomRoot::upcast(obj)
276        });
277        ($ctor:ident, $($arg:expr),+) => ({
278            let obj = $ctor::new(cx, name.local, prefix, document, proto, $($arg),+);
279            DomRoot::upcast(obj)
280        })
281    );
282
283    // This is a big match, and the IDs for inline-interned atoms are not very structured.
284    // Perhaps we should build a perfect hash from those IDs instead.
285    // https://html.spec.whatwg.org/multipage/#elements-in-the-dom
286    match name.local {
287        local_name!("a") => make!(HTMLAnchorElement),
288        local_name!("abbr") => make!(HTMLElement),
289        local_name!("acronym") => make!(HTMLElement),
290        local_name!("address") => make!(HTMLElement),
291        local_name!("area") => make!(HTMLAreaElement),
292        local_name!("article") => make!(HTMLElement),
293        local_name!("aside") => make!(HTMLElement),
294        local_name!("audio") => make!(HTMLAudioElement),
295        local_name!("b") => make!(HTMLElement),
296        local_name!("base") => make!(HTMLBaseElement),
297        local_name!("bdi") => make!(HTMLElement),
298        local_name!("bdo") => make!(HTMLElement),
299        // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:bgsound
300        local_name!("bgsound") => make!(HTMLUnknownElement),
301        local_name!("big") => make!(HTMLElement),
302        // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:blink
303        local_name!("blink") => make!(HTMLUnknownElement),
304        // https://html.spec.whatwg.org/multipage/#the-blockquote-element
305        local_name!("blockquote") => make!(HTMLQuoteElement),
306        local_name!("body") => make!(HTMLBodyElement),
307        local_name!("br") => make!(HTMLBRElement),
308        local_name!("button") => make!(HTMLButtonElement),
309        local_name!("canvas") => make!(HTMLCanvasElement),
310        local_name!("caption") => make!(HTMLTableCaptionElement),
311        local_name!("center") => make!(HTMLElement),
312        local_name!("cite") => make!(HTMLElement),
313        local_name!("code") => make!(HTMLElement),
314        local_name!("col") => make!(HTMLTableColElement),
315        local_name!("colgroup") => make!(HTMLTableColElement),
316        local_name!("data") => make!(HTMLDataElement),
317        local_name!("datalist") => make!(HTMLDataListElement),
318        local_name!("dd") => make!(HTMLElement),
319        local_name!("del") => make!(HTMLModElement),
320        local_name!("details") => make!(HTMLDetailsElement),
321        local_name!("dfn") => make!(HTMLElement),
322        local_name!("dialog") => make!(HTMLDialogElement),
323        local_name!("dir") => make!(HTMLDirectoryElement),
324        local_name!("div") => make!(HTMLDivElement),
325        local_name!("dl") => make!(HTMLDListElement),
326        local_name!("dt") => make!(HTMLElement),
327        local_name!("em") => make!(HTMLElement),
328        local_name!("embed") => make!(HTMLEmbedElement),
329        local_name!("fieldset") => make!(HTMLFieldSetElement),
330        local_name!("figcaption") => make!(HTMLElement),
331        local_name!("figure") => make!(HTMLElement),
332        local_name!("font") => make!(HTMLFontElement),
333        local_name!("footer") => make!(HTMLElement),
334        local_name!("form") => make!(HTMLFormElement),
335        local_name!("frame") => make!(HTMLFrameElement),
336        local_name!("frameset") => make!(HTMLFrameSetElement),
337        local_name!("h1") => make!(HTMLHeadingElement, HeadingLevel::Heading1),
338        local_name!("h2") => make!(HTMLHeadingElement, HeadingLevel::Heading2),
339        local_name!("h3") => make!(HTMLHeadingElement, HeadingLevel::Heading3),
340        local_name!("h4") => make!(HTMLHeadingElement, HeadingLevel::Heading4),
341        local_name!("h5") => make!(HTMLHeadingElement, HeadingLevel::Heading5),
342        local_name!("h6") => make!(HTMLHeadingElement, HeadingLevel::Heading6),
343        local_name!("head") => make!(HTMLHeadElement),
344        local_name!("header") => make!(HTMLElement),
345        local_name!("hgroup") => make!(HTMLElement),
346        local_name!("hr") => make!(HTMLHRElement),
347        local_name!("html") => make!(HTMLHtmlElement),
348        local_name!("i") => make!(HTMLElement),
349        local_name!("iframe") => make!(HTMLIFrameElement),
350        local_name!("img") => make!(HTMLImageElement, creator),
351        local_name!("input") => make!(HTMLInputElement),
352        local_name!("ins") => make!(HTMLModElement),
353        // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:isindex-2
354        local_name!("isindex") => make!(HTMLUnknownElement),
355        local_name!("kbd") => make!(HTMLElement),
356        // https://html.spec.whatwg.org/multipage/#keygen
357        local_name!("keygen") => make!(HTMLUnknownElement),
358        local_name!("label") => make!(HTMLLabelElement),
359        local_name!("legend") => make!(HTMLLegendElement),
360        local_name!("li") => make!(HTMLLIElement),
361        local_name!("link") => make!(HTMLLinkElement, creator),
362        // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:listing
363        local_name!("listing") => make!(HTMLPreElement),
364        local_name!("main") => make!(HTMLElement),
365        local_name!("map") => make!(HTMLMapElement),
366        local_name!("mark") => make!(HTMLElement),
367        local_name!("marquee") => make!(HTMLMarqueeElement),
368        local_name!("menu") => make!(HTMLMenuElement),
369        local_name!("meta") => make!(HTMLMetaElement),
370        local_name!("meter") => make!(HTMLMeterElement),
371        // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:multicol
372        local_name!("multicol") => make!(HTMLUnknownElement),
373        local_name!("nav") => make!(HTMLElement),
374        // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:nextid
375        local_name!("nextid") => make!(HTMLUnknownElement),
376        local_name!("nobr") => make!(HTMLElement),
377        local_name!("noframes") => make!(HTMLElement),
378        local_name!("noscript") => make!(HTMLElement),
379        local_name!("object") => make!(HTMLObjectElement),
380        local_name!("ol") => make!(HTMLOListElement),
381        local_name!("optgroup") => make!(HTMLOptGroupElement),
382        local_name!("option") => make!(HTMLOptionElement),
383        local_name!("output") => make!(HTMLOutputElement),
384        local_name!("p") => make!(HTMLParagraphElement),
385        local_name!("param") => make!(HTMLParamElement),
386        local_name!("picture") => make!(HTMLPictureElement),
387        local_name!("plaintext") => make!(HTMLPreElement),
388        local_name!("pre") => make!(HTMLPreElement),
389        local_name!("progress") => make!(HTMLProgressElement),
390        local_name!("q") => make!(HTMLQuoteElement),
391        local_name!("rp") => make!(HTMLElement),
392        local_name!("rt") => make!(HTMLElement),
393        local_name!("ruby") => make!(HTMLElement),
394        local_name!("s") => make!(HTMLElement),
395        local_name!("samp") => make!(HTMLElement),
396        local_name!("script") => make!(HTMLScriptElement, creator),
397        local_name!("section") => make!(HTMLElement),
398        local_name!("select") => make!(HTMLSelectElement),
399        local_name!("slot") => make!(HTMLSlotElement),
400        local_name!("small") => make!(HTMLElement),
401        local_name!("source") => make!(HTMLSourceElement),
402        // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:spacer
403        local_name!("spacer") => make!(HTMLUnknownElement),
404        local_name!("span") => make!(HTMLSpanElement),
405        local_name!("strike") => make!(HTMLElement),
406        local_name!("strong") => make!(HTMLElement),
407        local_name!("style") => make!(HTMLStyleElement, creator),
408        local_name!("sub") => make!(HTMLElement),
409        local_name!("summary") => make!(HTMLElement),
410        local_name!("sup") => make!(HTMLElement),
411        local_name!("table") => make!(HTMLTableElement),
412        local_name!("tbody") => make!(HTMLTableSectionElement),
413        local_name!("td") => make!(HTMLTableCellElement),
414        local_name!("template") => make!(HTMLTemplateElement),
415        local_name!("textarea") => make!(HTMLTextAreaElement),
416        // https://html.spec.whatwg.org/multipage/#the-tfoot-element:concept-element-dom
417        local_name!("tfoot") => make!(HTMLTableSectionElement),
418        local_name!("th") => make!(HTMLTableCellElement),
419        // https://html.spec.whatwg.org/multipage/#the-thead-element:concept-element-dom
420        local_name!("thead") => make!(HTMLTableSectionElement),
421        local_name!("time") => make!(HTMLTimeElement),
422        local_name!("title") => make!(HTMLTitleElement),
423        local_name!("tr") => make!(HTMLTableRowElement),
424        local_name!("tt") => make!(HTMLElement),
425        local_name!("track") => make!(HTMLTrackElement),
426        local_name!("u") => make!(HTMLElement),
427        local_name!("ul") => make!(HTMLUListElement),
428        local_name!("var") => make!(HTMLElement),
429        local_name!("video") => make!(HTMLVideoElement),
430        local_name!("wbr") => make!(HTMLElement),
431        local_name!("xmp") => make!(HTMLPreElement),
432        _ if is_valid_custom_element_name(&name.local) => make!(HTMLElement),
433        _ => make!(HTMLUnknownElement),
434    }
435}
436
437pub(crate) fn create_element(
438    cx: &mut JSContext,
439    name: QualName,
440    is: Option<LocalName>,
441    document: &Document,
442    creator: ElementCreator,
443    mode: CustomElementCreationMode,
444    proto: Option<HandleObject>,
445) -> DomRoot<Element> {
446    let prefix = name.prefix.clone();
447    match name.ns {
448        ns!(html) => create_html_element(cx, name, prefix, is, document, creator, mode, proto),
449        ns!(svg) => create_svg_element(cx, name, prefix, document, proto),
450        _ => Element::new(cx, name.local, name.ns, prefix, document, proto),
451    }
452}