Bug 1399626 - Part 1: Add some helper methods to RangeBoundary, r=masayuki draft
authorMichael Layzell <michael@thelayzells.com>
Wed, 13 Sep 2017 16:51:09 -0400
changeset 672780 208a2e1f766954b292de85f34f740c5a10d224db
parent 672779 7d8228ae31579cf271ffdd8695eb326c1e48c6e9
child 672781 c28fe5cd1c2b4936e2884116a7f252616d9b321d
push id82373
push userbmo:nika@thelayzells.com
push dateFri, 29 Sep 2017 19:23:28 +0000
reviewersmasayuki
bugs1399626
milestone58.0a1
Bug 1399626 - Part 1: Add some helper methods to RangeBoundary, r=masayuki
dom/base/RangeBoundary.h
--- a/dom/base/RangeBoundary.h
+++ b/dom/base/RangeBoundary.h
@@ -2,16 +2,20 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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/. */
 
 #ifndef mozilla_RangeBoundary_h
 #define mozilla_RangeBoundary_h
 
+#include "nsCOMPtr.h"
+#include "nsIContent.h"
+#include "mozilla/Maybe.h"
+
 namespace mozilla {
 
 // This class will maintain a reference to the child immediately
 // before the boundary's offset. We try to avoid computing the
 // offset as much as possible and just ensure mRef points to the
 // correct child.
 //
 // mParent
@@ -198,16 +202,37 @@ public:
     }
 
     if (Ref()) {
       return Ref()->GetParentNode() == Container();
     }
     return Offset() <= Container()->Length();
   }
 
+  bool
+  IsStartOfContainer() const
+  {
+    // We're at the first point in the container if we don't have a reference,
+    // and our offset is 0. If we don't have a Ref, we should already have an
+    // offset, so we can just directly fetch it.
+    return !Ref() && mOffset.value() == 0;
+  }
+
+  bool
+  IsEndOfContainer() const
+  {
+    // We're at the last point in the container if Ref is a pointer to the last
+    // child in Container(), or our Offset() is the same as the length of our
+    // container. If we don't have a Ref, then we should already have an offset,
+    // so we can just directly fetch it.
+    return Ref()
+      ? !Ref()->GetNextSibling()
+      : mOffset.value() == Container()->Length();
+  }
+
   // Convenience methods for switching between the two types
   // of RangeBoundary.
   RangeBoundaryBase<nsINode*, nsIContent*>
   AsRaw() const
   {
     return RangeBoundaryBase<nsINode*, nsIContent*>(*this);
   }
 
@@ -222,16 +247,22 @@ public:
 
   template<typename A, typename B>
   bool operator==(const RangeBoundaryBase<A, B>& aOther) const
   {
     return mParent == aOther.mParent &&
       (mRef ? mRef == aOther.mRef : mOffset == aOther.mOffset);
   }
 
+  template<typename A, typename B>
+  bool operator!=(const RangeBoundaryBase<A, B>& aOther) const
+  {
+    return !(*this == aOther);
+  }
+
 private:
   ParentType mParent;
   RefType mRef;
 
   mutable mozilla::Maybe<uint32_t> mOffset;
 };
 
 inline void