Bug 1427419 - Part 18: Move inIDOMUtils.getParentForNode to InspectorUtils. r=bz draft
authorCameron McCormack <cam@mcc.id.au>
Sat, 06 Jan 2018 15:08:15 +0800
changeset 716763 e05fc71f5ec4a1a2dbfdb87c9227d496ab7991ca
parent 716762 33edc022a0add1b722ec60650c5bd63604c08ed6
child 716764 32871dcf0c52d69278e2e1fd9e3ef285397e28d8
push id94496
push userbmo:cam@mcc.id.au
push dateSat, 06 Jan 2018 07:08:40 +0000
reviewersbz
bugs1427419
milestone59.0a1
Bug 1427419 - Part 18: Move inIDOMUtils.getParentForNode to InspectorUtils. r=bz MozReview-Commit-ID: 5GxNmyVHf8u
dom/webidl/InspectorUtils.webidl
dom/xbl/test/test_bug398492.xul
layout/inspector/InspectorUtils.h
layout/inspector/inDOMUtils.cpp
layout/inspector/inDOMView.cpp
layout/inspector/inDeepTreeWalker.cpp
layout/inspector/inIDOMUtils.idl
layout/inspector/inLayoutUtils.cpp
layout/inspector/inLayoutUtils.h
layout/inspector/tests/test_bug462787.html
layout/inspector/tests/test_bug462789.html
--- a/dom/webidl/InspectorUtils.webidl
+++ b/dom/webidl/InspectorUtils.webidl
@@ -48,16 +48,17 @@ namespace InspectorUtils {
   const unsigned long TYPE_TIME = 6;
   const unsigned long TYPE_GRADIENT = 7;
   const unsigned long TYPE_TIMING_FUNCTION = 8;
   const unsigned long TYPE_IMAGE_RECT = 9;
   const unsigned long TYPE_NUMBER = 10;
   [Throws] boolean cssPropertySupportsType(DOMString property, unsigned long type);
 
   boolean isIgnorableWhitespace(CharacterData dataNode);
+  Node? getParentForNode(Node node, boolean showingAnonymousContent);
 };
 
 dictionary PropertyNamesOptions {
   boolean includeAliases = false;
 };
 
 dictionary InspectorRGBATuple {
   /*
--- a/dom/xbl/test/test_bug398492.xul
+++ b/dom/xbl/test/test_bug398492.xul
@@ -27,20 +27,20 @@ https://bugzilla.mozilla.org/show_bug.cg
   <hbox id="testbox" style="-moz-binding: url(#test)">Text</hbox>
   
   <!-- test code goes here -->
   <script type="application/javascript"><![CDATA[
 
     /** Test for Bug 398492 **/
     SimpleTest.waitForExplicitFinish();
 
+    const InspectorUtils = SpecialPowers.InspectorUtils;
+
     function getXBLParent(node) {
-      var utils = Components.classes["@mozilla.org/inspector/dom-utils;1"]
-                            .getService(Components.interfaces.inIDOMUtils);
-      return utils.getParentForNode(node, true);
+      return SpecialPowers.unwrap(InspectorUtils.getParentForNode(node, true));
     }
 
     addLoadEvent(function() {
       var n = $("testbox");
       var kid = n.firstChild;
       var anonKid = document.getAnonymousNodes(n)[0];
       is(anonKid instanceof XULElement, true, "Must be a XUL element");
       is(anonKid, getXBLParent(kid), "Unexpected anonymous nodes");
--- a/layout/inspector/InspectorUtils.h
+++ b/layout/inspector/InspectorUtils.h
@@ -162,16 +162,28 @@ public:
 
   static bool IsIgnorableWhitespace(GlobalObject& aGlobalObject,
                                     nsGenericDOMDataNode& aDataNode)
   {
     return IsIgnorableWhitespace(aDataNode);
   }
   static bool IsIgnorableWhitespace(nsGenericDOMDataNode& aDataNode);
 
+  // Returns the "parent" of a node.  The parent of a document node is the
+  // frame/iframe containing that document.  aShowingAnonymousContent says
+  // whether we are showing anonymous content.
+  static nsINode* GetParentForNode(nsINode& aNode,
+                                   bool aShowingAnonymousContent);
+  static nsINode* GetParentForNode(GlobalObject& aGlobalObject,
+                                   nsINode& aNode,
+                                   bool aShowingAnonymousContent)
+  {
+    return GetParentForNode(aNode, aShowingAnonymousContent);
+  }
+
 private:
   static already_AddRefed<nsStyleContext>
     GetCleanStyleContextForElement(Element* aElement, nsAtom* aPseudo);
 };
 
 } // namespace dom
 } // namespace mozilla
 
--- a/layout/inspector/inDOMUtils.cpp
+++ b/layout/inspector/inDOMUtils.cpp
@@ -131,49 +131,43 @@ InspectorUtils::IsIgnorableWhitespace(ns
   if (nsIFrame* frame = aDataNode.GetPrimaryFrame()) {
     return !frame->StyleText()->WhiteSpaceIsSignificant();
   }
 
   // empty inter-tag text node without frame, e.g., in between <table>\n<tr>
   return true;
 }
 
-} // namespace dom
-} // namespace mozilla
-
-NS_IMETHODIMP
-inDOMUtils::GetParentForNode(nsIDOMNode* aNode,
-                             bool aShowingAnonymousContent,
-                             nsIDOMNode** aParent)
+/* static */ nsINode*
+InspectorUtils::GetParentForNode(nsINode& aNode,
+                                 bool aShowingAnonymousContent)
 {
-  NS_ENSURE_ARG_POINTER(aNode);
-
   // First do the special cases -- document nodes and anonymous content
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(aNode));
-  nsCOMPtr<nsIDOMNode> parent;
+  nsINode* parent = nullptr;
 
-  if (doc) {
-    parent = inLayoutUtils::GetContainerFor(*doc);
+  if (aNode.IsNodeOfType(nsINode::eDOCUMENT)) {
+    auto& doc = static_cast<nsIDocument&>(aNode);
+    parent = inLayoutUtils::GetContainerFor(doc);
   } else if (aShowingAnonymousContent) {
-    nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
-    if (content) {
-      nsIContent* bparent = content->GetFlattenedTreeParent();
-      parent = do_QueryInterface(bparent);
+    if (aNode.IsContent()) {
+      parent = aNode.AsContent()->GetFlattenedTreeParent();
     }
   }
 
   if (!parent) {
     // Ok, just get the normal DOM parent node
-    aNode->GetParentNode(getter_AddRefs(parent));
+    return aNode.GetParentNode();
   }
 
-  NS_IF_ADDREF(*aParent = parent);
-  return NS_OK;
+  return parent;
 }
 
+} // namespace dom
+} // namespace mozilla
+
 NS_IMETHODIMP
 inDOMUtils::GetChildrenForNode(nsIDOMNode* aNode,
                                bool aShowingAnonymousContent,
                                nsIDOMNodeList** aChildren)
 {
   NS_ENSURE_ARG_POINTER(aNode);
   NS_PRECONDITION(aChildren, "Must have an out parameter");
 
--- a/layout/inspector/inDOMView.cpp
+++ b/layout/inspector/inDOMView.cpp
@@ -1,16 +1,15 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "inDOMView.h"
-#include "inIDOMUtils.h"
 
 #include "inLayoutUtils.h"
 
 #include "nsString.h"
 #include "nsReadableUtils.h"
 #include "nsIDOMNode.h"
 #include "nsIDOMNodeFilter.h"
 #include "nsIDOMNodeList.h"
@@ -21,16 +20,17 @@
 #include "nsBindingManager.h"
 #include "nsNameSpaceManager.h"
 #include "nsIDocument.h"
 #include "nsIServiceManager.h"
 #include "nsITreeColumns.h"
 #include "nsITreeBoxObject.h"
 #include "mozilla/dom/Element.h"
 #include "mozilla/Services.h"
+#include "mozilla/dom/InspectorUtils.h"
 
 #ifdef ACCESSIBILITY
 #include "nsAccessibilityService.h"
 #endif
 
 using namespace mozilla;
 
 ////////////////////////////////////////////////////////////////////////
@@ -771,34 +771,42 @@ inDOMView::ContentAppended(nsIDocument *
   }
 
   for (nsIContent* cur = aFirstNewContent; cur; cur = cur->GetNextSibling()) {
     // Our ContentInserted impl doesn't use the index
     ContentInserted(aDocument, aContainer, cur);
   }
 }
 
+static already_AddRefed<nsIDOMNode>
+GetParentForNode(nsINode* aChild, bool aShowAnonymous)
+{
+  MOZ_ASSERT(aChild);
+  nsINode* parent = InspectorUtils::GetParentForNode(*aChild, aShowAnonymous);
+
+  nsCOMPtr<nsIDOMNode> parentDOMNode = do_QueryInterface(parent);
+  return parentDOMNode.forget();
+}
+
 void
 inDOMView::ContentInserted(nsIDocument *aDocument, nsIContent* aContainer,
                            nsIContent* aChild)
 {
   if (!mTree)
     return;
 
   nsresult rv;
   nsCOMPtr<nsIDOMNode> childDOMNode(do_QueryInterface(aChild));
-  nsCOMPtr<nsIDOMNode> parent;
   if (!mDOMUtils) {
     mDOMUtils = services::GetInDOMUtils();
     if (!mDOMUtils) {
       return;
     }
   }
-  mDOMUtils->GetParentForNode(childDOMNode, mShowAnonymous,
-                              getter_AddRefs(parent));
+  nsCOMPtr<nsIDOMNode> parent = GetParentForNode(aChild, mShowAnonymous);
 
   // find the inDOMViewNode for the parent of the inserted content
   int32_t parentRow = 0;
   if (NS_FAILED(rv = NodeToRow(parent, &parentRow)))
     return;
   inDOMViewNode* parentNode = nullptr;
   if (NS_FAILED(rv = RowToNode(parentRow, &parentNode)))
     return;
--- a/layout/inspector/inDeepTreeWalker.cpp
+++ b/layout/inspector/inDeepTreeWalker.cpp
@@ -11,16 +11,17 @@
 #include "nsIDOMDocument.h"
 #include "nsIDOMNodeFilter.h"
 #include "nsIDOMNodeList.h"
 #include "nsServiceManagerUtils.h"
 #include "inIDOMUtils.h"
 #include "nsIContent.h"
 #include "ChildIterator.h"
 #include "mozilla/dom/Element.h"
+#include "mozilla/dom/InspectorUtils.h"
 
 /*****************************************************************************
  * This implementation does not currently operaate according to the W3C spec.
  * In particular it does NOT handle DOM mutations during the walk.  It also
  * ignores whatToShow and the filter.
  *****************************************************************************/
 
 ////////////////////////////////////////////////////
@@ -130,38 +131,43 @@ inDeepTreeWalker::GetCurrentNode(nsIDOMN
   *aCurrentNode = mCurrentNode;
   NS_IF_ADDREF(*aCurrentNode);
   return NS_OK;
 }
 
 already_AddRefed<nsIDOMNode>
 inDeepTreeWalker::GetParent()
 {
+  MOZ_ASSERT(mCurrentNode);
+
   if (mCurrentNode == mRoot) {
     return nullptr;
   }
 
+  nsCOMPtr<nsINode> currentNode = do_QueryInterface(mCurrentNode);
+  nsCOMPtr<nsINode> root = do_QueryInterface(mRoot);
+
   nsCOMPtr<nsIDOMNode> parent;
-  MOZ_ASSERT(mDOMUtils, "mDOMUtils should have been initiated already in Init");
-  mDOMUtils->GetParentForNode(mCurrentNode, mShowAnonymousContent,
-                              getter_AddRefs(parent));
+  nsINode* parentNode =
+    InspectorUtils::GetParentForNode(*currentNode, mShowAnonymousContent);
 
   uint16_t nodeType = 0;
-  if (parent) {
-    parent->GetNodeType(&nodeType);
+  if (parentNode) {
+    nodeType = parentNode->NodeType();
   }
   // For compatibility reasons by default we skip the document nodes
   // from the walk.
   if (!mShowDocumentsAsNodes &&
       nodeType == nsIDOMNode::DOCUMENT_NODE &&
-      parent != mRoot) {
-    mDOMUtils->GetParentForNode(parent, mShowAnonymousContent,
-                                getter_AddRefs(parent));
+      parentNode != root) {
+    parentNode =
+      InspectorUtils::GetParentForNode(*parentNode, mShowAnonymousContent);
   }
 
+  parent = do_QueryInterface(parentNode);
   return parent.forget();
 }
 
 static already_AddRefed<nsINodeList>
 GetChildren(nsIDOMNode* aParent,
             bool aShowAnonymousContent,
             bool aShowSubDocuments)
 {
--- a/layout/inspector/inIDOMUtils.idl
+++ b/layout/inspector/inIDOMUtils.idl
@@ -15,21 +15,16 @@ interface nsIDOMNode;
 interface nsIDOMNodeList;
 interface nsIDOMFontFaceList;
 interface nsIDOMRange;
 interface nsIDOMCSSStyleSheet;
 
 [scriptable, uuid(362e98c3-82c2-4ad8-8dcb-00e8e4eab497)]
 interface inIDOMUtils : nsISupports
 {
-  // Returns the "parent" of a node.  The parent of a document node is the
-  // frame/iframe containing that document.  aShowingAnonymousContent says
-  // whether we are showing anonymous content.
-  nsIDOMNode getParentForNode(in nsIDOMNode aNode,
-                              in boolean aShowingAnonymousContent);
   nsIDOMNodeList getChildrenForNode(in nsIDOMNode aNode,
                                     in boolean aShowingAnonymousContent);
 
   // XBL utilities
   nsIArray getBindingURLs(in nsIDOMElement aElement);
 
   // content state utilities
   unsigned long long getContentState(in nsIDOMElement aElement);
--- a/layout/inspector/inLayoutUtils.cpp
+++ b/layout/inspector/inLayoutUtils.cpp
@@ -53,20 +53,19 @@ inLayoutUtils::GetSubDocumentFor(nsIDOMN
 
       return domdoc;
     }
   }
 
   return nullptr;
 }
 
-nsIDOMNode*
+nsINode*
 inLayoutUtils::GetContainerFor(const nsIDocument& aDoc)
 {
   nsPIDOMWindowOuter* pwin = aDoc.GetWindow();
   if (!pwin) {
     return nullptr;
   }
 
-  nsCOMPtr<nsIDOMNode> node = do_QueryInterface(pwin->GetFrameElementInternal());
-  return node;
+  return pwin->GetFrameElementInternal();
 }
 
--- a/layout/inspector/inLayoutUtils.h
+++ b/layout/inspector/inLayoutUtils.h
@@ -17,12 +17,12 @@ class EventStateManager;
 } // namespace mozilla
 
 class inLayoutUtils
 {
 public:
   static mozilla::EventStateManager*
            GetEventStateManagerFor(nsIDOMElement *aElement);
   static nsIDOMDocument* GetSubDocumentFor(nsIDOMNode* aNode);
-  static nsIDOMNode* GetContainerFor(const nsIDocument& aDoc);
+  static nsINode* GetContainerFor(const nsIDocument& aDoc);
 };
 
 #endif // __inLayoutUtils_h__
--- a/layout/inspector/tests/test_bug462787.html
+++ b/layout/inspector/tests/test_bug462787.html
@@ -46,22 +46,21 @@ function do_test() {
     InspectorUtils.isIgnorableWhitespace(null);
     ok(false, "expected an exception");
   }
   catch(e) {
     is(e.name, "TypeError", "got the expected exception");
   }
 
   try {
-    utils.getParentForNode(null, true); 
+    InspectorUtils.getParentForNode(null, true);
     ok(false, "expected an exception");
   }
   catch(e) {
-    e = SpecialPowers.wrap(e);
-    is(e.result, INVALID_POINTER, "got the expected exception");
+    is(e.name, "TypeError", "got the expected exception");
   }
 
   try {
     utils.getBindingURLs(null); 
     ok(false, "expected an exception"); 
   }
   catch(e) {
     e = SpecialPowers.wrap(e);
--- a/layout/inspector/tests/test_bug462789.html
+++ b/layout/inspector/tests/test_bug462789.html
@@ -50,19 +50,19 @@ function do_test() {
 
   try {
     var res = InspectorUtils.isIgnorableWhitespace(text);
     is(res, false, "isIgnorableWhitespace");
   }
   catch(e) { ok(false, "got an unexpected exception:" + e); }
 
   try {
-    var res = utils.getParentForNode(docElement, true);
+    var res = InspectorUtils.getParentForNode(docElement, true);
     is(res.nodeType, DOCUMENT_NODE_TYPE, "getParentForNode(docElement, true)");
-    res = utils.getParentForNode(text, true);
+    res = InspectorUtils.getParentForNode(text, true);
     is(res.tagName, "BODY", "getParentForNode(text, true)");
   }
   catch(e) { ok(false, "got an unexpected exception:" + e); }
 
   try {
     var res = utils.getBindingURLs(docElement);
     ok(SpecialPowers.call_Instanceof(res, SpecialPowers.Ci["nsIArray"]), "getBindingURLs result type");
     is(res.length, 0, "getBindingURLs array length");