ElementFactory.cpp 42 KB

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