LibWeb: Add HTML::TagNames namespace for global tag name FlyStrings

Instead of "iframe", we can now say HTML::TagNames::iframe and avoid
a FlyString lookup.
This commit is contained in:
Andreas Kling 2020-06-07 23:27:03 +02:00
parent 10851d87a2
commit 992697d99f
Notes: sideshowbarker 2024-07-19 05:46:03 +09:00
23 changed files with 210 additions and 42 deletions

View file

@ -62,6 +62,7 @@ set(SOURCES
DOM/ImageData.cpp DOM/ImageData.cpp
DOM/Node.cpp DOM/Node.cpp
DOM/ParentNode.cpp DOM/ParentNode.cpp
DOM/TagNames.cpp
DOM/Text.cpp DOM/Text.cpp
DOM/Window.cpp DOM/Window.cpp
DOM/XMLHttpRequest.cpp DOM/XMLHttpRequest.cpp

View file

@ -68,6 +68,7 @@ Document::Document(const URL& url)
, m_window(Window::create_with_document(*this)) , m_window(Window::create_with_document(*this))
{ {
HTML::AttributeNames::initialize(); HTML::AttributeNames::initialize();
HTML::TagNames::initialize();
m_style_update_timer = Core::Timer::create_single_shot(0, [this] { m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
update_style(); update_style();

View file

@ -31,6 +31,7 @@
#include <LibWeb/DOM/Attribute.h> #include <LibWeb/DOM/Attribute.h>
#include <LibWeb/DOM/AttributeNames.h> #include <LibWeb/DOM/AttributeNames.h>
#include <LibWeb/DOM/ParentNode.h> #include <LibWeb/DOM/ParentNode.h>
#include <LibWeb/DOM/TagNames.h>
#include <LibWeb/Layout/LayoutNode.h> #include <LibWeb/Layout/LayoutNode.h>
namespace Web { namespace Web {

View file

@ -52,55 +52,47 @@ namespace Web {
NonnullRefPtr<Element> create_element(Document& document, const FlyString& tag_name) NonnullRefPtr<Element> create_element(Document& document, const FlyString& tag_name)
{ {
auto lowercase_tag_name = tag_name.to_lowercase(); auto lowercase_tag_name = tag_name.to_lowercase();
if (lowercase_tag_name == "a") if (lowercase_tag_name == HTML::TagNames::a)
return adopt(*new HTMLAnchorElement(document, lowercase_tag_name)); return adopt(*new HTMLAnchorElement(document, lowercase_tag_name));
if (lowercase_tag_name == "html") if (lowercase_tag_name == HTML::TagNames::html)
return adopt(*new HTMLHtmlElement(document, lowercase_tag_name)); return adopt(*new HTMLHtmlElement(document, lowercase_tag_name));
if (lowercase_tag_name == "head") if (lowercase_tag_name == HTML::TagNames::head)
return adopt(*new HTMLHeadElement(document, lowercase_tag_name)); return adopt(*new HTMLHeadElement(document, lowercase_tag_name));
if (lowercase_tag_name == "body") if (lowercase_tag_name == HTML::TagNames::body)
return adopt(*new HTMLBodyElement(document, lowercase_tag_name)); return adopt(*new HTMLBodyElement(document, lowercase_tag_name));
if (lowercase_tag_name == "font") if (lowercase_tag_name == HTML::TagNames::font)
return adopt(*new HTMLFontElement(document, lowercase_tag_name)); return adopt(*new HTMLFontElement(document, lowercase_tag_name));
if (lowercase_tag_name == "hr") if (lowercase_tag_name == HTML::TagNames::hr)
return adopt(*new HTMLHRElement(document, lowercase_tag_name)); return adopt(*new HTMLHRElement(document, lowercase_tag_name));
if (lowercase_tag_name == "style") if (lowercase_tag_name == HTML::TagNames::style)
return adopt(*new HTMLStyleElement(document, lowercase_tag_name)); return adopt(*new HTMLStyleElement(document, lowercase_tag_name));
if (lowercase_tag_name == "title") if (lowercase_tag_name == HTML::TagNames::title)
return adopt(*new HTMLTitleElement(document, lowercase_tag_name)); return adopt(*new HTMLTitleElement(document, lowercase_tag_name));
if (lowercase_tag_name == "link") if (lowercase_tag_name == HTML::TagNames::link)
return adopt(*new HTMLLinkElement(document, lowercase_tag_name)); return adopt(*new HTMLLinkElement(document, lowercase_tag_name));
if (lowercase_tag_name == "img") if (lowercase_tag_name == HTML::TagNames::img)
return adopt(*new HTMLImageElement(document, lowercase_tag_name)); return adopt(*new HTMLImageElement(document, lowercase_tag_name));
if (lowercase_tag_name == "blink") if (lowercase_tag_name == HTML::TagNames::blink)
return adopt(*new HTMLBlinkElement(document, lowercase_tag_name)); return adopt(*new HTMLBlinkElement(document, lowercase_tag_name));
if (lowercase_tag_name == "form") if (lowercase_tag_name == HTML::TagNames::form)
return adopt(*new HTMLFormElement(document, lowercase_tag_name)); return adopt(*new HTMLFormElement(document, lowercase_tag_name));
if (lowercase_tag_name == "input") if (lowercase_tag_name == HTML::TagNames::input)
return adopt(*new HTMLInputElement(document, lowercase_tag_name)); return adopt(*new HTMLInputElement(document, lowercase_tag_name));
if (lowercase_tag_name == "br") if (lowercase_tag_name == HTML::TagNames::br)
return adopt(*new HTMLBRElement(document, lowercase_tag_name)); return adopt(*new HTMLBRElement(document, lowercase_tag_name));
if (lowercase_tag_name == "iframe") if (lowercase_tag_name == HTML::TagNames::iframe)
return adopt(*new HTMLIFrameElement(document, lowercase_tag_name)); return adopt(*new HTMLIFrameElement(document, lowercase_tag_name));
if (lowercase_tag_name == "table") if (lowercase_tag_name == HTML::TagNames::table)
return adopt(*new HTMLTableElement(document, lowercase_tag_name)); return adopt(*new HTMLTableElement(document, lowercase_tag_name));
if (lowercase_tag_name == "tr") if (lowercase_tag_name == HTML::TagNames::tr)
return adopt(*new HTMLTableRowElement(document, lowercase_tag_name)); return adopt(*new HTMLTableRowElement(document, lowercase_tag_name));
if (lowercase_tag_name == "td" || lowercase_tag_name == "th") if (lowercase_tag_name == HTML::TagNames::td || lowercase_tag_name == HTML::TagNames::th)
return adopt(*new HTMLTableCellElement(document, lowercase_tag_name)); return adopt(*new HTMLTableCellElement(document, lowercase_tag_name));
if (lowercase_tag_name == "iframe") 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))
return adopt(*new HTMLIFrameElement(document, lowercase_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, lowercase_tag_name)); return adopt(*new HTMLHeadingElement(document, lowercase_tag_name));
} if (lowercase_tag_name == HTML::TagNames::script)
if (lowercase_tag_name == "script")
return adopt(*new HTMLScriptElement(document, lowercase_tag_name)); return adopt(*new HTMLScriptElement(document, lowercase_tag_name));
if (lowercase_tag_name == "canvas") if (lowercase_tag_name == HTML::TagNames::canvas)
return adopt(*new HTMLCanvasElement(document, lowercase_tag_name)); return adopt(*new HTMLCanvasElement(document, lowercase_tag_name));
return adopt(*new Element(document, lowercase_tag_name)); return adopt(*new Element(document, lowercase_tag_name));
} }

View file

@ -42,7 +42,7 @@ public:
template<> template<>
inline bool is<HTMLAnchorElement>(const Node& node) inline bool is<HTMLAnchorElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("a"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::a;
} }
} }

View file

@ -41,7 +41,7 @@ public:
template<> template<>
inline bool is<HTMLBRElement>(const Node& node) inline bool is<HTMLBRElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("br"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::br;
} }
} }

View file

@ -42,4 +42,10 @@ private:
NonnullRefPtr<Core::Timer> m_timer; NonnullRefPtr<Core::Timer> m_timer;
}; };
template<>
inline bool is<HTMLBlinkElement>(const Node& node)
{
return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::blink;
}
} }

View file

@ -45,7 +45,7 @@ private:
template<> template<>
inline bool is<HTMLBodyElement>(const Node& node) inline bool is<HTMLBodyElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("body"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::body;
} }
} }

View file

@ -60,7 +60,7 @@ private:
template<> template<>
inline bool is<HTMLCanvasElement>(const Node& node) inline bool is<HTMLCanvasElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("canvas"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::canvas;
} }
} }

View file

@ -38,4 +38,10 @@ public:
virtual void apply_presentational_hints(StyleProperties&) const override; virtual void apply_presentational_hints(StyleProperties&) const override;
}; };
template<>
inline bool is<HTMLFontElement>(const Node& node)
{
return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::font;
}
} }

View file

@ -45,7 +45,7 @@ public:
template<> template<>
inline bool is<HTMLFormElement>(const Node& node) inline bool is<HTMLFormElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("form"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::form;
} }
} }

View file

@ -36,4 +36,10 @@ public:
virtual ~HTMLHRElement() override; virtual ~HTMLHRElement() override;
}; };
template<>
inline bool is<HTMLHRElement>(const Node& node)
{
return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::hr;
}
} }

View file

@ -52,7 +52,7 @@ private:
template<> template<>
inline bool is<HTMLIFrameElement>(const Node& node) inline bool is<HTMLIFrameElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("iframe"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::iframe;
} }
} }

View file

@ -82,7 +82,7 @@ private:
template<> template<>
inline bool is<HTMLImageElement>(const Node& node) inline bool is<HTMLImageElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("img"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::img;
} }
} }

View file

@ -45,7 +45,7 @@ public:
template<> template<>
inline bool is<HTMLInputElement>(const Node& node) inline bool is<HTMLInputElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("input"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::input;
} }
} }

View file

@ -57,7 +57,7 @@ private:
template<> template<>
inline bool is<HTMLLinkElement>(const Node& node) inline bool is<HTMLLinkElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("link"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::link;
} }
} }

View file

@ -69,7 +69,7 @@ private:
template<> template<>
inline bool is<HTMLScriptElement>(const Node& node) inline bool is<HTMLScriptElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("script"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::script;
} }
} }

View file

@ -44,4 +44,11 @@ private:
RefPtr<StyleSheet> m_stylesheet; RefPtr<StyleSheet> m_stylesheet;
}; };
template<>
inline bool is<HTMLStyleElement>(const Node& node)
{
return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::style;
}
} }

View file

@ -39,7 +39,7 @@ public:
template<> template<>
inline bool is<HTMLTableCellElement>(const Node& node) inline bool is<HTMLTableCellElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().is_one_of("td", "th"); return is<Element>(node) && to<Element>(node).tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th);
} }
} }

View file

@ -39,7 +39,7 @@ public:
template<> template<>
inline bool is<HTMLTableElement>(const Node& node) inline bool is<HTMLTableElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("table"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::table;
} }
} }

View file

@ -39,7 +39,7 @@ public:
template<> template<>
inline bool is<HTMLTableRowElement>(const Node& node) inline bool is<HTMLTableRowElement>(const Node& node)
{ {
return is<Element>(node) && to<Element>(node).tag_name().equals_ignoring_case("tr"); return is<Element>(node) && to<Element>(node).tag_name() == HTML::TagNames::tr;
} }
} }

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibWeb/DOM/TagNames.h>
namespace Web {
namespace HTML {
namespace TagNames {
#define __ENUMERATE_HTML_TAG(name) FlyString name;
ENUMERATE_HTML_TAGS
#undef __ENUMERATE_HTML_TAG
void initialize()
{
static bool s_initialized = false;
if (s_initialized)
return;
#define __ENUMERATE_HTML_TAG(name) \
name = #name;
ENUMERATE_HTML_TAGS
#undef __ENUMERATE_HTML_TAG
s_initialized = true;
}
}
}
}

View file

@ -0,0 +1,95 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/FlyString.h>
namespace Web {
namespace HTML {
namespace TagNames {
void initialize();
#define ENUMERATE_HTML_TAGS \
__ENUMERATE_HTML_TAG(a) \
__ENUMERATE_HTML_TAG(article) \
__ENUMERATE_HTML_TAG(b) \
__ENUMERATE_HTML_TAG(blink) \
__ENUMERATE_HTML_TAG(body) \
__ENUMERATE_HTML_TAG(br) \
__ENUMERATE_HTML_TAG(button) \
__ENUMERATE_HTML_TAG(canvas) \
__ENUMERATE_HTML_TAG(div) \
__ENUMERATE_HTML_TAG(em) \
__ENUMERATE_HTML_TAG(font) \
__ENUMERATE_HTML_TAG(form) \
__ENUMERATE_HTML_TAG(h1) \
__ENUMERATE_HTML_TAG(h2) \
__ENUMERATE_HTML_TAG(h3) \
__ENUMERATE_HTML_TAG(h4) \
__ENUMERATE_HTML_TAG(h5) \
__ENUMERATE_HTML_TAG(h6) \
__ENUMERATE_HTML_TAG(head) \
__ENUMERATE_HTML_TAG(hr) \
__ENUMERATE_HTML_TAG(html) \
__ENUMERATE_HTML_TAG(i) \
__ENUMERATE_HTML_TAG(iframe) \
__ENUMERATE_HTML_TAG(img) \
__ENUMERATE_HTML_TAG(input) \
__ENUMERATE_HTML_TAG(li) \
__ENUMERATE_HTML_TAG(link) \
__ENUMERATE_HTML_TAG(main) \
__ENUMERATE_HTML_TAG(marquee) \
__ENUMERATE_HTML_TAG(meta) \
__ENUMERATE_HTML_TAG(nav) \
__ENUMERATE_HTML_TAG(ol) \
__ENUMERATE_HTML_TAG(pre) \
__ENUMERATE_HTML_TAG(s) \
__ENUMERATE_HTML_TAG(script) \
__ENUMERATE_HTML_TAG(section) \
__ENUMERATE_HTML_TAG(span) \
__ENUMERATE_HTML_TAG(strong) \
__ENUMERATE_HTML_TAG(style) \
__ENUMERATE_HTML_TAG(table) \
__ENUMERATE_HTML_TAG(tbody) \
__ENUMERATE_HTML_TAG(td) \
__ENUMERATE_HTML_TAG(textarea) \
__ENUMERATE_HTML_TAG(tfoot) \
__ENUMERATE_HTML_TAG(th) \
__ENUMERATE_HTML_TAG(thead) \
__ENUMERATE_HTML_TAG(title) \
__ENUMERATE_HTML_TAG(tr) \
__ENUMERATE_HTML_TAG(u) \
__ENUMERATE_HTML_TAG(ul)
#define __ENUMERATE_HTML_TAG(name) extern FlyString name;
ENUMERATE_HTML_TAGS
#undef __ENUMERATE_HTML_TAG
}
}
}