Bug 1272409 part 2: Add GetNodeDepth() to nsContentUtils. r?dholbert draft
authorFariskhi Vidyan <farislab@gmail.com>
Wed, 07 Sep 2016 17:06:45 -0700
changeset 411469 63cee837e5e1c91a5b7c2c0247516d65d559a428
parent 411468 9b0a46322626998460898f97705e70920c442147
child 411470 ac78d9e7b8d3ba6998535d5e590a0ba1aded0829
push id28904
push userfarislab@gmail.com
push dateThu, 08 Sep 2016 05:12:39 +0000
reviewersdholbert
bugs1272409
milestone51.0a1
Bug 1272409 part 2: Add GetNodeDepth() to nsContentUtils. r?dholbert MozReview-Commit-ID: HyYuK3RXbSR
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);