Browse Source

LibWeb: Ensure `document.getElementsByName("")` returns no elements

Previously, if a document had any element with a name attribute that
was  set to the empty string, then `document.getElementsByName("")` and
`element.getElementsByName("")` would return a collection including
those elements.
Tim Ledbetter 11 months ago
parent
commit
e40352b6b5

+ 1 - 0
Tests/LibWeb/Text/expected/DOM/getElementsName-empty-string.txt

@@ -0,0 +1 @@
+  document.getElementsByName("").length: 0

+ 8 - 0
Tests/LibWeb/Text/input/DOM/getElementsName-empty-string.html

@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div name=""></div>
+<script>
+    test(() => {
+        println(`document.getElementsByName("").length: ${document.getElementsByName("").length}`);
+    });
+</script>

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -464,7 +464,7 @@ void Element::attribute_changed(FlyString const& name, Optional<String> const&,
 
 
         document().element_id_changed({}, *this);
         document().element_id_changed({}, *this);
     } else if (name == HTML::AttributeNames::name) {
     } else if (name == HTML::AttributeNames::name) {
-        if (!value.has_value())
+        if (value_or_empty.is_empty())
             m_name = {};
             m_name = {};
         else
         else
             m_name = value_or_empty;
             m_name = value_or_empty;