Bug 1454236: Introduce nsINode::{IsDocumentFragment, AsDocumentFragment}. r=bz
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sun, 15 Apr 2018 12:08:36 +0200
changeset 785477 67ddc6418466b2033a12029533d3abd19123ce2f
parent 785476 faf615fcb4c11e8d4efe4c3eade10d308b0c74e1
child 785478 658bffdfcc1d1597eb9a8a7639838a901f417eaa
push id107236
push userbmo:emilio@crisal.io
push dateFri, 20 Apr 2018 08:31:50 +0000
reviewersbz
bugs1454236
milestone61.0a1
Bug 1454236: Introduce nsINode::{IsDocumentFragment, AsDocumentFragment}. r=bz I wonder if we should use some macros to reduce a bit the copy pasta in the As* functions... Also whether they should start returning references... In any case, those are followups. MozReview-Commit-ID: 7w4518fSHnj
dom/base/DocumentFragment.h
dom/base/nsINode.h
--- a/dom/base/DocumentFragment.h
+++ b/dom/base/DocumentFragment.h
@@ -105,10 +105,23 @@ protected:
   nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
                  bool aPreallocateChildren) const override;
   RefPtr<Element> mHost;
 };
 
 } // namespace dom
 } // namespace mozilla
 
+inline mozilla::dom::DocumentFragment*
+nsINode::AsDocumentFragment()
+{
+  MOZ_ASSERT(IsDocumentFragment());
+  return static_cast<mozilla::dom::DocumentFragment*>(this);
+}
+
+inline const mozilla::dom::DocumentFragment*
+nsINode::AsDocumentFragment() const
+{
+  MOZ_ASSERT(IsDocumentFragment());
+  return static_cast<const mozilla::dom::DocumentFragment*>(this);
+}
 
 #endif // mozilla_dom_DocumentFragment_h__
--- a/dom/base/nsINode.h
+++ b/dom/base/nsINode.h
@@ -72,16 +72,17 @@ inline bool IsSpaceCharacter(char16_t aC
 inline bool IsSpaceCharacter(char aChar) {
   return aChar == ' ' || aChar == '\t' || aChar == '\n' || aChar == '\r' ||
          aChar == '\f';
 }
 class AccessibleNode;
 struct BoxQuadOptions;
 struct ConvertCoordinateOptions;
 class DocGroup;
+class DocumentFragment;
 class DOMPoint;
 class DOMQuad;
 class DOMRectReadOnly;
 class Element;
 class EventHandlerNonNull;
 class L10nCallback;
 template<typename T> class Optional;
 class OwningNodeOrString;
@@ -464,16 +465,32 @@ public:
   /**
    * Return this node as a document. Asserts IsDocument().
    *
    * This is defined inline in nsIDocument.h.
    */
   inline nsIDocument* AsDocument();
   inline const nsIDocument* AsDocument() const;
 
+  /**
+   * Returns true if this is a document fragment node.
+   */
+  bool IsDocumentFragment() const
+  {
+    return NodeType() == DOCUMENT_FRAGMENT_NODE;
+  }
+
+  /**
+   * Return this node as a document fragment. Asserts IsDocumentFragment().
+   *
+   * This is defined inline in DocumentFragment.h.
+   */
+  inline mozilla::dom::DocumentFragment* AsDocumentFragment();
+  inline const mozilla::dom::DocumentFragment* AsDocumentFragment() const;
+
   virtual JSObject* WrapObject(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   /**
    * returns true if we are in priviliged code or
    * layout.css.getBoxQuads.enabled == true.
    */
   static bool HasBoxQuadsSupport(JSContext* aCx, JSObject* /* unused */);
 
@@ -786,17 +803,17 @@ public:
   inline bool IsAnyOfMathMLElements(First aFirst, Args... aArgs) const
   {
     return IsMathMLElement() && IsNodeInternal(aFirst, aArgs...);
   }
 
   bool IsShadowRoot() const
   {
     const bool isShadowRoot = IsInShadowTree() && !GetParentNode();
-    MOZ_ASSERT_IF(isShadowRoot, NodeType() == DOCUMENT_FRAGMENT_NODE);
+    MOZ_ASSERT_IF(isShadowRoot, IsDocumentFragment());
     return isShadowRoot;
   }
 
   /**
    * Insert a content node before another or at the end.
    * This method handles calling BindToTree on the child appropriately.
    *
    * @param aKid the content to insert