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.
This commit is contained in:
Andreas Kling 2019-10-12 13:02:38 +02:00
parent 1f9c4ffd21
commit 35def88c8b
Notes: sideshowbarker 2024-07-19 11:43:49 +09:00
5 changed files with 59 additions and 50 deletions

View file

@ -4,6 +4,7 @@
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/DocumentType.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/DOM/ElementFactory.h>
#include <LibHTML/DOM/HTMLBodyElement.h>
#include <LibHTML/DOM/HTMLHeadElement.h>
#include <LibHTML/DOM/HTMLHtmlElement.h>
@ -36,8 +37,8 @@ void Document::fixup()
if (is<HTMLHtmlElement>(first_child()->next_sibling()))
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);
this->donate_all_children_to(body);
this->append_child(html);

View file

@ -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));
}

View file

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

View file

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

View file

@ -3,59 +3,12 @@
#include <AK/StringBuilder.h>
#include <LibHTML/DOM/DocumentType.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/Parser/HTMLParser.h>
#include <ctype.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)
{
return isalnum(ch) || ch == '_' || ch == '-';