Truly independent web browser
LibWeb: Don’t redo a select’s options for text between them
sideshowbarker1aea10bb2fff
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cppindex cd12ff20c16..7c4e2ab1972 100644--- a/Libraries/LibWeb/DOM/Node.cpp+++ b/Libraries/LibWeb/DOM/Node.cpp@@ -1127,7 +1127,7 @@ void Node::insert_nodes_before(ReadonlySpan<GC::Ref<Node>> nodes, GC::Ptr<Node>}// 9. Run the children changed steps for parent.- ChildrenChangedMetadata metadata { ChildrenChangedMetadata::Type::Inserted, metadata_node, affects_elements };+ ChildrenChangedMetadata metadata { ChildrenChangedMetadata::Type::Inserted, metadata_node, affects_elements, {}, nodes };children_changed(metadata);invalidate_html_collection_caches_in_ancestors(affects_elements);diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.hindex 9e8e5f3618d..066a0a40aa8 100644--- a/Libraries/LibWeb/DOM/Node.h+++ b/Libraries/LibWeb/DOM/Node.h@@ -449,6 +449,8 @@ public:AffectsElements affects_elements { AffectsElements::No };// For a removal, the sibling that was before the removed node, if any.GC::Ptr<Node> old_previous_sibling {};+ // For an insertion of several nodes, such as a document fragment's children, the nodes inserted.+ ReadonlySpan<GC::Ref<Node>> inserted_nodes {};};// FIXME: It would be good if we could always provide this metadata for use in optimizations.virtual void children_changed(ChildrenChangedMetadata const&) { }diff --git a/Libraries/LibWeb/HTML/HTMLOptGroupElement.cpp b/Libraries/LibWeb/HTML/HTMLOptGroupElement.cppindex 3e1bce25792..d1c2581e99d 100644--- a/Libraries/LibWeb/HTML/HTMLOptGroupElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLOptGroupElement.cpp@@ -29,16 +29,6 @@ void HTMLOptGroupElement::inserted()static_cast<HTMLSelectElement&>(*parent()).update_selectedness();}-void HTMLOptGroupElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor, Node& old_root)-{- Base::removed_from(is_subtree_root, old_ancestor, old_root);-- // The optgroup HTML element removing steps, given removedNode and oldParent, are:- // 1. If oldParent is a select element and removedNode has an option child, then run oldParent's selectedness setting algorithm.- if (old_ancestor && is<HTMLSelectElement>(*old_ancestor) && first_child_of_type<HTMLOptionElement>())- static_cast<HTMLSelectElement&>(*old_ancestor).update_selectedness();-}-void HTMLOptGroupElement::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_);diff --git a/Libraries/LibWeb/HTML/HTMLOptGroupElement.h b/Libraries/LibWeb/HTML/HTMLOptGroupElement.hindex 67d8543eeca..d00d6db3b10 100644--- a/Libraries/LibWeb/HTML/HTMLOptGroupElement.h+++ b/Libraries/LibWeb/HTML/HTMLOptGroupElement.h@@ -26,7 +26,6 @@ private:HTMLOptGroupElement(DOM::Document&, DOM::QualifiedName);virtual bool is_html_optgroup_element() const final { return true; }- virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;virtual void inserted() override;virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;};diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp b/Libraries/LibWeb/HTML/HTMLSelectElement.cppindex 5fe9156d4c8..28472f1ad49 100644--- a/Libraries/LibWeb/HTML/HTMLSelectElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLSelectElement.cpp@@ -7,6 +7,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/AnyOf.h>#include <LibJS/Runtime/NativeFunction.h>#include <LibWeb/Bindings/Wrappable.h>#include <LibWeb/CSS/CSSStyleProperties.h>@@ -373,25 +374,80 @@ bool HTMLSelectElement::can_skip_selectedness_update_for_inserted_option(HTMLOptreturn true;}-bool HTMLSelectElement::can_skip_children_changed_selectedness_update(ChildrenChangedMetadata const& metadata) const+// Whether node, a child of a select or one just removed from it, holds an option of the select's list of options.+static bool holds_listed_option(DOM::Node const& node){- // If the following criteria are met, there is no need to re-run the selectedness algorithm.- // FIXME: We can tighten up these conditions and skip even more work!- if (metadata.type != ChildrenChangedMetadata::Type::Inserted)+ if (!is<DOM::Element>(node))return false;- if (auto* option = as_if<HTMLOptionElement>(*metadata.node))- return can_skip_selectedness_update_for_inserted_option(*option);+ // An optgroup with an ancestor optgroup in between itself and the select.+ auto is_nested_optgroup = [&node](DOM::Node const& descendant) {+ if (!is<HTMLOptGroupElement>(descendant) || &descendant == &node)+ return false;+ for (auto const* ancestor = descendant.parent(); ancestor; ancestor = ancestor->parent()) {+ if (is<HTMLOptGroupElement>(*ancestor))+ return true;+ if (ancestor == &node)+ break;+ }+ return false;+ };- return false;+ bool found_option = false;+ node.for_each_in_inclusive_subtree([&](DOM::Node const& descendant) {+ if (is<HTMLOptionElement>(descendant)) {+ found_option = true;+ return TraversalDecision::Break;+ }+ // NB: The list of options leaves out the options in these, as the steps that build it skip their descendants.+ if (is<HTMLSelectElement>(descendant) || is<HTMLHRElement>(descendant) || is<HTMLDataListElement>(descendant) || is_nested_optgroup(descendant))+ return TraversalDecision::SkipChildrenAndContinue;+ return TraversalDecision::Continue;+ });+ return found_option;+}++bool HTMLSelectElement::can_skip_children_changed_selectedness_update(ChildrenChangedMetadata const& metadata) const+{+ // If the following criteria are met, there is no need to re-run the selectedness algorithm.+ // NB: The selectedness setting algorithm runs when the list of options gains or loses an option element, so a+ // child that can't change the list, such as the text and comments between the options, leaves it alone.+ switch (metadata.type) {+ case ChildrenChangedMetadata::Type::Mutation:+ // The data of a text or comment child changed.+ return true;+ case ChildrenChangedMetadata::Type::Inserted:+ if (auto* option = as_if<HTMLOptionElement>(*metadata.node))+ return can_skip_selectedness_update_for_inserted_option(*option);+ // NB: An insertion of several nodes, such as a document fragment's children, names the nodes it inserted.+ if (!metadata.inserted_nodes.is_empty())+ return !any_of(metadata.inserted_nodes, [](auto const& node) { return holds_listed_option(*node); });+ return !holds_listed_option(*metadata.node);+ case ChildrenChangedMetadata::Type::Removal:+ return !holds_listed_option(*metadata.node);+ case ChildrenChangedMetadata::Type::AllChildrenRemoved:+ return false;+ }+ VERIFY_NOT_REACHED();}void HTMLSelectElement::children_changed(ChildrenChangedMetadata const& metadata){Base::children_changed(metadata);- if (can_skip_children_changed_selectedness_update(metadata))+ if (can_skip_children_changed_selectedness_update(metadata)) {+ // A list box still shows a row for the label of each optgroup child, even one with no options.+ auto changes_optgroup_children = [&] {+ if (metadata.type != ChildrenChangedMetadata::Type::Inserted && metadata.type != ChildrenChangedMetadata::Type::Removal)+ return false;+ if (!metadata.inserted_nodes.is_empty())+ return any_of(metadata.inserted_nodes, [](auto const& node) { return is<HTMLOptGroupElement>(*node); });+ return is<HTMLOptGroupElement>(*metadata.node);+ };+ if (changes_optgroup_children())+ list_box_options_did_change();return;+ }update_cached_list_of_options();update_selectedness();diff --git a/Tests/LibWeb/Layout/expected/select-list-box-optgroups-replace-children.txt b/Tests/LibWeb/Layout/expected/select-list-box-optgroups-replace-children.txtnew file mode 100644index 00000000000..6a481abd0a3--- /dev/null+++ b/Tests/LibWeb/Layout/expected/select-list-box-optgroups-replace-children.txt@@ -0,0 +1,18 @@+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 83.96875 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 67.96875 0+0+8] children: inline+ frag 0 from BlockContainer start: 0, length: 0, rect: [9,10 55.8125x63.96875] baseline: 64.96875+ BlockContainer <select> at [9,10] inline-block [0+1+0 55.8125 0+1+0] [0+1+1 63.96875 1+1+0] [BFC] children: not-inline+ BlockContainer <div> at [9,10] [0+0+0 55.8125 0+0+0] [0+0+0 63.96875 0+0+0] [BFC] children: not-inline+ BlockContainer <div> at [13,10] [0+0+4 47.8125 4+0+0] [0+0+0 16 0+0+0] children: inline+ frag 0 from TextNode start: 0, length: 5, rect: [13,11 35.109375x14] baseline: 11+ "First"+ TextNode <#text> (not painted)+ BlockContainer <div> at [13,26] [0+0+4 47.8125 4+0+0] [0+0+0 16 0+0+0] children: inline+ frag 0 from TextNode start: 0, length: 6, rect: [13,27 47.8125x14] baseline: 11+ "Second"+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)++SC for Viewport<#document> [0,0 800x600] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x83.96875] (z-index: auto)diff --git a/Tests/LibWeb/Layout/input/select-list-box-optgroups-replace-children.html b/Tests/LibWeb/Layout/input/select-list-box-optgroups-replace-children.htmlnew file mode 100644index 00000000000..2f1ea26ae70--- /dev/null+++ b/Tests/LibWeb/Layout/input/select-list-box-optgroups-replace-children.html@@ -0,0 +1,12 @@+<!DOCTYPE html>+<select size="4"></select>+<script>+ document.body.offsetWidth;+ const select = document.querySelector("select");+ const group = label => {+ const optgroup = document.createElement("optgroup");+ optgroup.label = label;+ return optgroup;+ };+ select.replaceChildren(group("First"), group("Second"));+</script>diff --git a/Tests/LibWeb/Text/expected/HTML/select-non-option-children-keep-selectedness.txt b/Tests/LibWeb/Text/expected/HTML/select-non-option-children-keep-selectedness.txtnew file mode 100644index 00000000000..7dd91b60b33--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/select-non-option-children-keep-selectedness.txt@@ -0,0 +1,22 @@+Append text: -1+Append comment: -1+Change text: -1+Remove comment: -1+Remove text: -1+Append fragment of text and comment: -1+Append fragment with option: 0+Append div: -1+Remove div: -1+Append empty optgroup: -1+Remove empty optgroup: -1+Append datalist with option: -1+Remove datalist with option: -1+Append hr with option: -1+Remove hr with option: -1+Append optgroup in optgroup with option: -1+Remove optgroup in optgroup with option: -1+Append optgroup with option: 0+Remove optgroup with option: 0+Append option: 0+Append div with option: 0+Remove div with option: 0diff --git a/Tests/LibWeb/Text/input/HTML/select-non-option-children-keep-selectedness.html b/Tests/LibWeb/Text/input/HTML/select-non-option-children-keep-selectedness.htmlnew file mode 100644index 00000000000..05ee6fdef7f--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/select-non-option-children-keep-selectedness.html@@ -0,0 +1,67 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<select id="select"><option>a</option><option>b</option></select>+<script>+ test(() => {+ // With no option selected, the selectedness setting algorithm would select the first one again. It only runs when+ // the list of options gains or loses an option, so a child that can't change the list leaves none selected.+ function check(description, mutate) {+ select.selectedIndex = -1;+ mutate();+ println(`${description}: ${select.selectedIndex}`);+ }++ check("Append text", () => select.append("\n"));+ check("Append comment", () => select.append(document.createComment("comment")));+ check("Change text", () => select.lastChild.previousSibling.data = "text");+ check("Remove comment", () => select.lastChild.remove());+ check("Remove text", () => select.lastChild.remove());+ check("Append fragment of text and comment", () => {+ const fragment = document.createDocumentFragment();+ fragment.append("text", document.createComment("comment"));+ select.append(fragment);+ });+ check("Append fragment with option", () => {+ const fragment = document.createDocumentFragment();+ fragment.append("text", new Option("e"));+ select.append(fragment);+ });+ check("Append div", () => select.append(document.createElement("div")));+ check("Remove div", () => select.lastChild.remove());+ check("Append empty optgroup", () => select.append(document.createElement("optgroup")));+ check("Remove empty optgroup", () => select.lastChild.remove());+ check("Append datalist with option", () => {+ const datalist = document.createElement("datalist");+ datalist.append(new Option("f"));+ select.append(datalist);+ });+ check("Remove datalist with option", () => select.lastChild.remove());+ check("Append hr with option", () => {+ const hr = document.createElement("hr");+ hr.append(new Option("g"));+ select.append(hr);+ });+ check("Remove hr with option", () => select.lastChild.remove());+ check("Append optgroup in optgroup with option", () => {+ const outer = document.createElement("optgroup");+ const inner = document.createElement("optgroup");+ inner.append(new Option("h"));+ outer.append(inner);+ select.append(outer);+ });+ check("Remove optgroup in optgroup with option", () => select.lastChild.remove());+ check("Append optgroup with option", () => {+ const group = document.createElement("optgroup");+ group.append(new Option("i"));+ select.append(group);+ });+ check("Remove optgroup with option", () => select.lastChild.remove());+ check("Append option", () => select.append(new Option("c")));+ check("Append div with option", () => {+ const div = document.createElement("div");+ div.append(new Option("d"));+ select.append(div);+ });+ check("Remove div with option", () => select.lastChild.remove());+ });+</script>