ElementFactory.cpp 42 KB

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