ElementFactory.cpp 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/HTMLImageElement.h>
  39. #include <LibWeb/DOM/HTMLInputElement.h>
  40. #include <LibWeb/DOM/HTMLLinkElement.h>
  41. #include <LibWeb/DOM/HTMLScriptElement.h>
  42. #include <LibWeb/DOM/HTMLStyleElement.h>
  43. #include <LibWeb/DOM/HTMLTitleElement.h>
  44. namespace Web {
  45. NonnullRefPtr<Element> create_element(Document& document, const FlyString& tag_name)
  46. {
  47. auto lowercase_tag_name = tag_name.to_lowercase();
  48. if (lowercase_tag_name == "a")
  49. return adopt(*new HTMLAnchorElement(document, lowercase_tag_name));
  50. if (lowercase_tag_name == "html")
  51. return adopt(*new HTMLHtmlElement(document, lowercase_tag_name));
  52. if (lowercase_tag_name == "head")
  53. return adopt(*new HTMLHeadElement(document, lowercase_tag_name));
  54. if (lowercase_tag_name == "body")
  55. return adopt(*new HTMLBodyElement(document, lowercase_tag_name));
  56. if (lowercase_tag_name == "font")
  57. return adopt(*new HTMLFontElement(document, lowercase_tag_name));
  58. if (lowercase_tag_name == "hr")
  59. return adopt(*new HTMLHRElement(document, lowercase_tag_name));
  60. if (lowercase_tag_name == "style")
  61. return adopt(*new HTMLStyleElement(document, lowercase_tag_name));
  62. if (lowercase_tag_name == "title")
  63. return adopt(*new HTMLTitleElement(document, lowercase_tag_name));
  64. if (lowercase_tag_name == "link")
  65. return adopt(*new HTMLLinkElement(document, lowercase_tag_name));
  66. if (lowercase_tag_name == "img")
  67. return adopt(*new HTMLImageElement(document, lowercase_tag_name));
  68. if (lowercase_tag_name == "blink")
  69. return adopt(*new HTMLBlinkElement(document, lowercase_tag_name));
  70. if (lowercase_tag_name == "form")
  71. return adopt(*new HTMLFormElement(document, lowercase_tag_name));
  72. if (lowercase_tag_name == "input")
  73. return adopt(*new HTMLInputElement(document, lowercase_tag_name));
  74. if (lowercase_tag_name == "br")
  75. return adopt(*new HTMLBRElement(document, lowercase_tag_name));
  76. if (lowercase_tag_name == "h1"
  77. || lowercase_tag_name == "h2"
  78. || lowercase_tag_name == "h3"
  79. || lowercase_tag_name == "h4"
  80. || lowercase_tag_name == "h5"
  81. || lowercase_tag_name == "h6") {
  82. return adopt(*new HTMLHeadingElement(document, lowercase_tag_name));
  83. }
  84. if (lowercase_tag_name == "script")
  85. return adopt(*new HTMLScriptElement(document, lowercase_tag_name));
  86. if (lowercase_tag_name == "canvas")
  87. return adopt(*new HTMLCanvasElement(document, lowercase_tag_name));
  88. return adopt(*new Element(document, lowercase_tag_name));
  89. }
  90. }