Browse Source

LibHTML: Move Element construction to a separate file

We now have create_element(document, tag_name) in ElementFactory.
This will be useful for constructing new elements outside of parsing.
Andreas Kling 5 years ago
parent
commit
35def88c8b

+ 3 - 2
Libraries/LibHTML/DOM/Document.cpp

@@ -4,6 +4,7 @@
 #include <LibHTML/DOM/Document.h>
 #include <LibHTML/DOM/Document.h>
 #include <LibHTML/DOM/DocumentType.h>
 #include <LibHTML/DOM/DocumentType.h>
 #include <LibHTML/DOM/Element.h>
 #include <LibHTML/DOM/Element.h>
+#include <LibHTML/DOM/ElementFactory.h>
 #include <LibHTML/DOM/HTMLBodyElement.h>
 #include <LibHTML/DOM/HTMLBodyElement.h>
 #include <LibHTML/DOM/HTMLHeadElement.h>
 #include <LibHTML/DOM/HTMLHeadElement.h>
 #include <LibHTML/DOM/HTMLHtmlElement.h>
 #include <LibHTML/DOM/HTMLHtmlElement.h>
@@ -36,8 +37,8 @@ void Document::fixup()
     if (is<HTMLHtmlElement>(first_child()->next_sibling()))
     if (is<HTMLHtmlElement>(first_child()->next_sibling()))
         return;
         return;
 
 
-    auto body = adopt(*new HTMLBodyElement(*this, "body"));
-    auto html = adopt(*new HTMLHtmlElement(*this, "html"));
+    auto body = create_element(*this, "body");
+    auto html = create_element(*this, "html");
     html->append_child(body);
     html->append_child(body);
     this->donate_all_children_to(body);
     this->donate_all_children_to(body);
     this->append_child(html);
     this->append_child(html);

+ 49 - 0
Libraries/LibHTML/DOM/ElementFactory.cpp

@@ -0,0 +1,49 @@
+#include <LibHTML/DOM/ElementFactory.h>
+#include <LibHTML/DOM/HTMLAnchorElement.h>
+#include <LibHTML/DOM/HTMLBlinkElement.h>
+#include <LibHTML/DOM/HTMLBodyElement.h>
+#include <LibHTML/DOM/HTMLFontElement.h>
+#include <LibHTML/DOM/HTMLHRElement.h>
+#include <LibHTML/DOM/HTMLHeadElement.h>
+#include <LibHTML/DOM/HTMLHeadingElement.h>
+#include <LibHTML/DOM/HTMLHtmlElement.h>
+#include <LibHTML/DOM/HTMLImageElement.h>
+#include <LibHTML/DOM/HTMLLinkElement.h>
+#include <LibHTML/DOM/HTMLStyleElement.h>
+#include <LibHTML/DOM/HTMLTitleElement.h>
+
+NonnullRefPtr<Element> create_element(Document& document, const String& tag_name)
+{
+    auto lowercase_tag_name = tag_name.to_lowercase();
+    if (lowercase_tag_name == "a")
+        return adopt(*new HTMLAnchorElement(document, tag_name));
+    if (lowercase_tag_name == "html")
+        return adopt(*new HTMLHtmlElement(document, tag_name));
+    if (lowercase_tag_name == "head")
+        return adopt(*new HTMLHeadElement(document, tag_name));
+    if (lowercase_tag_name == "body")
+        return adopt(*new HTMLBodyElement(document, tag_name));
+    if (lowercase_tag_name == "font")
+        return adopt(*new HTMLFontElement(document, tag_name));
+    if (lowercase_tag_name == "hr")
+        return adopt(*new HTMLHRElement(document, tag_name));
+    if (lowercase_tag_name == "style")
+        return adopt(*new HTMLStyleElement(document, tag_name));
+    if (lowercase_tag_name == "title")
+        return adopt(*new HTMLTitleElement(document, tag_name));
+    if (lowercase_tag_name == "link")
+        return adopt(*new HTMLLinkElement(document, tag_name));
+    if (lowercase_tag_name == "img")
+        return adopt(*new HTMLImageElement(document, tag_name));
+    if (lowercase_tag_name == "blink")
+        return adopt(*new HTMLBlinkElement(document, tag_name));
+    if (lowercase_tag_name == "h1"
+        || lowercase_tag_name == "h2"
+        || lowercase_tag_name == "h3"
+        || lowercase_tag_name == "h4"
+        || lowercase_tag_name == "h5"
+        || lowercase_tag_name == "h6") {
+        return adopt(*new HTMLHeadingElement(document, tag_name));
+    }
+    return adopt(*new Element(document, tag_name));
+}

+ 5 - 0
Libraries/LibHTML/DOM/ElementFactory.h

@@ -0,0 +1,5 @@
+#pragma once
+
+#include <LibHTML/DOM/Element.h>
+
+NonnullRefPtr<Element> create_element(Document&, const String& tag_name);

+ 1 - 0
Libraries/LibHTML/Makefile.shared

@@ -18,6 +18,7 @@ LIBHTML_OBJS = \
     DOM/Document.o \
     DOM/Document.o \
     DOM/Text.o \
     DOM/Text.o \
     DOM/DocumentType.o \
     DOM/DocumentType.o \
+    DOM/ElementFactory.o \
     CSS/Selector.o \
     CSS/Selector.o \
     CSS/StyleSheet.o \
     CSS/StyleSheet.o \
     CSS/StyleRule.o \
     CSS/StyleRule.o \

+ 1 - 48
Libraries/LibHTML/Parser/HTMLParser.cpp

@@ -3,59 +3,12 @@
 #include <AK/StringBuilder.h>
 #include <AK/StringBuilder.h>
 #include <LibHTML/DOM/DocumentType.h>
 #include <LibHTML/DOM/DocumentType.h>
 #include <LibHTML/DOM/Element.h>
 #include <LibHTML/DOM/Element.h>
-#include <LibHTML/DOM/HTMLAnchorElement.h>
-#include <LibHTML/DOM/HTMLBlinkElement.h>
-#include <LibHTML/DOM/HTMLBodyElement.h>
-#include <LibHTML/DOM/HTMLFontElement.h>
-#include <LibHTML/DOM/HTMLHRElement.h>
-#include <LibHTML/DOM/HTMLHeadElement.h>
-#include <LibHTML/DOM/HTMLHeadingElement.h>
-#include <LibHTML/DOM/HTMLHtmlElement.h>
-#include <LibHTML/DOM/HTMLImageElement.h>
-#include <LibHTML/DOM/HTMLLinkElement.h>
-#include <LibHTML/DOM/HTMLStyleElement.h>
-#include <LibHTML/DOM/HTMLTitleElement.h>
+#include <LibHTML/DOM/ElementFactory.h>
 #include <LibHTML/DOM/Text.h>
 #include <LibHTML/DOM/Text.h>
 #include <LibHTML/Parser/HTMLParser.h>
 #include <LibHTML/Parser/HTMLParser.h>
 #include <ctype.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <stdio.h>
 
 
-static NonnullRefPtr<Element> create_element(Document& document, const String& tag_name)
-{
-    auto lowercase_tag_name = tag_name.to_lowercase();
-    if (lowercase_tag_name == "a")
-        return adopt(*new HTMLAnchorElement(document, tag_name));
-    if (lowercase_tag_name == "html")
-        return adopt(*new HTMLHtmlElement(document, tag_name));
-    if (lowercase_tag_name == "head")
-        return adopt(*new HTMLHeadElement(document, tag_name));
-    if (lowercase_tag_name == "body")
-        return adopt(*new HTMLBodyElement(document, tag_name));
-    if (lowercase_tag_name == "font")
-        return adopt(*new HTMLFontElement(document, tag_name));
-    if (lowercase_tag_name == "hr")
-        return adopt(*new HTMLHRElement(document, tag_name));
-    if (lowercase_tag_name == "style")
-        return adopt(*new HTMLStyleElement(document, tag_name));
-    if (lowercase_tag_name == "title")
-        return adopt(*new HTMLTitleElement(document, tag_name));
-    if (lowercase_tag_name == "link")
-        return adopt(*new HTMLLinkElement(document, tag_name));
-    if (lowercase_tag_name == "img")
-        return adopt(*new HTMLImageElement(document, tag_name));
-    if (lowercase_tag_name == "blink")
-        return adopt(*new HTMLBlinkElement(document, tag_name));
-    if (lowercase_tag_name == "h1"
-        || lowercase_tag_name == "h2"
-        || lowercase_tag_name == "h3"
-        || lowercase_tag_name == "h4"
-        || lowercase_tag_name == "h5"
-        || lowercase_tag_name == "h6") {
-        return adopt(*new HTMLHeadingElement(document, tag_name));
-    }
-    return adopt(*new Element(document, tag_name));
-}
-
 static bool is_valid_in_attribute_name(char ch)
 static bool is_valid_in_attribute_name(char ch)
 {
 {
     return isalnum(ch) || ch == '_' || ch == '-';
     return isalnum(ch) || ch == '_' || ch == '-';