1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +00:00

LibWeb: Use HTML::AttributeNames::foo instead of FlyString("foo")

To avoid the costly instantiation of FlyStrings whenever we're looking
up attributes, use the premade HTML::AttributeNames globals. :^)
This commit is contained in:
Andreas Kling 2020-06-03 20:51:28 +02:00
parent ff55d00261
commit 2149820260
16 changed files with 71 additions and 43 deletions

View file

@ -30,22 +30,30 @@ namespace Web {
namespace HTML { namespace HTML {
namespace AttributeNames { namespace AttributeNames {
FlyString id; #define __ENUMERATE_HTML_ATTRIBUTE(name) FlyString name;
FlyString class_; ENUMERATE_HTML_ATTRIBUTES
FlyString type; #undef __ENUMERATE_HTML_ATTRIBUTE
FlyString href;
FlyString style;
void initialize() void initialize()
{ {
static bool s_initialized = false; static bool s_initialized = false;
if (s_initialized) if (s_initialized)
return; return;
#define __ENUMERATE_HTML_ATTRIBUTE(name) \
name = #name; \
if (name.ends_with("_")) \
name = name.view().substring_view(0, name.length() - 1);
ENUMERATE_HTML_ATTRIBUTES
#undef __ENUMERATE_HTML_ATTRIBUTE
id = "id"; id = "id";
class_ = "class"; class_ = "class";
type = "type"; type = "type";
href = "href"; href = "href";
style = "style"; style = "style";
name = "name";
s_initialized = true; s_initialized = true;
} }

View file

@ -34,11 +34,30 @@ namespace AttributeNames {
void initialize(); void initialize();
extern FlyString id; #define ENUMERATE_HTML_ATTRIBUTES \
extern FlyString class_; __ENUMERATE_HTML_ATTRIBUTE(id) \
extern FlyString type; __ENUMERATE_HTML_ATTRIBUTE(class_) \
extern FlyString href; __ENUMERATE_HTML_ATTRIBUTE(type) \
extern FlyString style; __ENUMERATE_HTML_ATTRIBUTE(href) \
__ENUMERATE_HTML_ATTRIBUTE(style) \
__ENUMERATE_HTML_ATTRIBUTE(name) \
__ENUMERATE_HTML_ATTRIBUTE(target) \
__ENUMERATE_HTML_ATTRIBUTE(width) \
__ENUMERATE_HTML_ATTRIBUTE(height) \
__ENUMERATE_HTML_ATTRIBUTE(title) \
__ENUMERATE_HTML_ATTRIBUTE(action) \
__ENUMERATE_HTML_ATTRIBUTE(method) \
__ENUMERATE_HTML_ATTRIBUTE(alt) \
__ENUMERATE_HTML_ATTRIBUTE(src) \
__ENUMERATE_HTML_ATTRIBUTE(value) \
__ENUMERATE_HTML_ATTRIBUTE(rel) \
__ENUMERATE_HTML_ATTRIBUTE(async) \
__ENUMERATE_HTML_ATTRIBUTE(defer) \
__ENUMERATE_HTML_ATTRIBUTE(size)
#define __ENUMERATE_HTML_ATTRIBUTE(name) extern FlyString name;
ENUMERATE_HTML_ATTRIBUTES
#undef __ENUMERATE_HTML_ATTRIBUTE
} }
} }

View file

@ -316,7 +316,7 @@ Vector<const Element*> Document::get_elements_by_name(const String& name) const
{ {
Vector<const Element*> elements; Vector<const Element*> elements;
for_each_in_subtree_of_type<Element>([&](auto& element) { for_each_in_subtree_of_type<Element>([&](auto& element) {
if (element.attribute("name") == name) if (element.attribute(HTML::AttributeNames::name) == name)
elements.append(&element); elements.append(&element);
return IterationDecision::Continue; return IterationDecision::Continue;
}); });

View file

@ -29,6 +29,7 @@
#include <AK/FlyString.h> #include <AK/FlyString.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibWeb/DOM/Attribute.h> #include <LibWeb/DOM/Attribute.h>
#include <LibWeb/DOM/AttributeNames.h>
#include <LibWeb/DOM/ParentNode.h> #include <LibWeb/DOM/ParentNode.h>
#include <LibWeb/Layout/LayoutNode.h> #include <LibWeb/Layout/LayoutNode.h>
@ -68,7 +69,7 @@ public:
LayoutNodeWithStyle* layout_node() { return static_cast<LayoutNodeWithStyle*>(Node::layout_node()); } LayoutNodeWithStyle* layout_node() { return static_cast<LayoutNodeWithStyle*>(Node::layout_node()); }
const LayoutNodeWithStyle* layout_node() const { return static_cast<const LayoutNodeWithStyle*>(Node::layout_node()); } const LayoutNodeWithStyle* layout_node() const { return static_cast<const LayoutNodeWithStyle*>(Node::layout_node()); }
String name() const { return attribute("name"); } String name() const { return attribute(HTML::AttributeNames::name); }
const StyleProperties* resolved_style() const { return m_resolved_style.ptr(); } const StyleProperties* resolved_style() const { return m_resolved_style.ptr(); }
NonnullRefPtr<StyleProperties> computed_style(); NonnullRefPtr<StyleProperties> computed_style();

View file

@ -35,8 +35,8 @@ public:
HTMLAnchorElement(Document&, const FlyString& tag_name); HTMLAnchorElement(Document&, const FlyString& tag_name);
virtual ~HTMLAnchorElement() override; virtual ~HTMLAnchorElement() override;
String href() const { return attribute("href"); } String href() const { return attribute(HTML::AttributeNames::href); }
String target() const { return attribute("target"); } String target() const { return attribute(HTML::AttributeNames::target); }
}; };
template<> template<>

View file

@ -48,7 +48,7 @@ HTMLCanvasElement::~HTMLCanvasElement()
int HTMLCanvasElement::requested_width() const int HTMLCanvasElement::requested_width() const
{ {
bool ok = false; bool ok = false;
unsigned width = attribute("width").to_int(ok); unsigned width = attribute(HTML::AttributeNames::width).to_int(ok);
if (ok) if (ok)
return width; return width;
@ -58,7 +58,7 @@ int HTMLCanvasElement::requested_width() const
int HTMLCanvasElement::requested_height() const int HTMLCanvasElement::requested_height() const
{ {
bool ok = false; bool ok = false;
unsigned height = attribute("height").to_int(ok); unsigned height = attribute(HTML::AttributeNames::height).to_int(ok);
if (ok) if (ok)
return height; return height;

View file

@ -35,7 +35,7 @@ public:
HTMLElement(Document&, const FlyString& tag_name); HTMLElement(Document&, const FlyString& tag_name);
virtual ~HTMLElement() override; virtual ~HTMLElement() override;
String title() const { return attribute("title"); } String title() const { return attribute(HTML::AttributeNames::title); }
private: private:
virtual bool is_html_element() const final { return true; } virtual bool is_html_element() const final { return true; }

View file

@ -36,8 +36,8 @@ public:
HTMLFormElement(Document&, const FlyString& tag_name); HTMLFormElement(Document&, const FlyString& tag_name);
virtual ~HTMLFormElement() override; virtual ~HTMLFormElement() override;
String action() const { return attribute("action"); } String action() const { return attribute(HTML::AttributeNames::action); }
String method() const { return attribute("method"); } String method() const { return attribute(HTML::AttributeNames::method); }
void submit(RefPtr<HTMLInputElement> submitter); void submit(RefPtr<HTMLInputElement> submitter);
}; };

View file

@ -122,7 +122,7 @@ void HTMLImageElement::animate()
int HTMLImageElement::preferred_width() const int HTMLImageElement::preferred_width() const
{ {
bool ok = false; bool ok = false;
int width = attribute("width").to_int(ok); int width = attribute(HTML::AttributeNames::width).to_int(ok);
if (ok) if (ok)
return width; return width;
@ -135,7 +135,7 @@ int HTMLImageElement::preferred_width() const
int HTMLImageElement::preferred_height() const int HTMLImageElement::preferred_height() const
{ {
bool ok = false; bool ok = false;
int height = attribute("height").to_int(ok); int height = attribute(HTML::AttributeNames::height).to_int(ok);
if (ok) if (ok)
return height; return height;

View file

@ -47,8 +47,8 @@ public:
virtual void parse_attribute(const FlyString& name, const String& value) override; virtual void parse_attribute(const FlyString& name, const String& value) override;
String alt() const { return attribute("alt"); } String alt() const { return attribute(HTML::AttributeNames::alt); }
String src() const { return attribute("src"); } String src() const { return attribute(HTML::AttributeNames::src); }
int preferred_width() const; int preferred_width() const;
int preferred_height() const; int preferred_height() const;

View file

@ -81,10 +81,10 @@ RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const StyleProperties*)
text_box.set_text(value()); text_box.set_text(value());
text_box.on_change = [this] { text_box.on_change = [this] {
auto& widget = to<LayoutWidget>(layout_node())->widget(); auto& widget = to<LayoutWidget>(layout_node())->widget();
const_cast<HTMLInputElement*>(this)->set_attribute("value", static_cast<const GUI::TextBox&>(widget).text()); const_cast<HTMLInputElement*>(this)->set_attribute(HTML::AttributeNames::value, static_cast<const GUI::TextBox&>(widget).text());
}; };
int text_width = Gfx::Font::default_font().width(value()); int text_width = Gfx::Font::default_font().width(value());
auto size_value = attribute("size"); auto size_value = attribute(HTML::AttributeNames::size);
if (!size_value.is_null()) { if (!size_value.is_null()) {
bool ok; bool ok;
auto size = size_value.to_int(ok); auto size = size_value.to_int(ok);

View file

@ -37,9 +37,9 @@ public:
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override; virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
String type() const { return attribute("type"); } String type() const { return attribute(HTML::AttributeNames::type); }
String value() const { return attribute("value"); } String value() const { return attribute(HTML::AttributeNames::value); }
String name() const { return attribute("name"); } String name() const { return attribute(HTML::AttributeNames::name); }
}; };
template<> template<>

View file

@ -40,9 +40,9 @@ public:
virtual void inserted_into(Node&) override; virtual void inserted_into(Node&) override;
String rel() const { return attribute("rel"); } String rel() const { return attribute(HTML::AttributeNames::rel); }
String type() const { return attribute("type"); } String type() const { return attribute(HTML::AttributeNames::type); }
String href() const { return attribute("href"); } String href() const { return attribute(HTML::AttributeNames::href); }
private: private:
// ^ResourceClient // ^ResourceClient

View file

@ -57,7 +57,7 @@ void HTMLScriptElement::children_changed()
{ {
HTMLElement::children_changed(); HTMLElement::children_changed();
if (has_attribute("src")) if (has_attribute(HTML::AttributeNames::src))
return; return;
StringBuilder builder; StringBuilder builder;
@ -82,7 +82,7 @@ void HTMLScriptElement::inserted_into(Node& new_parent)
{ {
HTMLElement::inserted_into(new_parent); HTMLElement::inserted_into(new_parent);
auto src = attribute("src"); auto src = attribute(HTML::AttributeNames::src);
if (src.is_null()) if (src.is_null())
return; return;
@ -133,12 +133,12 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
RefPtr<Document> parser_document = m_parser_document.ptr(); RefPtr<Document> parser_document = m_parser_document.ptr();
m_parser_document = nullptr; m_parser_document = nullptr;
if (parser_document && !has_attribute("async")) { if (parser_document && !has_attribute(HTML::AttributeNames::async)) {
m_non_blocking = true; m_non_blocking = true;
} }
auto source_text = child_text_content(); auto source_text = child_text_content();
if (!has_attribute("src") && source_text.is_empty()) if (!has_attribute(HTML::AttributeNames::src) && source_text.is_empty())
return; return;
if (!is_connected()) if (!is_connected())
@ -173,8 +173,8 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
// FIXME: Check fetch options // FIXME: Check fetch options
if (has_attribute("src")) { if (has_attribute(HTML::AttributeNames::src)) {
auto src = attribute("src"); auto src = attribute(HTML::AttributeNames::src);
if (src.is_empty()) { if (src.is_empty()) {
// FIXME: Fire an "error" event at the element and return // FIXME: Fire an "error" event at the element and return
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
@ -205,11 +205,11 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
} }
// FIXME: Check classic vs. module // FIXME: Check classic vs. module
if (has_attribute("src") && has_attribute("defer") && m_parser_inserted && !has_attribute("async")) { if (has_attribute(HTML::AttributeNames::src) && has_attribute(HTML::AttributeNames::defer) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
document().add_script_to_execute_when_parsing_has_finished({}, *this); document().add_script_to_execute_when_parsing_has_finished({}, *this);
} }
else if (has_attribute("src") && m_parser_inserted && !has_attribute("async")) { else if (has_attribute(HTML::AttributeNames::src) && m_parser_inserted && !has_attribute(HTML::AttributeNames::async)) {
document().set_pending_parsing_blocking_script({}, this); document().set_pending_parsing_blocking_script({}, this);
when_the_script_is_ready([this] { when_the_script_is_ready([this] {
@ -217,11 +217,11 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
}); });
} }
else if (has_attribute("src") && !has_attribute("async") && !m_non_blocking) { else if (has_attribute(HTML::AttributeNames::src) && !has_attribute(HTML::AttributeNames::async) && !m_non_blocking) {
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
else if (has_attribute("src")) { else if (has_attribute(HTML::AttributeNames::src)) {
m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this); m_preparation_time_document->add_script_to_execute_as_soon_as_possible({}, *this);
} }

View file

@ -93,7 +93,7 @@ void dump_tree(const LayoutNode& layout_node)
String identifier = ""; String identifier = "";
if (layout_node.node() && is<Element>(*layout_node.node())) { if (layout_node.node() && is<Element>(*layout_node.node())) {
auto id = to<Element>(*layout_node.node()).attribute("id"); auto id = to<Element>(*layout_node.node()).attribute(HTML::AttributeNames::id);
if (!id.is_empty()) { if (!id.is_empty()) {
StringBuilder builder; StringBuilder builder;
builder.append('#'); builder.append('#');

View file

@ -388,7 +388,7 @@ static RefPtr<Document> create_image_document(const ByteBuffer& data, const URL&
html_element->append_child(body_element); html_element->append_child(body_element);
auto image_element = create_element(document, "img"); auto image_element = create_element(document, "img");
image_element->set_attribute("src", url.to_string()); image_element->set_attribute(HTML::AttributeNames::src, url.to_string());
body_element->append_child(image_element); body_element->append_child(image_element);
return document; return document;