Bug 986857 - Replace the Attr element getters by something that returns an Element; r=bz
authorAryeh Gregor <ayg@aryeh.name>
Thu, 10 Apr 2014 19:09:50 +0300
changeset 179936 58ec5153323748263df58027db13bb4b2c83aba8
parent 179935 0db7896686a02d030077812bf7bc4230c551561c
child 179937 76d97ee6133f83ce35b86dd38f2260eb42cb102a
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersbz
bugs986857
milestone31.0a1
Bug 986857 - Replace the Attr element getters by something that returns an Element; r=bz
content/base/public/nsIAttribute.h
content/base/src/Attr.cpp
content/base/src/Attr.h
content/base/src/nsINode.cpp
dom/xslt/xpath/nsXPathResult.cpp
dom/xslt/xpath/txMozillaXPathTreeWalker.cpp
--- a/content/base/public/nsIAttribute.h
+++ b/content/base/public/nsIAttribute.h
@@ -7,18 +7,18 @@
 #define nsIAttribute_h___
 
 #include "nsINode.h"
 
 class nsDOMAttributeMap;
 class nsIContent;
 
 #define NS_IATTRIBUTE_IID  \
-{ 0x8d9d7dbf, 0xc42d, 0x4715, \
-  { 0x95, 0xcf, 0x7a, 0x5e, 0xd5, 0xa4, 0x47, 0x70 } }
+{ 0x233a9c4d, 0xb27f, 0x4662, \
+    { 0xbd, 0x90, 0xba, 0xd6, 0x2e, 0x76, 0xc8, 0xe1 } }
 
 class nsIAttribute : public nsINode
 {
 public:
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_IATTRIBUTE_IID)
 
   virtual void SetMap(nsDOMAttributeMap *aMap) = 0;
   
@@ -27,18 +27,16 @@ public:
     return mAttrMap;
   }
 
   nsINodeInfo *NodeInfo() const
   {
     return mNodeInfo;
   }
 
-  virtual nsIContent* GetContent() const = 0;
-
   /**
    * Called when our ownerElement is moved into a new document.
    * Updates the nodeinfo of this node.
    */
   virtual nsresult SetOwnerDocument(nsIDocument* aDocument) = 0;
 
 protected:
 #ifdef MOZILLA_INTERNAL_API
--- a/content/base/src/Attr.cpp
+++ b/content/base/src/Attr.cpp
@@ -73,17 +73,17 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Attr)
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Attr)
   nsINode::Unlink(tmp);
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mAttrMap)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(Attr)
-  Element* ownerElement = tmp->GetContentInternal();
+  Element* ownerElement = tmp->GetElement();
   if (tmp->IsBlack()) {
     if (ownerElement) {
       // The attribute owns the element via attribute map so we can
       // mark it when the attribute is certainly alive.
       mozilla::dom::FragmentOrElement::MarkNodeChildren(ownerElement);
     }
     return true;
   }
@@ -124,20 +124,24 @@ Attr::SetMap(nsDOMAttributeMap *aMap)
     // We're breaking a relationship with content and not getting a new one,
     // need to locally cache value. GetValue() does that.
     GetValue(mValue);
   }
 
   mAttrMap = aMap;
 }
 
-nsIContent*
-Attr::GetContent() const
+Element*
+Attr::GetElement() const
 {
-  return GetContentInternal();
+  if (!mAttrMap) {
+    return nullptr;
+  }
+  nsIContent* content = mAttrMap->GetContent();
+  return content ? content->AsElement() : nullptr;
 }
 
 nsresult
 Attr::SetOwnerDocument(nsIDocument* aDocument)
 {
   NS_ASSERTION(aDocument, "Missing document");
 
   nsIDocument *doc = OwnerDoc();
@@ -177,39 +181,39 @@ Attr::GetNameAtom(nsIContent* aContent)
   }
   nsCOMPtr<nsIAtom> nameAtom = mNodeInfo->NameAtom();
   return nameAtom.forget();
 }
 
 NS_IMETHODIMP
 Attr::GetValue(nsAString& aValue)
 {
-  nsIContent* content = GetContentInternal();
-  if (content) {
-    nsCOMPtr<nsIAtom> nameAtom = GetNameAtom(content);
-    content->GetAttr(mNodeInfo->NamespaceID(), nameAtom, aValue);
+  Element* element = GetElement();
+  if (element) {
+    nsCOMPtr<nsIAtom> nameAtom = GetNameAtom(element);
+    element->GetAttr(mNodeInfo->NamespaceID(), nameAtom, aValue);
   }
   else {
     aValue = mValue;
   }
 
   return NS_OK;
 }
 
 void
 Attr::SetValue(const nsAString& aValue, ErrorResult& aRv)
 {
-  nsIContent* content = GetContentInternal();
-  if (!content) {
+  Element* element = GetElement();
+  if (!element) {
     mValue = aValue;
     return;
   }
 
-  nsCOMPtr<nsIAtom> nameAtom = GetNameAtom(content);
-  aRv = content->SetAttr(mNodeInfo->NamespaceID(),
+  nsCOMPtr<nsIAtom> nameAtom = GetNameAtom(element);
+  aRv = element->SetAttr(mNodeInfo->NamespaceID(),
                          nameAtom,
                          mNodeInfo->GetPrefixAtom(),
                          aValue,
                          true);
 }
 
 NS_IMETHODIMP
 Attr::SetValue(const nsAString& aValue)
@@ -233,28 +237,28 @@ Attr::GetSpecified(bool* aSpecified)
   *aSpecified = Specified();
   return NS_OK;
 }
 
 Element*
 Attr::GetOwnerElement(ErrorResult& aRv)
 {
   OwnerDoc()->WarnOnceAbout(nsIDocument::eOwnerElement);
-  return GetContentInternal();
+  return GetElement();
 }
 
 NS_IMETHODIMP
 Attr::GetOwnerElement(nsIDOMElement** aOwnerElement)
 {
   NS_ENSURE_ARG_POINTER(aOwnerElement);
   OwnerDoc()->WarnOnceAbout(nsIDocument::eOwnerElement);
 
-  nsIContent* content = GetContentInternal();
-  if (content) {
-    return CallQueryInterface(content, aOwnerElement);
+  Element* element = GetElement();
+  if (element) {
+    return CallQueryInterface(element, aOwnerElement);
   }
 
   *aOwnerElement = nullptr;
 
   return NS_OK;
 }
 
 void
@@ -288,17 +292,17 @@ Attr::Clone(nsINodeInfo *aNodeInfo, nsIN
   NS_ADDREF(*aResult);
 
   return NS_OK;
 }
 
 already_AddRefed<nsIURI>
 Attr::GetBaseURI(bool aTryUseXHRDocBaseURI) const
 {
-  nsINode *parent = GetContentInternal();
+  Element* parent = GetElement();
 
   return parent ? parent->GetBaseURI(aTryUseXHRDocBaseURI) : nullptr;
 }
 
 void
 Attr::GetTextContentInternal(nsAString& aTextContent)
 {
   OwnerDoc()->WarnOnceAbout(nsIDocument::eTextContent);
@@ -313,26 +317,24 @@ Attr::SetTextContentInternal(const nsASt
   OwnerDoc()->WarnOnceAbout(nsIDocument::eTextContent);
 
   SetNodeValueInternal(aTextContent, aError);
 }
 
 NS_IMETHODIMP
 Attr::GetIsId(bool* aReturn)
 {
-  nsIContent* content = GetContentInternal();
-  if (!content)
-  {
+  Element* element = GetElement();
+  if (!element) {
     *aReturn = false;
     return NS_OK;
   }
 
-  nsIAtom* idAtom = content->GetIDAttributeName();
-  if (!idAtom)
-  {
+  nsIAtom* idAtom = element->GetIDAttributeName();
+  if (!idAtom) {
     *aReturn = false;
     return NS_OK;
   }
 
   *aReturn = mNodeInfo->Equals(idAtom, kNameSpaceID_None);
   return NS_OK;
 }
 
@@ -399,16 +401,10 @@ Attr::Shutdown()
 }
 
 JSObject*
 Attr::WrapObject(JSContext* aCx)
 {
   return AttrBinding::Wrap(aCx, this);
 }
 
-Element*
-Attr::GetContentInternal() const
-{
-  return mAttrMap ? mAttrMap->GetContent() : nullptr;
-}
-
 } // namespace dom
 } // namespace mozilla
--- a/content/base/src/Attr.h
+++ b/content/base/src/Attr.h
@@ -51,17 +51,17 @@ public:
 
   // nsIDOMAttr interface
   NS_DECL_NSIDOMATTR
 
   virtual nsresult PreHandleEvent(EventChainPreVisitor& aVisitor) MOZ_OVERRIDE;
 
   // nsIAttribute interface
   void SetMap(nsDOMAttributeMap *aMap) MOZ_OVERRIDE;
-  nsIContent *GetContent() const MOZ_OVERRIDE;
+  Element* GetElement() const;
   nsresult SetOwnerDocument(nsIDocument* aDocument) MOZ_OVERRIDE;
 
   // nsINode interface
   virtual bool IsNodeOfType(uint32_t aFlags) const MOZ_OVERRIDE;
   virtual uint32_t GetChildCount() const MOZ_OVERRIDE;
   virtual nsIContent *GetChildAt(uint32_t aIndex) const MOZ_OVERRIDE;
   virtual nsIContent * const * GetChildArray(uint32_t* aChildCount) const MOZ_OVERRIDE;
   virtual int32_t IndexOf(const nsINode* aPossibleChild) const MOZ_OVERRIDE;
@@ -93,24 +93,23 @@ public:
   // XPCOM GetPrefix() is OK
   // XPCOM GetLocalName() is OK
 
   Element* GetOwnerElement(ErrorResult& aRv);
 
 protected:
   virtual Element* GetNameSpaceElement()
   {
-    return GetContentInternal();
+    return GetElement();
   }
 
   static bool sInitialized;
 
 private:
   already_AddRefed<nsIAtom> GetNameAtom(nsIContent* aContent);
-  Element* GetContentInternal() const;
 
   nsString mValue;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif /* mozilla_dom_Attr_h */
--- a/content/base/src/nsINode.cpp
+++ b/content/base/src/nsINode.cpp
@@ -804,30 +804,30 @@ nsINode::CompareDocumentPosition(nsINode
     return static_cast<uint16_t>(nsIDOMNode::DOCUMENT_POSITION_FOLLOWING);
   }
 
   nsAutoTArray<const nsINode*, 32> parents1, parents2;
 
   const nsINode *node1 = &aOtherNode, *node2 = this;
 
   // Check if either node is an attribute
-  const nsIAttribute* attr1 = nullptr;
+  const Attr* attr1 = nullptr;
   if (node1->IsNodeOfType(nsINode::eATTRIBUTE)) {
-    attr1 = static_cast<const nsIAttribute*>(node1);
-    const nsIContent* elem = attr1->GetContent();
+    attr1 = static_cast<const Attr*>(node1);
+    const Element* elem = attr1->GetElement();
     // If there is an owner element add the attribute
     // to the chain and walk up to the element
     if (elem) {
       node1 = elem;
       parents1.AppendElement(attr1);
     }
   }
   if (node2->IsNodeOfType(nsINode::eATTRIBUTE)) {
-    const nsIAttribute* attr2 = static_cast<const nsIAttribute*>(node2);
-    const nsIContent* elem = attr2->GetContent();
+    const Attr* attr2 = static_cast<const Attr*>(node2);
+    const Element* elem = attr2->GetElement();
     if (elem == node1 && attr1) {
       // Both nodes are attributes on the same element.
       // Compare position between the attributes.
 
       uint32_t i;
       const nsAttrName* attrName;
       for (i = 0; (attrName = elem->GetAttrNameAt(i)); ++i) {
         if (attrName->Equals(attr1->NodeInfo())) {
--- a/dom/xslt/xpath/nsXPathResult.cpp
+++ b/dom/xslt/xpath/nsXPathResult.cpp
@@ -2,18 +2,18 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsXPathResult.h"
 #include "txExprResult.h"
 #include "txNodeSet.h"
 #include "nsError.h"
+#include "mozilla/dom/Attr.h"
 #include "mozilla/dom/Element.h"
-#include "nsIAttribute.h"
 #include "nsDOMClassInfoID.h"
 #include "nsIDOMNode.h"
 #include "nsIDOMDocument.h"
 #include "nsDOMString.h"
 #include "txXPathTreeWalker.h"
 #include "nsCycleCollectionParticipant.h"
 
 using namespace mozilla::dom;
@@ -356,18 +356,18 @@ nsXPathResult::Invalidate(const nsIConte
         // the changes are happening in a different anonymous trees, no
         // invalidation should happen.
         nsIContent* ctxBindingParent = nullptr;
         if (contextNode->IsNodeOfType(nsINode::eCONTENT)) {
             ctxBindingParent =
                 static_cast<nsIContent*>(contextNode.get())
                     ->GetBindingParent();
         } else if (contextNode->IsNodeOfType(nsINode::eATTRIBUTE)) {
-            nsIContent* parent =
-              static_cast<nsIAttribute*>(contextNode.get())->GetContent();
+            Element* parent =
+              static_cast<Attr*>(contextNode.get())->GetElement();
             if (parent) {
                 ctxBindingParent = parent->GetBindingParent();
             }
         }
         if (ctxBindingParent != aChangeRoot->GetBindingParent()) {
           return;
         }
     }
--- a/dom/xslt/xpath/txMozillaXPathTreeWalker.cpp
+++ b/dom/xslt/xpath/txMozillaXPathTreeWalker.cpp
@@ -16,20 +16,23 @@
 #include "nsReadableUtils.h"
 #include "nsString.h"
 #include "nsTextFragment.h"
 #include "txXMLUtils.h"
 #include "txLog.h"
 #include "nsUnicharUtils.h"
 #include "nsAttrName.h"
 #include "nsTArray.h"
+#include "mozilla/dom/Attr.h"
 #include "mozilla/dom/Element.h"
 #include <stdint.h>
 #include <algorithm>
 
+using mozilla::dom::Attr;
+
 const uint32_t kUnknownIndex = uint32_t(-1);
 
 txXPathTreeWalker::txXPathTreeWalker(const txXPathTreeWalker& aOther)
     : mPosition(aOther.mPosition),
       mCurrentIndex(aOther.mCurrentIndex)
 {
 }
 
@@ -686,17 +689,18 @@ txXPathNativeNode::createXPathNode(nsIDO
     uint16_t nodeType;
     aNode->GetNodeType(&nodeType);
 
     if (nodeType == nsIDOMNode::ATTRIBUTE_NODE) {
         nsCOMPtr<nsIAttribute> attr = do_QueryInterface(aNode);
         NS_ASSERTION(attr, "doesn't implement nsIAttribute");
 
         nsINodeInfo *nodeInfo = attr->NodeInfo();
-        nsIContent *parent = attr->GetContent();
+        mozilla::dom::Element* parent =
+          static_cast<Attr*>(attr.get())->GetElement();
         if (!parent) {
             return nullptr;
         }
 
         nsINode* root = aKeepRootAlive ? txXPathNode::RootOf(parent) : nullptr;
 
         uint32_t i, total = parent->GetAttrCount();
         for (i = 0; i < total; ++i) {