Truly independent web browser
LibWeb: Keep shadow tree elements out of the potentially named elements
Andreas Klinga5b95e4a0c10
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 06843e48261..bf9a83c62ba 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -9570,10 +9570,13 @@ static bool is_named_element_by_id(DOM::Element const& element)// Document, or a named property by name of Window. Window looks up every other element by id in the element by id// map. Keeping any other element out of the list keeps the tree order insertions below cheap, which matters for// documents with thousands of elements with an id, such as SVG icon sprites.+// Only elements in a document tree with the document as their root contribute, so an element in a shadow tree is+// kept out as well.static bool should_be_in_potentially_named_elements(DOM::Element const& element){- return (element.name().has_value() && is_potentially_named_element(element))- || (element.id().has_value() && is_potentially_named_element_by_id(element));+ return ((element.name().has_value() && is_potentially_named_element(element))+ || (element.id().has_value() && is_potentially_named_element_by_id(element)))+ && element.root().is_document();}static void insert_in_tree_order(Vector<GC::Ref<DOM::Element>>& elements, DOM::Element& element)@@ -9695,7 +9698,7 @@ void Document::element_with_id_was_added(Badge<DOM::Element>, GC::Ref<DOM::Elemefor (auto* form_associated_element : m_form_associated_elements_with_form_attribute)form_associated_element->element_with_id_was_added_or_removed({});- if (is_potentially_named_element_by_id(*element)) {+ if (is_potentially_named_element_by_id(*element) && element->root().is_document()) {insert_in_tree_order(m_potentially_named_elements, element);if (is_named_element_by_id(*element))did_add_supported_property_name();@@ -9735,7 +9738,7 @@ void Document::element_name_changed(Badge<DOM::Element>, GC::Ref<DOM::Element> evoid Document::element_with_name_was_added(Badge<DOM::Element>, GC::Ref<DOM::Element> element){- if (is_potentially_named_element(element)) {+ if (is_potentially_named_element(element) && element->root().is_document()) {insert_in_tree_order(m_potentially_named_elements, element);did_add_supported_property_name();}diff --git a/Tests/LibWeb/Text/expected/DOM/Document-named-properties-ignore-shadow-trees.txt b/Tests/LibWeb/Text/expected/DOM/Document-named-properties-ignore-shadow-trees.txtnew file mode 100644index 00000000000..850efa8adfc--- /dev/null+++ b/Tests/LibWeb/Text/expected/DOM/Document-named-properties-ignore-shadow-trees.txt@@ -0,0 +1,8 @@+document.shadowform: undefined+document.shadowimg: undefined+document.shadowobject: undefined+window.shadowimg: undefined+document.shadowform after moving: [object HTMLFormElement]+document.shadowimg after moving: [object HTMLImageElement]+document.shadowobject after moving: [object HTMLObjectElement]+window.shadowimg after moving: [object HTMLImageElement]diff --git a/Tests/LibWeb/Text/input/DOM/Document-named-properties-ignore-shadow-trees.html b/Tests/LibWeb/Text/input/DOM/Document-named-properties-ignore-shadow-trees.htmlnew file mode 100644index 00000000000..9e71706a36a--- /dev/null+++ b/Tests/LibWeb/Text/input/DOM/Document-named-properties-ignore-shadow-trees.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<div id="host"></div>+<script>+ test(() => {+ const root = host.attachShadow({ mode: "open" });++ const form = document.createElement("form");+ root.appendChild(form);+ form.setAttribute("name", "shadowform");++ const img = document.createElement("img");+ img.setAttribute("name", "shadowimg");+ root.appendChild(img);++ const object = document.createElement("object");+ root.appendChild(object);+ object.id = "shadowobject";++ println(`document.shadowform: ${document.shadowform}`);+ println(`document.shadowimg: ${document.shadowimg}`);+ println(`document.shadowobject: ${document.shadowobject}`);+ println(`window.shadowimg: ${window.shadowimg}`);++ document.body.append(form, img, object);+ println(`document.shadowform after moving: ${document.shadowform}`);+ println(`document.shadowimg after moving: ${document.shadowimg}`);+ println(`document.shadowobject after moving: ${document.shadowobject}`);+ println(`window.shadowimg after moving: ${window.shadowimg}`);+ });+</script>