Truly independent web browser
LibWeb: Mark datalist suggestions on range inputs with ticks
Andreas Kling6ee93a68cc8a
diff --git a/Libraries/LibWeb/HTML/HTMLDataListElement.cpp b/Libraries/LibWeb/HTML/HTMLDataListElement.cppindex ad93aed8927..f752571cd30 100644--- a/Libraries/LibWeb/HTML/HTMLDataListElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLDataListElement.cpp@@ -4,7 +4,9 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <LibWeb/DOM/Document.h>#include <LibWeb/HTML/HTMLDataListElement.h>+#include <LibWeb/HTML/HTMLInputElement.h>#include <LibWeb/HTML/HTMLOptionElement.h>namespace Web::HTML {@@ -34,4 +36,60 @@ GC::Ref<DOM::HTMLCollection> HTMLDataListElement::options()return *m_options;}+void HTMLDataListElement::inserted()+{+ Base::inserted();+ notify_inputs_using_this_as_suggestions_source();+}++void HTMLDataListElement::children_changed(ChildrenChangedMetadata const& metadata)+{+ Base::children_changed(metadata);+ notify_inputs_using_this_as_suggestions_source();+}++void HTMLDataListElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor, Node& old_root)+{+ Base::removed_from(is_subtree_root, old_ancestor, old_root);+ notify_inputs_with_suggestions_sources_in(old_root);+}++void HTMLDataListElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)+{+ Base::attribute_changed(name, old_value, value, namespace_);+ if (name == AttributeNames::id && old_value != value)+ notify_inputs_with_suggestions_sources_in(root());+}++// NB: An input whose suggestions source element was this datalist, under an id it no longer has or in a tree it has+// left, shows the suggestions of whatever element its list attribute now names.+void HTMLDataListElement::notify_inputs_with_suggestions_sources_in(DOM::Node& root)+{+ queue_an_element_task(HTML::Task::Source::DOMManipulation, [root = GC::Ref { root }] {+ root->for_each_in_inclusive_subtree_of_type<HTMLInputElement>([](HTMLInputElement& input) {+ if (input.has_attribute(AttributeNames::list))+ input.suggestions_source_element_did_change();+ return TraversalDecision::Continue;+ });+ });+}++// NB: An input that shows the suggestions of a datalist, such as the ticks of a slider, shows them again once the+// datalist is inserted or its options change. The inputs are told once for any number of changes before the next+// task, so that parsing a long list of options does not search the document for each of them.+void HTMLDataListElement::notify_inputs_using_this_as_suggestions_source()+{+ if (m_input_notification_queued || !id().has_value())+ return;+ m_input_notification_queued = true;+ queue_an_element_task(HTML::Task::Source::DOMManipulation, [this] {+ m_input_notification_queued = false;+ root().for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](HTMLInputElement& input) {+ if (auto list = input.get_attribute(AttributeNames::list); list.has_value() && id() == *list)+ input.suggestions_source_element_did_change();+ return TraversalDecision::Continue;+ });+ });+}+}diff --git a/Libraries/LibWeb/HTML/HTMLDataListElement.h b/Libraries/LibWeb/HTML/HTMLDataListElement.hindex 031ad156fd2..65c7f7c0f37 100644--- a/Libraries/LibWeb/HTML/HTMLDataListElement.h+++ b/Libraries/LibWeb/HTML/HTMLDataListElement.h@@ -24,13 +24,22 @@ public:GC::Ref<DOM::HTMLCollection> options();+ void notify_inputs_using_this_as_suggestions_source();+private:HTMLDataListElement(DOM::Document&, DOM::QualifiedName);virtual void visit_edges(Cell::Visitor&) override;virtual bool is_html_data_list_element() const override { return true; }+ virtual void inserted() override;+ virtual void children_changed(ChildrenChangedMetadata const&) override;+ virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;+ virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;+ void notify_inputs_with_suggestions_sources_in(DOM::Node& root);+GC::Ptr<DOM::HTMLCollection> m_options;+ bool m_input_notification_queued { false };};}diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cppindex c3f5a8b29a3..9d79dc085bd 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp@@ -51,6 +51,7 @@#include <LibWeb/HTML/HTMLDataListElement.h>#include <LibWeb/HTML/HTMLFormElement.h>#include <LibWeb/HTML/HTMLInputElement.h>+#include <LibWeb/HTML/HTMLOptionElement.h>#include <LibWeb/HTML/Numbers.h>#include <LibWeb/HTML/Parser/HTMLParser.h>#include <LibWeb/HTML/RadioButtonGroupRegistry.h>@@ -143,6 +144,7 @@ void HTMLInputElement::visit_edges(Cell::Visitor& visitor)visitor.visit(m_slider_runnable_track);visitor.visit(m_slider_progress_element);visitor.visit(m_slider_thumb);+ visitor.visit(m_slider_ticks_element);visitor.visit(m_resource_request);}@@ -1462,6 +1464,8 @@ void HTMLInputElement::create_range_input_shadow_tree()MUST(m_slider_runnable_track->append_child(*m_slider_thumb));m_slider_thumb->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::SliderThumb);+ m_slider_ticks_element = nullptr;+ m_slider_tick_positions.clear();update_slider_shadow_tree_elements();auto keydown_callback_function = JS::NativeFunction::create(@@ -1622,6 +1626,8 @@ void HTMLInputElement::update_slider_shadow_tree_elements()MUST(m_slider_progress_element->style()->set_property(CSS::PropertyID::BackgroundColor, fill_color));}+ update_slider_ticks();+if (m_slider_thumb) {// NB: The thumb moves back by the same fraction of its own width, whatever width an author gives it.auto margin_left = Utf16String::formatted("{}%", position);@@ -1631,6 +1637,62 @@ void HTMLInputElement::update_slider_shadow_tree_elements()}}+// NB: A native slider marks the values its suggestions source element suggests with ticks below its track, at the+// positions the center of its thumb takes for them.+void HTMLInputElement::update_slider_ticks()+{+ if (!m_slider_runnable_track)+ return;++ Vector<double> positions;+ auto style = computed_style();+ bool const is_native = !style || style->appearance() != CSS::Appearance::None;+ if (is_native && has_attribute(HTML::AttributeNames::list)) {+ if (auto data_list = list()) {+ double minimum = *min();+ double maximum = *max();+ data_list->for_each_in_subtree_of_type<HTMLOptionElement>([&](HTMLOptionElement const& option) {+ if (option.disabled())+ return TraversalDecision::Continue;+ auto value = convert_string_to_number(option.value());+ if (value.has_value() && *value >= minimum && *value <= maximum && maximum > minimum)+ positions.append((*value - minimum) / (maximum - minimum) * 100);+ return TraversalDecision::Continue;+ });+ }+ }++ if (positions == m_slider_tick_positions)+ return;+ m_slider_tick_positions = move(positions);++ if (m_slider_tick_positions.is_empty()) {+ if (m_slider_ticks_element)+ m_slider_ticks_element->remove();+ m_slider_ticks_element = nullptr;+ return;+ }+ if (!m_slider_ticks_element) {+ m_slider_ticks_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));+ m_slider_ticks_element->set_attribute_value(HTML::AttributeNames::style, "position: absolute; inset-inline: 0; top: calc(100% + 2px); height: 4px; pointer-events: none;"_utf16);+ MUST(m_slider_runnable_track->append_child(*m_slider_ticks_element));+ }++ static constexpr double thumb_width = 16;+ m_slider_ticks_element->replace_all(nullptr);+ for (auto position : m_slider_tick_positions) {+ auto tick = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));+ tick->set_attribute_value(HTML::AttributeNames::style, Utf16String::formatted("position: absolute; left: calc({}% + {}px); width: 1px; height: 100%; background-color: color-mix(in srgb, CanvasText 40%, transparent);", position, thumb_width / 2 - thumb_width * position / 100 - 0.5));+ MUST(m_slider_ticks_element->append_child(tick));+ }+}++void HTMLInputElement::suggestions_source_element_did_change()+{+ if (type_state() == TypeAttributeState::Range)+ update_slider_ticks();+}+void HTMLInputElement::did_receive_focus(){if (!m_text_node)@@ -1712,6 +1774,8 @@ void HTMLInputElement::form_associated_element_attribute_changed(Utf16FlyStringupdate_shadow_tree();}+ } else if (name == HTML::AttributeNames::list) {+ suggestions_source_element_did_change();} else if (name == HTML::AttributeNames::alpha || name == HTML::AttributeNames::colorspace) {// https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color)// Whenever the element's alpha or colorspace attributes are changed, the user agent must run update a colordiff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.hindex 6ee555200f4..8ba934aa57f 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.h+++ b/Libraries/LibWeb/HTML/HTMLInputElement.h@@ -122,6 +122,7 @@ public:bool can_have_text_editing_cursor() const;GC::Ptr<HTMLDataListElement const> list() const;+ void suggestions_source_element_did_change();void did_pick_color(Optional<Color> picked_color, ColorPickerUpdateState state);@@ -415,9 +416,12 @@ private:GC::Ptr<DOM::Element> m_file_label;void update_slider_shadow_tree_elements();+ void update_slider_ticks();GC::Ptr<DOM::Element> m_slider_runnable_track;GC::Ptr<DOM::Element> m_slider_progress_element;GC::Ptr<DOM::Element> m_slider_thumb;+ GC::Ptr<DOM::Element> m_slider_ticks_element;+ Vector<double> m_slider_tick_positions;GC::Ptr<DecodedImageData> image_data() const;GC::Ptr<SharedResourceRequest> m_resource_request;diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cppindex 05b622b11e4..620b09c6762 100644--- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp@@ -101,6 +101,12 @@ void HTMLOptionElement::attribute_changed(Utf16FlyString const& name, Optional<Uif (namespace_.has_value())return;+ // NB: The inputs that show this option's datalist as ticks read its value and whether it is disabled.+ if (name == HTML::AttributeNames::value || name == HTML::AttributeNames::disabled) {+ if (auto* data_list = first_ancestor_of_type<HTMLDataListElement>())+ data_list->notify_inputs_using_this_as_suggestions_source();+ }+if (name == HTML::AttributeNames::selected) {// https://html.spec.whatwg.org/multipage/form-elements.html#attr-option-selected// Whenever an option element option's selected attribute is added or removed:@@ -436,6 +442,12 @@ void HTMLOptionElement::children_changed(ChildrenChangedMetadata const& metadataBase::children_changed(metadata);update_selection_label();++ // NB: Without a value attribute, the text of the option is its value.+ if (!has_attribute(AttributeNames::value)) {+ if (auto* data_list = first_ancestor_of_type<HTMLDataListElement>())+ data_list->notify_inputs_using_this_as_suggestions_source();+ }}}diff --git a/Tests/LibWeb/Layout/expected/input-range-datalist-ticks-attribute-changes.txt b/Tests/LibWeb/Layout/expected/input-range-datalist-ticks-attribute-changes.txtnew file mode 100644index 00000000000..b90ff3d318d--- /dev/null+++ b/Tests/LibWeb/Layout/expected/input-range-datalist-ticks-attribute-changes.txt@@ -0,0 +1,31 @@+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 37.203125 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 21.203125 0+0+8] children: inline+ frag 0 from RangeInputBox start: 0, length: 0, rect: [10,10 129x16] baseline: 18+ frag 1 from TextNode start: 0, length: 1, rect: [141,13 8x16] baseline: 12.796875+ " "+ frag 2 from RangeInputBox start: 0, length: 0, rect: [151,10 129x16] baseline: 18+ RangeInputBox <input#later> at [10,10] inline-block [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline+ Box <div> at [10,10] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [10,16] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [10,16] positioned [0+0+0 64.5 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [10,22] positioned [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [17.5,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [74,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [130.5,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [74.5,10] positioned [64.5+0+0 16 0+0+48.5] [-6+0+0 16 0+0+0] children: not-inline+ TextNode <#text> (not painted)+ RangeInputBox <input#orphaned> at [151,10] inline-block [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline+ Box <div> at [151,10] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [151,16] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [151,16] positioned [0+0+0 64.5 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [215.5,10] positioned [64.5+0+0 16 0+0+48.5] [-6+0+0 16 0+0+0] children: not-inline+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)++SC for Viewport<#document> [0,0 800x600] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x37.203125] (z-index: auto)+ SC for BlockContainer<DIV> [74.5,10 16x16] (z-index: auto), has_transform+ SC for BlockContainer<DIV> [215.5,10 16x16] (z-index: auto), has_transformdiff --git a/Tests/LibWeb/Layout/expected/input-range-datalist-ticks-option-changes.txt b/Tests/LibWeb/Layout/expected/input-range-datalist-ticks-option-changes.txtnew file mode 100644index 00000000000..820dfdf99bc--- /dev/null+++ b/Tests/LibWeb/Layout/expected/input-range-datalist-ticks-option-changes.txt@@ -0,0 +1,20 @@+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 37.203125 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 21.203125 0+0+8] children: inline+ frag 0 from RangeInputBox start: 0, length: 0, rect: [10,10 129x16] baseline: 18+ RangeInputBox <input> at [10,10] inline-block [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline+ Box <div> at [10,10] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [10,16] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [10,16] positioned [0+0+0 64.5 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [10,22] positioned [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [28.796875,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [74,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [102.25,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [74.5,10] positioned [64.5+0+0 16 0+0+48.5] [-6+0+0 16 0+0+0] children: not-inline+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)++SC for Viewport<#document> [0,0 800x600] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x37.203125] (z-index: auto)+ SC for BlockContainer<DIV> [74.5,10 16x16] (z-index: auto), has_transformdiff --git a/Tests/LibWeb/Layout/expected/input-range-datalist-ticks.txt b/Tests/LibWeb/Layout/expected/input-range-datalist-ticks.txtnew file mode 100644index 00000000000..75cbc804e97--- /dev/null+++ b/Tests/LibWeb/Layout/expected/input-range-datalist-ticks.txt@@ -0,0 +1,29 @@+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 42 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 26 0+0+8] children: inline+ frag 0 from RangeInputBox start: 0, length: 0, rect: [10,10 129x16] baseline: 18+ frag 1 from TextNode start: 0, length: 1, rect: [141,13 8x16] baseline: 12.796875+ " "+ frag 2 from BlockContainer start: 0, length: 0, rect: [151,16 129x16] baseline: 12+ RangeInputBox <input> at [10,10] inline-block [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline+ Box <div> at [10,10] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [10,16] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [10,16] positioned [0+0+0 41.890625 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [48.6875,10] positioned [38.6875+0+0 16 0+0+74.3125] [-6+0+0 16 0+0+0] children: not-inline+ BlockContainer <div> at [10,22] positioned [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [17.5,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [74,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [130.5,22] positioned [0+0+0 1 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ TextNode <#text> (not painted)+ BlockContainer <input> at [151,16] inline-block [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline+ Box <div> at [151,16] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [151,22] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [151,22] positioned [0+0+0 64.5 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [215.5,16] positioned [64.5+0+0 16 0+0+48.5] [-6+0+0 16 0+0+0] children: not-inline+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)++SC for Viewport<#document> [0,0 800x600] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x42] (z-index: auto)+ SC for BlockContainer<DIV> [48.6875,10 16x16] (z-index: 1), has_transform+ SC for BlockContainer<DIV> [215.5,16 16x16] (z-index: 1), has_transformdiff --git a/Tests/LibWeb/Layout/input/input-range-datalist-ticks-attribute-changes.html b/Tests/LibWeb/Layout/input/input-range-datalist-ticks-attribute-changes.htmlnew file mode 100644index 00000000000..4d9dc5cb530--- /dev/null+++ b/Tests/LibWeb/Layout/input/input-range-datalist-ticks-attribute-changes.html@@ -0,0 +1,9 @@+<!DOCTYPE html>+<input id="later" type="range">+<input id="orphaned" type="range" list="other">+<datalist id="marks"><option value="0"><option value="50"><option value="100"></datalist>+<datalist id="other"><option value="25"><option value="75"></datalist>+<script>+ document.getElementById("later").setAttribute("list", "marks");+ document.getElementById("other").remove();+</script>diff --git a/Tests/LibWeb/Layout/input/input-range-datalist-ticks-option-changes.html b/Tests/LibWeb/Layout/input/input-range-datalist-ticks-option-changes.htmlnew file mode 100644index 00000000000..df7800a4e28--- /dev/null+++ b/Tests/LibWeb/Layout/input/input-range-datalist-ticks-option-changes.html@@ -0,0 +1,10 @@+<!DOCTYPE html>+<input type="range" list="marks">+<datalist id="marks"><option value="0"><option value="50"><option value="100"><option>25</option></datalist>+<script>+ document.body.offsetWidth;+ const options = document.getElementById("marks").options;+ options[0].setAttribute("value", "10");+ options[2].disabled = true;+ options[3].textContent = "75";+</script>diff --git a/Tests/LibWeb/Layout/input/input-range-datalist-ticks.html b/Tests/LibWeb/Layout/input/input-range-datalist-ticks.htmlnew file mode 100644index 00000000000..fd9bb176144--- /dev/null+++ b/Tests/LibWeb/Layout/input/input-range-datalist-ticks.html@@ -0,0 +1,4 @@+<!DOCTYPE html>+<input type="range" list="marks" value="30">+<input type="range" list="marks" style="appearance: none">+<datalist id="marks"><option value="0"><option value="50"><option value="100"><option value="200"></datalist>