2017-09-08 00:05:51 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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
|
|
|
|
|
2017-10-02 17:58:23 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
template<typename T, typename U>
|
|
|
|
class EditorDOMPointBase;
|
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
// 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
|
|
|
|
// |
|
|
|
|
// [child0] [child1] [child2]
|
|
|
|
// / |
|
|
|
|
// mRef mOffset=2
|
|
|
|
//
|
|
|
|
// If mOffset == 0, mRef is null.
|
|
|
|
// For text nodes, mRef will always be null and the offset will
|
|
|
|
// be kept up-to-date.
|
|
|
|
|
|
|
|
template<typename ParentType, typename RefType>
|
|
|
|
class RangeBoundaryBase;
|
|
|
|
|
|
|
|
typedef RangeBoundaryBase<nsCOMPtr<nsINode>, nsCOMPtr<nsIContent>> RangeBoundary;
|
|
|
|
typedef RangeBoundaryBase<nsINode*, nsIContent*> RawRangeBoundary;
|
|
|
|
|
|
|
|
// This class has two specializations, one using reference counting
|
|
|
|
// pointers and one using raw pointers. This helps us avoid unnecessary
|
|
|
|
// AddRef/Release calls.
|
|
|
|
template<typename ParentType, typename RefType>
|
|
|
|
class RangeBoundaryBase
|
|
|
|
{
|
|
|
|
template<typename T, typename U>
|
|
|
|
friend class RangeBoundaryBase;
|
2017-11-22 18:15:29 +03:00
|
|
|
template<typename T, typename U>
|
|
|
|
friend class EditorDOMPointBase;
|
2017-09-08 00:05:51 +03:00
|
|
|
|
|
|
|
friend void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback&,
|
|
|
|
RangeBoundary&, const char*,
|
|
|
|
uint32_t);
|
|
|
|
friend void ImplCycleCollectionUnlink(RangeBoundary&);
|
|
|
|
|
|
|
|
public:
|
|
|
|
RangeBoundaryBase(nsINode* aContainer, nsIContent* aRef)
|
|
|
|
: mParent(aContainer)
|
|
|
|
, mRef(aRef)
|
|
|
|
{
|
|
|
|
if (!mRef) {
|
|
|
|
mOffset = mozilla::Some(0);
|
|
|
|
} else {
|
2017-10-02 17:58:36 +03:00
|
|
|
NS_WARNING_ASSERTION(mRef->GetParentNode() == mParent,
|
|
|
|
"Initializing RangeBoundary with invalid value");
|
2017-09-08 00:05:51 +03:00
|
|
|
mOffset.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeBoundaryBase(nsINode* aContainer, int32_t aOffset)
|
|
|
|
: mParent(aContainer)
|
|
|
|
, mRef(nullptr)
|
|
|
|
, mOffset(mozilla::Some(aOffset))
|
|
|
|
{
|
2017-12-04 10:28:50 +03:00
|
|
|
if (mParent && mParent->IsContainerNode()) {
|
|
|
|
// Find a reference node
|
|
|
|
if (aOffset == static_cast<int32_t>(aContainer->GetChildCount())) {
|
|
|
|
mRef = aContainer->GetLastChild();
|
|
|
|
} else if (aOffset != 0) {
|
2018-01-03 15:59:54 +03:00
|
|
|
mRef = mParent->GetChildAt_Deprecated(aOffset - 1);
|
2017-12-04 10:28:50 +03:00
|
|
|
}
|
2017-09-08 00:05:51 +03:00
|
|
|
|
2017-12-04 10:28:50 +03:00
|
|
|
NS_WARNING_ASSERTION(mRef || aOffset == 0,
|
|
|
|
"Constructing RangeBoundary with invalid value");
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
2017-12-04 10:28:50 +03:00
|
|
|
|
|
|
|
NS_WARNING_ASSERTION(!mRef || mRef->GetParentNode() == mParent,
|
|
|
|
"Constructing RangeBoundary with invalid value");
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
RangeBoundaryBase()
|
|
|
|
: mParent(nullptr)
|
|
|
|
, mRef(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed for initializing RawRangeBoundary from an existing RangeBoundary.
|
|
|
|
template<typename PT, typename RT>
|
|
|
|
explicit RangeBoundaryBase(const RangeBoundaryBase<PT, RT>& aOther)
|
|
|
|
: mParent(aOther.mParent)
|
|
|
|
, mRef(aOther.mRef)
|
|
|
|
, mOffset(aOther.mOffset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
Ref() const
|
|
|
|
{
|
|
|
|
return mRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode*
|
|
|
|
Container() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
GetChildAtOffset() const
|
|
|
|
{
|
|
|
|
if (!mParent || !mParent->IsContainerNode()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!mRef) {
|
2017-10-02 17:58:36 +03:00
|
|
|
MOZ_ASSERT(Offset() == 0, "invalid RangeBoundary");
|
2017-09-08 00:05:51 +03:00
|
|
|
return mParent->GetFirstChild();
|
|
|
|
}
|
2018-01-03 15:59:54 +03:00
|
|
|
MOZ_ASSERT(mParent->GetChildAt_Deprecated(Offset()) == mRef->GetNextSibling());
|
2017-09-08 00:05:51 +03:00
|
|
|
return mRef->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2017-11-01 08:41:03 +03:00
|
|
|
/**
|
|
|
|
* GetNextSiblingOfChildOffset() returns next sibling of a child at offset.
|
|
|
|
* If this refers after the last child or the container cannot have children,
|
|
|
|
* this returns nullptr with warning.
|
|
|
|
*/
|
|
|
|
nsIContent*
|
|
|
|
GetNextSiblingOfChildAtOffset() const
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mParent) || NS_WARN_IF(!mParent->IsContainerNode())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!mRef->GetNextSibling())) {
|
|
|
|
// Already referring the end of the container.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mRef->GetNextSibling()->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GetPreviousSiblingOfChildAtOffset() returns previous sibling of a child
|
|
|
|
* at offset. If this refers the first child or the container cannot have
|
|
|
|
* children, this returns nullptr with warning.
|
|
|
|
*/
|
|
|
|
nsIContent*
|
|
|
|
GetPreviousSiblingOfChildAtOffset() const
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mParent) || NS_WARN_IF(!mParent->IsContainerNode())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!mRef)) {
|
|
|
|
// Already referring the start of the container.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mRef;
|
|
|
|
}
|
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
uint32_t
|
|
|
|
Offset() const
|
|
|
|
{
|
|
|
|
if (mOffset.isSome()) {
|
|
|
|
return mOffset.value();
|
|
|
|
}
|
2017-12-04 10:28:50 +03:00
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
if (!mParent) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-04 10:28:50 +03:00
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
MOZ_ASSERT(mRef);
|
|
|
|
MOZ_ASSERT(mRef->GetParentNode() == mParent);
|
2018-01-23 16:30:18 +03:00
|
|
|
mOffset = mozilla::Some(mParent->ComputeIndexOf(mRef) + 1);
|
2017-12-04 10:28:50 +03:00
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
return mOffset.value();
|
|
|
|
}
|
|
|
|
|
2017-11-06 11:01:33 +03:00
|
|
|
void
|
2017-12-04 10:28:50 +03:00
|
|
|
InvalidateOffset()
|
2017-11-06 11:01:33 +03:00
|
|
|
{
|
2017-12-04 10:28:50 +03:00
|
|
|
MOZ_ASSERT(mParent);
|
|
|
|
MOZ_ASSERT(mParent->IsContainerNode(), "Range is positioned on a text node!");
|
|
|
|
|
2017-11-06 11:01:33 +03:00
|
|
|
if (!mRef) {
|
2017-12-04 10:28:50 +03:00
|
|
|
MOZ_ASSERT(mOffset.isSome() && mOffset.value() == 0,
|
|
|
|
"Invalidating offset of invalid RangeBoundary?");
|
|
|
|
return;
|
2017-11-06 11:01:33 +03:00
|
|
|
}
|
|
|
|
mOffset.reset();
|
|
|
|
}
|
2017-09-08 00:05:51 +03:00
|
|
|
|
2017-12-04 10:28:50 +03:00
|
|
|
void
|
|
|
|
Set(nsINode* aContainer, int32_t aOffset)
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
2017-12-04 10:28:50 +03:00
|
|
|
mParent = aContainer;
|
|
|
|
if (mParent && mParent->IsContainerNode()) {
|
|
|
|
// Find a reference node
|
|
|
|
if (aOffset == static_cast<int32_t>(aContainer->GetChildCount())) {
|
|
|
|
mRef = aContainer->GetLastChild();
|
|
|
|
} else if (aOffset == 0) {
|
|
|
|
mRef = nullptr;
|
|
|
|
} else {
|
2018-01-03 15:59:54 +03:00
|
|
|
mRef = mParent->GetChildAt_Deprecated(aOffset - 1);
|
2017-12-04 10:28:50 +03:00
|
|
|
MOZ_ASSERT(mRef);
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-12-04 10:28:50 +03:00
|
|
|
NS_WARNING_ASSERTION(mRef || aOffset == 0,
|
|
|
|
"Setting RangeBoundary to invalid value");
|
|
|
|
} else {
|
|
|
|
mRef = nullptr;
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-12-04 10:28:50 +03:00
|
|
|
mOffset = mozilla::Some(aOffset);
|
2017-11-01 08:41:03 +03:00
|
|
|
|
2017-12-04 10:28:50 +03:00
|
|
|
NS_WARNING_ASSERTION(!mRef || mRef->GetParentNode() == mParent,
|
|
|
|
"Setting RangeBoundary to invalid value");
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
void
|
|
|
|
SetAfterRef(nsINode* aParent, nsIContent* aRef)
|
|
|
|
{
|
|
|
|
mParent = aParent;
|
|
|
|
mRef = aRef;
|
|
|
|
if (!mRef) {
|
|
|
|
mOffset = mozilla::Some(0);
|
|
|
|
} else {
|
|
|
|
mOffset.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsSet() const
|
|
|
|
{
|
|
|
|
return mParent && (mRef || mOffset.isSome());
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:19:19 +03:00
|
|
|
bool
|
|
|
|
IsSetAndValid() const
|
|
|
|
{
|
|
|
|
if (!IsSet()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-04 10:28:50 +03:00
|
|
|
if (Ref()) {
|
|
|
|
return Ref()->GetParentNode() == Container();
|
2017-11-02 15:25:14 +03:00
|
|
|
}
|
2017-12-04 10:28:50 +03:00
|
|
|
return Offset() <= Container()->Length();
|
2017-09-08 18:19:19 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:23 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
// Convenience methods for switching between the two types
|
|
|
|
// of RangeBoundary.
|
|
|
|
RangeBoundaryBase<nsINode*, nsIContent*>
|
|
|
|
AsRaw() const
|
|
|
|
{
|
|
|
|
return RangeBoundaryBase<nsINode*, nsIContent*>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
RangeBoundaryBase& operator=(const RangeBoundaryBase<A,B>& aOther)
|
|
|
|
{
|
2018-01-08 22:27:44 +03:00
|
|
|
// mParent and mRef can be strong pointers, so better to try to avoid any
|
|
|
|
// extra AddRef/Release calls.
|
|
|
|
if (mParent != aOther.mParent) {
|
|
|
|
mParent = aOther.mParent;
|
|
|
|
}
|
|
|
|
if (mRef != aOther.mRef) {
|
|
|
|
mRef = aOther.mRef;
|
|
|
|
}
|
2017-09-08 00:05:51 +03:00
|
|
|
mOffset = aOther.mOffset;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
bool operator==(const RangeBoundaryBase<A, B>& aOther) const
|
|
|
|
{
|
2017-12-04 10:28:50 +03:00
|
|
|
return mParent == aOther.mParent &&
|
|
|
|
(mRef ? mRef == aOther.mRef : mOffset == aOther.mOffset);
|
2017-09-08 00:05:51 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:23 +03:00
|
|
|
template<typename A, typename B>
|
|
|
|
bool operator!=(const RangeBoundaryBase<A, B>& aOther) const
|
|
|
|
{
|
|
|
|
return !(*this == aOther);
|
|
|
|
}
|
|
|
|
|
2017-09-08 00:05:51 +03:00
|
|
|
private:
|
|
|
|
ParentType mParent;
|
2017-12-04 10:28:50 +03:00
|
|
|
RefType mRef;
|
2017-09-08 00:05:51 +03:00
|
|
|
|
|
|
|
mutable mozilla::Maybe<uint32_t> mOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(RangeBoundary& aField)
|
|
|
|
{
|
|
|
|
ImplCycleCollectionUnlink(aField.mParent);
|
|
|
|
ImplCycleCollectionUnlink(aField.mRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
RangeBoundary& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
ImplCycleCollectionTraverse(aCallback, aField.mParent, "mParent", 0);
|
|
|
|
ImplCycleCollectionTraverse(aCallback, aField.mRef, "mRef", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // defined(mozilla_RangeBoundary_h)
|