ElementFactory.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/HTMLScriptElement.h>
  43. #include <LibWeb/DOM/HTMLStyleElement.h>
  44. #include <LibWeb/DOM/HTMLTableCellElement.h>
  45. #include <LibWeb/DOM/HTMLTableElement.h>
  46. #include <LibWeb/DOM/HTMLTableRowElement.h>
  47. #include <LibWeb/DOM/HTMLTitleElement.h>
  48. namespace Web {
  49. NonnullRefPtr<Element> create_element(Document& document, const FlyString& tag_name)
  50. {
  51. auto lowercase_tag_name = tag_name.to_lowercase();
  52. if (lowercase_tag_name == HTML::TagNames::a)
  53. return adopt(*new HTMLAnchorElement(document, lowercase_tag_name));
  54. if (lowercase_tag_name == HTML::TagNames::html)
  55. return adopt(*new HTMLHtmlElement(document, lowercase_tag_name));
  56. if (lowercase_tag_name == HTML::TagNames::head)
  57. return adopt(*new HTMLHeadElement(document, lowercase_tag_name));
  58. if (lowercase_tag_name == HTML::TagNames::body)
  59. return adopt(*new HTMLBodyElement(document, lowercase_tag_name));
  60. if (lowercase_tag_name == HTML::TagNames::font)
  61. return adopt(*new HTMLFontElement(document, lowercase_tag_name));
  62. if (lowercase_tag_name == HTML::TagNames::hr)
  63. return adopt(*new HTMLHRElement(document, lowercase_tag_name));
  64. if (lowercase_tag_name == HTML::TagNames::style)
  65. return adopt(*new HTMLStyleElement(document, lowercase_tag_name));
  66. if (lowercase_tag_name == HTML::TagNames::title)
  67. return adopt(*new HTMLTitleElement(document, lowercase_tag_name));
  68. if (lowercase_tag_name == HTML::TagNames::link)
  69. return adopt(*new HTMLLinkElement(document, lowercase_tag_name));
  70. if (lowercase_tag_name == HTML::TagNames::img)
  71. return adopt(*new HTMLImageElement(document, lowercase_tag_name));
  72. if (lowercase_tag_name == HTML::TagNames::blink)
  73. return adopt(*new HTMLBlinkElement(document, lowercase_tag_name));
  74. if (lowercase_tag_name == HTML::TagNames::form)
  75. return adopt(*new HTMLFormElement(document, lowercase_tag_name));
  76. if (lowercase_tag_name == HTML::TagNames::input)
  77. return adopt(*new HTMLInputElement(document, lowercase_tag_name));
  78. if (lowercase_tag_name == HTML::TagNames::br)
  79. return adopt(*new HTMLBRElement(document, lowercase_tag_name));
  80. if (lowercase_tag_name == HTML::TagNames::iframe)
  81. return adopt(*new HTMLIFrameElement(document, lowercase_tag_name));
  82. if (lowercase_tag_name == HTML::TagNames::table)
  83. return adopt(*new HTMLTableElement(document, lowercase_tag_name));
  84. if (lowercase_tag_name == HTML::TagNames::tr)
  85. return adopt(*new HTMLTableRowElement(document, lowercase_tag_name));
  86. if (lowercase_tag_name == HTML::TagNames::td || lowercase_tag_name == HTML::TagNames::th)
  87. return adopt(*new HTMLTableCellElement(document, lowercase_tag_name));
  88. 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))
  89. return adopt(*new HTMLHeadingElement(document, lowercase_tag_name));
  90. if (lowercase_tag_name == HTML::TagNames::script)
  91. return adopt(*new HTMLScriptElement(document, lowercase_tag_name));
  92. if (lowercase_tag_name == HTML::TagNames::canvas)
  93. return adopt(*new HTMLCanvasElement(document, lowercase_tag_name));
  94. return adopt(*new Element(document, lowercase_tag_name));
  95. }
  96. }