Bug 958596 - Add a Describe() method to nsIContent. r=bz
☠☠ backed out by c9f4f70e46e1 ☠ ☠
authorBotond Ballo <botond@mozilla.com>
Wed, 22 Jan 2014 16:15:05 -0500
changeset 170929 5329fa8c8847cf7f8da286109429899f1081664b
parent 170928 0d1d26d588e3b9641aa344657e55b8b527307b7c
child 170930 2834110533403b231e5bec7b9b0cfcf8ca68c7a0
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersbz
bugs958596
milestone30.0a1
Bug 958596 - Add a Describe() method to nsIContent. r=bz
content/base/public/Element.h
content/base/public/nsIContent.h
content/base/src/Element.cpp
--- a/content/base/public/Element.h
+++ b/content/base/public/Element.h
@@ -487,16 +487,18 @@ public:
   {
     List(out, aIndent, EmptyCString());
   }
   virtual void DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const MOZ_OVERRIDE;
   void List(FILE* out, int32_t aIndent, const nsCString& aPrefix) const;
   void ListAttributes(FILE* out) const;
 #endif
 
+  void Describe(nsAString& aOutDescription) const MOZ_OVERRIDE;
+
   /*
    * Attribute Mapping Helpers
    */
   struct MappedAttributeEntry {
     nsIAtom** attribute;
   };
 
   /**
@@ -509,16 +511,18 @@ public:
   static bool
   FindAttributeDependence(const nsIAtom* aAttribute,
                           const MappedAttributeEntry* const (&aMaps)[N])
   {
     return FindAttributeDependence(aAttribute, aMaps, N);
   }
 
 private:
+  void DescribeAttribute(uint32_t index, nsAString& aOutDescription) const;
+
   static bool
   FindAttributeDependence(const nsIAtom* aAttribute,
                           const MappedAttributeEntry* const aMaps[],
                           uint32_t aMapCount);
 
 protected:
   inline bool GetAttr(int32_t aNameSpaceID, nsIAtom* aName,
                       DOMString& aResult) const
--- a/content/base/public/nsIContent.h
+++ b/content/base/public/nsIContent.h
@@ -945,16 +945,25 @@ public:
   /**
    * Dump the content (and anything it contains) out to the given
    * file stream. Use aIndent as the base indent during formatting.
    */
   virtual void DumpContent(FILE* out = stdout, int32_t aIndent = 0,
                            bool aDumpAll = true) const = 0;
 #endif
 
+  /**
+   * Append to aOutDescription a short (preferably one line) string
+   * describing the content.
+   * Currently implemented for elements only.
+   */
+  virtual void Describe(nsAString& aOutDescription) const {
+    aOutDescription = NS_LITERAL_STRING("(not an element)");
+  }
+
   enum ETabFocusType {
   //eTabFocus_textControlsMask = (1<<0),  // unused - textboxes always tabbable
     eTabFocus_formElementsMask = (1<<1),  // non-text form elements
     eTabFocus_linksMask = (1<<2),         // links
     eTabFocus_any = 1 + (1<<1) + (1<<2)   // everything that can be focused
   };
 
   // Tab focus model bit field:
--- a/content/base/src/Element.cpp
+++ b/content/base/src/Element.cpp
@@ -2127,40 +2127,45 @@ Element::GetAttrNameAt(uint32_t aIndex) 
 }
 
 uint32_t
 Element::GetAttrCount() const
 {
   return mAttrsAndChildren.AttrCount();
 }
 
+void
+Element::DescribeAttribute(uint32_t index, nsAString& aOutDescription) const
+{
+  // name
+  mAttrsAndChildren.AttrNameAt(index)->GetQualifiedName(aOutDescription);
+
+  // value
+  aOutDescription.AppendLiteral("=\"");
+  nsAutoString value;
+  mAttrsAndChildren.AttrAt(index)->ToString(value);
+  for (int i = value.Length(); i >= 0; --i) {
+    if (value[i] == char16_t('"'))
+      value.Insert(char16_t('\\'), uint32_t(i));
+  }
+  aOutDescription.Append(value);
+  aOutDescription.AppendLiteral("\"");
+}
+
 #ifdef DEBUG
 void
 Element::ListAttributes(FILE* out) const
 {
   uint32_t index, count = mAttrsAndChildren.AttrCount();
   for (index = 0; index < count; index++) {
-    nsAutoString buffer;
-
-    // name
-    mAttrsAndChildren.AttrNameAt(index)->GetQualifiedName(buffer);
-
-    // value
-    buffer.AppendLiteral("=\"");
-    nsAutoString value;
-    mAttrsAndChildren.AttrAt(index)->ToString(value);
-    for (int i = value.Length(); i >= 0; --i) {
-      if (value[i] == char16_t('"'))
-        value.Insert(char16_t('\\'), uint32_t(i));
-    }
-    buffer.Append(value);
-    buffer.AppendLiteral("\"");
+    nsAutoString attributeDescription;
+    DescribeAttribute(index, attributeDescription);
 
     fputs(" ", out);
-    fputs(NS_LossyConvertUTF16toASCII(buffer).get(), out);
+    fputs(NS_LossyConvertUTF16toASCII(attributeDescription).get(), out);
   }
 }
 
 void
 Element::List(FILE* out, int32_t aIndent,
               const nsCString& aPrefix) const
 {
   int32_t indent;
@@ -2274,16 +2279,31 @@ Element::DumpContent(FILE* out, int32_t 
   fputs("</", out);
   fputs(NS_LossyConvertUTF16toASCII(buf).get(), out);
   fputs(">", out);
 
   if(aIndent) fputs("\n", out);
 }
 #endif
 
+void
+Element::Describe(nsAString& aOutDescription) const
+{
+  aOutDescription.Append(mNodeInfo->QualifiedName());
+  aOutDescription.AppendPrintf("@%p", (void *)this);
+
+  uint32_t index, count = mAttrsAndChildren.AttrCount();
+  for (index = 0; index < count; index++) {
+    aOutDescription.Append(' ');
+    nsAutoString attributeDescription;
+    DescribeAttribute(index, attributeDescription);
+    aOutDescription.Append(attributeDescription);
+  }
+}
+
 bool
 Element::CheckHandleEventForLinksPrecondition(nsEventChainVisitor& aVisitor,
                                               nsIURI** aURI) const
 {
   if (aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault ||
       (!aVisitor.mEvent->mFlags.mIsTrusted &&
        (aVisitor.mEvent->message != NS_MOUSE_CLICK) &&
        (aVisitor.mEvent->message != NS_KEY_PRESS) &&