1use std::ptr;
6
7use html5ever::interface::QualName;
8use html5ever::{LocalName, local_name, ns};
9use js::glue::{UnwrapObjectDynamic, UnwrapObjectStatic};
10use js::jsapi::{CallArgs, CurrentGlobalOrNull, JSAutoRealm, JSObject};
11use js::rust::wrappers::{JS_SetPrototype, JS_WrapObject};
12use js::rust::{HandleObject, MutableHandleObject, MutableHandleValue};
13use script_bindings::conversions::SafeToJSValConvertible;
14use script_bindings::interface::get_desired_proto;
15
16use super::utils::ProtoOrIfaceArray;
17use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
18use crate::dom::bindings::codegen::Bindings::{
19 HTMLAnchorElementBinding, HTMLAreaElementBinding, HTMLAudioElementBinding,
20 HTMLBRElementBinding, HTMLBaseElementBinding, HTMLBodyElementBinding, HTMLButtonElementBinding,
21 HTMLCanvasElementBinding, HTMLDListElementBinding, HTMLDataElementBinding,
22 HTMLDataListElementBinding, HTMLDetailsElementBinding, HTMLDialogElementBinding,
23 HTMLDirectoryElementBinding, HTMLDivElementBinding, HTMLElementBinding,
24 HTMLEmbedElementBinding, HTMLFieldSetElementBinding, HTMLFontElementBinding,
25 HTMLFormElementBinding, HTMLFrameElementBinding, HTMLFrameSetElementBinding,
26 HTMLHRElementBinding, HTMLHeadElementBinding, HTMLHeadingElementBinding,
27 HTMLHtmlElementBinding, HTMLIFrameElementBinding, HTMLImageElementBinding,
28 HTMLInputElementBinding, HTMLLIElementBinding, HTMLLabelElementBinding,
29 HTMLLegendElementBinding, HTMLLinkElementBinding, HTMLMapElementBinding,
30 HTMLMenuElementBinding, HTMLMetaElementBinding, HTMLMeterElementBinding, HTMLModElementBinding,
31 HTMLOListElementBinding, HTMLObjectElementBinding, HTMLOptGroupElementBinding,
32 HTMLOptionElementBinding, HTMLOutputElementBinding, HTMLParagraphElementBinding,
33 HTMLParamElementBinding, HTMLPictureElementBinding, HTMLPreElementBinding,
34 HTMLProgressElementBinding, HTMLQuoteElementBinding, HTMLScriptElementBinding,
35 HTMLSelectElementBinding, HTMLSlotElementBinding, HTMLSourceElementBinding,
36 HTMLSpanElementBinding, HTMLStyleElementBinding, HTMLTableCaptionElementBinding,
37 HTMLTableCellElementBinding, HTMLTableColElementBinding, HTMLTableElementBinding,
38 HTMLTableRowElementBinding, HTMLTableSectionElementBinding, HTMLTemplateElementBinding,
39 HTMLTextAreaElementBinding, HTMLTimeElementBinding, HTMLTitleElementBinding,
40 HTMLTrackElementBinding, HTMLUListElementBinding, HTMLVideoElementBinding,
41};
42use crate::dom::bindings::codegen::PrototypeList;
43use crate::dom::bindings::conversions::DerivedFrom;
44use crate::dom::bindings::error::{Error, throw_dom_exception};
45use crate::dom::bindings::inheritance::Castable;
46use crate::dom::bindings::reflector::DomObject;
47use crate::dom::bindings::root::DomRoot;
48use crate::dom::create::create_native_html_element;
49use crate::dom::customelementregistry::{ConstructionStackEntry, CustomElementState};
50use crate::dom::element::{Element, ElementCreator};
51use crate::dom::globalscope::GlobalScope;
52use crate::dom::html::htmlelement::HTMLElement;
53use crate::dom::window::Window;
54use crate::script_runtime::{CanGc, JSContext, JSContext as SafeJSContext};
55
56fn html_constructor(
58 cx: JSContext,
59 global: &GlobalScope,
60 call_args: &CallArgs,
61 check_type: fn(&Element) -> bool,
62 proto_id: PrototypeList::ID,
63 creator: unsafe fn(SafeJSContext, HandleObject, *mut ProtoOrIfaceArray),
64 can_gc: CanGc,
65) -> Result<(), ()> {
66 let window = global.downcast::<Window>().unwrap();
67 let document = window.Document();
68
69 let registry = window.CustomElements();
71
72 rooted!(in(*cx) let new_target_unwrapped = unsafe {
78 UnwrapObjectDynamic(call_args.new_target().to_object(), *cx, true)
79 });
80 if new_target_unwrapped.is_null() {
81 throw_dom_exception(
82 cx,
83 global,
84 Error::Type("new.target is null".to_owned()),
85 can_gc,
86 );
87 return Err(());
88 }
89 if call_args.callee() == new_target_unwrapped.get() {
90 throw_dom_exception(
91 cx,
92 global,
93 Error::Type("new.target must not be the active function object".to_owned()),
94 can_gc,
95 );
96 return Err(());
97 }
98
99 rooted!(in(*cx) let new_target = call_args.new_target().to_object());
102 let definition = match registry.lookup_definition_by_constructor(new_target.handle()) {
103 Some(definition) => definition,
104 None => {
105 throw_dom_exception(
106 cx,
107 global,
108 Error::Type("No custom element definition found for new.target".to_owned()),
109 can_gc,
110 );
111 return Err(());
112 },
113 };
114
115 let mut is_value = None;
117
118 rooted!(in(*cx) let callee = unsafe { UnwrapObjectStatic(call_args.callee()) });
119 if callee.is_null() {
120 throw_dom_exception(cx, global, Error::Security, can_gc);
121 return Err(());
122 }
123
124 {
125 let _ac = JSAutoRealm::new(*cx, callee.get());
126 rooted!(in(*cx) let mut constructor = ptr::null_mut::<JSObject>());
127 rooted!(in(*cx) let global_object = unsafe { CurrentGlobalOrNull(*cx) });
128
129 if definition.is_autonomous() {
132 HTMLElementBinding::GetConstructorObject(
135 cx,
136 global_object.handle(),
137 constructor.handle_mut(),
138 );
139 }
140 else {
142 get_constructor_object_from_local_name(
143 definition.local_name.clone(),
144 cx,
145 global_object.handle(),
146 constructor.handle_mut(),
147 );
148
149 is_value = Some(definition.name.clone());
151 }
152 if constructor.get() != callee.get() {
154 throw_dom_exception(
155 cx,
156 global,
157 Error::Type("Custom element does not extend the proper interface".to_owned()),
158 can_gc,
159 );
160 return Err(());
161 }
162 }
163
164 rooted!(in(*cx) let mut prototype = ptr::null_mut::<JSObject>());
166 get_desired_proto(cx, call_args, proto_id, creator, prototype.handle_mut())?;
167
168 let entry = definition.construction_stack.borrow().last().cloned();
169 let result = match entry {
170 None => {
172 let name = QualName::new(None, ns!(html), definition.local_name.clone());
174 let element = if definition.is_autonomous() {
177 DomRoot::upcast(HTMLElement::new(name.local, None, &document, None, can_gc))
178 } else {
179 create_native_html_element(
180 name,
181 None,
182 &document,
183 ElementCreator::ScriptCreated,
184 None,
185 )
186 };
187
188 element.set_custom_element_state(CustomElementState::Custom);
192
193 element.set_custom_element_definition(definition.clone());
195
196 if let Some(is_value) = is_value {
198 element.set_is(is_value);
199 }
200
201 if !check_type(&element) {
202 throw_dom_exception(cx, global, Error::InvalidState, can_gc);
203 return Err(());
204 } else {
205 element
207 }
208 },
209 Some(ConstructionStackEntry::Element(element)) => {
211 let mut construction_stack = definition.construction_stack.borrow_mut();
215 construction_stack.pop();
216 construction_stack.push(ConstructionStackEntry::AlreadyConstructedMarker);
217
218 if !check_type(&element) {
220 throw_dom_exception(cx, global, Error::InvalidState, can_gc);
221 return Err(());
222 } else {
223 element
224 }
225 },
226 Some(ConstructionStackEntry::AlreadyConstructedMarker) => {
228 let s = "Top of construction stack marked AlreadyConstructed due to \
229 a custom element constructor constructing itself after super()"
230 .to_string();
231 throw_dom_exception(cx, global, Error::Type(s), can_gc);
232 return Err(());
233 },
234 };
235
236 rooted!(in(*cx) let mut element = result.reflector().get_jsobject().get());
237 unsafe {
238 if !JS_WrapObject(*cx, element.handle_mut()) {
239 return Err(());
240 }
241
242 JS_SetPrototype(*cx, element.handle(), prototype.handle());
243
244 result.safe_to_jsval(cx, MutableHandleValue::from_raw(call_args.rval()));
245 }
246 Ok(())
247}
248
249fn get_constructor_object_from_local_name(
254 name: LocalName,
255 cx: JSContext,
256 global: HandleObject,
257 rval: MutableHandleObject,
258) -> bool {
259 let constructor_fn = match name {
260 local_name!("a") => HTMLAnchorElementBinding::GetConstructorObject,
261 local_name!("abbr") => HTMLElementBinding::GetConstructorObject,
262 local_name!("acronym") => HTMLElementBinding::GetConstructorObject,
263 local_name!("address") => HTMLElementBinding::GetConstructorObject,
264 local_name!("area") => HTMLAreaElementBinding::GetConstructorObject,
265 local_name!("article") => HTMLElementBinding::GetConstructorObject,
266 local_name!("aside") => HTMLElementBinding::GetConstructorObject,
267 local_name!("audio") => HTMLAudioElementBinding::GetConstructorObject,
268 local_name!("b") => HTMLElementBinding::GetConstructorObject,
269 local_name!("base") => HTMLBaseElementBinding::GetConstructorObject,
270 local_name!("bdi") => HTMLElementBinding::GetConstructorObject,
271 local_name!("bdo") => HTMLElementBinding::GetConstructorObject,
272 local_name!("big") => HTMLElementBinding::GetConstructorObject,
273 local_name!("blockquote") => HTMLQuoteElementBinding::GetConstructorObject,
274 local_name!("body") => HTMLBodyElementBinding::GetConstructorObject,
275 local_name!("br") => HTMLBRElementBinding::GetConstructorObject,
276 local_name!("button") => HTMLButtonElementBinding::GetConstructorObject,
277 local_name!("canvas") => HTMLCanvasElementBinding::GetConstructorObject,
278 local_name!("caption") => HTMLTableCaptionElementBinding::GetConstructorObject,
279 local_name!("center") => HTMLElementBinding::GetConstructorObject,
280 local_name!("cite") => HTMLElementBinding::GetConstructorObject,
281 local_name!("code") => HTMLElementBinding::GetConstructorObject,
282 local_name!("col") => HTMLTableColElementBinding::GetConstructorObject,
283 local_name!("colgroup") => HTMLTableColElementBinding::GetConstructorObject,
284 local_name!("data") => HTMLDataElementBinding::GetConstructorObject,
285 local_name!("datalist") => HTMLDataListElementBinding::GetConstructorObject,
286 local_name!("dd") => HTMLElementBinding::GetConstructorObject,
287 local_name!("del") => HTMLModElementBinding::GetConstructorObject,
288 local_name!("details") => HTMLDetailsElementBinding::GetConstructorObject,
289 local_name!("dfn") => HTMLElementBinding::GetConstructorObject,
290 local_name!("dialog") => HTMLDialogElementBinding::GetConstructorObject,
291 local_name!("dir") => HTMLDirectoryElementBinding::GetConstructorObject,
292 local_name!("div") => HTMLDivElementBinding::GetConstructorObject,
293 local_name!("dl") => HTMLDListElementBinding::GetConstructorObject,
294 local_name!("dt") => HTMLElementBinding::GetConstructorObject,
295 local_name!("em") => HTMLElementBinding::GetConstructorObject,
296 local_name!("embed") => HTMLEmbedElementBinding::GetConstructorObject,
297 local_name!("fieldset") => HTMLFieldSetElementBinding::GetConstructorObject,
298 local_name!("figcaption") => HTMLElementBinding::GetConstructorObject,
299 local_name!("figure") => HTMLElementBinding::GetConstructorObject,
300 local_name!("font") => HTMLFontElementBinding::GetConstructorObject,
301 local_name!("footer") => HTMLElementBinding::GetConstructorObject,
302 local_name!("form") => HTMLFormElementBinding::GetConstructorObject,
303 local_name!("frame") => HTMLFrameElementBinding::GetConstructorObject,
304 local_name!("frameset") => HTMLFrameSetElementBinding::GetConstructorObject,
305 local_name!("h1") => HTMLHeadingElementBinding::GetConstructorObject,
306 local_name!("h2") => HTMLHeadingElementBinding::GetConstructorObject,
307 local_name!("h3") => HTMLHeadingElementBinding::GetConstructorObject,
308 local_name!("h4") => HTMLHeadingElementBinding::GetConstructorObject,
309 local_name!("h5") => HTMLHeadingElementBinding::GetConstructorObject,
310 local_name!("h6") => HTMLHeadingElementBinding::GetConstructorObject,
311 local_name!("head") => HTMLHeadElementBinding::GetConstructorObject,
312 local_name!("header") => HTMLElementBinding::GetConstructorObject,
313 local_name!("hgroup") => HTMLElementBinding::GetConstructorObject,
314 local_name!("hr") => HTMLHRElementBinding::GetConstructorObject,
315 local_name!("html") => HTMLHtmlElementBinding::GetConstructorObject,
316 local_name!("i") => HTMLElementBinding::GetConstructorObject,
317 local_name!("iframe") => HTMLIFrameElementBinding::GetConstructorObject,
318 local_name!("img") => HTMLImageElementBinding::GetConstructorObject,
319 local_name!("input") => HTMLInputElementBinding::GetConstructorObject,
320 local_name!("ins") => HTMLModElementBinding::GetConstructorObject,
321 local_name!("kbd") => HTMLElementBinding::GetConstructorObject,
322 local_name!("label") => HTMLLabelElementBinding::GetConstructorObject,
323 local_name!("legend") => HTMLLegendElementBinding::GetConstructorObject,
324 local_name!("li") => HTMLLIElementBinding::GetConstructorObject,
325 local_name!("link") => HTMLLinkElementBinding::GetConstructorObject,
326 local_name!("listing") => HTMLPreElementBinding::GetConstructorObject,
327 local_name!("main") => HTMLElementBinding::GetConstructorObject,
328 local_name!("map") => HTMLMapElementBinding::GetConstructorObject,
329 local_name!("mark") => HTMLElementBinding::GetConstructorObject,
330 local_name!("marquee") => HTMLElementBinding::GetConstructorObject,
331 local_name!("menu") => HTMLMenuElementBinding::GetConstructorObject,
332 local_name!("meta") => HTMLMetaElementBinding::GetConstructorObject,
333 local_name!("meter") => HTMLMeterElementBinding::GetConstructorObject,
334 local_name!("nav") => HTMLElementBinding::GetConstructorObject,
335 local_name!("nobr") => HTMLElementBinding::GetConstructorObject,
336 local_name!("noframes") => HTMLElementBinding::GetConstructorObject,
337 local_name!("noscript") => HTMLElementBinding::GetConstructorObject,
338 local_name!("object") => HTMLObjectElementBinding::GetConstructorObject,
339 local_name!("ol") => HTMLOListElementBinding::GetConstructorObject,
340 local_name!("optgroup") => HTMLOptGroupElementBinding::GetConstructorObject,
341 local_name!("option") => HTMLOptionElementBinding::GetConstructorObject,
342 local_name!("output") => HTMLOutputElementBinding::GetConstructorObject,
343 local_name!("p") => HTMLParagraphElementBinding::GetConstructorObject,
344 local_name!("param") => HTMLParamElementBinding::GetConstructorObject,
345 local_name!("picture") => HTMLPictureElementBinding::GetConstructorObject,
346 local_name!("plaintext") => HTMLPreElementBinding::GetConstructorObject,
347 local_name!("pre") => HTMLPreElementBinding::GetConstructorObject,
348 local_name!("progress") => HTMLProgressElementBinding::GetConstructorObject,
349 local_name!("q") => HTMLQuoteElementBinding::GetConstructorObject,
350 local_name!("rp") => HTMLElementBinding::GetConstructorObject,
351 local_name!("rt") => HTMLElementBinding::GetConstructorObject,
352 local_name!("ruby") => HTMLElementBinding::GetConstructorObject,
353 local_name!("s") => HTMLElementBinding::GetConstructorObject,
354 local_name!("samp") => HTMLElementBinding::GetConstructorObject,
355 local_name!("script") => HTMLScriptElementBinding::GetConstructorObject,
356 local_name!("section") => HTMLElementBinding::GetConstructorObject,
357 local_name!("select") => HTMLSelectElementBinding::GetConstructorObject,
358 local_name!("slot") => HTMLSlotElementBinding::GetConstructorObject,
359 local_name!("small") => HTMLElementBinding::GetConstructorObject,
360 local_name!("source") => HTMLSourceElementBinding::GetConstructorObject,
361 local_name!("span") => HTMLSpanElementBinding::GetConstructorObject,
362 local_name!("strike") => HTMLElementBinding::GetConstructorObject,
363 local_name!("strong") => HTMLElementBinding::GetConstructorObject,
364 local_name!("style") => HTMLStyleElementBinding::GetConstructorObject,
365 local_name!("sub") => HTMLElementBinding::GetConstructorObject,
366 local_name!("summary") => HTMLElementBinding::GetConstructorObject,
367 local_name!("sup") => HTMLElementBinding::GetConstructorObject,
368 local_name!("table") => HTMLTableElementBinding::GetConstructorObject,
369 local_name!("tbody") => HTMLTableSectionElementBinding::GetConstructorObject,
370 local_name!("td") => HTMLTableCellElementBinding::GetConstructorObject,
371 local_name!("template") => HTMLTemplateElementBinding::GetConstructorObject,
372 local_name!("textarea") => HTMLTextAreaElementBinding::GetConstructorObject,
373 local_name!("tfoot") => HTMLTableSectionElementBinding::GetConstructorObject,
374 local_name!("th") => HTMLTableCellElementBinding::GetConstructorObject,
375 local_name!("thead") => HTMLTableSectionElementBinding::GetConstructorObject,
376 local_name!("time") => HTMLTimeElementBinding::GetConstructorObject,
377 local_name!("title") => HTMLTitleElementBinding::GetConstructorObject,
378 local_name!("tr") => HTMLTableRowElementBinding::GetConstructorObject,
379 local_name!("tt") => HTMLElementBinding::GetConstructorObject,
380 local_name!("track") => HTMLTrackElementBinding::GetConstructorObject,
381 local_name!("u") => HTMLElementBinding::GetConstructorObject,
382 local_name!("ul") => HTMLUListElementBinding::GetConstructorObject,
383 local_name!("var") => HTMLElementBinding::GetConstructorObject,
384 local_name!("video") => HTMLVideoElementBinding::GetConstructorObject,
385 local_name!("wbr") => HTMLElementBinding::GetConstructorObject,
386 local_name!("xmp") => HTMLPreElementBinding::GetConstructorObject,
387 _ => return false,
388 };
389 constructor_fn(cx, global, rval);
390 true
391}
392
393pub(crate) fn call_html_constructor<T: DerivedFrom<Element> + DomObject>(
394 cx: JSContext,
395 args: &CallArgs,
396 global: &GlobalScope,
397 proto_id: PrototypeList::ID,
398 creator: unsafe fn(SafeJSContext, HandleObject, *mut ProtoOrIfaceArray),
399 can_gc: CanGc,
400) -> bool {
401 fn element_derives_interface<T: DerivedFrom<Element>>(element: &Element) -> bool {
402 element.is::<T>()
403 }
404
405 html_constructor(
406 cx,
407 global,
408 args,
409 element_derives_interface::<T>,
410 proto_id,
411 creator,
412 can_gc,
413 )
414 .is_ok()
415}