ElementFactory.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/ElementFactory.h>
  8. #include <LibWeb/HTML/HTMLAnchorElement.h>
  9. #include <LibWeb/HTML/HTMLAreaElement.h>
  10. #include <LibWeb/HTML/HTMLAudioElement.h>
  11. #include <LibWeb/HTML/HTMLBRElement.h>
  12. #include <LibWeb/HTML/HTMLBaseElement.h>
  13. #include <LibWeb/HTML/HTMLBlinkElement.h>
  14. #include <LibWeb/HTML/HTMLBodyElement.h>
  15. #include <LibWeb/HTML/HTMLButtonElement.h>
  16. #include <LibWeb/HTML/HTMLCanvasElement.h>
  17. #include <LibWeb/HTML/HTMLDListElement.h>
  18. #include <LibWeb/HTML/HTMLDataElement.h>
  19. #include <LibWeb/HTML/HTMLDataListElement.h>
  20. #include <LibWeb/HTML/HTMLDetailsElement.h>
  21. #include <LibWeb/HTML/HTMLDialogElement.h>
  22. #include <LibWeb/HTML/HTMLDirectoryElement.h>
  23. #include <LibWeb/HTML/HTMLDivElement.h>
  24. #include <LibWeb/HTML/HTMLEmbedElement.h>
  25. #include <LibWeb/HTML/HTMLFieldSetElement.h>
  26. #include <LibWeb/HTML/HTMLFontElement.h>
  27. #include <LibWeb/HTML/HTMLFormElement.h>
  28. #include <LibWeb/HTML/HTMLFrameElement.h>
  29. #include <LibWeb/HTML/HTMLFrameSetElement.h>
  30. #include <LibWeb/HTML/HTMLHRElement.h>
  31. #include <LibWeb/HTML/HTMLHeadElement.h>
  32. #include <LibWeb/HTML/HTMLHeadingElement.h>
  33. #include <LibWeb/HTML/HTMLHtmlElement.h>
  34. #include <LibWeb/HTML/HTMLIFrameElement.h>
  35. #include <LibWeb/HTML/HTMLImageElement.h>
  36. #include <LibWeb/HTML/HTMLInputElement.h>
  37. #include <LibWeb/HTML/HTMLLIElement.h>
  38. #include <LibWeb/HTML/HTMLLabelElement.h>
  39. #include <LibWeb/HTML/HTMLLegendElement.h>
  40. #include <LibWeb/HTML/HTMLLinkElement.h>
  41. #include <LibWeb/HTML/HTMLMapElement.h>
  42. #include <LibWeb/HTML/HTMLMarqueeElement.h>
  43. #include <LibWeb/HTML/HTMLMenuElement.h>
  44. #include <LibWeb/HTML/HTMLMetaElement.h>
  45. #include <LibWeb/HTML/HTMLMeterElement.h>
  46. #include <LibWeb/HTML/HTMLModElement.h>
  47. #include <LibWeb/HTML/HTMLOListElement.h>
  48. #include <LibWeb/HTML/HTMLObjectElement.h>
  49. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  50. #include <LibWeb/HTML/HTMLOptionElement.h>
  51. #include <LibWeb/HTML/HTMLOutputElement.h>
  52. #include <LibWeb/HTML/HTMLParagraphElement.h>
  53. #include <LibWeb/HTML/HTMLParamElement.h>
  54. #include <LibWeb/HTML/HTMLPictureElement.h>
  55. #include <LibWeb/HTML/HTMLPreElement.h>
  56. #include <LibWeb/HTML/HTMLProgressElement.h>
  57. #include <LibWeb/HTML/HTMLQuoteElement.h>
  58. #include <LibWeb/HTML/HTMLScriptElement.h>
  59. #include <LibWeb/HTML/HTMLSelectElement.h>
  60. #include <LibWeb/HTML/HTMLSlotElement.h>
  61. #include <LibWeb/HTML/HTMLSourceElement.h>
  62. #include <LibWeb/HTML/HTMLSpanElement.h>
  63. #include <LibWeb/HTML/HTMLStyleElement.h>
  64. #include <LibWeb/HTML/HTMLTableCaptionElement.h>
  65. #include <LibWeb/HTML/HTMLTableCellElement.h>
  66. #include <LibWeb/HTML/HTMLTableColElement.h>
  67. #include <LibWeb/HTML/HTMLTableElement.h>
  68. #include <LibWeb/HTML/HTMLTableRowElement.h>
  69. #include <LibWeb/HTML/HTMLTableSectionElement.h>
  70. #include <LibWeb/HTML/HTMLTemplateElement.h>
  71. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  72. #include <LibWeb/HTML/HTMLTimeElement.h>
  73. #include <LibWeb/HTML/HTMLTitleElement.h>
  74. #include <LibWeb/HTML/HTMLTrackElement.h>
  75. #include <LibWeb/HTML/HTMLUListElement.h>
  76. #include <LibWeb/HTML/HTMLUnknownElement.h>
  77. #include <LibWeb/HTML/HTMLVideoElement.h>
  78. #include <LibWeb/SVG/SVGPathElement.h>
  79. #include <LibWeb/SVG/SVGSVGElement.h>
  80. #include <LibWeb/SVG/TagNames.h>
  81. namespace Web::DOM {
  82. NonnullRefPtr<Element> create_element(Document& document, const FlyString& tag_name, const FlyString& namespace_)
  83. {
  84. auto lowercase_tag_name = tag_name.to_lowercase();
  85. // FIXME: Add prefix when we support it.
  86. auto qualified_name = QualifiedName(tag_name, {}, namespace_);
  87. if (lowercase_tag_name == HTML::TagNames::a)
  88. return adopt_ref(*new HTML::HTMLAnchorElement(document, move(qualified_name)));
  89. if (lowercase_tag_name == HTML::TagNames::area)
  90. return adopt_ref(*new HTML::HTMLAreaElement(document, move(qualified_name)));
  91. if (lowercase_tag_name == HTML::TagNames::audio)
  92. return adopt_ref(*new HTML::HTMLAudioElement(document, move(qualified_name)));
  93. if (lowercase_tag_name == HTML::TagNames::base)
  94. return adopt_ref(*new HTML::HTMLBaseElement(document, move(qualified_name)));
  95. if (lowercase_tag_name == HTML::TagNames::blink)
  96. return adopt_ref(*new HTML::HTMLBlinkElement(document, move(qualified_name)));
  97. if (lowercase_tag_name == HTML::TagNames::body)
  98. return adopt_ref(*new HTML::HTMLBodyElement(document, move(qualified_name)));
  99. if (lowercase_tag_name == HTML::TagNames::br)
  100. return adopt_ref(*new HTML::HTMLBRElement(document, move(qualified_name)));
  101. if (lowercase_tag_name == HTML::TagNames::button)
  102. return adopt_ref(*new HTML::HTMLButtonElement(document, move(qualified_name)));
  103. if (lowercase_tag_name == HTML::TagNames::canvas)
  104. return adopt_ref(*new HTML::HTMLCanvasElement(document, move(qualified_name)));
  105. if (lowercase_tag_name == HTML::TagNames::data)
  106. return adopt_ref(*new HTML::HTMLDataElement(document, move(qualified_name)));
  107. if (lowercase_tag_name == HTML::TagNames::datalist)
  108. return adopt_ref(*new HTML::HTMLDataListElement(document, move(qualified_name)));
  109. if (lowercase_tag_name == HTML::TagNames::details)
  110. return adopt_ref(*new HTML::HTMLDetailsElement(document, move(qualified_name)));
  111. if (lowercase_tag_name == HTML::TagNames::dialog)
  112. return adopt_ref(*new HTML::HTMLDialogElement(document, move(qualified_name)));
  113. if (lowercase_tag_name == HTML::TagNames::dir)
  114. return adopt_ref(*new HTML::HTMLDirectoryElement(document, move(qualified_name)));
  115. if (lowercase_tag_name == HTML::TagNames::div)
  116. return adopt_ref(*new HTML::HTMLDivElement(document, move(qualified_name)));
  117. if (lowercase_tag_name == HTML::TagNames::dl)
  118. return adopt_ref(*new HTML::HTMLDListElement(document, move(qualified_name)));
  119. if (lowercase_tag_name == HTML::TagNames::embed)
  120. return adopt_ref(*new HTML::HTMLEmbedElement(document, move(qualified_name)));
  121. if (lowercase_tag_name == HTML::TagNames::fieldset)
  122. return adopt_ref(*new HTML::HTMLFieldSetElement(document, move(qualified_name)));
  123. if (lowercase_tag_name == HTML::TagNames::font)
  124. return adopt_ref(*new HTML::HTMLFontElement(document, move(qualified_name)));
  125. if (lowercase_tag_name == HTML::TagNames::form)
  126. return adopt_ref(*new HTML::HTMLFormElement(document, move(qualified_name)));
  127. if (lowercase_tag_name == HTML::TagNames::frame)
  128. return adopt_ref(*new HTML::HTMLFrameElement(document, move(qualified_name)));
  129. if (lowercase_tag_name == HTML::TagNames::frameset)
  130. return adopt_ref(*new HTML::HTMLFrameSetElement(document, move(qualified_name)));
  131. if (lowercase_tag_name == HTML::TagNames::head)
  132. return adopt_ref(*new HTML::HTMLHeadElement(document, move(qualified_name)));
  133. 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))
  134. return adopt_ref(*new HTML::HTMLHeadingElement(document, move(qualified_name)));
  135. if (lowercase_tag_name == HTML::TagNames::hr)
  136. return adopt_ref(*new HTML::HTMLHRElement(document, move(qualified_name)));
  137. if (lowercase_tag_name == HTML::TagNames::html)
  138. return adopt_ref(*new HTML::HTMLHtmlElement(document, move(qualified_name)));
  139. if (lowercase_tag_name == HTML::TagNames::iframe)
  140. return adopt_ref(*new HTML::HTMLIFrameElement(document, move(qualified_name)));
  141. if (lowercase_tag_name == HTML::TagNames::img)
  142. return adopt_ref(*new HTML::HTMLImageElement(document, move(qualified_name)));
  143. if (lowercase_tag_name == HTML::TagNames::input)
  144. return adopt_ref(*new HTML::HTMLInputElement(document, move(qualified_name)));
  145. if (lowercase_tag_name == HTML::TagNames::label)
  146. return adopt_ref(*new HTML::HTMLLabelElement(document, move(qualified_name)));
  147. if (lowercase_tag_name == HTML::TagNames::legend)
  148. return adopt_ref(*new HTML::HTMLLegendElement(document, move(qualified_name)));
  149. if (lowercase_tag_name == HTML::TagNames::li)
  150. return adopt_ref(*new HTML::HTMLLIElement(document, move(qualified_name)));
  151. if (lowercase_tag_name == HTML::TagNames::link)
  152. return adopt_ref(*new HTML::HTMLLinkElement(document, move(qualified_name)));
  153. if (lowercase_tag_name == HTML::TagNames::map)
  154. return adopt_ref(*new HTML::HTMLMapElement(document, move(qualified_name)));
  155. if (lowercase_tag_name == HTML::TagNames::marquee)
  156. return adopt_ref(*new HTML::HTMLMarqueeElement(document, move(qualified_name)));
  157. if (lowercase_tag_name == HTML::TagNames::menu)
  158. return adopt_ref(*new HTML::HTMLMenuElement(document, move(qualified_name)));
  159. if (lowercase_tag_name == HTML::TagNames::meta)
  160. return adopt_ref(*new HTML::HTMLMetaElement(document, move(qualified_name)));
  161. if (lowercase_tag_name == HTML::TagNames::meter)
  162. return adopt_ref(*new HTML::HTMLMeterElement(document, move(qualified_name)));
  163. if (lowercase_tag_name.is_one_of(HTML::TagNames::ins, HTML::TagNames::del))
  164. return adopt_ref(*new HTML::HTMLModElement(document, move(qualified_name)));
  165. if (lowercase_tag_name == HTML::TagNames::object)
  166. return adopt_ref(*new HTML::HTMLObjectElement(document, move(qualified_name)));
  167. if (lowercase_tag_name == HTML::TagNames::ol)
  168. return adopt_ref(*new HTML::HTMLOListElement(document, move(qualified_name)));
  169. if (lowercase_tag_name == HTML::TagNames::optgroup)
  170. return adopt_ref(*new HTML::HTMLOptGroupElement(document, move(qualified_name)));
  171. if (lowercase_tag_name == HTML::TagNames::option)
  172. return adopt_ref(*new HTML::HTMLOptionElement(document, move(qualified_name)));
  173. if (lowercase_tag_name == HTML::TagNames::output)
  174. return adopt_ref(*new HTML::HTMLOutputElement(document, move(qualified_name)));
  175. if (lowercase_tag_name == HTML::TagNames::p)
  176. return adopt_ref(*new HTML::HTMLParagraphElement(document, move(qualified_name)));
  177. if (lowercase_tag_name == HTML::TagNames::param)
  178. return adopt_ref(*new HTML::HTMLParamElement(document, move(qualified_name)));
  179. if (lowercase_tag_name == HTML::TagNames::picture)
  180. return adopt_ref(*new HTML::HTMLPictureElement(document, move(qualified_name)));
  181. // NOTE: The obsolete elements "listing" and "xmp" are explicitly mapped to HTMLPreElement in the specification.
  182. if (lowercase_tag_name.is_one_of(HTML::TagNames::pre, HTML::TagNames::listing, HTML::TagNames::xmp))
  183. return adopt_ref(*new HTML::HTMLPreElement(document, move(qualified_name)));
  184. if (lowercase_tag_name == HTML::TagNames::progress)
  185. return adopt_ref(*new HTML::HTMLProgressElement(document, move(qualified_name)));
  186. if (lowercase_tag_name.is_one_of(HTML::TagNames::blockquote, HTML::TagNames::q))
  187. return adopt_ref(*new HTML::HTMLQuoteElement(document, move(qualified_name)));
  188. if (lowercase_tag_name == HTML::TagNames::script)
  189. return adopt_ref(*new HTML::HTMLScriptElement(document, move(qualified_name)));
  190. if (lowercase_tag_name == HTML::TagNames::select)
  191. return adopt_ref(*new HTML::HTMLSelectElement(document, move(qualified_name)));
  192. if (lowercase_tag_name == HTML::TagNames::slot)
  193. return adopt_ref(*new HTML::HTMLSlotElement(document, move(qualified_name)));
  194. if (lowercase_tag_name == HTML::TagNames::source)
  195. return adopt_ref(*new HTML::HTMLSourceElement(document, move(qualified_name)));
  196. if (lowercase_tag_name == HTML::TagNames::span)
  197. return adopt_ref(*new HTML::HTMLSpanElement(document, move(qualified_name)));
  198. if (lowercase_tag_name == HTML::TagNames::style)
  199. return adopt_ref(*new HTML::HTMLStyleElement(document, move(qualified_name)));
  200. if (lowercase_tag_name == HTML::TagNames::caption)
  201. return adopt_ref(*new HTML::HTMLTableCaptionElement(document, move(qualified_name)));
  202. if (lowercase_tag_name.is_one_of(Web::HTML::TagNames::td, Web::HTML::TagNames::th))
  203. return adopt_ref(*new HTML::HTMLTableCellElement(document, move(qualified_name)));
  204. if (lowercase_tag_name.is_one_of(HTML::TagNames::colgroup, HTML::TagNames::col))
  205. return adopt_ref(*new HTML::HTMLTableColElement(document, move(qualified_name)));
  206. if (lowercase_tag_name == HTML::TagNames::table)
  207. return adopt_ref(*new HTML::HTMLTableElement(document, move(qualified_name)));
  208. if (lowercase_tag_name == HTML::TagNames::tr)
  209. return adopt_ref(*new HTML::HTMLTableRowElement(document, move(qualified_name)));
  210. if (lowercase_tag_name.is_one_of(HTML::TagNames::tbody, HTML::TagNames::thead, HTML::TagNames::tfoot))
  211. return adopt_ref(*new HTML::HTMLTableSectionElement(document, move(qualified_name)));
  212. if (lowercase_tag_name == HTML::TagNames::template_)
  213. return adopt_ref(*new HTML::HTMLTemplateElement(document, move(qualified_name)));
  214. if (lowercase_tag_name == HTML::TagNames::textarea)
  215. return adopt_ref(*new HTML::HTMLTextAreaElement(document, move(qualified_name)));
  216. if (lowercase_tag_name == HTML::TagNames::time)
  217. return adopt_ref(*new HTML::HTMLTimeElement(document, move(qualified_name)));
  218. if (lowercase_tag_name == HTML::TagNames::title)
  219. return adopt_ref(*new HTML::HTMLTitleElement(document, move(qualified_name)));
  220. if (lowercase_tag_name == HTML::TagNames::track)
  221. return adopt_ref(*new HTML::HTMLTrackElement(document, move(qualified_name)));
  222. if (lowercase_tag_name == HTML::TagNames::ul)
  223. return adopt_ref(*new HTML::HTMLUListElement(document, move(qualified_name)));
  224. if (lowercase_tag_name == HTML::TagNames::video)
  225. return adopt_ref(*new HTML::HTMLVideoElement(document, move(qualified_name)));
  226. if (lowercase_tag_name.is_one_of(
  227. 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,
  228. // Obsolete
  229. 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))
  230. return adopt_ref(*new HTML::HTMLElement(document, move(qualified_name)));
  231. if (lowercase_tag_name == SVG::TagNames::svg)
  232. return adopt_ref(*new SVG::SVGSVGElement(document, move(qualified_name)));
  233. if (lowercase_tag_name == SVG::TagNames::path)
  234. return adopt_ref(*new SVG::SVGPathElement(document, move(qualified_name)));
  235. // FIXME: If name is a valid custom element name, then return HTMLElement.
  236. return adopt_ref(*new HTML::HTMLUnknownElement(document, move(qualified_name)));
  237. }
  238. }