Bug 1587433: part 5) Add `RangeBoundaryBase::Offset(OffsetFilter)`. r=smaug
☠☠ backed out by c0c22dbdd5b8 ☠ ☠
authorMirko Brodesser <mbrodesser@mozilla.com>
Wed, 11 Dec 2019 12:24:48 +0000
changeset 507070 7ebbcb2da48889068c9606106861ebcd95217de5
parent 507069 4032df295a67aca4366d33ebb0ba9a57305d1bcc
child 507071 b1d9e55ece86cf22e837849549c0390b2d9b0411
push id36922
push userncsoregi@mozilla.com
push dateMon, 16 Dec 2019 17:21:47 +0000
treeherdermozilla-central@27d0d6cc2131 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1587433
milestone73.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 1587433: part 5) Add `RangeBoundaryBase::Offset(OffsetFilter)`. r=smaug Once `RawRangeBoundary::Offset()` is removed, callers are forced to pass a filter to `Offset()`. Making it less likely for new callers to undeliberately retrieve invalid values. `RawRangeBoundary::Offset()` is removed in separate commit, once all callers adapted. Differential Revision: https://phabricator.services.mozilla.com/D54456
dom/base/RangeBoundary.h
--- a/dom/base/RangeBoundary.h
+++ b/dom/base/RangeBoundary.h
@@ -48,16 +48,18 @@ class RangeBoundaryBase {
   template <typename T, typename U>
   friend class EditorDOMPointBase;
 
   friend void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback&,
                                           RangeBoundary&, const char*,
                                           uint32_t);
   friend void ImplCycleCollectionUnlink(RangeBoundary&);
 
+  static const uint32_t kFallbackOffset = 0;
+
  public:
   RangeBoundaryBase(nsINode* aContainer, nsIContent* aRef)
       : mParent(aContainer), mRef(aRef) {
     if (mRef) {
       NS_WARNING_ASSERTION(mRef->GetParentNode() == mParent,
                            "Initializing RangeBoundary with invalid value");
     } else {
       mOffset.emplace(0);
@@ -133,23 +135,45 @@ class RangeBoundaryBase {
     }
     if (NS_WARN_IF(!mRef)) {
       // Already referring the start of the container.
       return nullptr;
     }
     return mRef;
   }
 
+  enum class OffsetFilter { kValidOffsets, kValidOrInvalidOffsets };
+
+  /**
+   * @return maybe an offset, depending on aOffsetFilter. If it is:
+   *         kValidOffsets: if the offset is valid, it, Nothing{} otherwise.
+   *         kValidOrInvalidOffsets: the internally stored offset, even if
+   *                                 invalid, or if not available, a defined
+   *                                 default value. That is, always some value.
+   */
+  Maybe<uint32_t> Offset(const OffsetFilter aOffsetFilter) const {
+    switch (aOffsetFilter) {
+      case OffsetFilter::kValidOffsets: {
+        return IsSetAndValid() ? Some(Offset()) : Nothing();
+      }
+      case OffsetFilter::kValidOrInvalidOffsets: {
+        return Some(Offset());
+      }
+    }
+  }
+
+  // TODO(mbrodesser): merge into the other Offset() method once all callers are
+  // switched to it.
   uint32_t Offset() const {
     if (mOffset.isSome()) {
       return mOffset.value();
     }
 
     if (!mParent) {
-      return 0;
+      return kFallbackOffset;
     }
 
     MOZ_ASSERT(mRef);
     MOZ_ASSERT(mRef->GetParentNode() == mParent);
 
     const int32_t index = mParent->ComputeIndexOf(mRef);
     MOZ_ASSERT(index >= 0);
     mOffset.emplace(static_cast<uint32_t>(index + 1));
@@ -232,16 +256,19 @@ class RangeBoundaryBase {
 
  private:
   ParentType mParent;
   RefType mRef;
 
   mutable mozilla::Maybe<uint32_t> mOffset;
 };
 
+template <typename ParentType, typename RefType>
+const uint32_t RangeBoundaryBase<ParentType, RefType>::kFallbackOffset;
+
 inline void ImplCycleCollectionUnlink(RangeBoundary& aField) {
   ImplCycleCollectionUnlink(aField.mParent);
   ImplCycleCollectionUnlink(aField.mRef);
 }
 
 inline void ImplCycleCollectionTraverse(
     nsCycleCollectionTraversalCallback& aCallback, RangeBoundary& aField,
     const char* aName, uint32_t aFlags) {