diff options
author | Andreas Kling <kling@serenityos.org> | 2020-06-03 20:51:28 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-03 21:53:00 +0200 |
commit | 214982026080f411ea38e1dbb1a51186d10362fe (patch) | |
tree | e085f127f47bc49a751b03efa66b1907a37889cf /Libraries/LibWeb | |
parent | ff55d00261adc0add19e910b33896eacd33b0ec2 (diff) | |
download | serenity-214982026080f411ea38e1dbb1a51186d10362fe.zip |
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. :^)
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r-- | Libraries/LibWeb/DOM/AttributeNames.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/AttributeNames.h | 29 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Element.h | 3 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLAnchorElement.h | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLCanvasElement.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLElement.h | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLFormElement.h | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLImageElement.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLImageElement.h | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLInputElement.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLInputElement.h | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLLinkElement.h | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/HTMLScriptElement.cpp | 20 | ||||
-rw-r--r-- | Libraries/LibWeb/Dump.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/PageView.cpp | 2 |
16 files changed, 71 insertions, 43 deletions
diff --git a/Libraries/LibWeb/DOM/AttributeNames.cpp b/Libraries/LibWeb/DOM/AttributeNames.cpp index eda5c3f7e1..8b162d9403 100644 --- a/Libraries/LibWeb/DOM/AttributeNames.cpp +++ b/Libraries/LibWeb/DOM/AttributeNames.cpp @@ -30,22 +30,30 @@ namespace Web { namespace HTML { namespace AttributeNames { -FlyString id; -FlyString class_; -FlyString type; -FlyString href; -FlyString style; +#define __ENUMERATE_HTML_ATTRIBUTE(name) FlyString name; +ENUMERATE_HTML_ATTRIBUTES +#undef __ENUMERATE_HTML_ATTRIBUTE void initialize() { static bool s_initialized = false; if (s_initialized) 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"; class_ = "class"; type = "type"; href = "href"; style = "style"; + name = "name"; + s_initialized = true; } diff --git a/Libraries/LibWeb/DOM/AttributeNames.h b/Libraries/LibWeb/DOM/AttributeNames.h index 5c4393ad09..81c9461fc2 100644 --- a/Libraries/LibWeb/DOM/AttributeNames.h +++ b/Libraries/LibWeb/DOM/AttributeNames.h @@ -34,11 +34,30 @@ namespace AttributeNames { void initialize(); -extern FlyString id; -extern FlyString class_; -extern FlyString type; -extern FlyString href; -extern FlyString style; +#define ENUMERATE_HTML_ATTRIBUTES \ + __ENUMERATE_HTML_ATTRIBUTE(id) \ + __ENUMERATE_HTML_ATTRIBUTE(class_) \ + __ENUMERATE_HTML_ATTRIBUTE(type) \ + __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 } } diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 3ff49702dd..48acc74d8a 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -316,7 +316,7 @@ Vector<const Element*> Document::get_elements_by_name(const String& name) const { Vector<const Element*> elements; for_each_in_subtree_of_type<Element>([&](auto& element) { - if (element.attribute("name") == name) + if (element.attribute(HTML::AttributeNames::name) == name) elements.append(&element); return IterationDecision::Continue; }); diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h index 7fbf3ab394..e7d88d84ae 100644 --- a/Libraries/LibWeb/DOM/Element.h +++ b/Libraries/LibWeb/DOM/Element.h @@ -29,6 +29,7 @@ #include <AK/FlyString.h> #include <AK/String.h> #include <LibWeb/DOM/Attribute.h> +#include <LibWeb/DOM/AttributeNames.h> #include <LibWeb/DOM/ParentNode.h> #include <LibWeb/Layout/LayoutNode.h> @@ -68,7 +69,7 @@ public: LayoutNodeWithStyle* layout_node() { return static_cast<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(); } NonnullRefPtr<StyleProperties> computed_style(); diff --git a/Libraries/LibWeb/DOM/HTMLAnchorElement.h b/Libraries/LibWeb/DOM/HTMLAnchorElement.h index e823dff76a..925d149cf5 100644 --- a/Libraries/LibWeb/DOM/HTMLAnchorElement.h +++ b/Libraries/LibWeb/DOM/HTMLAnchorElement.h @@ -35,8 +35,8 @@ public: HTMLAnchorElement(Document&, const FlyString& tag_name); virtual ~HTMLAnchorElement() override; - String href() const { return attribute("href"); } - String target() const { return attribute("target"); } + String href() const { return attribute(HTML::AttributeNames::href); } + String target() const { return attribute(HTML::AttributeNames::target); } }; template<> diff --git a/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp b/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp index b2fd8b4389..3a39d06487 100644 --- a/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp +++ b/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp @@ -48,7 +48,7 @@ HTMLCanvasElement::~HTMLCanvasElement() int HTMLCanvasElement::requested_width() const { bool ok = false; - unsigned width = attribute("width").to_int(ok); + unsigned width = attribute(HTML::AttributeNames::width).to_int(ok); if (ok) return width; @@ -58,7 +58,7 @@ int HTMLCanvasElement::requested_width() const int HTMLCanvasElement::requested_height() const { bool ok = false; - unsigned height = attribute("height").to_int(ok); + unsigned height = attribute(HTML::AttributeNames::height).to_int(ok); if (ok) return height; diff --git a/Libraries/LibWeb/DOM/HTMLElement.h b/Libraries/LibWeb/DOM/HTMLElement.h index e99f5f5254..42c9fa2ffa 100644 --- a/Libraries/LibWeb/DOM/HTMLElement.h +++ b/Libraries/LibWeb/DOM/HTMLElement.h @@ -35,7 +35,7 @@ public: HTMLElement(Document&, const FlyString& tag_name); virtual ~HTMLElement() override; - String title() const { return attribute("title"); } + String title() const { return attribute(HTML::AttributeNames::title); } private: virtual bool is_html_element() const final { return true; } diff --git a/Libraries/LibWeb/DOM/HTMLFormElement.h b/Libraries/LibWeb/DOM/HTMLFormElement.h index e6a6f18a4f..dbfb97f671 100644 --- a/Libraries/LibWeb/DOM/HTMLFormElement.h +++ b/Libraries/LibWeb/DOM/HTMLFormElement.h @@ -36,8 +36,8 @@ public: HTMLFormElement(Document&, const FlyString& tag_name); virtual ~HTMLFormElement() override; - String action() const { return attribute("action"); } - String method() const { return attribute("method"); } + String action() const { return attribute(HTML::AttributeNames::action); } + String method() const { return attribute(HTML::AttributeNames::method); } void submit(RefPtr<HTMLInputElement> submitter); }; diff --git a/Libraries/LibWeb/DOM/HTMLImageElement.cpp b/Libraries/LibWeb/DOM/HTMLImageElement.cpp index e9fa59b1ef..97b718d7e3 100644 --- a/Libraries/LibWeb/DOM/HTMLImageElement.cpp +++ b/Libraries/LibWeb/DOM/HTMLImageElement.cpp @@ -122,7 +122,7 @@ void HTMLImageElement::animate() int HTMLImageElement::preferred_width() const { bool ok = false; - int width = attribute("width").to_int(ok); + int width = attribute(HTML::AttributeNames::width).to_int(ok); if (ok) return width; @@ -135,7 +135,7 @@ int HTMLImageElement::preferred_width() const int HTMLImageElement::preferred_height() const { bool ok = false; - int height = attribute("height").to_int(ok); + int height = attribute(HTML::AttributeNames::height).to_int(ok); if (ok) return height; diff --git a/Libraries/LibWeb/DOM/HTMLImageElement.h b/Libraries/LibWeb/DOM/HTMLImageElement.h index 0d4f2ed4c3..533569514f 100644 --- a/Libraries/LibWeb/DOM/HTMLImageElement.h +++ b/Libraries/LibWeb/DOM/HTMLImageElement.h @@ -47,8 +47,8 @@ public: virtual void parse_attribute(const FlyString& name, const String& value) override; - String alt() const { return attribute("alt"); } - String src() const { return attribute("src"); } + String alt() const { return attribute(HTML::AttributeNames::alt); } + String src() const { return attribute(HTML::AttributeNames::src); } int preferred_width() const; int preferred_height() const; diff --git a/Libraries/LibWeb/DOM/HTMLInputElement.cpp b/Libraries/LibWeb/DOM/HTMLInputElement.cpp index dd6bc15ae1..219af95e19 100644 --- a/Libraries/LibWeb/DOM/HTMLInputElement.cpp +++ b/Libraries/LibWeb/DOM/HTMLInputElement.cpp @@ -81,10 +81,10 @@ RefPtr<LayoutNode> HTMLInputElement::create_layout_node(const StyleProperties*) text_box.set_text(value()); text_box.on_change = [this] { 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()); - auto size_value = attribute("size"); + auto size_value = attribute(HTML::AttributeNames::size); if (!size_value.is_null()) { bool ok; auto size = size_value.to_int(ok); diff --git a/Libraries/LibWeb/DOM/HTMLInputElement.h b/Libraries/LibWeb/DOM/HTMLInputElement.h index 5e693437c0..0bc318e9da 100644 --- a/Libraries/LibWeb/DOM/HTMLInputElement.h +++ b/Libraries/LibWeb/DOM/HTMLInputElement.h @@ -37,9 +37,9 @@ public: virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override; - String type() const { return attribute("type"); } - String value() const { return attribute("value"); } - String name() const { return attribute("name"); } + String type() const { return attribute(HTML::AttributeNames::type); } + String value() const { return attribute(HTML::AttributeNames::value); } + String name() const { return attribute(HTML::AttributeNames::name); } }; template<> diff --git a/Libraries/LibWeb/DOM/HTMLLinkElement.h b/Libraries/LibWeb/DOM/HTMLLinkElement.h index fcd28d9610..b0a85c0195 100644 --- a/Libraries/LibWeb/DOM/HTMLLinkElement.h +++ b/Libraries/LibWeb/DOM/HTMLLinkElement.h @@ -40,9 +40,9 @@ public: virtual void inserted_into(Node&) override; - String rel() const { return attribute("rel"); } - String type() const { return attribute("type"); } - String href() const { return attribute("href"); } + String rel() const { return attribute(HTML::AttributeNames::rel); } + String type() const { return attribute(HTML::AttributeNames::type); } + String href() const { return attribute(HTML::AttributeNames::href); } private: // ^ResourceClient diff --git a/Libraries/LibWeb/DOM/HTMLScriptElement.cpp b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp index bfb4ebad9a..f627b52dac 100644 --- a/Libraries/LibWeb/DOM/HTMLScriptElement.cpp +++ b/Libraries/LibWeb/DOM/HTMLScriptElement.cpp @@ -57,7 +57,7 @@ void HTMLScriptElement::children_changed() { HTMLElement::children_changed(); - if (has_attribute("src")) + if (has_attribute(HTML::AttributeNames::src)) return; StringBuilder builder; @@ -82,7 +82,7 @@ void HTMLScriptElement::inserted_into(Node& new_parent) { HTMLElement::inserted_into(new_parent); - auto src = attribute("src"); + auto src = attribute(HTML::AttributeNames::src); if (src.is_null()) return; @@ -133,12 +133,12 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>) RefPtr<Document> parser_document = m_parser_document.ptr(); m_parser_document = nullptr; - if (parser_document && !has_attribute("async")) { + if (parser_document && !has_attribute(HTML::AttributeNames::async)) { m_non_blocking = true; } 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; if (!is_connected()) @@ -173,8 +173,8 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>) // FIXME: Check fetch options - if (has_attribute("src")) { - auto src = attribute("src"); + if (has_attribute(HTML::AttributeNames::src)) { + auto src = attribute(HTML::AttributeNames::src); if (src.is_empty()) { // FIXME: Fire an "error" event at the element and return ASSERT_NOT_REACHED(); @@ -205,11 +205,11 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>) } // 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); } - 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); 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(); } - 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); } diff --git a/Libraries/LibWeb/Dump.cpp b/Libraries/LibWeb/Dump.cpp index 42cd88fb33..c6173c1696 100644 --- a/Libraries/LibWeb/Dump.cpp +++ b/Libraries/LibWeb/Dump.cpp @@ -93,7 +93,7 @@ void dump_tree(const LayoutNode& layout_node) String identifier = ""; 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()) { StringBuilder builder; builder.append('#'); diff --git a/Libraries/LibWeb/PageView.cpp b/Libraries/LibWeb/PageView.cpp index d0d3833f58..00c36afe5d 100644 --- a/Libraries/LibWeb/PageView.cpp +++ b/Libraries/LibWeb/PageView.cpp @@ -388,7 +388,7 @@ static RefPtr<Document> create_image_document(const ByteBuffer& data, const URL& html_element->append_child(body_element); 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); return document; |