Bug 1454236: Introduce nsINode::{IsDocumentFragment, AsDocumentFragment}. r?bz draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sun, 15 Apr 2018 12:08:36 +0200
changeset 782369 448f6e15b813523885fd83a899c85796f9c58374
parent 782322 af10ac5fe4d092995d51b188ed4879ff9a104840
child 782370 2fc96d9ffd2cd92ff4b83836cd2f36115cb59e9d
push id106517
push userbmo:emilio@crisal.io
push dateSun, 15 Apr 2018 10:17:38 +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
@@ -108,10 +108,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