Bug 1545239 - Move GetNodeDepth into ResizeObserver.cpp r=dholbert
authorBoris Chiou <boris.chiou@gmail.com>
Wed, 08 May 2019 20:52:51 +0000
changeset 531967 2e3522f77b0f686663145bdce20ffceef3909896
parent 531966 244b0ee2c89066b418c7088acf880f5f4eb6fa3d
child 531968 dc3570e93c4150f913a20baa6ee42c109ab85d60
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)
reviewersdholbert
bugs1545239
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 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
dom/base/ResizeObserver.cpp
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
--- a/dom/base/ResizeObserver.cpp
+++ b/dom/base/ResizeObserver.cpp
@@ -2,23 +2,53 @@
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/ResizeObserver.h"
 
 #include "mozilla/dom/Document.h"
-#include "nsContentUtils.h"
+#include "nsIContent.h"
 #include "nsSVGUtils.h"
 #include <limits>
 
 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)
 
 bool ResizeObservation::IsActive() const {
   nsRect rect = GetTargetRect();
   return (rect.width != mBroadcastWidth || rect.height != mBroadcastHeight);
 }
@@ -131,17 +161,17 @@ void ResizeObserver::GatherActiveObserva
   mActiveTargets.Clear();
   mHasSkippedTargets = false;
 
   for (auto observation : mObservationList) {
     if (!observation->IsActive()) {
       continue;
     }
 
-    uint32_t targetDepth = nsContentUtils::GetNodeDepth(observation->Target());
+    uint32_t targetDepth = GetNodeDepth(observation->Target());
 
     if (targetDepth > aDepth) {
       mActiveTargets.AppendElement(observation);
     } else {
       mHasSkippedTargets = true;
     }
   }
 }
@@ -167,17 +197,17 @@ uint32_t ResizeObserver::BroadcastActive
       // Out of memory.
       break;
     }
 
     // Sync the broadcast size of observation so the next size inspection
     // 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;
     }
   }
 
   RefPtr<ResizeObserverCallback> callback(mCallback);
   callback->Call(this, entries, *this);
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -10520,31 +10520,16 @@ 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
@@ -3251,34 +3251,16 @@ 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);