Преглед изворни кода

LibWeb: Ensure `document.getElementById("")` returns null

Previously, if a document had an element whose id was the empty string,
then `document.getElementById("")` and `element.getElementById("")`
would return that element.
Tim Ledbetter пре 1 година
родитељ
комит
f666d967d6

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

@@ -0,0 +1 @@
+  document.getElementById(""): null

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

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

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

@@ -457,7 +457,7 @@ void Element::attribute_changed(FlyString const& name, Optional<String> const&,
     auto value_or_empty = value.value_or(String {});
 
     if (name == HTML::AttributeNames::id) {
-        if (!value.has_value())
+        if (value_or_empty.is_empty())
             m_id = {};
         else
             m_id = value_or_empty;