From 8319c7cfb847a066c4cdad4d9ab020f68dd03712 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 25 Feb 2024 10:43:17 -0500 Subject: [PATCH] LibWeb: Move code to update HTMLInputElement's shadow tree to a helper We currently copy-paste a series of if statements to selectively update the shadow tree elements for some types. This will soon become longer as more shadow trees are implemented for other types. This patch just moves those checks to a single location to make adding more shadow trees easier. --- .../LibWeb/HTML/HTMLInputElement.cpp | 43 +++++++++++-------- .../Libraries/LibWeb/HTML/HTMLInputElement.h | 1 + 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index e4fb38586b..48470893d1 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -355,8 +355,7 @@ void HTMLInputElement::did_pick_color(Optional picked_color) m_value = value_sanitization_algorithm(picked_color.value().to_string_without_alpha()); m_dirty_value = true; - if (m_color_well_element) - update_color_well_element(); + update_color_well_element(); // the user agent must queue an element task on the user interaction task source queue_an_element_task(HTML::Task::Source::UserInteraction, [this] { @@ -442,11 +441,7 @@ WebIDL::ExceptionOr HTMLInputElement::set_value(String const& value) browsing_context->set_cursor_position(DOM::Position::create(realm, *m_text_node, m_text_node->data().bytes().size())); } - if (type_state() == TypeAttributeState::Color && m_color_well_element) - update_color_well_element(); - - if (type_state() == TypeAttributeState::Range && m_slider_thumb) - update_slider_thumb_element(); + update_shadow_tree(); } break; @@ -615,6 +610,20 @@ void HTMLInputElement::create_shadow_tree_if_needed() } } +void HTMLInputElement::update_shadow_tree() +{ + switch (type_state()) { + case TypeAttributeState::Color: + update_color_well_element(); + break; + case TypeAttributeState::Range: + update_slider_thumb_element(); + break; + default: + break; + } +} + void HTMLInputElement::create_text_input_shadow_tree() { auto shadow_root = heap().allocate(realm(), document(), *this, Bindings::ShadowRootMode::Closed); @@ -737,6 +746,9 @@ void HTMLInputElement::create_color_input_shadow_tree() void HTMLInputElement::update_color_well_element() { + if (!m_color_well_element) + return; + MUST(m_color_well_element->style_for_bindings()->set_property(CSS::PropertyID::BackgroundColor, m_value)); } @@ -846,6 +858,9 @@ void HTMLInputElement::create_range_input_shadow_tree() void HTMLInputElement::update_slider_thumb_element() { + if (!m_slider_thumb) + return; + double value = value_as_number(); double minimum = *min(); double maximum = *max(); @@ -891,13 +906,9 @@ void HTMLInputElement::form_associated_element_attribute_changed(FlyString const } else { m_value = value_sanitization_algorithm(*value); } + update_placeholder_visibility(); - - if (type_state() == TypeAttributeState::Color && m_color_well_element) - update_color_well_element(); - - if (type_state() == TypeAttributeState::Range && m_slider_thumb) - update_slider_thumb_element(); + update_shadow_tree(); } } else if (name == HTML::AttributeNames::placeholder) { if (m_placeholder_text_node) @@ -1133,11 +1144,7 @@ void HTMLInputElement::reset_algorithm() update_placeholder_visibility(); } - if (type_state() == TypeAttributeState::Color && m_color_well_element) - update_color_well_element(); - - if (type_state() == TypeAttributeState::Range && m_slider_thumb) - update_slider_thumb_element(); + update_shadow_tree(); } void HTMLInputElement::form_associated_element_was_inserted() diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index b0e0826e17..468372aa23 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -223,6 +223,7 @@ private: static TypeAttributeState parse_type_attribute(StringView); void create_shadow_tree_if_needed(); + void update_shadow_tree(); void create_text_input_shadow_tree(); void create_color_input_shadow_tree(); void create_range_input_shadow_tree();