ElementFactory.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/DOM/ElementFactory.h>
  27. #include <LibWeb/DOM/HTMLAnchorElement.h>
  28. #include <LibWeb/DOM/HTMLBRElement.h>
  29. #include <LibWeb/DOM/HTMLBlinkElement.h>
  30. #include <LibWeb/DOM/HTMLBodyElement.h>
  31. #include <LibWeb/DOM/HTMLCanvasElement.h>
  32. #include <LibWeb/DOM/HTMLFontElement.h>
  33. #include <LibWeb/DOM/HTMLFormElement.h>
  34. #include <LibWeb/DOM/HTMLHRElement.h>
  35. #include <LibWeb/DOM/HTMLHeadElement.h>
  36. #include <LibWeb/DOM/HTMLHeadingElement.h>
  37. #include <LibWeb/DOM/HTMLHtmlElement.h>
  38. #include <LibWeb/DOM/HTMLIFrameElement.h>
  39. #include <LibWeb/DOM/HTMLImageElement.h>
  40. #include <LibWeb/DOM/HTMLInputElement.h>
  41. #include <LibWeb/DOM/HTMLLinkElement.h>
  42. #include <LibWeb/DOM/HTMLObjectElement.h>
  43. #include <LibWeb/DOM/HTMLScriptElement.h>
  44. #include <LibWeb/DOM/HTMLStyleElement.h>
  45. #include <LibWeb/DOM/HTMLTableCellElement.h>
  46. #include <LibWeb/DOM/HTMLTableElement.h>
  47. #include <LibWeb/DOM/HTMLTableRowElement.h>
  48. #include <LibWeb/DOM/HTMLTitleElement.h>
  49. namespace Web {
  50. NonnullRefPtr<Element> create_element(Document& document, const FlyString& tag_name)
  51. {
  52. auto lowercase_tag_name = tag_name.to_lowercase();
  53. if (lowercase_tag_name == HTML::TagNames::a)
  54. return adopt(*new HTMLAnchorElement(document, lowercase_tag_name));
  55. if (lowercase_tag_name == HTML::TagNames::html)
  56. return adopt(*new HTMLHtmlElement(document, lowercase_tag_name));
  57. if (lowercase_tag_name == HTML::TagNames::head)
  58. return adopt(*new HTMLHeadElement(document, lowercase_tag_name));
  59. if (lowercase_tag_name == HTML::TagNames::body)
  60. return adopt(*new HTMLBodyElement(document, lowercase_tag_name));
  61. if (lowercase_tag_name == HTML::TagNames::font)
  62. return adopt(*new HTMLFontElement(document, lowercase_tag_name));
  63. if (lowercase_tag_name == HTML::TagNames::hr)
  64. return adopt(*new HTMLHRElement(document, lowercase_tag_name));
  65. if (lowercase_tag_name == HTML::TagNames::style)
  66. return adopt(*new HTMLStyleElement(document, lowercase_tag_name));
  67. if (lowercase_tag_name == HTML::TagNames::title)
  68. return adopt(*new HTMLTitleElement(document, lowercase_tag_name));
  69. if (lowercase_tag_name == HTML::TagNames::link)
  70. return adopt(*new HTMLLinkElement(document, lowercase_tag_name));
  71. if (lowercase_tag_name == HTML::TagNames::img)
  72. return adopt(*new HTMLImageElement(document, lowercase_tag_name));
  73. if (lowercase_tag_name == HTML::TagNames::blink)
  74. return adopt(*new HTMLBlinkElement(document, lowercase_tag_name));
  75. if (lowercase_tag_name == HTML::TagNames::form)
  76. return adopt(*new HTMLFormElement(document, lowercase_tag_name));
  77. if (lowercase_tag_name == HTML::TagNames::input)
  78. return adopt(*new HTMLInputElement(document, lowercase_tag_name));
  79. if (lowercase_tag_name == HTML::TagNames::br)
  80. return adopt(*new HTMLBRElement(document, lowercase_tag_name));
  81. if (lowercase_tag_name == HTML::TagNames::iframe)
  82. return adopt(*new HTMLIFrameElement(document, lowercase_tag_name));
  83. if (lowercase_tag_name == HTML::TagNames::table)
  84. return adopt(*new HTMLTableElement(document, lowercase_tag_name));
  85. if (lowercase_tag_name == HTML::TagNames::tr)
  86. return adopt(*new HTMLTableRowElement(document, lowercase_tag_name));
  87. if (lowercase_tag_name == HTML::TagNames::td || lowercase_tag_name == HTML::TagNames::th)
  88. return adopt(*new HTMLTableCellElement(document, lowercase_tag_name));
  89. 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))
  90. return adopt(*new HTMLHeadingElement(document, lowercase_tag_name));
  91. if (lowercase_tag_name == HTML::TagNames::script)
  92. return adopt(*new HTMLScriptElement(document, lowercase_tag_name));
  93. if (lowercase_tag_name == HTML::TagNames::canvas)
  94. return adopt(*new HTMLCanvasElement(document, lowercase_tag_name));
  95. if (lowercase_tag_name == HTML::TagNames::object)
  96. return adopt(*new HTMLObjectElement(document, lowercase_tag_name));
  97. return adopt(*new Element(document, lowercase_tag_name));
  98. }
  99. }