Bug 852135 - Part a: Reindent some code in nsDOMAttributeMap; r=bz
authorMs2ger <ms2ger@gmail.com>
Fri, 26 Apr 2013 08:48:15 +0200
changeset 140854 0fa3623e2a718894e5d7bebe75a242441e05895d
parent 140853 74182501aee7636033143c48eaae15ae66dae021
child 140855 61e10c9a711ffd4925ae95579162bca4b301acac
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs852135
milestone23.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 852135 - Part a: Reindent some code in nsDOMAttributeMap; r=bz
content/base/src/nsDOMAttributeMap.cpp
--- a/content/base/src/nsDOMAttributeMap.cpp
+++ b/content/base/src/nsDOMAttributeMap.cpp
@@ -169,25 +169,24 @@ nsDOMAttributeMap::GetAttribute(nsINodeI
   }
 
   return node;
 }
 
 Attr*
 nsDOMAttributeMap::GetNamedItem(const nsAString& aAttrName)
 {
-  if (mContent) {
-    nsCOMPtr<nsINodeInfo> ni =
-      mContent->GetExistingAttrNameFromQName(aAttrName);
-    if (ni) {
-      return GetAttribute(ni, false);
-    }
+  NS_ENSURE_TRUE(mContent, nullptr);
+
+  nsCOMPtr<nsINodeInfo> ni = mContent->GetExistingAttrNameFromQName(aAttrName);
+  if (!ni) {
+    return nullptr;
   }
 
-  return nullptr;
+  return GetAttribute(ni, false);
 }
 
 NS_IMETHODIMP
 nsDOMAttributeMap::GetNamedItem(const nsAString& aAttrName,
                                 nsIDOMAttr** aAttribute)
 {
   NS_ENSURE_ARG_POINTER(aAttribute);
 
@@ -212,164 +211,155 @@ nsDOMAttributeMap::SetNamedItemNS(nsIDOM
   return rv.ErrorCode();
 }
 
 already_AddRefed<Attr>
 nsDOMAttributeMap::SetNamedItemInternal(nsIDOMAttr* aAttr,
                                         bool aWithNS,
                                         ErrorResult& aError)
 {
-  if (mContent) {
-    // XXX should check same-origin between mContent and aAttr however
-    // nsContentUtils::CheckSameOrigin can't deal with attributenodes yet
-    
-    nsCOMPtr<nsIAttribute> iAttribute(do_QueryInterface(aAttr));
-    if (!iAttribute) {
-      aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
+  NS_ENSURE_TRUE(mContent, nullptr);
+
+  // XXX should check same-origin between mContent and aAttr however
+  // nsContentUtils::CheckSameOrigin can't deal with attributenodes yet
+  
+  nsCOMPtr<nsIAttribute> iAttribute(do_QueryInterface(aAttr));
+  if (!iAttribute) {
+    aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
+    return nullptr;
+  }
+
+  Attr *attribute = static_cast<Attr*>(iAttribute.get());
+
+  // Check that attribute is not owned by somebody else
+  nsDOMAttributeMap* owner = iAttribute->GetMap();
+  if (owner) {
+    if (owner != this) {
+      aError.Throw(NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR);
+      return nullptr;
+    }
+
+    // setting a preexisting attribute is a no-op, just return the same
+    // node.
+    NS_ADDREF(attribute);
+    return attribute;
+  }
+
+  nsresult rv;
+  if (!mContent->HasSameOwnerDoc(iAttribute)) {
+    nsCOMPtr<nsIDOMDocument> domDoc =
+      do_QueryInterface(mContent->OwnerDoc(), &rv);
+    if (NS_FAILED(rv)) {
+      aError.Throw(rv);
       return nullptr;
     }
 
-    Attr *attribute = static_cast<Attr*>(iAttribute.get());
+    nsCOMPtr<nsIDOMNode> adoptedNode;
+    rv = domDoc->AdoptNode(aAttr, getter_AddRefs(adoptedNode));
+    if (NS_FAILED(rv)) {
+      aError.Throw(rv);
+      return nullptr;
+    }
+
+    NS_ASSERTION(adoptedNode == aAttr, "Uh, adopt node changed nodes?");
+  }
+
+  // Get nodeinfo and preexisting attribute (if it exists)
+  nsAutoString name;
+  nsCOMPtr<nsINodeInfo> ni;
 
-    // Check that attribute is not owned by somebody else
-    nsDOMAttributeMap* owner = iAttribute->GetMap();
-    if (owner) {
-      if (owner != this) {
-        aError.Throw(NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR);
-        return nullptr;
+  nsRefPtr<Attr> attr;
+  // SetNamedItemNS()
+  if (aWithNS) {
+    // Return existing attribute, if present
+    ni = iAttribute->NodeInfo();
+
+    if (mContent->HasAttr(ni->NamespaceID(), ni->NameAtom())) {
+      attr = RemoveAttribute(ni);
+    }
+  }
+  else { // SetNamedItem()
+    attribute->GetName(name);
+
+    // get node-info of old attribute
+    ni = mContent->GetExistingAttrNameFromQName(name);
+    if (ni) {
+      attr = RemoveAttribute(ni);
+    }
+    else {
+      if (mContent->IsInHTMLDocument() &&
+          mContent->IsHTML()) {
+        nsContentUtils::ASCIIToLower(name);
       }
 
-      // setting a preexisting attribute is a no-op, just return the same
-      // node.
-      NS_ADDREF(attribute);
-      return attribute;
-    }
-
-    nsresult rv;
-    if (!mContent->HasSameOwnerDoc(iAttribute)) {
-      nsCOMPtr<nsIDOMDocument> domDoc =
-        do_QueryInterface(mContent->OwnerDoc(), &rv);
-      if (NS_FAILED(rv)) {
-        aError.Throw(rv);
-        return nullptr;
-      }
-
-      nsCOMPtr<nsIDOMNode> adoptedNode;
-      rv = domDoc->AdoptNode(aAttr, getter_AddRefs(adoptedNode));
+      rv = mContent->NodeInfo()->NodeInfoManager()->
+        GetNodeInfo(name, nullptr, kNameSpaceID_None,
+                    nsIDOMNode::ATTRIBUTE_NODE, getter_AddRefs(ni));
       if (NS_FAILED(rv)) {
         aError.Throw(rv);
         return nullptr;
       }
-
-      NS_ASSERTION(adoptedNode == aAttr, "Uh, adopt node changed nodes?");
-    }
-
-    // Get nodeinfo and preexisting attribute (if it exists)
-    nsAutoString name;
-    nsCOMPtr<nsINodeInfo> ni;
-
-    nsRefPtr<Attr> attr;
-    // SetNamedItemNS()
-    if (aWithNS) {
-      // Return existing attribute, if present
-      ni = iAttribute->NodeInfo();
-
-      if (mContent->HasAttr(ni->NamespaceID(), ni->NameAtom())) {
-        attr = RemoveAttribute(ni);
-      }
+      // value is already empty
     }
-    else { // SetNamedItem()
-      attribute->GetName(name);
-
-      // get node-info of old attribute
-      ni = mContent->GetExistingAttrNameFromQName(name);
-      if (ni) {
-        attr = RemoveAttribute(ni);
-      }
-      else {
-        if (mContent->IsInHTMLDocument() &&
-            mContent->IsHTML()) {
-          nsContentUtils::ASCIIToLower(name);
-        }
-
-        rv = mContent->NodeInfo()->NodeInfoManager()->
-          GetNodeInfo(name, nullptr, kNameSpaceID_None,
-                      nsIDOMNode::ATTRIBUTE_NODE, getter_AddRefs(ni));
-        if (NS_FAILED(rv)) {
-          aError.Throw(rv);
-          return nullptr;
-        }
-        // value is already empty
-      }
-    }
-
-    nsAutoString value;
-    attribute->GetValue(value);
-
-    // Add the new attribute to the attribute map before updating
-    // its value in the element. @see bug 364413.
-    nsAttrKey attrkey(ni->NamespaceID(), ni->NameAtom());
-    mAttributeCache.Put(attrkey, attribute);
-    iAttribute->SetMap(this);
-
-    rv = mContent->SetAttr(ni->NamespaceID(), ni->NameAtom(),
-                           ni->GetPrefixAtom(), value, true);
-    if (NS_FAILED(rv)) {
-      aError.Throw(rv);
-      DropAttribute(ni->NamespaceID(), ni->NameAtom());
-    }
-
-    return attr.forget();
   }
 
-  return nullptr;
+  nsAutoString value;
+  attribute->GetValue(value);
+
+  // Add the new attribute to the attribute map before updating
+  // its value in the element. @see bug 364413.
+  nsAttrKey attrkey(ni->NamespaceID(), ni->NameAtom());
+  mAttributeCache.Put(attrkey, attribute);
+  iAttribute->SetMap(this);
+
+  rv = mContent->SetAttr(ni->NamespaceID(), ni->NameAtom(),
+                         ni->GetPrefixAtom(), value, true);
+  if (NS_FAILED(rv)) {
+    aError.Throw(rv);
+    DropAttribute(ni->NamespaceID(), ni->NameAtom());
+  }
+
+  return attr.forget();
 }
 
 NS_IMETHODIMP
 nsDOMAttributeMap::RemoveNamedItem(const nsAString& aName,
                                    nsIDOMAttr** aReturn)
 {
   NS_ENSURE_ARG_POINTER(aReturn);
   *aReturn = nullptr;
 
-  nsresult rv = NS_OK;
+  NS_ENSURE_TRUE(mContent, NS_OK);
 
-  if (mContent) {
-    nsCOMPtr<nsINodeInfo> ni = mContent->GetExistingAttrNameFromQName(aName);
-    if (!ni) {
-      return NS_ERROR_DOM_NOT_FOUND_ERR;
-    }
-
-    NS_ADDREF(*aReturn = GetAttribute(ni, true));
-
-    // This removes the attribute node from the attribute map.
-    rv = mContent->UnsetAttr(ni->NamespaceID(), ni->NameAtom(), true);
+  nsCOMPtr<nsINodeInfo> ni = mContent->GetExistingAttrNameFromQName(aName);
+  if (!ni) {
+    return NS_ERROR_DOM_NOT_FOUND_ERR;
   }
 
-  return rv;
+  NS_ADDREF(*aReturn = GetAttribute(ni, true));
+
+  // This removes the attribute node from the attribute map.
+  return mContent->UnsetAttr(ni->NamespaceID(), ni->NameAtom(), true);
 }
 
 
 Attr*
 nsDOMAttributeMap::GetItemAt(uint32_t aIndex)
 {
-  Attr* node = nullptr;
+  NS_ENSURE_TRUE(mContent, nullptr);
+
+  const nsAttrName* name = mContent->GetAttrNameAt(aIndex);
+  NS_ENSURE_TRUE(name, nullptr);
 
-  const nsAttrName* name;
-  if (mContent && (name = mContent->GetAttrNameAt(aIndex))) {
-    // Don't use the nodeinfo even if one exists since it can
-    // have the wrong owner document.
-    nsCOMPtr<nsINodeInfo> ni;
-    ni = mContent->NodeInfo()->NodeInfoManager()->
-      GetNodeInfo(name->LocalName(), name->GetPrefix(), name->NamespaceID(),
-                  nsIDOMNode::ATTRIBUTE_NODE);
-    node = GetAttribute(ni, true);
-  }
-
-  return node;
+  // Don't use the nodeinfo even if one exists since it can
+  // have the wrong owner document.
+  nsCOMPtr<nsINodeInfo> ni = mContent->NodeInfo()->NodeInfoManager()->
+    GetNodeInfo(name->LocalName(), name->GetPrefix(), name->NamespaceID(),
+                nsIDOMNode::ATTRIBUTE_NODE);
+  return GetAttribute(ni, true);
 }
 
 NS_IMETHODIMP
 nsDOMAttributeMap::Item(uint32_t aIndex, nsIDOMAttr** aReturn)
 {
   NS_IF_ADDREF(*aReturn = GetItemAt(aIndex));
   return NS_OK;
 }