Skip to content

Commit e30f279

Browse files
Psychpsyopbrw
authored andcommitted
LibWeb: Make select element use option's label, not text content
1 parent 015b9be commit e30f279

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

Libraries/LibWeb/HTML/HTMLOptionElement.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ String HTMLOptionElement::label() const
114114
void HTMLOptionElement::set_label(String const& label)
115115
{
116116
MUST(set_attribute(HTML::AttributeNames::label, label));
117+
118+
// NOTE: This option's select element may need to show different contents now.
119+
if (selected()) {
120+
if (auto select_element = first_ancestor_of_type<HTMLSelectElement>()) {
121+
select_element->update_inner_text_element({});
122+
}
123+
}
117124
}
118125

119126
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text

Libraries/LibWeb/HTML/HTMLSelectElement.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,15 @@ void HTMLSelectElement::show_the_picker_if_applicable()
413413
for (auto const& child : opt_group_element.children_as_vector()) {
414414
if (is<HTMLOptionElement>(*child)) {
415415
auto& option_element = verify_cast<HTMLOptionElement>(*child);
416-
option_group_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.text_content()), option_element.value() });
416+
option_group_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.label()), option_element.value() });
417417
}
418418
}
419419
m_select_items.append(SelectItemOptionGroup { opt_group_element.get_attribute(AttributeNames::label).value_or(String {}), option_group_items });
420420
}
421421

422422
if (is<HTMLOptionElement>(*child)) {
423423
auto& option_element = verify_cast<HTMLOptionElement>(*child);
424-
m_select_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.text_content()), option_element.value() });
424+
m_select_items.append(SelectItemOption { id_counter++, option_element.selected(), option_element.disabled(), option_element, strip_newlines(option_element.label()), option_element.value() });
425425
}
426426

427427
if (is<HTMLHRElement>(*child))
@@ -567,15 +567,21 @@ void HTMLSelectElement::create_shadow_tree_if_needed()
567567
update_inner_text_element();
568568
}
569569

570+
void HTMLSelectElement::update_inner_text_element(Badge<HTMLOptionElement>)
571+
{
572+
update_inner_text_element();
573+
}
574+
575+
// FIXME: This needs to be called any time the selected option's children are modified.
570576
void HTMLSelectElement::update_inner_text_element()
571577
{
572578
if (!m_inner_text_element)
573579
return;
574580

575-
// Update inner text element to text content of selected option
581+
// Update inner text element to the label of the selected option
576582
for (auto const& option_element : list_of_options()) {
577583
if (option_element->selected()) {
578-
m_inner_text_element->set_text_content(strip_newlines(option_element->text_content()));
584+
m_inner_text_element->set_text_content(strip_newlines(option_element->label()));
579585
return;
580586
}
581587
}

Libraries/LibWeb/HTML/HTMLSelectElement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class HTMLSelectElement final
9696

9797
void update_selectedness();
9898

99+
void update_inner_text_element(Badge<HTMLOptionElement>);
100+
99101
private:
100102
HTMLSelectElement(DOM::Document&, DOM::QualifiedName);
101103

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!doctype html>
2+
<select>
3+
<option>label</option>
4+
</select>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!doctype html>
2+
<link rel="match" href="../expected/select-option-use-label.html" />
3+
<select>
4+
<option label="label"></option>
5+
</select>

0 commit comments

Comments
 (0)