From 95b084a08f2af6c8c63b539e094bdb652eae7d34 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 16 Mar 2022 07:44:21 -0400 Subject: [PATCH] LibWeb: Cache HTMLInputElement's parsed type attribute when it changes This will let us avoid parsing the type each time type() or type_state() are invoked. --- .../Libraries/LibWeb/HTML/HTMLInputElement.cpp | 16 ++++++++++++++++ .../Libraries/LibWeb/HTML/HTMLInputElement.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index adffc32471..abd9f8bebf 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -207,9 +207,25 @@ void HTMLInputElement::parse_attribute(FlyString const& name, String const& valu // the user agent must set the checkedness of the element to true if (!m_dirty_checkedness) set_checked(true, ChangeSource::Programmatic); + } else if (name == HTML::AttributeNames::type) { + m_type = parse_type_attribute(value); } } +HTMLInputElement::TypeAttributeState HTMLInputElement::parse_type_attribute(StringView type) +{ +#define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \ + if (type.equals_ignoring_case(#keyword)) \ + return HTMLInputElement::TypeAttributeState::state; + ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES +#undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE + + // The missing value default and the invalid value default are the Text state. + // https://html.spec.whatwg.org/multipage/input.html#the-input-element:missing-value-default + // https://html.spec.whatwg.org/multipage/input.html#the-input-element:invalid-value-default + return HTMLInputElement::TypeAttributeState::Text; +} + void HTMLInputElement::did_remove_attribute(FlyString const& name) { FormAssociatedElement::did_remove_attribute(name); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index 4a8794a000..80e50e79a7 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -105,6 +105,7 @@ private: virtual void legacy_cancelled_activation_behavior() override; virtual void legacy_cancelled_activation_behavior_was_not_called() override; + static TypeAttributeState parse_type_attribute(StringView); void create_shadow_tree_if_needed(); void run_input_activation_behavior(); void set_checked_within_group(); @@ -121,6 +122,8 @@ private: // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior bool m_before_legacy_pre_activation_behavior_checked { false }; RefPtr m_legacy_pre_activation_behavior_checked_element_in_group; + + TypeAttributeState m_type { TypeAttributeState::Text }; }; }