From 6521583602f38ef8f6c972e591214f3aeabdccd8 Mon Sep 17 00:00:00 2001 From: Boris Chiou Date: Wed, 8 May 2019 20:52:51 +0000 Subject: [PATCH] Bug 1545239 - Move GetNodeDepth into ResizeObserver.cpp r=dholbert GetNodeDepth() is a special version for ResizeObserver to get the depth of node (across Shadow DOM). Based on the comment in D27615, it's better to move it into ResizeObserver.cpp. Differential Revision: https://phabricator.services.mozilla.com/D28736 --HG-- extra : moz-landing-system : lando --- dom/base/ResizeObserver.cpp | 36 +++++++++++++++++++++++++++++++++--- dom/base/nsContentUtils.cpp | 15 --------------- dom/base/nsContentUtils.h | 18 ------------------ 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/dom/base/ResizeObserver.cpp b/dom/base/ResizeObserver.cpp index a7ba5d0d93a3..be1faab85153 100644 --- a/dom/base/ResizeObserver.cpp +++ b/dom/base/ResizeObserver.cpp @@ -7,13 +7,43 @@ #include "mozilla/dom/ResizeObserver.h" #include "mozilla/dom/Document.h" -#include "nsContentUtils.h" +#include "nsIContent.h" #include "nsSVGUtils.h" #include namespace mozilla { namespace dom { +/** + * 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. + * Note: The shadow root is not part of the calculation because the caller, + * ResizeObserver, doesn't observe the shadow root, and only needs relative + * depths among all the observed targets. In other words, we calculate the + * depth of the flattened tree. + * + * However, these is a spec issue about how to handle shadow DOM case. We + * may need to update this function later: + * https://github.com/w3c/csswg-drafts/issues/3840 + * + * https://drafts.csswg.org/resize-observer/#calculate-depth-for-node-h + */ +static uint32_t GetNodeDepth(nsINode* aNode) { + uint32_t depth = 1; + + MOZ_ASSERT(aNode, "Node shouldn't be null"); + + // Use GetFlattenedTreeParentNode to bypass the shadow root and cross the + // shadow boundary to calculate the node depth without the shadow root. + while ((aNode = aNode->GetFlattenedTreeParentNode())) { + ++depth; + } + + return depth; +} + NS_IMPL_CYCLE_COLLECTION(ResizeObservation, mTarget) NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(ResizeObservation, AddRef) NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(ResizeObservation, Release) @@ -136,7 +166,7 @@ void ResizeObserver::GatherActiveObservations(uint32_t aDepth) { continue; } - uint32_t targetDepth = nsContentUtils::GetNodeDepth(observation->Target()); + uint32_t targetDepth = GetNodeDepth(observation->Target()); if (targetDepth > aDepth) { mActiveTargets.AppendElement(observation); @@ -172,7 +202,7 @@ uint32_t ResizeObserver::BroadcastActiveObservations() { // will be based on the updated size from last delivered observations. observation->UpdateBroadcastSize(rect.Size()); - uint32_t targetDepth = nsContentUtils::GetNodeDepth(observation->Target()); + uint32_t targetDepth = GetNodeDepth(observation->Target()); if (targetDepth < shallowestTargetDepth) { shallowestTargetDepth = targetDepth; diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 4a8311b27623..d2ab99cc922f 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -10524,21 +10524,6 @@ bool nsContentUtils:: nsThreadManager::MainThreadHasPendingHighPriorityEvents(); } -/* static */ -uint32_t nsContentUtils::GetNodeDepth(nsINode* aNode) { - uint32_t depth = 1; - - MOZ_ASSERT(aNode, "Node shouldn't be null"); - - // Use GetFlattenedTreeParentNode to bypass the shadow root and cross the - // shadow boundary to calculate the node depth without the shadow root. - while ((aNode = aNode->GetFlattenedTreeParentNode())) { - ++depth; - } - - return depth; -} - /* static */ bool nsContentUtils::IsURIInPrefList(nsIURI* aURI, const char* aPrefName) { MOZ_ASSERT(aPrefName); diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 34ecb67203f8..38f577db1e1d 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -3256,24 +3256,6 @@ class nsContentUtils { static bool HighPriorityEventPendingForTopLevelDocumentBeforeContentfulPaint( Document* aDocument); - /** - * 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. - * Note: The shadow root is not part of the calculation because the caller, - * ResizeObserver, doesn't observe the shadow root, and only needs relative - * depths among all the observed targets. In other words, we calculate the - * depth of the flattened tree. - * - * However, these is a spec issue about how to handle shadow DOM case. We - * may need to update this function later: - * https://github.com/w3c/csswg-drafts/issues/3840 - * - * https://drafts.csswg.org/resize-observer/#calculate-depth-for-node-h - */ - static uint32_t GetNodeDepth(nsINode* aNode); - private: static bool InitializeEventTable();