Truly independent web browser
LibWeb: Restore option insertion fast path for selectedness updates
Shannon Boothe7b21e59de66
diff --git a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp b/Libraries/LibWeb/HTML/HTMLOptionElement.cppindex 8b169bc843d..12fc0602409 100644--- a/Libraries/LibWeb/HTML/HTMLOptionElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLOptionElement.cpp@@ -87,6 +87,12 @@ void HTMLOptionElement::set_selected_internal(bool selected)if (m_selected != selected) {CSS::Invalidation::invalidate_style_after_option_selected_state_change(*this, selected);document().bump_option_selectedness_version();+ if (m_cached_nearest_select_element) {+ if (selected)+ ++m_cached_nearest_select_element->m_number_of_selected_options;+ else+ --m_cached_nearest_select_element->m_number_of_selected_options;+ }}m_selected = selected;@@ -224,17 +230,27 @@ void HTMLOptionElement::update_nearest_select_element()// 3. If oldSelect is not newSelect:if (old_select != new_select) {+ // OPTIMIZATION: Do step 4 first and move this option between the selects' counts of selected options, so that+ // selectedness changes made by the algorithms below are counted against newSelect.+ if (selected()) {+ if (old_select)+ --old_select->m_number_of_selected_options;+ if (new_select)+ ++new_select->m_number_of_selected_options;+ }+ m_cached_nearest_select_element = new_select;+// 1. If oldSelect is not null, then run the selectedness setting algorithm given oldSelect.if (old_select)old_select->update_selectedness();// 2. If newSelect is not null, then run the selectedness setting algorithm given newSelect.- if (new_select)+ if (new_select && !new_select->can_skip_selectedness_update_for_inserted_option(*this))new_select->update_selectedness();}// 4. Set option's cached nearest ancestor select element to newSelect.- m_cached_nearest_select_element = new_select;+ // NOTE: Done in step 3.}Optional<ARIA::Role> HTMLOptionElement::default_role() constdiff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cppindex 08ad2cf275e..dda2bd209ac 100644--- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp@@ -203,7 +203,6 @@ void HTMLSelectElement::update_cached_list_of_options() const{// 1. Let options be « ».m_cached_list_of_options.clear();- m_cached_number_of_selected_options = 0;// Check if node is an optgroup element and node has an ancestor optgroup in between itself and this selectauto is_nested_optgroup = [this](DOM::Node const& node) {@@ -223,11 +222,8 @@ void HTMLSelectElement::update_cached_list_of_options() const// 3. While node is not null:for_each_in_subtree([&](auto& node) {// 1. If node is an option element, then append node to options.- if (auto maybe_option = as_if<HTMLOptionElement>(node)) {- if (maybe_option->selected())- ++m_cached_number_of_selected_options;+ if (auto maybe_option = as_if<HTMLOptionElement>(node))m_cached_list_of_options.append(const_cast<HTMLOptionElement&>(*maybe_option));- }// 2. If any of the following conditions are true:// - node is a select element;@@ -354,10 +350,10 @@ bool HTMLSelectElement::can_skip_selectedness_update_for_inserted_option(HTMLOptif (option.selected())return false;- if (m_cached_number_of_selected_options >= 2)+ if (m_number_of_selected_options >= 2)return false;- if (display_size() == 1 && m_cached_number_of_selected_options == 0)+ if (display_size() == 1 && m_number_of_selected_options == 0)return false;return true;@@ -770,7 +766,7 @@ void HTMLSelectElement::update_selectedness()// 2. If element 's multiple attribute is absent, and element's display size is 1,// and no option elements in the element's list of options have their selectedness set to true, then- if (!has_attribute(AttributeNames::multiple) && display_size() == 1 && m_cached_number_of_selected_options == 0) {+ if (!has_attribute(AttributeNames::multiple) && display_size() == 1 && m_number_of_selected_options == 0) {// 1. Set the selectedness of the first option element in the list of options in tree order// that is not disabled, if any, to true.for (auto const& option_element : m_cached_list_of_options) {@@ -785,7 +781,7 @@ void HTMLSelectElement::update_selectedness()}// Otherwise, if element's multiple attribute is absent,// and two or more option elements in element's list of options have their selectedness set to true, then:- else if (!has_attribute(AttributeNames::multiple) && m_cached_number_of_selected_options >= 2) {+ else if (!has_attribute(AttributeNames::multiple) && m_number_of_selected_options >= 2) {// 1. Set the selectedness of all but the last option element with its selectedness set to true// in the list of options in tree order to false.GC::Ptr<HTML::HTMLOptionElement> last_selected_option;diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.h b/Libraries/LibWeb/HTML/HTMLSelectElement.hindex f127eeb0f7f..d9e4eada053 100644--- a/Libraries/LibWeb/HTML/HTMLSelectElement.h+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.h@@ -129,6 +129,8 @@ public:virtual bool is_mutable() const override;private:+ friend class HTMLOptionElement;+HTMLSelectElement(DOM::Document&, DOM::QualifiedName);virtual void visit_edges(Cell::Visitor&) override;@@ -150,7 +152,7 @@ private:u32 display_size() const;mutable Vector<GC::Ref<HTMLOptionElement>> m_cached_list_of_options;- mutable size_t m_cached_number_of_selected_options { 0 };+ size_t m_number_of_selected_options { 0 };mutable GC::Ptr<HTMLOptionsCollection> m_options;GC::Ptr<DOM::HTMLCollection> m_selected_options;diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-selectedness-after-option-insertion.txt b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-selectedness-after-option-insertion.txtnew file mode 100644index 00000000000..8584e2f00d7--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/HTMLSelectElement-selectedness-after-option-insertion.txt@@ -0,0 +1,4 @@+After selectedIndex = -1: 0+After setting a value with no matching option: 0+After adding a selected attribute: 1, a=false, b=true+After moving a selected option: from=0, to=1diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/select/options-length-too-large.txt b/Tests/LibWeb/Text/expected/wpt-import/html/select/options-length-too-large.txtnew file mode 100644index 00000000000..76270d837f8--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/select/options-length-too-large.txt@@ -0,0 +1,10 @@+Harness status: OK++Found 5 tests++5 Pass+Pass select options.length too large+Pass select options.length too large 1+Pass select options.length too large 2+Pass select options.length too large 3+Pass select options.length too large 4\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-selectedness-after-option-insertion.html b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-selectedness-after-option-insertion.htmlnew file mode 100644index 00000000000..1e28075391a--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/HTMLSelectElement-selectedness-after-option-insertion.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ test(() => {+ let select = document.createElement("select");+ select.append(new Option("a"));+ select.selectedIndex = -1;+ select.append(new Option("b"));+ println(`After selectedIndex = -1: ${select.selectedIndex}`);++ select = document.createElement("select");+ select.append(new Option("a"));+ select.value = "missing";+ select.append(new Option("b"));+ println(`After setting a value with no matching option: ${select.selectedIndex}`);++ select = document.createElement("select");+ const a = new Option("a");+ const b = new Option("b");+ select.append(a, b);+ b.setAttribute("selected", "");+ select.append(new Option("c"));+ println(`After adding a selected attribute: ${select.selectedIndex}, a=${a.selected}, b=${b.selected}`);++ const from = document.createElement("select");+ const moved = new Option("moved");+ from.append(new Option("x"), moved);+ moved.selected = true;+ const to = document.createElement("select");+ to.append(new Option("y"));+ to.append(moved);+ from.append(new Option("z"));+ to.append(new Option("w"));+ println(`After moving a selected option: from=${from.selectedIndex}, to=${to.selectedIndex}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/select/options-length-too-large.html b/Tests/LibWeb/Text/input/wpt-import/html/select/options-length-too-large.htmlnew file mode 100644index 00000000000..e670b004379--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/select/options-length-too-large.html@@ -0,0 +1,50 @@+<!DOCTYPE html>+<html>+ <head>+ <meta charset="utf-8">+ <meta name="timeout" content="long">+ <title>select options.length too large</title>++ <script src="../../resources/testharness.js"></script>+ <script src="../../resources/testharnessreport.js"></script>+ </head>+ <body>+ <select id="test">+ <option value="1"></option>+ <option value="2"></option>+ <option value="3"></option>+ </select>++ <script>+ var mySelect = document.getElementById("test");++ test(function() {+ mySelect.options.length = -1;+ assert_equals(mySelect.options.length, 3, "Length of <select> should remain unchanged");+ });++ test(function() {+ mySelect.options.length = 100001;+ assert_equals(mySelect.options.length, 3, "Length of <select> should remain unchanged");+ });++ test(function() {+ mySelect.options.length = Number.MAX_SAFE_INTEGER;+ assert_equals(mySelect.options.length, 3, "Length of <select> should remain unchanged");+ });++ test(function() {+ mySelect.options.length = 100000;+ assert_equals(mySelect.options.length, 100000, "Length of <select> should be 100,000");+ });++ test(function() {+ mySelect.appendChild(new Option());+ mySelect.appendChild(new Option());+ assert_equals(mySelect.options.length, 100002, "Manual expansion still works");+ mySelect.options.length = 100001;+ assert_equals(mySelect.options.length, 100001, "Truncation works if over the limit");+ });+ </script>+ </body>+</html>