# HG changeset patch # User Boris Zbarsky # Date 1258664326 18000 # Node ID 54cf88d0981913ffd98cbb35f22499f3d1f3738f # Parent c6991463f32ac1b57aedba95bae33e109853eb04 Bug 529819. Don't double-add a node to form.elements['foo']. r=smaug diff --git a/content/html/content/src/nsHTMLFormElement.cpp b/content/html/content/src/nsHTMLFormElement.cpp --- a/content/html/content/src/nsHTMLFormElement.cpp +++ b/content/html/content/src/nsHTMLFormElement.cpp @@ -2274,37 +2274,47 @@ nsFormControlList::AddElementToTable(nsI NS_ENSURE_TRUE(nodeList, NS_ERROR_FAILURE); // Upcast, uggly, but it works! nsBaseContentList *list = static_cast ((nsIDOMNodeList *)nodeList.get()); NS_ASSERTION(list->Length() > 1, "List should have been converted back to a single element"); - + + // Fast-path appends; this check is ok even if the child is + // already in the list, since if it tests true the child would + // have come at the end of the list, and the PositionIsBefore + // will test false. if(nsContentUtils::PositionIsBefore(list->GetNodeAt(list->Length() - 1), newChild)) { - list->AppendElement(newChild); - return NS_OK; + list->AppendElement(newChild); + return NS_OK; + } + + // If a control has a name equal to its id, it could be in the + // list already. + if (list->IndexOf(newChild) != -1) { + return NS_OK; } // first is the first possible insertion index, last is the last possible // insertion index PRUint32 first = 0; PRUint32 last = list->Length() - 1; PRUint32 mid; - //Stop when there is only one index in our range + // Stop when there is only one index in our range while (last != first) { - mid = (first + last) / 2; + mid = (first + last) / 2; - if (nsContentUtils::PositionIsBefore(newChild, list->GetNodeAt(mid))) - last = mid; - else - first = mid + 1; - } + if (nsContentUtils::PositionIsBefore(newChild, list->GetNodeAt(mid))) + last = mid; + else + first = mid + 1; + } list->InsertElementAt(newChild, first); } } return NS_OK; } diff --git a/content/html/content/test/Makefile.in b/content/html/content/test/Makefile.in --- a/content/html/content/test/Makefile.in +++ b/content/html/content/test/Makefile.in @@ -132,12 +132,13 @@ _TEST_FILES = test_bug589.html \ 347174transformable.xml \ 347174transform.xsl \ test_bug481335.xhtml \ test_bug514856.html \ bug514856_iframe.html \ test_bug519987.html \ test_bug523771.html \ form_submit_server.sjs \ + test_bug529819.html \ $(NULL) libs:: $(_TEST_FILES) $(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir) diff --git a/content/html/content/test/test_bug529819.html b/content/html/content/test/test_bug529819.html new file mode 100644 --- /dev/null +++ b/content/html/content/test/test_bug529819.html @@ -0,0 +1,33 @@ + + + + + Test for Bug 529819 + + + + + +Mozilla Bug 529819 +

+ +
+
+
+ +