Skip to content

Commit

Permalink
LibWeb: Move code to update HTMLInputElement's shadow tree to a helper
Browse files Browse the repository at this point in the history
We currently copy-paste a series of if statements to selectively update
the shadow tree elements for some <input> 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.
  • Loading branch information
trflynn89 authored and awesomekling committed Feb 26, 2024
1 parent 5232afa commit 8319c7c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
43 changes: 25 additions & 18 deletions Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ void HTMLInputElement::did_pick_color(Optional<Color> 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] {
Expand Down Expand Up @@ -442,11 +441,7 @@ WebIDL::ExceptionOr<void> 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;
Expand Down Expand Up @@ -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<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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_placeholder_visibility();
update_shadow_tree();
}
} else if (name == HTML::AttributeNames::placeholder) {
if (m_placeholder_text_node)
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/HTMLInputElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class HTMLInputElement final

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();
Expand Down

0 comments on commit 8319c7c

Please sign in to comment.