Skip to content

Truly independent web browser

LibWeb: Show option text when its label attribute is empty

sideshowbarker3436dcd7cf8a
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
index 6c7e9d8ecff..42933b98ec2 100644
--- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp
@@ -74,7 +74,7 @@ void HTMLOptionElement::update_label_sizer_text()
if (!label_sizer)
return;
- auto text = label();
+ auto text = display_label();
if (text.is_empty()) {
if (m_label_sizer_text) {
if (auto* row = m_label_sizer_text->parent())
@@ -213,6 +213,14 @@ static void concatenate_descendants_text_content(DOM::Node const* node, Utf16Str
});
}
+// https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-label
+Utf16String HTMLOptionElement::display_label() const
+{
+ if (auto label = get_attribute_ns({}, HTML::AttributeNames::label); label.has_value() && !label->is_empty())
+ return label.release_value();
+ return text();
+}
+
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-label
Utf16String HTMLOptionElement::label() const
{
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.h b/Libraries/LibWeb/HTML/HTMLOptionElement.h
index 052314e44e0..4f686d50b13 100644
--- a/Libraries/LibWeb/HTML/HTMLOptionElement.h
+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.h
@@ -72,6 +72,7 @@ private:
void ask_for_a_reset();
void update_selection_label();
void update_label_sizer_text();
+ Utf16String display_label() const;
// https://html.spec.whatwg.org/multipage/form-elements.html#update-an-option's-nearest-ancestor-select
void update_nearest_select_element();
diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
index 85a2687d0fa..c473361d6ff 100644
--- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp
@@ -541,7 +541,7 @@ void HTMLSelectElement::show_the_picker_if_applicable()
m_select_item_option_elements.clear();
auto create_select_item_option = [&](HTMLOptionElement& option_element) {
m_select_item_option_elements.append(option_element);
- return SelectItemOption { static_cast<u32>(m_select_item_option_elements.size()), option_element.selected(), option_element.disabled(), Infra::strip_and_collapse_whitespace(option_element.label()), option_element.value() };
+ return SelectItemOption { static_cast<u32>(m_select_item_option_elements.size()), option_element.selected(), option_element.disabled(), Infra::strip_and_collapse_whitespace(option_element.display_label()), option_element.value() };
};
for (auto const& child : children_as_vector()) {
if (auto const* opt_group_element = as_if<HTMLOptGroupElement>(*child)) {
@@ -820,7 +820,7 @@ void HTMLSelectElement::clone_selected_option_into_select_button()
// 3. If option is not null, then set text to option's label.
if (option.has_value())
- text = (*option)->label();
+ text = (*option)->display_label();
// 4. Set select's select fallback button text to text.
m_selected_label_element->string_replace_all(move(text));
@@ -1205,7 +1205,7 @@ void HTMLSelectElement::update_list_box()
if (option.has_attribute(AttributeNames::hidden))
return;
auto row = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
- MUST(row->append_child(document().create_text_node(Infra::strip_and_collapse_whitespace(option.label()))));
+ MUST(row->append_child(document().create_text_node(Infra::strip_and_collapse_whitespace(option.display_label()))));
update_list_box_row_style(row, option);
MUST(m_list_box_element->append_child(row));
m_list_box_rows.append({ row, option });
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/the-select-element/option-label-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/the-select-element/option-label-ref.html
new file mode 100644
index 00000000000..3dd07b8dc2b
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/the-select-element/option-label-ref.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>OPTION's label attribute in SELECT (reference)</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/rendering.html#the-select-element-2">
+
+<select>
+ <option>Label Text</option>
+</select>
+<br/>
+<select size="4">
+ <option>Label Text</option>
+</select>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/the-select-element/option-empty-label-to-empty-string.html b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/the-select-element/option-empty-label-to-empty-string.html
new file mode 100644
index 00000000000..9d6821912e0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/the-select-element/option-empty-label-to-empty-string.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>OPTION's label attribute in SELECT -- Adding a label</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/rendering.html#the-select-element-2">
+<link rel="match" href="../../../../../../expected/wpt-import/html/rendering/widgets/the-select-element/option-label-ref.html">
+<meta name="assert" content="An option element is expected to be rendered by displaying the element's label.">
+
+<select>
+ <option label="Element Text">Label Text</option>
+</select>
+<br/>
+<select size="4">
+ <option label="Element Text">Label Text</option>
+</select>
+<script>
+let options = document.querySelectorAll("option");
+options[0].getBoundingClientRect(); // force layout.
+for (let option of options) {
+ option.setAttribute("label", "");
+}
+</script>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/the-select-element/option-empty-label.html b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/the-select-element/option-empty-label.html
new file mode 100644
index 00000000000..855969b5146
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/the-select-element/option-empty-label.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>OPTION's label attribute in SELECT -- Empty label uses Element text</title>
+<link rel="help" href="https://html.spec.whatwg.org/multipage/rendering.html#the-select-element-2">
+<link rel="match" href="../../../../../../expected/wpt-import/html/rendering/widgets/the-select-element/option-label-ref.html">
+<meta name="assert" content="An option element is expected to be rendered by displaying the element's label.">
+
+<select>
+ <option label>Label Text</option>
+</select>
+<br/>
+<select size="4">
+ <option label>Label Text</option>
+</select>