Bug 1272409 part 1: Add GetNodeDepth() to nsContentUtils. r=dholbert r?smaug draft
authorFariskhi Vidyan <farislab@gmail.com>
Wed, 14 Sep 2016 16:24:51 -0700
changeset 413819 6392fe9e45ea71128e67152270c4b1f833b0fafe
parent 413818 a7389ca6c0c2dd2101a96d0f9b364495a6e824a6
child 413820 ec07a987ff6ed2ef1df266c527f939455903aa45
child 413824 7c161b464bae8d40cd6ba457a777c7dfb3bc8d2a
push id29522
push userfarislab@gmail.com
push dateWed, 14 Sep 2016 23:43:14 +0000
reviewersdholbert, smaug
bugs1272409
milestone51.0a1
Bug 1272409 part 1: Add GetNodeDepth() to nsContentUtils. r=dholbert r?smaug MozReview-Commit-ID: 4enyckAawVO
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -9547,8 +9547,22 @@ 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 = 1;
+
+  MOZ_ASSERT(aNode, "Node shouldn't be null");
+
+  while ((aNode = aNode->GetParentNode())) {
+    ++depth;
+  }
+
+  return depth;
+}
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -2697,16 +2697,24 @@ 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 (in terms of the number of
+   * nodes) to an unparented/root node from aNode. An unparented/root node is
+   * considered to have a depth of 1, its children have a depth of 2, etc.
+   * aNode is expected to be non-null.
+   */
+  static uint32_t GetNodeDepth(nsINode* aNode);
+
 private:
   static bool InitializeEventTable();
 
   static nsresult EnsureStringBundle(PropertiesFile aFile);
 
   static bool CanCallerAccess(nsIPrincipal* aSubjectPrincipal,
                                 nsIPrincipal* aPrincipal);