Bug 893909 - Part e: Cleanup HTMLSelectElement::SetLength; r=dzbarsky
authorMs2ger <ms2ger@gmail.com>
Wed, 24 Jul 2013 09:36:59 +0200
changeset 151943 3ec0906fcf685eb6121323113901a946bcd713b9
parent 151942 8a1ecc6236c1985be069259fbf13650589312d64
child 151944 39fc7ef972b870e356ed4e9d6a371463226f9224
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdzbarsky
bugs893909
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 893909 - Part e: Cleanup HTMLSelectElement::SetLength; r=dzbarsky
content/html/content/src/HTMLSelectElement.cpp
content/html/content/src/HTMLSelectElement.h
--- a/content/html/content/src/HTMLSelectElement.cpp
+++ b/content/html/content/src/HTMLSelectElement.cpp
@@ -712,67 +712,65 @@ HTMLSelectElement::GetLength(uint32_t* a
   return mOptions->GetLength(aLength);
 }
 
 #define MAX_DYNAMIC_SELECT_LENGTH 10000
 
 NS_IMETHODIMP
 HTMLSelectElement::SetLength(uint32_t aLength)
 {
-  uint32_t curlen;
-  nsresult rv = GetLength(&curlen);
-  if (NS_FAILED(rv)) {
-    curlen = 0;
-  }
+  ErrorResult rv;
+  SetLength(aLength, rv);
+  return rv.ErrorCode();
+}
+
+void
+HTMLSelectElement::SetLength(uint32_t aLength, ErrorResult& aRv)
+{
+  uint32_t curlen = Length();
 
   if (curlen > aLength) { // Remove extra options
-    for (uint32_t i = curlen; i > aLength && NS_SUCCEEDED(rv); --i) {
-      rv = Remove(i - 1);
+    for (uint32_t i = curlen; i > aLength; --i) {
+      MOZ_ALWAYS_TRUE(NS_SUCCEEDED(Remove(i - 1)));
     }
   } else if (aLength > curlen) {
     if (aLength > MAX_DYNAMIC_SELECT_LENGTH) {
-      return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
+      aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
+      return;
     }
-    
-    // This violates the W3C DOM but we do this for backwards compatibility
+
     nsCOMPtr<nsINodeInfo> nodeInfo;
 
     nsContentUtils::NameChanged(mNodeInfo, nsGkAtoms::option,
                                 getter_AddRefs(nodeInfo));
 
-    nsCOMPtr<nsIContent> element = NS_NewHTMLOptionElement(nodeInfo.forget());
-    if (!element) {
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
+    nsCOMPtr<nsINode> node = NS_NewHTMLOptionElement(nodeInfo.forget());
 
     nsRefPtr<nsTextNode> text = new nsTextNode(mNodeInfo->NodeInfoManager());
 
-    rv = element->AppendChildTo(text, false);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    nsCOMPtr<nsIDOMNode> node(do_QueryInterface(element));
+    aRv = node->AppendChildTo(text, false);
+    if (aRv.Failed()) {
+      return;
+    }
 
     for (uint32_t i = curlen; i < aLength; i++) {
-      nsCOMPtr<nsIDOMNode> tmpNode;
-
-      rv = AppendChild(node, getter_AddRefs(tmpNode));
-      NS_ENSURE_SUCCESS(rv, rv);
+      nsINode::AppendChild(*node, aRv);
+      if (aRv.Failed()) {
+        return;
+      }
 
       if (i + 1 < aLength) {
-        nsCOMPtr<nsIDOMNode> newNode;
-
-        rv = node->CloneNode(true, 1, getter_AddRefs(newNode));
-        NS_ENSURE_SUCCESS(rv, rv);
-
-        node = newNode;
+        node = node->CloneNode(true, aRv);
+        if (aRv.Failed()) {
+          return;
+        }
+        MOZ_ASSERT(node);
       }
     }
   }
-
-  return NS_OK;
 }
 
 //NS_IMPL_INT_ATTR(HTMLSelectElement, SelectedIndex, selectedindex)
 
 NS_IMETHODIMP
 HTMLSelectElement::GetSelectedIndex(int32_t* aValue)
 {
   *aValue = SelectedIndex();
@@ -1116,17 +1114,17 @@ HTMLSelectElement::IsOptionDisabled(HTML
   MOZ_ASSERT(aOption);
   if (aOption->Disabled()) {
     return true;
   }
 
   // Check for disabled optgroups
   // If there are no artifacts, there are no optgroups
   if (mNonOptionChildren) {
-    for (nsCOMPtr<Element> node = aOption->GetParentElement();
+    for (nsCOMPtr<Element> node = static_cast<nsINode*>(aOption)->GetParentElement();
          node;
          node = node->GetParentElement()) {
       // If we reached the select element, we're done
       if (node->IsHTML(nsGkAtoms::select)) {
         return false;
       }
 
       nsRefPtr<HTMLOptGroupElement> optGroupElement =
--- a/content/html/content/src/HTMLSelectElement.h
+++ b/content/html/content/src/HTMLSelectElement.h
@@ -189,20 +189,17 @@ public:
   HTMLOptionsCollection* Options() const
   {
     return mOptions;
   }
   uint32_t Length() const
   {
     return mOptions->Length();
   }
-  void SetLength(uint32_t aLength, ErrorResult& aRv)
-  {
-    aRv = SetLength(aLength);
-  }
+  void SetLength(uint32_t aLength, ErrorResult& aRv);
   Element* IndexedGetter(uint32_t aIdx, bool& aFound) const
   {
     return mOptions->IndexedGetter(aIdx, aFound);
   }
   HTMLOptionElement* Item(uint32_t aIdx) const
   {
     return mOptions->ItemAsOption(aIdx);
   }