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