ElementFactory.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/ElementFactory.h>
  9. #include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
  10. #include <LibWeb/HTML/CustomElements/CustomElementName.h>
  11. #include <LibWeb/HTML/HTMLAnchorElement.h>
  12. #include <LibWeb/HTML/HTMLAreaElement.h>
  13. #include <LibWeb/HTML/HTMLAudioElement.h>
  14. #include <LibWeb/HTML/HTMLBRElement.h>
  15. #include <LibWeb/HTML/HTMLBaseElement.h>
  16. #include <LibWeb/HTML/HTMLBlinkElement.h>
  17. #include <LibWeb/HTML/HTMLBodyElement.h>
  18. #include <LibWeb/HTML/HTMLButtonElement.h>
  19. #include <LibWeb/HTML/HTMLCanvasElement.h>
  20. #include <LibWeb/HTML/HTMLDListElement.h>
  21. #include <LibWeb/HTML/HTMLDataElement.h>
  22. #include <LibWeb/HTML/HTMLDataListElement.h>
  23. #include <LibWeb/HTML/HTMLDetailsElement.h>
  24. #include <LibWeb/HTML/HTMLDialogElement.h>
  25. #include <LibWeb/HTML/HTMLDirectoryElement.h>
  26. #include <LibWeb/HTML/HTMLDivElement.h>
  27. #include <LibWeb/HTML/HTMLEmbedElement.h>
  28. #include <LibWeb/HTML/HTMLFieldSetElement.h>
  29. #include <LibWeb/HTML/HTMLFontElement.h>
  30. #include <LibWeb/HTML/HTMLFormElement.h>
  31. #include <LibWeb/HTML/HTMLFrameElement.h>
  32. #include <LibWeb/HTML/HTMLFrameSetElement.h>
  33. #include <LibWeb/HTML/HTMLHRElement.h>
  34. #include <LibWeb/HTML/HTMLHeadElement.h>
  35. #include <LibWeb/HTML/HTMLHeadingElement.h>
  36. #include <LibWeb/HTML/HTMLHtmlElement.h>
  37. #include <LibWeb/HTML/HTMLIFrameElement.h>
  38. #include <LibWeb/HTML/HTMLImageElement.h>
  39. #include <LibWeb/HTML/HTMLInputElement.h>
  40. #include <LibWeb/HTML/HTMLLIElement.h>
  41. #include <LibWeb/HTML/HTMLLabelElement.h>
  42. #include <LibWeb/HTML/HTMLLegendElement.h>
  43. #include <LibWeb/HTML/HTMLLinkElement.h>
  44. #include <LibWeb/HTML/HTMLMapElement.h>
  45. #include <LibWeb/HTML/HTMLMarqueeElement.h>
  46. #include <LibWeb/HTML/HTMLMenuElement.h>
  47. #include <LibWeb/HTML/HTMLMetaElement.h>
  48. #include <LibWeb/HTML/HTMLMeterElement.h>
  49. #include <LibWeb/HTML/HTMLModElement.h>
  50. #include <LibWeb/HTML/HTMLOListElement.h>
  51. #include <LibWeb/HTML/HTMLObjectElement.h>
  52. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  53. #include <LibWeb/HTML/HTMLOptionElement.h>
  54. #include <LibWeb/HTML/HTMLOutputElement.h>
  55. #include <LibWeb/HTML/HTMLParagraphElement.h>
  56. #include <LibWeb/HTML/HTMLParamElement.h>
  57. #include <LibWeb/HTML/HTMLPictureElement.h>
  58. #include <LibWeb/HTML/HTMLPreElement.h>
  59. #include <LibWeb/HTML/HTMLProgressElement.h>
  60. #include <LibWeb/HTML/HTMLQuoteElement.h>
  61. #include <LibWeb/HTML/HTMLScriptElement.h>
  62. #include <LibWeb/HTML/HTMLSelectElement.h>
  63. #include <LibWeb/HTML/HTMLSlotElement.h>
  64. #include <LibWeb/HTML/HTMLSourceElement.h>
  65. #include <LibWeb/HTML/HTMLSpanElement.h>
  66. #include <LibWeb/HTML/HTMLStyleElement.h>
  67. #include <LibWeb/HTML/HTMLSummaryElement.h>
  68. #include <LibWeb/HTML/HTMLTableCaptionElement.h>
  69. #include <LibWeb/HTML/HTMLTableCellElement.h>
  70. #include <LibWeb/HTML/HTMLTableColElement.h>
  71. #include <LibWeb/HTML/HTMLTableElement.h>
  72. #include <LibWeb/HTML/HTMLTableRowElement.h>
  73. #include <LibWeb/HTML/HTMLTableSectionElement.h>
  74. #include <LibWeb/HTML/HTMLTemplateElement.h>
  75. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  76. #include <LibWeb/HTML/HTMLTimeElement.h>
  77. #include <LibWeb/HTML/HTMLTitleElement.h>
  78. #include <LibWeb/HTML/HTMLTrackElement.h>
  79. #include <LibWeb/HTML/HTMLUListElement.h>
  80. #include <LibWeb/HTML/HTMLUnknownElement.h>
  81. #include <LibWeb/HTML/HTMLVideoElement.h>
  82. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  83. #include <LibWeb/Namespace.h>
  84. #include <LibWeb/SVG/SVGCircleElement.h>
  85. #include <LibWeb/SVG/SVGClipPathElement.h>
  86. #include <LibWeb/SVG/SVGDefsElement.h>
  87. #include <LibWeb/SVG/SVGEllipseElement.h>
  88. #include <LibWeb/SVG/SVGForeignObjectElement.h>
  89. #include <LibWeb/SVG/SVGGElement.h>
  90. #include <LibWeb/SVG/SVGLineElement.h>
  91. #include <LibWeb/SVG/SVGLinearGradientElement.h>
  92. #include <LibWeb/SVG/SVGPathElement.h>
  93. #include <LibWeb/SVG/SVGPolygonElement.h>
  94. #include <LibWeb/SVG/SVGPolylineElement.h>
  95. #include <LibWeb/SVG/SVGRadialGradientElement.h>
  96. #include <LibWeb/SVG/SVGRectElement.h>
  97. #include <LibWeb/SVG/SVGSVGElement.h>
  98. #include <LibWeb/SVG/SVGStopElement.h>
  99. #include <LibWeb/SVG/SVGStyleElement.h>
  100. #include <LibWeb/SVG/SVGSymbolElement.h>
  101. #include <LibWeb/SVG/SVGTSpanElement.h>
  102. #include <LibWeb/SVG/SVGTextElement.h>
  103. #include <LibWeb/SVG/SVGTitleElement.h>
  104. #include <LibWeb/SVG/SVGUseElement.h>
  105. #include <LibWeb/SVG/TagNames.h>
  106. #include <LibWeb/WebIDL/AbstractOperations.h>
  107. namespace Web::DOM {
  108. ErrorOr<FixedArray<DeprecatedFlyString>> valid_local_names_for_given_html_element_interface(StringView html_element_interface_name)
  109. {
  110. if (html_element_interface_name == "HTMLAnchorElement"sv)
  111. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::a });
  112. if (html_element_interface_name == "HTMLAreaElement"sv)
  113. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::area });
  114. if (html_element_interface_name == "HTMLAudioElement"sv)
  115. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::audio });
  116. if (html_element_interface_name == "HTMLBaseElement"sv)
  117. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::base });
  118. if (html_element_interface_name == "HTMLBodyElement"sv)
  119. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::body });
  120. if (html_element_interface_name == "HTMLBRElement"sv)
  121. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::br });
  122. if (html_element_interface_name == "HTMLButtonElement"sv)
  123. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::button });
  124. if (html_element_interface_name == "HTMLCanvasElement"sv)
  125. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::canvas });
  126. if (html_element_interface_name == "HTMLDataElement"sv)
  127. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::data });
  128. if (html_element_interface_name == "HTMLDataListElement"sv)
  129. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::datalist });
  130. if (html_element_interface_name == "HTMLDetailsElement"sv)
  131. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::details });
  132. if (html_element_interface_name == "HTMLDialogElement"sv)
  133. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::dialog });
  134. if (html_element_interface_name == "HTMLDirectoryElement"sv)
  135. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::dir });
  136. if (html_element_interface_name == "HTMLDivElement"sv)
  137. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::div });
  138. if (html_element_interface_name == "HTMLDListElement"sv)
  139. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::dl });
  140. if (html_element_interface_name == "HTMLEmbedElement"sv)
  141. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::embed });
  142. if (html_element_interface_name == "HTMLFieldsetElement"sv)
  143. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::fieldset });
  144. if (html_element_interface_name == "HTMLFontElement"sv)
  145. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::font });
  146. if (html_element_interface_name == "HTMLFormElement"sv)
  147. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::form });
  148. if (html_element_interface_name == "HTMLFrameElement"sv)
  149. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::frame });
  150. if (html_element_interface_name == "HTMLFrameSetElement"sv)
  151. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::frameset });
  152. if (html_element_interface_name == "HTMLHeadElement"sv)
  153. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::head });
  154. if (html_element_interface_name == "HTMLHeadingElement"sv)
  155. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::h1, HTML::TagNames::h2, HTML::TagNames::h3, HTML::TagNames::h4, HTML::TagNames::h5, HTML::TagNames::h6 });
  156. if (html_element_interface_name == "HTMLHRElement"sv)
  157. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::hr });
  158. if (html_element_interface_name == "HTMLHtmlElement"sv)
  159. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::html });
  160. if (html_element_interface_name == "HTMLIFrameElement"sv)
  161. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::iframe });
  162. if (html_element_interface_name == "HTMLImageElement"sv)
  163. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::img });
  164. if (html_element_interface_name == "HTMLInputElement"sv)
  165. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::input });
  166. if (html_element_interface_name == "HTMLLabelElement"sv)
  167. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::label });
  168. if (html_element_interface_name == "HTMLLIElement"sv)
  169. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::li });
  170. if (html_element_interface_name == "HTMLLinkElement"sv)
  171. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::link });
  172. if (html_element_interface_name == "HTMLMapElement"sv)
  173. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::map });
  174. if (html_element_interface_name == "HTMLMarqueeElement"sv)
  175. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::marquee });
  176. if (html_element_interface_name == "HTMLMenuElement"sv)
  177. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::menu });
  178. if (html_element_interface_name == "HTMLMeterElement"sv)
  179. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::meter });
  180. if (html_element_interface_name == "HTMLModElement"sv)
  181. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::ins, HTML::TagNames::del });
  182. if (html_element_interface_name == "HTMLObjectElement"sv)
  183. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::object });
  184. if (html_element_interface_name == "HTMLOutputElement"sv)
  185. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::output });
  186. if (html_element_interface_name == "HTMLParagraphElement"sv)
  187. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::p });
  188. if (html_element_interface_name == "HTMLParamElement"sv)
  189. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::param });
  190. if (html_element_interface_name == "HTMLPictureElement"sv)
  191. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::picture });
  192. if (html_element_interface_name == "HTMLPreElement"sv)
  193. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::pre, HTML::TagNames::listing, HTML::TagNames::xmp });
  194. if (html_element_interface_name == "HTMLProgressElement"sv)
  195. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::progress });
  196. if (html_element_interface_name == "HTMLQuoteElement"sv)
  197. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::blockquote, HTML::TagNames::q });
  198. if (html_element_interface_name == "HTMLScriptElement"sv)
  199. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::script });
  200. if (html_element_interface_name == "HTMLSelectElement"sv)
  201. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::select });
  202. if (html_element_interface_name == "HTMLSlotElement"sv)
  203. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::slot });
  204. if (html_element_interface_name == "HTMLSourceElement"sv)
  205. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::source });
  206. if (html_element_interface_name == "HTMLSpanElement"sv)
  207. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::span });
  208. if (html_element_interface_name == "HTMLStyleElement"sv)
  209. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::style });
  210. if (html_element_interface_name == "HTMLTableCaptionElement"sv)
  211. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::caption });
  212. if (html_element_interface_name == "HTMLTableCellElement"sv)
  213. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::td, HTML::TagNames::th });
  214. if (html_element_interface_name == "HTMLTableColElement"sv)
  215. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::colgroup, HTML::TagNames::col });
  216. if (html_element_interface_name == "HTMLTableElement"sv)
  217. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::table });
  218. if (html_element_interface_name == "HTMLTableSectionElement"sv)
  219. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::tbody, HTML::TagNames::thead, HTML::TagNames::tfoot });
  220. if (html_element_interface_name == "HTMLTemplateElement"sv)
  221. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::template_ });
  222. if (html_element_interface_name == "HTMLTextAreaElement"sv)
  223. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::textarea });
  224. if (html_element_interface_name == "HTMLTimeElement"sv)
  225. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::time });
  226. if (html_element_interface_name == "HTMLTitleElement"sv)
  227. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::title });
  228. if (html_element_interface_name == "HTMLTrackElement"sv)
  229. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::track });
  230. if (html_element_interface_name == "HTMLUListElement"sv)
  231. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::ul });
  232. if (html_element_interface_name == "HTMLVideoElement"sv)
  233. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::video });
  234. if (html_element_interface_name == "HTMLElement"sv)
  235. return FixedArray<DeprecatedFlyString>::create({ HTML::TagNames::article, HTML::TagNames::section, HTML::TagNames::nav, HTML::TagNames::aside, HTML::TagNames::hgroup, HTML::TagNames::header, HTML::TagNames::footer, HTML::TagNames::address, HTML::TagNames::dt, HTML::TagNames::dd, HTML::TagNames::figure, HTML::TagNames::figcaption, HTML::TagNames::main, HTML::TagNames::em, HTML::TagNames::strong, HTML::TagNames::small, HTML::TagNames::s, HTML::TagNames::cite, HTML::TagNames::dfn, HTML::TagNames::abbr, HTML::TagNames::ruby, HTML::TagNames::rt, HTML::TagNames::rp, HTML::TagNames::code, HTML::TagNames::var, HTML::TagNames::samp, HTML::TagNames::kbd, HTML::TagNames::sub, HTML::TagNames::sup, HTML::TagNames::i, HTML::TagNames::b, HTML::TagNames::u, HTML::TagNames::mark, HTML::TagNames::bdi, HTML::TagNames::bdo, HTML::TagNames::wbr, HTML::TagNames::summary, HTML::TagNames::noscript, HTML::TagNames::acronym, HTML::TagNames::basefont, HTML::TagNames::big, HTML::TagNames::center, HTML::TagNames::nobr, HTML::TagNames::noembed, HTML::TagNames::noframes, HTML::TagNames::plaintext, HTML::TagNames::rb, HTML::TagNames::rtc, HTML::TagNames::strike, HTML::TagNames::tt });
  236. return FixedArray<DeprecatedFlyString>::create({});
  237. }
  238. // https://html.spec.whatwg.org/multipage/dom.html#elements-in-the-dom%3Aelement-interface
  239. bool is_unknown_html_element(DeprecatedFlyString const& tag_name)
  240. {
  241. // NOTE: This is intentionally case-sensitive.
  242. // 1. If name is applet, bgsound, blink, isindex, keygen, multicol, nextid, or spacer, then return HTMLUnknownElement.
  243. if (tag_name.is_one_of(HTML::TagNames::applet, HTML::TagNames::bgsound, HTML::TagNames::blink, HTML::TagNames::isindex, HTML::TagNames::keygen, HTML::TagNames::multicol, HTML::TagNames::nextid, HTML::TagNames::spacer))
  244. return true;
  245. // 2. If name is acronym, basefont, big, center, nobr, noembed, noframes, plaintext, rb, rtc, strike, or tt, then return HTMLElement.
  246. // 3. If name is listing or xmp, then return HTMLPreElement.
  247. // 4. Otherwise, if this specification defines an interface appropriate for the element type corresponding to the local name name, then return that interface.
  248. // 5. If other applicable specifications define an appropriate interface for name, then return the interface they define.
  249. #define __ENUMERATE_HTML_TAG(name) \
  250. if (tag_name == HTML::TagNames::name) \
  251. return false;
  252. ENUMERATE_HTML_TAGS
  253. #undef __ENUMERATE_HTML_TAG
  254. // 6. If name is a valid custom element name, then return HTMLElement.
  255. if (HTML::is_valid_custom_element_name(tag_name))
  256. return false;
  257. // 7. Return HTMLUnknownElement.
  258. return true;
  259. }
  260. // https://html.spec.whatwg.org/#elements-in-the-dom:element-interface
  261. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_html_element(JS::Realm& realm, Document& document, QualifiedName qualified_name)
  262. {
  263. auto lowercase_tag_name = qualified_name.local_name().to_lowercase();
  264. if (lowercase_tag_name == HTML::TagNames::a)
  265. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLAnchorElement>(realm, document, move(qualified_name)));
  266. if (lowercase_tag_name == HTML::TagNames::area)
  267. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLAreaElement>(realm, document, move(qualified_name)));
  268. if (lowercase_tag_name == HTML::TagNames::audio)
  269. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLAudioElement>(realm, document, move(qualified_name)));
  270. if (lowercase_tag_name == HTML::TagNames::base)
  271. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLBaseElement>(realm, document, move(qualified_name)));
  272. if (lowercase_tag_name == HTML::TagNames::blink)
  273. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLBlinkElement>(realm, document, move(qualified_name)));
  274. if (lowercase_tag_name == HTML::TagNames::body)
  275. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLBodyElement>(realm, document, move(qualified_name)));
  276. if (lowercase_tag_name == HTML::TagNames::br)
  277. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLBRElement>(realm, document, move(qualified_name)));
  278. if (lowercase_tag_name == HTML::TagNames::button)
  279. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLButtonElement>(realm, document, move(qualified_name)));
  280. if (lowercase_tag_name == HTML::TagNames::canvas)
  281. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLCanvasElement>(realm, document, move(qualified_name)));
  282. if (lowercase_tag_name == HTML::TagNames::data)
  283. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLDataElement>(realm, document, move(qualified_name)));
  284. if (lowercase_tag_name == HTML::TagNames::datalist)
  285. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLDataListElement>(realm, document, move(qualified_name)));
  286. if (lowercase_tag_name == HTML::TagNames::details)
  287. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLDetailsElement>(realm, document, move(qualified_name)));
  288. if (lowercase_tag_name == HTML::TagNames::dialog)
  289. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLDialogElement>(realm, document, move(qualified_name)));
  290. if (lowercase_tag_name == HTML::TagNames::dir)
  291. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLDirectoryElement>(realm, document, move(qualified_name)));
  292. if (lowercase_tag_name == HTML::TagNames::div)
  293. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLDivElement>(realm, document, move(qualified_name)));
  294. if (lowercase_tag_name == HTML::TagNames::dl)
  295. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLDListElement>(realm, document, move(qualified_name)));
  296. if (lowercase_tag_name == HTML::TagNames::embed)
  297. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLEmbedElement>(realm, document, move(qualified_name)));
  298. if (lowercase_tag_name == HTML::TagNames::fieldset)
  299. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLFieldSetElement>(realm, document, move(qualified_name)));
  300. if (lowercase_tag_name == HTML::TagNames::font)
  301. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLFontElement>(realm, document, move(qualified_name)));
  302. if (lowercase_tag_name == HTML::TagNames::form)
  303. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLFormElement>(realm, document, move(qualified_name)));
  304. if (lowercase_tag_name == HTML::TagNames::frame)
  305. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLFrameElement>(realm, document, move(qualified_name)));
  306. if (lowercase_tag_name == HTML::TagNames::frameset)
  307. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLFrameSetElement>(realm, document, move(qualified_name)));
  308. if (lowercase_tag_name == HTML::TagNames::head)
  309. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLHeadElement>(realm, document, move(qualified_name)));
  310. if (lowercase_tag_name.is_one_of(HTML::TagNames::h1, HTML::TagNames::h2, HTML::TagNames::h3, HTML::TagNames::h4, HTML::TagNames::h5, HTML::TagNames::h6))
  311. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLHeadingElement>(realm, document, move(qualified_name)));
  312. if (lowercase_tag_name == HTML::TagNames::hr)
  313. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLHRElement>(realm, document, move(qualified_name)));
  314. if (lowercase_tag_name == HTML::TagNames::html)
  315. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLHtmlElement>(realm, document, move(qualified_name)));
  316. if (lowercase_tag_name == HTML::TagNames::iframe)
  317. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLIFrameElement>(realm, document, move(qualified_name)));
  318. if (lowercase_tag_name == HTML::TagNames::img)
  319. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLImageElement>(realm, document, move(qualified_name)));
  320. if (lowercase_tag_name == HTML::TagNames::input)
  321. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLInputElement>(realm, document, move(qualified_name)));
  322. if (lowercase_tag_name == HTML::TagNames::label)
  323. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLLabelElement>(realm, document, move(qualified_name)));
  324. if (lowercase_tag_name == HTML::TagNames::legend)
  325. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLLegendElement>(realm, document, move(qualified_name)));
  326. if (lowercase_tag_name == HTML::TagNames::li)
  327. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLLIElement>(realm, document, move(qualified_name)));
  328. if (lowercase_tag_name == HTML::TagNames::link)
  329. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLLinkElement>(realm, document, move(qualified_name)));
  330. if (lowercase_tag_name == HTML::TagNames::map)
  331. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLMapElement>(realm, document, move(qualified_name)));
  332. if (lowercase_tag_name == HTML::TagNames::marquee)
  333. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLMarqueeElement>(realm, document, move(qualified_name)));
  334. if (lowercase_tag_name == HTML::TagNames::menu)
  335. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLMenuElement>(realm, document, move(qualified_name)));
  336. if (lowercase_tag_name == HTML::TagNames::meta)
  337. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLMetaElement>(realm, document, move(qualified_name)));
  338. if (lowercase_tag_name == HTML::TagNames::meter)
  339. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLMeterElement>(realm, document, move(qualified_name)));
  340. if (lowercase_tag_name.is_one_of(HTML::TagNames::ins, HTML::TagNames::del))
  341. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLModElement>(realm, document, move(qualified_name)));
  342. if (lowercase_tag_name == HTML::TagNames::object)
  343. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLObjectElement>(realm, document, move(qualified_name)));
  344. if (lowercase_tag_name == HTML::TagNames::ol)
  345. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLOListElement>(realm, document, move(qualified_name)));
  346. if (lowercase_tag_name == HTML::TagNames::optgroup)
  347. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLOptGroupElement>(realm, document, move(qualified_name)));
  348. if (lowercase_tag_name == HTML::TagNames::option)
  349. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLOptionElement>(realm, document, move(qualified_name)));
  350. if (lowercase_tag_name == HTML::TagNames::output)
  351. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLOutputElement>(realm, document, move(qualified_name)));
  352. if (lowercase_tag_name == HTML::TagNames::p)
  353. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLParagraphElement>(realm, document, move(qualified_name)));
  354. if (lowercase_tag_name == HTML::TagNames::param)
  355. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLParamElement>(realm, document, move(qualified_name)));
  356. if (lowercase_tag_name == HTML::TagNames::picture)
  357. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLPictureElement>(realm, document, move(qualified_name)));
  358. // NOTE: The obsolete elements "listing" and "xmp" are explicitly mapped to HTMLPreElement in the specification.
  359. if (lowercase_tag_name.is_one_of(HTML::TagNames::pre, HTML::TagNames::listing, HTML::TagNames::xmp))
  360. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLPreElement>(realm, document, move(qualified_name)));
  361. if (lowercase_tag_name == HTML::TagNames::progress)
  362. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLProgressElement>(realm, document, move(qualified_name)));
  363. if (lowercase_tag_name.is_one_of(HTML::TagNames::blockquote, HTML::TagNames::q))
  364. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLQuoteElement>(realm, document, move(qualified_name)));
  365. if (lowercase_tag_name == HTML::TagNames::script)
  366. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLScriptElement>(realm, document, move(qualified_name)));
  367. if (lowercase_tag_name == HTML::TagNames::select)
  368. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLSelectElement>(realm, document, move(qualified_name)));
  369. if (lowercase_tag_name == HTML::TagNames::slot)
  370. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLSlotElement>(realm, document, move(qualified_name)));
  371. if (lowercase_tag_name == HTML::TagNames::source)
  372. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLSourceElement>(realm, document, move(qualified_name)));
  373. if (lowercase_tag_name == HTML::TagNames::span)
  374. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLSpanElement>(realm, document, move(qualified_name)));
  375. if (lowercase_tag_name == HTML::TagNames::style)
  376. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLStyleElement>(realm, document, move(qualified_name)));
  377. if (lowercase_tag_name == HTML::TagNames::summary)
  378. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLSummaryElement>(realm, document, move(qualified_name)));
  379. if (lowercase_tag_name == HTML::TagNames::caption)
  380. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTableCaptionElement>(realm, document, move(qualified_name)));
  381. if (lowercase_tag_name.is_one_of(Web::HTML::TagNames::td, Web::HTML::TagNames::th))
  382. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTableCellElement>(realm, document, move(qualified_name)));
  383. if (lowercase_tag_name.is_one_of(HTML::TagNames::colgroup, HTML::TagNames::col))
  384. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTableColElement>(realm, document, move(qualified_name)));
  385. if (lowercase_tag_name == HTML::TagNames::table)
  386. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTableElement>(realm, document, move(qualified_name)));
  387. if (lowercase_tag_name == HTML::TagNames::tr)
  388. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTableRowElement>(realm, document, move(qualified_name)));
  389. if (lowercase_tag_name.is_one_of(HTML::TagNames::tbody, HTML::TagNames::thead, HTML::TagNames::tfoot))
  390. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTableSectionElement>(realm, document, move(qualified_name)));
  391. if (lowercase_tag_name == HTML::TagNames::template_)
  392. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTemplateElement>(realm, document, move(qualified_name)));
  393. if (lowercase_tag_name == HTML::TagNames::textarea)
  394. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTextAreaElement>(realm, document, move(qualified_name)));
  395. if (lowercase_tag_name == HTML::TagNames::time)
  396. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTimeElement>(realm, document, move(qualified_name)));
  397. if (lowercase_tag_name == HTML::TagNames::title)
  398. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTitleElement>(realm, document, move(qualified_name)));
  399. if (lowercase_tag_name == HTML::TagNames::track)
  400. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLTrackElement>(realm, document, move(qualified_name)));
  401. if (lowercase_tag_name == HTML::TagNames::ul)
  402. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLUListElement>(realm, document, move(qualified_name)));
  403. if (lowercase_tag_name == HTML::TagNames::video)
  404. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLVideoElement>(realm, document, move(qualified_name)));
  405. if (lowercase_tag_name.is_one_of(
  406. HTML::TagNames::article, HTML::TagNames::section, HTML::TagNames::nav, HTML::TagNames::aside, HTML::TagNames::hgroup, HTML::TagNames::header, HTML::TagNames::footer, HTML::TagNames::address, HTML::TagNames::dt, HTML::TagNames::dd, HTML::TagNames::figure, HTML::TagNames::figcaption, HTML::TagNames::main, HTML::TagNames::em, HTML::TagNames::strong, HTML::TagNames::small, HTML::TagNames::s, HTML::TagNames::cite, HTML::TagNames::dfn, HTML::TagNames::abbr, HTML::TagNames::ruby, HTML::TagNames::rt, HTML::TagNames::rp, HTML::TagNames::code, HTML::TagNames::var, HTML::TagNames::samp, HTML::TagNames::kbd, HTML::TagNames::sub, HTML::TagNames::sup, HTML::TagNames::i, HTML::TagNames::b, HTML::TagNames::u, HTML::TagNames::mark, HTML::TagNames::bdi, HTML::TagNames::bdo, HTML::TagNames::wbr, HTML::TagNames::noscript,
  407. // Obsolete
  408. HTML::TagNames::acronym, HTML::TagNames::basefont, HTML::TagNames::big, HTML::TagNames::center, HTML::TagNames::nobr, HTML::TagNames::noembed, HTML::TagNames::noframes, HTML::TagNames::plaintext, HTML::TagNames::rb, HTML::TagNames::rtc, HTML::TagNames::strike, HTML::TagNames::tt))
  409. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLElement>(realm, document, move(qualified_name)));
  410. if (HTML::is_valid_custom_element_name(qualified_name.local_name()))
  411. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLElement>(realm, document, move(qualified_name)));
  412. return MUST_OR_THROW_OOM(realm.heap().allocate<HTML::HTMLUnknownElement>(realm, document, move(qualified_name)));
  413. }
  414. static WebIDL::ExceptionOr<JS::GCPtr<SVG::SVGElement>> create_svg_element(JS::Realm& realm, Document& document, QualifiedName qualified_name)
  415. {
  416. auto const& local_name = qualified_name.local_name();
  417. if (local_name == SVG::TagNames::svg)
  418. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGSVGElement>(realm, document, move(qualified_name)));
  419. // FIXME: Support SVG's mixedCase tag names properly.
  420. if (local_name.equals_ignoring_ascii_case(SVG::TagNames::clipPath))
  421. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGClipPathElement>(realm, document, move(qualified_name)));
  422. if (local_name == SVG::TagNames::circle)
  423. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGCircleElement>(realm, document, move(qualified_name)));
  424. if (local_name.equals_ignoring_ascii_case(SVG::TagNames::defs))
  425. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGDefsElement>(realm, document, move(qualified_name)));
  426. if (local_name == SVG::TagNames::ellipse)
  427. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGEllipseElement>(realm, document, move(qualified_name)));
  428. if (local_name.equals_ignoring_ascii_case(SVG::TagNames::foreignObject))
  429. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGForeignObjectElement>(realm, document, move(qualified_name)));
  430. if (local_name == SVG::TagNames::line)
  431. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGLineElement>(realm, document, move(qualified_name)));
  432. if (local_name == SVG::TagNames::linearGradient)
  433. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGLinearGradientElement>(realm, document, move(qualified_name)));
  434. if (local_name == SVG::TagNames::path)
  435. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGPathElement>(realm, document, move(qualified_name)));
  436. if (local_name == SVG::TagNames::polygon)
  437. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGPolygonElement>(realm, document, move(qualified_name)));
  438. if (local_name == SVG::TagNames::polyline)
  439. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGPolylineElement>(realm, document, move(qualified_name)));
  440. if (local_name == SVG::TagNames::radialGradient)
  441. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGRadialGradientElement>(realm, document, move(qualified_name)));
  442. if (local_name == SVG::TagNames::rect)
  443. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGRectElement>(realm, document, move(qualified_name)));
  444. if (local_name == SVG::TagNames::g)
  445. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGGElement>(realm, document, move(qualified_name)));
  446. if (local_name == SVG::TagNames::stop)
  447. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGStopElement>(realm, document, move(qualified_name)));
  448. if (local_name == SVG::TagNames::style)
  449. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGStyleElement>(realm, document, move(qualified_name)));
  450. if (local_name == SVG::TagNames::symbol)
  451. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGSymbolElement>(realm, document, move(qualified_name)));
  452. if (local_name == SVG::TagNames::text)
  453. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGTextElement>(realm, document, move(qualified_name)));
  454. if (local_name == SVG::TagNames::title)
  455. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGTitleElement>(realm, document, move(qualified_name)));
  456. if (local_name == SVG::TagNames::tspan)
  457. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGTSpanElement>(realm, document, move(qualified_name)));
  458. if (local_name == SVG::TagNames::use)
  459. return MUST_OR_THROW_OOM(realm.heap().allocate<SVG::SVGUseElement>(realm, document, move(qualified_name)));
  460. return nullptr;
  461. }
  462. // https://dom.spec.whatwg.org/#concept-create-element
  463. WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document, DeprecatedFlyString local_name, DeprecatedFlyString namespace_, DeprecatedFlyString prefix, Optional<String> is_value, bool synchronous_custom_elements_flag)
  464. {
  465. auto& realm = document.realm();
  466. // 1. If prefix was not given, let prefix be null.
  467. // NOTE: This is already taken care of by `prefix` having a default value.
  468. // 2. If is was not given, let is be null.
  469. // NOTE: This is already taken care of by `is` having a default value.
  470. // 3. Let result be null.
  471. // NOTE: We collapse this into just returning an element where necessary.
  472. // 4. Let definition be the result of looking up a custom element definition given document, namespace, localName, and is.
  473. auto definition = document.lookup_custom_element_definition(namespace_, local_name, is_value);
  474. // 5. If definition is non-null, and definition’s name is not equal to its local name (i.e., definition represents a customized built-in element), then:
  475. if (definition && definition->name() != definition->local_name()) {
  476. // 1. Let interface be the element interface for localName and the HTML namespace.
  477. // 2. Set result to a new element that implements interface, with no attributes, namespace set to the HTML namespace,
  478. // namespace prefix set to prefix, local name set to localName, custom element state set to "undefined", custom element definition set to null,
  479. // is value set to is, and node document set to document.
  480. auto element = TRY(create_html_element(realm, document, QualifiedName { local_name, prefix, Namespace::HTML }));
  481. // 3. If the synchronous custom elements flag is set, then run this step while catching any exceptions:
  482. if (synchronous_custom_elements_flag) {
  483. // 1. Upgrade element using definition.
  484. auto upgrade_result = element->upgrade_element(*definition);
  485. // If this step threw an exception, then:
  486. if (upgrade_result.is_throw_completion()) {
  487. // 1. Report the exception.
  488. HTML::report_exception(upgrade_result, realm);
  489. // 2. Set result’s custom element state to "failed".
  490. element->set_custom_element_state(CustomElementState::Failed);
  491. }
  492. }
  493. // 4. Otherwise, enqueue a custom element upgrade reaction given result and definition.
  494. else {
  495. element->enqueue_a_custom_element_upgrade_reaction(*definition);
  496. }
  497. return element;
  498. }
  499. // 6. Otherwise, if definition is non-null, then:
  500. if (definition) {
  501. // 1. If the synchronous custom elements flag is set, then run these steps while catching any exceptions:
  502. if (synchronous_custom_elements_flag) {
  503. auto synchronously_upgrade_custom_element = [&]() -> JS::ThrowCompletionOr<JS::NonnullGCPtr<HTML::HTMLElement>> {
  504. auto& vm = document.vm();
  505. // 1. Let C be definition’s constructor.
  506. auto& constructor = definition->constructor();
  507. // 2. Set result to the result of constructing C, with no arguments.
  508. auto result = TRY(WebIDL::construct(constructor));
  509. // FIXME: 3. Assert: result’s custom element state and custom element definition are initialized.
  510. // FIXME: 4. Assert: result’s namespace is the HTML namespace.
  511. // Spec Note: IDL enforces that result is an HTMLElement object, which all use the HTML namespace.
  512. // IDL does not currently convert the object for us, so we will have to do it here.
  513. if (!result.has_value() || !result->is_object() || !is<HTML::HTMLElement>(result->as_object()))
  514. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "HTMLElement"sv);
  515. JS::NonnullGCPtr<HTML::HTMLElement> element = verify_cast<HTML::HTMLElement>(result->as_object());
  516. // 5. If result’s attribute list is not empty, then throw a "NotSupportedError" DOMException.
  517. if (element->has_attributes())
  518. return JS::throw_completion(WebIDL::NotSupportedError::create(realm, "Synchronously created custom element cannot have attributes"sv));
  519. // 6. If result has children, then throw a "NotSupportedError" DOMException.
  520. if (element->has_children())
  521. return JS::throw_completion(WebIDL::NotSupportedError::create(realm, "Synchronously created custom element cannot have children"sv));
  522. // 7. If result’s parent is not null, then throw a "NotSupportedError" DOMException.
  523. if (element->parent())
  524. return JS::throw_completion(WebIDL::NotSupportedError::create(realm, "Synchronously created custom element cannot have a parent"sv));
  525. // 8. If result’s node document is not document, then throw a "NotSupportedError" DOMException.
  526. if (&element->document() != &document)
  527. return JS::throw_completion(WebIDL::NotSupportedError::create(realm, "Synchronously created custom element must be in the same document that element creation was invoked in"sv));
  528. // 9. If result’s local name is not equal to localName, then throw a "NotSupportedError" DOMException.
  529. if (element->local_name() != local_name)
  530. return JS::throw_completion(WebIDL::NotSupportedError::create(realm, "Synchronously created custom element must have the same local name that element creation was invoked with"sv));
  531. // 10. Set result’s namespace prefix to prefix.
  532. element->set_prefix(prefix);
  533. // 11. Set result’s is value to null.
  534. element->set_is_value(Optional<String> {});
  535. return element;
  536. };
  537. auto result = synchronously_upgrade_custom_element();
  538. // If any of these steps threw an exception, then:
  539. if (result.is_throw_completion()) {
  540. // 1. Report the exception.
  541. HTML::report_exception(result, realm);
  542. // 2. Set result to a new element that implements the HTMLUnknownElement interface, with no attributes, namespace set to the HTML namespace, namespace prefix set to prefix,
  543. // local name set to localName, custom element state set to "failed", custom element definition set to null, is value set to null, and node document set to document.
  544. JS::NonnullGCPtr<Element> element = realm.heap().allocate<HTML::HTMLUnknownElement>(realm, document, QualifiedName { local_name, prefix, Namespace::HTML }).release_allocated_value_but_fixme_should_propagate_errors();
  545. element->set_custom_element_state(CustomElementState::Failed);
  546. return element;
  547. }
  548. return result.release_value();
  549. }
  550. // 2. Otherwise:
  551. // 1. Set result to a new element that implements the HTMLElement interface, with no attributes, namespace set to the HTML namespace, namespace prefix set to prefix,
  552. // local name set to localName, custom element state set to "undefined", custom element definition set to null, is value set to null, and node document set to document.
  553. JS::NonnullGCPtr<Element> element = realm.heap().allocate<HTML::HTMLElement>(realm, document, QualifiedName { local_name, prefix, Namespace::HTML }).release_allocated_value_but_fixme_should_propagate_errors();
  554. element->set_custom_element_state(CustomElementState::Undefined);
  555. // 2. Enqueue a custom element upgrade reaction given result and definition.
  556. element->enqueue_a_custom_element_upgrade_reaction(*definition);
  557. return element;
  558. }
  559. // 7. Otherwise:
  560. // 1. Let interface be the element interface for localName and namespace.
  561. // 2. Set result to a new element that implements interface, with no attributes, namespace set to namespace, namespace prefix set to prefix,
  562. // local name set to localName, custom element state set to "uncustomized", custom element definition set to null, is value set to is,
  563. // and node document set to document.
  564. auto qualified_name = QualifiedName { local_name, prefix, namespace_ };
  565. if (namespace_ == Namespace::HTML) {
  566. auto element = TRY(create_html_element(realm, document, move(qualified_name)));
  567. element->set_is_value(move(is_value));
  568. element->set_custom_element_state(CustomElementState::Uncustomized);
  569. // 3. If namespace is the HTML namespace, and either localName is a valid custom element name or is is non-null,
  570. // then set result’s custom element state to "undefined".
  571. if (HTML::is_valid_custom_element_name(local_name) || is_value.has_value())
  572. element->set_custom_element_state(CustomElementState::Undefined);
  573. return element;
  574. }
  575. if (namespace_ == Namespace::SVG) {
  576. auto element = TRY(create_svg_element(realm, document, qualified_name));
  577. if (element) {
  578. element->set_is_value(move(is_value));
  579. element->set_custom_element_state(CustomElementState::Uncustomized);
  580. return JS::NonnullGCPtr<Element> { *element };
  581. }
  582. }
  583. // 8. Return result.
  584. // NOTE: See step 3.
  585. // https://dom.spec.whatwg.org/#concept-element-interface
  586. // The element interface for any name and namespace is Element, unless stated otherwise.
  587. dbgln("Potential FIXME: Creating unknown generic element '{}' in namespace '{}'", local_name, namespace_);
  588. JS::NonnullGCPtr<Element> element = realm.heap().allocate<DOM::Element>(realm, document, move(qualified_name)).release_allocated_value_but_fixme_should_propagate_errors();
  589. element->set_is_value(move(is_value));
  590. element->set_custom_element_state(CustomElementState::Uncustomized);
  591. return element;
  592. }
  593. }