Invalidate node list when associated form control element is removed
authordbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 3 Nov 2017 15:41:10 +0000 (15:41 +0000)
committerdbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 3 Nov 2017 15:41:10 +0000 (15:41 +0000)
https://bugs.webkit.org/show_bug.cgi?id=179232
<rdar://problem/35308269>

Reviewed by Ryosuke Niwa.

Source/WebCore:

A node list represents a live view of the DOM. Invalidate the node list
associated with a form element whenever one of its associated form control
elements is removed.

Test: fast/forms/node-list-remove-button-from-form.html

* html/HTMLFormElement.cpp:
(WebCore::HTMLFormElement::removeFormElement):

LayoutTests:

Add a test to ensure the node list returned by HTMLFormElement.elements stays synchronized
with the document.

* fast/forms/node-list-remove-button-from-form-expected.txt: Added.
* fast/forms/node-list-remove-button-from-form.html: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@224398 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/forms/node-list-remove-button-from-form-expected.txt [new file with mode: 0644]
LayoutTests/fast/forms/node-list-remove-button-from-form.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/html/HTMLFormElement.cpp

index f16b6ea..b035eef 100644 (file)
@@ -1,3 +1,17 @@
+2017-11-03  Daniel Bates  <dabates@apple.com>
+
+        Invalidate node list when associated form control element is removed
+        https://bugs.webkit.org/show_bug.cgi?id=179232
+        <rdar://problem/35308269>
+
+        Reviewed by Ryosuke Niwa.
+
+        Add a test to ensure the node list returned by HTMLFormElement.elements stays synchronized
+        with the document.
+
+        * fast/forms/node-list-remove-button-from-form-expected.txt: Added.
+        * fast/forms/node-list-remove-button-from-form.html: Added.
+
 2017-11-03  Antti Koivisto  <antti@apple.com>
 
         Crash in WebCore::RenderStyle::overflowX with display:contents
diff --git a/LayoutTests/fast/forms/node-list-remove-button-from-form-expected.txt b/LayoutTests/fast/forms/node-list-remove-button-from-form-expected.txt
new file mode 100644 (file)
index 0000000..f459f8d
--- /dev/null
@@ -0,0 +1,15 @@
+Tests that removing a <button> from the document removes it from HTMLFormElement.elements.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+Before removal of button:
+PASS document.getElementById('aForm').elements is non-null.
+PASS document.getElementById('aForm')['aButton'] is document.getElementById('aButton')
+
+After removal of button:
+PASS document.getElementById('aForm')['aButton'] is undefined.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/forms/node-list-remove-button-from-form.html b/LayoutTests/fast/forms/node-list-remove-button-from-form.html
new file mode 100644 (file)
index 0000000..f12db35
--- /dev/null
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../../resources/js-test.js"></script>
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<button id="aButton" form="aForm"></button>
+<form id="aForm"></form>
+<script>
+description("Tests that removing a &lt;button&gt; from the document removes it from HTMLFormElement.elements.");
+
+runTest();
+
+function runTest()
+{
+    debug("Before removal of button:");
+    shouldBeNonNull("document.getElementById('aForm').elements");
+    shouldBe("document.getElementById('aForm')['aButton']", "document.getElementById('aButton')");
+    document.getElementById('aButton').remove();
+
+    debug("<br>After removal of button:");
+    shouldBeUndefined("document.getElementById('aForm')['aButton']");
+}
+</script>
+</body>
+</html>
index 0a36ab6..3580e26 100644 (file)
@@ -1,3 +1,20 @@
+2017-11-03  Daniel Bates  <dabates@apple.com>
+
+        Invalidate node list when associated form control element is removed
+        https://bugs.webkit.org/show_bug.cgi?id=179232
+        <rdar://problem/35308269>
+
+        Reviewed by Ryosuke Niwa.
+
+        A node list represents a live view of the DOM. Invalidate the node list
+        associated with a form element whenever one of its associated form control
+        elements is removed.
+
+        Test: fast/forms/node-list-remove-button-from-form.html
+
+        * html/HTMLFormElement.cpp:
+        (WebCore::HTMLFormElement::removeFormElement):
+
 2017-11-03  Frederic Wang  <fwang@igalia.com>
 
         Add USE(APPLE_INTERNAL_SDK)-guards around SPI in ResourceHandle code
index dc6169c..09003d5 100644 (file)
@@ -552,6 +552,9 @@ void HTMLFormElement::removeFormElement(FormAssociatedElement* e)
     removeFromPastNamesMap(e);
     m_associatedElements.remove(index);
 
+    if (auto* nodeLists = this->nodeLists())
+        nodeLists->invalidateCaches();
+
     if (e == m_defaultButton)
         resetDefaultButton();
 }