Bug 1272409 - Part 1: Add GetNodeDepth() to nsContentUtils. r=smaug
☠☠ backed out by 3791fc50da34 ☠ ☠
authorFariskhi Vidyan <farislab@gmail.com>
Thu, 25 Apr 2019 18:30:04 +0000
changeset 530197 d817fbfc52b68c7940662a29fa612c704b51b044
parent 530196 b85a9bd9e728ef185193927b742dcdbfd7868f32
child 530198 c331a4a8b3439fdb15fd43544dd964cc7710847b
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1272409
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1272409 - Part 1: Add GetNodeDepth() to nsContentUtils. r=smaug `GetNodeDepth()` will be used in the next patch. Depends on D27614 Differential Revision: https://phabricator.services.mozilla.com/D27615
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -10624,16 +10624,31 @@ bool nsContentUtils::
   Document* topLevel = aDocument->GetTopLevelContentDocument();
   return topLevel && topLevel->GetPresShell() &&
          topLevel->GetPresShell()->GetPresContext() &&
          !topLevel->GetPresShell()->GetPresContext()->HadContentfulPaint() &&
          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);
 
   nsAutoCString blackList;
   Preferences::GetCString(aPrefName, blackList);
   ToLowerCase(blackList);
   return IsURIInList(aURI, blackList);
 }
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -3355,16 +3355,34 @@ class nsContentUtils {
   /**
    * Returns true if the top level ancestor content document of aDocument hasn't
    * yet had the first contentful paint and there is a high priority event
    * pending in the main thread.
    */
   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();
 
   static nsresult EnsureStringBundle(PropertiesFile aFile);
 
   static bool CanCallerAccess(nsIPrincipal* aSubjectPrincipal,
                               nsIPrincipal* aPrincipal);