Bug 1272409 part 2: Add GetNodeDepth() to nsContentUtils. r?dholbert draft
authorFariskhi Vidyan <farislab@gmail.com>
Fri, 09 Sep 2016 08:49:19 -0700
changeset 412224 1b3dadbb17ac34b082c341ad648f745baa9be8f5
parent 412223 06f9665a079ac8b0cdf2baec4213f9159d7b060c
child 412225 331688c56faa87d118deb481e7ba53cac72c9abc
child 412308 8731ed8d5edff073b634c9891445be764984f67b
push id29082
push userfarislab@gmail.com
push dateFri, 09 Sep 2016 15:46:39 +0000
reviewersdholbert
bugs1272409
milestone51.0a1
Bug 1272409 part 2: Add GetNodeDepth() to nsContentUtils. r?dholbert MozReview-Commit-ID: J5BFSSFbXTi
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -9547,8 +9547,25 @@ nsContentUtils::GetCustomPrototype(nsIDo
 
   RefPtr<CustomElementsRegistry> registry(window->CustomElements());
   if (!registry) {
     return;
   }
 
   return registry->GetCustomPrototype(aAtom, aPrototype);
 }
+
+/* static */ uint32_t
+nsContentUtils::GetNodeDepth(nsINode* aNode)
+{
+  uint32_t depth = 0;
+
+  if (aNode) {
+    nsINode* parent = aNode->GetParentNode();
+
+    while (parent) {
+      parent = parent->GetParentNode();
+      ++depth;
+    }
+  }
+
+  return depth;
+}
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -2697,16 +2697,21 @@ public:
                                        mozilla::dom::LifecycleCallbackArgs* aArgs = nullptr,
                                        mozilla::dom::CustomElementDefinition* aDefinition = nullptr);
 
   static void GetCustomPrototype(nsIDocument* aDoc,
                                  int32_t aNamespaceID,
                                  nsIAtom* aAtom,
                                  JS::MutableHandle<JSObject*> prototype);
 
+  /**
+   * Returns the length of the parent-traversal path from node to root.
+   */
+  static uint32_t GetNodeDepth(nsINode* aNode);
+
 private:
   static bool InitializeEventTable();
 
   static nsresult EnsureStringBundle(PropertiesFile aFile);
 
   static bool CanCallerAccess(nsIPrincipal* aSubjectPrincipal,
                                 nsIPrincipal* aPrincipal);