From ed1f54ab7312f2e7f5b2f46bc5e9d693f4ee6f06 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Wed, 16 Sep 2020 13:30:35 +0000 Subject: Issue #1643 - Part 1: Add GetNodeDepth() to nsContentUtils. --- dom/base/nsContentUtils.cpp | 16 +++++++++++++++- dom/base/nsContentUtils.h | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'dom/base') diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 1a3f5b5ca..ffe50a015 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -9843,4 +9843,18 @@ nsContentUtils::IsLocalRefURL(const nsString& aString) } return false; -} \ No newline at end of file +} + +/* 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; +} diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index ffbd15e78..08587b5e4 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -2745,6 +2745,14 @@ public: static bool IsLocalRefURL(const nsString& aString); + /** + * 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(); -- cgit v1.2.3