Bug 1399626 - Part 1: Add some helper methods to RangeBoundary, r=masayuki
--- 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