2017-11-01 08:41:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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_EditorDOMPoint_h
|
|
|
|
#define mozilla_EditorDOMPoint_h
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/RangeBoundary.h"
|
2017-12-07 12:45:52 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/Text.h"
|
|
|
|
#include "nsAtom.h"
|
2017-11-01 08:41:03 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIContent.h"
|
2017-12-07 12:45:52 +03:00
|
|
|
#include "nsIDOMNode.h"
|
2017-11-01 08:41:03 +03:00
|
|
|
#include "nsINode.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
template<typename ParentType, typename ChildType>
|
2017-11-01 08:41:03 +03:00
|
|
|
class EditorDOMPointBase;
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
/**
|
|
|
|
* EditorDOMPoint and EditorRawDOMPoint are simple classes which refers
|
|
|
|
* a point in the DOM tree at creating the instance or initializing the
|
|
|
|
* instance with calling Set().
|
|
|
|
*
|
|
|
|
* EditorDOMPoint refers container node (and child node if it's already set)
|
|
|
|
* with nsCOMPtr. EditorRawDOMPoint refers them with raw pointer.
|
|
|
|
* So, EditorRawDOMPoint is useful when you access the nodes only before
|
|
|
|
* changing DOM tree since increasing refcount may appear in micro benchmark
|
|
|
|
* if it's in a hot path. On the other hand, if you need to refer them even
|
|
|
|
* after changing DOM tree, you must use EditorDOMPoint.
|
|
|
|
*
|
|
|
|
* When initializing an instance only with child node or offset, the instance
|
|
|
|
* starts to refer the child node or offset in the container. In this case,
|
|
|
|
* the other information hasn't been initialized due to performance reason.
|
|
|
|
* When you retrieve the other information with calling Offset() or
|
2017-12-07 13:08:56 +03:00
|
|
|
* GetChild(), the other information is computed with the current DOM tree.
|
|
|
|
* Therefore, e.g., in the following case, the other information may be
|
|
|
|
* different:
|
2017-11-22 22:09:00 +03:00
|
|
|
*
|
|
|
|
* EditorDOMPoint pointA(container1, childNode1);
|
|
|
|
* EditorDOMPoint pointB(container1, childNode1);
|
|
|
|
* Unused << pointA.Offset(); // The offset is computed now.
|
|
|
|
* container1->RemoveChild(childNode1->GetPreviousSibling());
|
|
|
|
* Unused << pointB.Offset(); // Now, pointB.Offset() equals pointA.Offset() - 1
|
|
|
|
*
|
|
|
|
* similarly:
|
|
|
|
*
|
|
|
|
* EditorDOMPoint pointA(container1, 5);
|
|
|
|
* EditorDOMPoint pointB(container1, 5);
|
2017-12-07 13:08:56 +03:00
|
|
|
* Unused << pointA.GetChild(); // The child is computed now.
|
2017-11-22 22:09:00 +03:00
|
|
|
* container1->RemoveChild(childNode1->GetFirstChild());
|
2017-12-07 13:08:56 +03:00
|
|
|
* Unused << pointB.GetChild(); // Now, pointB.GetChild() equals
|
|
|
|
* // pointA.GetChild()->GetPreviousSibling().
|
2017-11-22 22:09:00 +03:00
|
|
|
*
|
|
|
|
* So, when you initialize an instance only with one information, you need to
|
|
|
|
* be careful when you access the other information after changing the DOM tree.
|
|
|
|
* When you need to lock the child node or offset and recompute the other
|
|
|
|
* information with new DOM tree, you can use
|
|
|
|
* AutoEditorDOMPointOffsetInvalidator and AutoEditorDOMPointChildInvalidator.
|
|
|
|
*/
|
|
|
|
|
2017-11-01 08:41:03 +03:00
|
|
|
typedef EditorDOMPointBase<nsCOMPtr<nsINode>,
|
|
|
|
nsCOMPtr<nsIContent>> EditorDOMPoint;
|
|
|
|
typedef EditorDOMPointBase<nsINode*, nsIContent*> EditorRawDOMPoint;
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
template<typename ParentType, typename ChildType>
|
2017-11-28 11:13:15 +03:00
|
|
|
class EditorDOMPointBase final
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
2017-11-22 22:09:00 +03:00
|
|
|
typedef EditorDOMPointBase<ParentType, ChildType> SelfType;
|
|
|
|
|
2017-11-01 08:41:03 +03:00
|
|
|
public:
|
|
|
|
EditorDOMPointBase()
|
2017-11-22 18:15:29 +03:00
|
|
|
: mParent(nullptr)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mChild(nullptr)
|
|
|
|
, mIsChildInitialized(false)
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
EditorDOMPointBase(nsINode* aContainer,
|
2017-11-01 08:41:03 +03:00
|
|
|
int32_t aOffset)
|
2017-11-22 18:15:29 +03:00
|
|
|
: mParent(aContainer)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mChild(nullptr)
|
2017-11-22 18:15:29 +03:00
|
|
|
, mOffset(mozilla::Some(aOffset))
|
2017-11-22 22:09:00 +03:00
|
|
|
, mIsChildInitialized(false)
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
2017-11-22 22:09:00 +03:00
|
|
|
NS_WARNING_ASSERTION(!mParent || mOffset.value() <= mParent->Length(),
|
|
|
|
"The offset is larger than the length of aContainer or negative");
|
2017-11-22 18:15:29 +03:00
|
|
|
if (!mParent) {
|
|
|
|
mOffset.reset();
|
|
|
|
}
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorDOMPointBase(nsIDOMNode* aDOMContainer,
|
|
|
|
int32_t aOffset)
|
2017-11-22 22:09:00 +03:00
|
|
|
: mIsChildInitialized(false)
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> container = do_QueryInterface(aDOMContainer);
|
|
|
|
this->Set(container, aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-07 13:50:25 +03:00
|
|
|
* Different from RangeBoundary, aPointedNode should be a child node
|
2017-11-22 22:09:00 +03:00
|
|
|
* which you want to refer.
|
2017-11-01 08:41:03 +03:00
|
|
|
*/
|
2017-11-07 13:50:25 +03:00
|
|
|
explicit EditorDOMPointBase(nsINode* aPointedNode)
|
2017-11-22 18:15:29 +03:00
|
|
|
: mParent(aPointedNode && aPointedNode->IsContent() ?
|
|
|
|
aPointedNode->GetParentNode() : nullptr)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mChild(aPointedNode && aPointedNode->IsContent() ?
|
|
|
|
aPointedNode->AsContent() : nullptr)
|
|
|
|
, mIsChildInitialized(false)
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
2017-11-30 07:21:12 +03:00
|
|
|
mIsChildInitialized = aPointedNode && mChild;
|
2017-11-22 22:09:00 +03:00
|
|
|
NS_WARNING_ASSERTION(IsSet(),
|
|
|
|
"The child is nullptr or doesn't have its parent");
|
|
|
|
NS_WARNING_ASSERTION(mChild && mChild->GetParentNode() == mParent,
|
|
|
|
"Initializing RangeBoundary with invalid value");
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:28:07 +03:00
|
|
|
explicit EditorDOMPointBase(nsIDOMNode* aDOMPointedNode)
|
|
|
|
: mIsChildInitialized(false)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> child = do_QueryInterface(aDOMPointedNode);
|
|
|
|
if (NS_WARN_IF(!child)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->Set(child);
|
|
|
|
}
|
|
|
|
|
2017-11-08 14:23:10 +03:00
|
|
|
EditorDOMPointBase(nsINode* aContainer,
|
2017-11-01 08:41:03 +03:00
|
|
|
nsIContent* aPointedNode,
|
|
|
|
int32_t aOffset)
|
2017-11-22 18:15:29 +03:00
|
|
|
: mParent(aContainer)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mChild(aPointedNode)
|
2017-11-22 18:15:29 +03:00
|
|
|
, mOffset(mozilla::Some(aOffset))
|
2017-11-22 22:09:00 +03:00
|
|
|
, mIsChildInitialized(true)
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
2017-11-22 18:15:29 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aContainer,
|
|
|
|
"This constructor shouldn't be used when pointing nowhere");
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(mOffset.value() <= mParent->Length());
|
|
|
|
MOZ_ASSERT(mChild || mParent->Length() == mOffset.value());
|
|
|
|
MOZ_ASSERT(!mChild || mParent == mChild->GetParentNode());
|
|
|
|
MOZ_ASSERT(mParent->GetChildAt(mOffset.value()) == mChild);
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
template<typename PT, typename CT>
|
|
|
|
explicit EditorDOMPointBase(const RangeBoundaryBase<PT, CT>& aOther)
|
2017-11-22 18:15:29 +03:00
|
|
|
: mParent(aOther.mParent)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mChild(aOther.mRef ? aOther.mRef->GetNextSibling() :
|
|
|
|
(aOther.mParent ? aOther.mParent->GetFirstChild() :
|
|
|
|
nullptr))
|
2017-11-22 18:15:29 +03:00
|
|
|
, mOffset(aOther.mOffset)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mIsChildInitialized(aOther.mRef ||
|
|
|
|
(aOther.mOffset.isSome() && !aOther.mOffset.value()))
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
template<typename PT, typename CT>
|
2017-12-05 09:36:49 +03:00
|
|
|
MOZ_IMPLICIT EditorDOMPointBase(const EditorDOMPointBase<PT, CT>& aOther)
|
2017-11-22 18:15:29 +03:00
|
|
|
: mParent(aOther.mParent)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mChild(aOther.mChild)
|
2017-11-22 18:15:29 +03:00
|
|
|
, mOffset(aOther.mOffset)
|
2017-11-22 22:09:00 +03:00
|
|
|
, mIsChildInitialized(aOther.mIsChildInitialized)
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-07 12:45:52 +03:00
|
|
|
/**
|
|
|
|
* GetContainer() returns the container node at the point.
|
|
|
|
* GetContainerAs*() returns the container node as specific type.
|
|
|
|
*/
|
2017-11-22 18:15:29 +03:00
|
|
|
nsINode*
|
2017-12-07 12:45:52 +03:00
|
|
|
GetContainer() const
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
2017-12-07 12:45:52 +03:00
|
|
|
nsIContent*
|
|
|
|
GetContainerAsContent() const
|
|
|
|
{
|
|
|
|
return mParent && mParent->IsContent() ? mParent->AsContent() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::Element*
|
|
|
|
GetContainerAsElement() const
|
|
|
|
{
|
|
|
|
return mParent && mParent->IsElement() ? mParent->AsElement() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::Text*
|
|
|
|
GetContainerAsText() const
|
|
|
|
{
|
|
|
|
return mParent ? mParent->GetAsText() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDOMNode*
|
|
|
|
GetContainerAsDOMNode() const
|
|
|
|
{
|
|
|
|
return mParent ? mParent->AsDOMNode() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CanContainerHaveChildren() returns true if the container node can have
|
|
|
|
* child nodes. Otherwise, e.g., when the container is a text node, returns
|
|
|
|
* false.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
CanContainerHaveChildren() const
|
|
|
|
{
|
|
|
|
return mParent && mParent->IsContainerNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IsInDataNode() returns true if the container node is a data node including
|
|
|
|
* text node.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
IsInDataNode() const
|
|
|
|
{
|
|
|
|
return mParent && mParent->IsNodeOfType(nsINode::eDATA_NODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IsInTextNode() returns true if the container node is a text node.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
IsInTextNode() const
|
|
|
|
{
|
|
|
|
return mParent && mParent->IsNodeOfType(nsINode::eTEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IsContainerHTMLElement() returns true if the container node is an HTML
|
|
|
|
* element node and its node name is aTag.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
IsContainerHTMLElement(nsAtom* aTag) const
|
|
|
|
{
|
|
|
|
return mParent && mParent->IsHTMLElement(aTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IsContainerAnyOfHTMLElements() returns true if the container node is an
|
|
|
|
* HTML element node and its node name is one of the arguments.
|
|
|
|
*/
|
|
|
|
template<typename First, typename... Args>
|
|
|
|
bool
|
|
|
|
IsContainerAnyOfHTMLElements(First aFirst, Args... aArgs) const
|
|
|
|
{
|
|
|
|
return mParent && mParent->IsAnyOfHTMLElements(aFirst, aArgs...);
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:08:56 +03:00
|
|
|
/**
|
|
|
|
* GetChild() returns a child node which is pointed by the instance.
|
|
|
|
* If mChild hasn't been initialized yet, this computes the child node
|
|
|
|
* from mParent and mOffset with *current* DOM tree.
|
|
|
|
*/
|
2017-11-22 18:15:29 +03:00
|
|
|
nsIContent*
|
2017-12-07 13:08:56 +03:00
|
|
|
GetChild() const
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
|
|
|
if (!mParent || !mParent->IsContainerNode()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mIsChildInitialized) {
|
|
|
|
return mChild;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
// Fix child node now.
|
|
|
|
const_cast<SelfType*>(this)->EnsureChild();
|
|
|
|
return mChild;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-07 13:08:56 +03:00
|
|
|
* GetNextSiblingOfChild() returns next sibling of the child node.
|
2017-11-22 18:15:29 +03:00
|
|
|
* If this refers after the last child or the container cannot have children,
|
|
|
|
* this returns nullptr with warning.
|
2017-12-07 13:08:56 +03:00
|
|
|
* If mChild hasn't been initialized yet, this computes the child node
|
|
|
|
* from mParent and mOffset with *current* DOM tree.
|
2017-11-22 18:15:29 +03:00
|
|
|
*/
|
|
|
|
nsIContent*
|
2017-12-07 13:08:56 +03:00
|
|
|
GetNextSiblingOfChild() const
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mParent) || NS_WARN_IF(!mParent->IsContainerNode())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mIsChildInitialized) {
|
|
|
|
return mChild ? mChild->GetNextSibling() : nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
|
|
|
if (NS_WARN_IF(mOffset.value() > mParent->Length())) {
|
|
|
|
// If this has been set only offset and now the offset is invalid,
|
|
|
|
// let's just return nullptr.
|
2017-11-22 18:15:29 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
// Fix child node now.
|
|
|
|
const_cast<SelfType*>(this)->EnsureChild();
|
|
|
|
return mChild ? mChild->GetNextSibling() : nullptr;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-07 13:08:56 +03:00
|
|
|
* GetPreviousSiblingOfChild() returns previous sibling of a child
|
2017-11-22 18:15:29 +03:00
|
|
|
* at offset. If this refers the first child or the container cannot have
|
|
|
|
* children, this returns nullptr with warning.
|
2017-12-07 13:08:56 +03:00
|
|
|
* If mChild hasn't been initialized yet, this computes the child node
|
|
|
|
* from mParent and mOffset with *current* DOM tree.
|
2017-11-22 18:15:29 +03:00
|
|
|
*/
|
|
|
|
nsIContent*
|
2017-12-07 13:08:56 +03:00
|
|
|
GetPreviousSiblingOfChild() const
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mParent) || NS_WARN_IF(!mParent->IsContainerNode())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mIsChildInitialized) {
|
|
|
|
return mChild ? mChild->GetPreviousSibling() : mParent->GetLastChild();
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
|
|
|
if (NS_WARN_IF(mOffset.value() > mParent->Length())) {
|
|
|
|
// If this has been set only offset and now the offset is invalid,
|
|
|
|
// let's just return nullptr.
|
2017-11-22 18:15:29 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
// Fix child node now.
|
|
|
|
const_cast<SelfType*>(this)->EnsureChild();
|
|
|
|
return mChild ? mChild->GetPreviousSibling() : mParent->GetLastChild();
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
Offset() const
|
|
|
|
{
|
|
|
|
if (mOffset.isSome()) {
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
2017-11-22 18:15:29 +03:00
|
|
|
return mOffset.value();
|
|
|
|
}
|
|
|
|
if (!mParent) {
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(!mChild);
|
2017-11-22 18:15:29 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mParent->IsContainerNode(),
|
|
|
|
"If the container cannot have children, mOffset.isSome() should be true");
|
2017-11-22 22:09:00 +03:00
|
|
|
if (!mChild) {
|
|
|
|
// We're referring after the last child. Fix offset now.
|
|
|
|
const_cast<SelfType*>(this)->mOffset = mozilla::Some(mParent->Length());
|
2017-11-22 18:15:29 +03:00
|
|
|
return mOffset.value();
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(mChild->GetParentNode() == mParent);
|
|
|
|
// Fix offset now.
|
|
|
|
if (mChild == mParent->GetFirstChild()) {
|
|
|
|
const_cast<SelfType*>(this)->mOffset = mozilla::Some(0);
|
|
|
|
} else {
|
|
|
|
const_cast<SelfType*>(this)->mOffset =
|
|
|
|
mozilla::Some(mParent->IndexOf(mChild));
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
return mOffset.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set() sets a point to aOffset or aChild.
|
2017-11-22 22:09:00 +03:00
|
|
|
* If it's set with aOffset, mChild is invalidated. If it's set with aChild,
|
|
|
|
* mOffset may be invalidated.
|
2017-11-22 18:15:29 +03:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Set(nsINode* aContainer, int32_t aOffset)
|
|
|
|
{
|
|
|
|
mParent = aContainer;
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild = nullptr;
|
2017-11-22 18:15:29 +03:00
|
|
|
mOffset = mozilla::Some(aOffset);
|
2017-11-22 22:09:00 +03:00
|
|
|
mIsChildInitialized = false;
|
|
|
|
NS_WARNING_ASSERTION(!mParent || mOffset.value() <= mParent->Length(),
|
|
|
|
"The offset is out of bounds");
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
Set(const nsINode* aChild)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aChild);
|
2017-11-22 22:09:00 +03:00
|
|
|
if (NS_WARN_IF(!aChild->IsContent())) {
|
2017-11-22 18:15:29 +03:00
|
|
|
Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mParent = aChild->GetParentNode();
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild = const_cast<nsIContent*>(aChild->AsContent());
|
|
|
|
mOffset.reset();
|
|
|
|
mIsChildInitialized = true;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
2017-12-07 11:27:20 +03:00
|
|
|
/**
|
|
|
|
* SetToEndOf() sets this to the end of aContainer. Then, mChild is always
|
|
|
|
* nullptr but marked as initialized and mOffset is always set.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
SetToEndOf(const nsINode* aContainer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContainer);
|
|
|
|
mParent = const_cast<nsINode*>(aContainer);
|
|
|
|
mChild = nullptr;
|
|
|
|
mOffset = mozilla::Some(mParent->Length());
|
|
|
|
mIsChildInitialized = true;
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
/**
|
|
|
|
* Clear() makes the instance not point anywhere.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Clear()
|
|
|
|
{
|
|
|
|
mParent = nullptr;
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild = nullptr;
|
2017-11-22 18:15:29 +03:00
|
|
|
mOffset.reset();
|
2017-11-22 22:09:00 +03:00
|
|
|
mIsChildInitialized = false;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-22 22:09:00 +03:00
|
|
|
* AdvanceOffset() tries to refer next sibling of mChild and/of next offset.
|
|
|
|
* If the container can have children and there is no next sibling or the
|
|
|
|
* offset reached the length of the container, this outputs warning and does
|
|
|
|
* nothing. So, callers need to check if there is next sibling which you
|
|
|
|
* need to refer.
|
2017-11-22 18:15:29 +03:00
|
|
|
*
|
2017-11-22 22:09:00 +03:00
|
|
|
* @return true if there is a next DOM point to refer.
|
2017-11-22 18:15:29 +03:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
AdvanceOffset()
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mParent)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
// If only mOffset is available, just compute the offset.
|
|
|
|
if ((mOffset.isSome() && !mIsChildInitialized) ||
|
|
|
|
!mParent->IsContainerNode()) {
|
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
|
|
|
MOZ_ASSERT(!mChild);
|
|
|
|
if (NS_WARN_IF(mOffset.value() >= mParent->Length())) {
|
|
|
|
// We're already referring the start of the container.
|
2017-11-22 18:15:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
mOffset = mozilla::Some(mOffset.value() + 1);
|
2017-11-22 18:15:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(mIsChildInitialized);
|
|
|
|
MOZ_ASSERT(!mOffset.isSome() || mOffset.isSome());
|
|
|
|
if (NS_WARN_IF(!mParent->HasChildren()) ||
|
|
|
|
NS_WARN_IF(!mChild) ||
|
|
|
|
NS_WARN_IF(mOffset.isSome() && mOffset.value() >= mParent->Length())) {
|
|
|
|
// We're already referring the end of the container (or outside).
|
2017-11-22 18:15:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
if (mOffset.isSome()) {
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
2017-11-22 18:15:29 +03:00
|
|
|
mOffset = mozilla::Some(mOffset.value() + 1);
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild = mChild->GetNextSibling();
|
2017-11-22 18:15:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-22 22:09:00 +03:00
|
|
|
* RewindOffset() tries to refer previous sibling of mChild and/or previous
|
|
|
|
* offset. If the container can have children and there is no next previous
|
|
|
|
* or the offset is 0, this outputs warning and does nothing. So, callers
|
|
|
|
* need to check if there is previous sibling which you need to refer.
|
2017-11-22 18:15:29 +03:00
|
|
|
*
|
2017-11-22 22:09:00 +03:00
|
|
|
* @return true if there is a previous DOM point to refer.
|
2017-11-22 18:15:29 +03:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
RewindOffset()
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mParent)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
// If only mOffset is available, just compute the offset.
|
|
|
|
if ((mOffset.isSome() && !mIsChildInitialized) ||
|
|
|
|
!mParent->IsContainerNode()) {
|
2017-11-22 18:15:29 +03:00
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(!mChild);
|
|
|
|
if (NS_WARN_IF(!mOffset.value()) ||
|
|
|
|
NS_WARN_IF(mOffset.value() >= mParent->Length())) {
|
|
|
|
// We're already referring the start of the container.
|
2017-11-22 18:15:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mOffset = mozilla::Some(mOffset.value() - 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(mIsChildInitialized);
|
|
|
|
MOZ_ASSERT(!mOffset.isSome() || mOffset.isSome());
|
|
|
|
if (NS_WARN_IF(!mParent->HasChildren()) ||
|
|
|
|
NS_WARN_IF(mChild && !mChild->GetPreviousSibling()) ||
|
|
|
|
NS_WARN_IF(mOffset.isSome() && !mOffset.value())) {
|
|
|
|
// We're already referring the start of the container (or the child has
|
|
|
|
// been moved from the container?).
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent* previousSibling =
|
|
|
|
mChild ? mChild->GetPreviousSibling() : mParent->GetLastChild();
|
|
|
|
if (NS_WARN_IF(!previousSibling)) {
|
|
|
|
// We're already referring the first child of the container.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
if (mOffset.isSome()) {
|
|
|
|
mOffset = mozilla::Some(mOffset.value() - 1);
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild = previousSibling;
|
2017-11-22 18:15:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsSet() const
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
2017-11-22 22:09:00 +03:00
|
|
|
return mParent && (mIsChildInitialized || mOffset.isSome());
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
bool
|
|
|
|
IsSetAndValid() const
|
2017-11-01 08:41:03 +03:00
|
|
|
{
|
2017-11-22 18:15:29 +03:00
|
|
|
if (!IsSet()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mChild && mChild->GetParentNode() != mParent) {
|
2017-11-22 18:15:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mOffset.isSome() && mOffset.value() > mParent->Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-11-01 08:41:03 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
bool
|
|
|
|
IsStartOfContainer() const
|
|
|
|
{
|
2017-11-22 22:09:00 +03:00
|
|
|
// If we're referring the first point in the container:
|
|
|
|
// If mParent is not a container like a text node, mOffset is 0.
|
|
|
|
// If mChild is initialized and it's first child of mParent.
|
|
|
|
// If mChild isn't initialized and the offset is 0.
|
|
|
|
if (NS_WARN_IF(!mParent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!mParent->IsContainerNode()) {
|
|
|
|
return !mOffset.value();
|
|
|
|
}
|
|
|
|
if (mIsChildInitialized) {
|
2017-11-30 07:21:12 +03:00
|
|
|
if (mParent->GetFirstChild() == mChild) {
|
|
|
|
NS_WARNING_ASSERTION(!mOffset.isSome() || !mOffset.value(),
|
|
|
|
"If mOffset was initialized, it should be 0");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_WARNING_ASSERTION(!mOffset.isSome() ||
|
|
|
|
mParent->GetChildAt(mOffset.value()) == mChild,
|
|
|
|
"If mOffset and mChild are mismatched");
|
|
|
|
return false;
|
2017-11-22 22:09:00 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
|
|
|
return !mOffset.value();
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsEndOfContainer() const
|
|
|
|
{
|
2017-11-22 22:09:00 +03:00
|
|
|
// If we're referring after the last point of the container:
|
|
|
|
// If mParent is not a container like text node, mOffset is same as the
|
|
|
|
// length of the container.
|
|
|
|
// If mChild is initialized and it's nullptr.
|
|
|
|
// If mChild isn't initialized and mOffset is same as the length of the
|
|
|
|
// container.
|
|
|
|
if (NS_WARN_IF(!mParent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mIsChildInitialized) {
|
2017-11-30 07:21:12 +03:00
|
|
|
if (!mChild) {
|
|
|
|
NS_WARNING_ASSERTION(!mOffset.isSome() ||
|
|
|
|
mOffset.value() == mParent->Length(),
|
|
|
|
"If mOffset was initialized, it should be length of the container");
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
NS_WARNING_ASSERTION(!mOffset.isSome() ||
|
2017-11-30 07:21:12 +03:00
|
|
|
mParent->GetChildAt(mOffset.value()) == mChild,
|
|
|
|
"If mOffset and mChild are mismatched");
|
|
|
|
return false;
|
2017-11-22 22:09:00 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
|
|
|
return mOffset.value() == mParent->Length();
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience methods for switching between the two types
|
|
|
|
// of EditorDOMPointBase.
|
2017-11-08 19:00:36 +03:00
|
|
|
EditorDOMPointBase<nsINode*, nsIContent*>
|
|
|
|
AsRaw() const
|
|
|
|
{
|
2017-11-22 18:15:29 +03:00
|
|
|
return EditorRawDOMPoint(*this);
|
2017-11-08 19:00:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
2017-11-22 18:15:29 +03:00
|
|
|
EditorDOMPointBase& operator=(const RangeBoundaryBase<A,B>& aOther)
|
2017-11-08 19:00:36 +03:00
|
|
|
{
|
2017-11-22 18:15:29 +03:00
|
|
|
mParent = aOther.mParent;
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild =
|
|
|
|
aOther.mRef ? aOther.mRef->GetNextSibling() :
|
|
|
|
(aOther.mParent && aOther.mParent->IsContainerNode() ?
|
|
|
|
aOther.mParent->GetFirstChild() : nullptr);
|
2017-11-22 18:15:29 +03:00
|
|
|
mOffset = aOther.mOffset;
|
2017-11-22 22:09:00 +03:00
|
|
|
mIsChildInitialized =
|
|
|
|
aOther.mRef ||
|
|
|
|
(aOther.mParent && !aOther.mParent->IsContainerNode()) ||
|
|
|
|
(aOther.mOffset.isSome() && !aOther.mOffset.value());
|
2017-11-08 19:00:36 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-10 10:10:06 +03:00
|
|
|
template<typename A, typename B>
|
2017-11-22 18:15:29 +03:00
|
|
|
EditorDOMPointBase& operator=(const EditorDOMPointBase<A, B>& aOther)
|
2017-11-10 10:10:06 +03:00
|
|
|
{
|
2017-11-22 18:15:29 +03:00
|
|
|
mParent = aOther.mParent;
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild = aOther.mChild;
|
2017-11-22 18:15:29 +03:00
|
|
|
mOffset = aOther.mOffset;
|
2017-11-22 22:09:00 +03:00
|
|
|
mIsChildInitialized = aOther.mIsChildInitialized;
|
2017-11-10 10:10:06 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:15:29 +03:00
|
|
|
template<typename A, typename B>
|
|
|
|
bool operator==(const EditorDOMPointBase<A, B>& aOther) const
|
|
|
|
{
|
|
|
|
if (mParent != aOther.mParent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOffset.isSome() && aOther.mOffset.isSome()) {
|
|
|
|
// If both mOffset are set, we need to compare both mRef too because
|
|
|
|
// the relation of mRef and mOffset have already broken by DOM tree
|
|
|
|
// changes.
|
|
|
|
if (mOffset != aOther.mOffset) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mChild == aOther.mChild) {
|
2017-11-22 18:15:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
if (NS_WARN_IF(mIsChildInitialized && aOther.mIsChildInitialized)) {
|
|
|
|
// In this case, relation between mChild and mOffset of one of or both
|
|
|
|
// of them doesn't match with current DOM tree since the DOM tree might
|
|
|
|
// have been changed after computing mChild or mOffset.
|
2017-11-22 18:15:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
// If one of mChild hasn't been computed yet, we should compare them only
|
|
|
|
// with mOffset. Perhaps, we shouldn't copy mChild from non-nullptr one
|
|
|
|
// to the other since if we copy it here, it may be unexpected behavior
|
|
|
|
// for some callers.
|
2017-11-22 18:15:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
MOZ_ASSERT(mIsChildInitialized || aOther.mIsChildInitialized);
|
|
|
|
|
|
|
|
if (mOffset.isSome() && !mIsChildInitialized &&
|
|
|
|
!aOther.mOffset.isSome() && aOther.mIsChildInitialized) {
|
|
|
|
// If this has only mOffset and the other has only mChild, this needs to
|
|
|
|
// compute mChild now.
|
|
|
|
const_cast<SelfType*>(this)->EnsureChild();
|
|
|
|
return mChild == aOther.mChild;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
if (!mOffset.isSome() && mIsChildInitialized &&
|
|
|
|
aOther.mOffset.isSome() && !aOther.mIsChildInitialized) {
|
|
|
|
// If this has only mChild and the other has only mOffset, the other needs
|
|
|
|
// to compute mChild now.
|
|
|
|
const_cast<EditorDOMPointBase<A, B>&>(aOther).EnsureChild();
|
|
|
|
return mChild == aOther.mChild;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
// If mOffset of one of them hasn't been computed from mChild yet, we should
|
|
|
|
// compare only with mChild. Perhaps, we shouldn't copy mOffset from being
|
2017-11-22 18:15:29 +03:00
|
|
|
// some one to not being some one since if we copy it here, it may be
|
|
|
|
// unexpected behavior for some callers.
|
2017-11-22 22:09:00 +03:00
|
|
|
return mChild == aOther.mChild;
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
bool operator!=(const EditorDOMPointBase<A, B>& aOther) const
|
|
|
|
{
|
|
|
|
return !(*this == aOther);
|
|
|
|
}
|
|
|
|
|
2017-11-28 11:13:15 +03:00
|
|
|
/**
|
|
|
|
* This operator should be used if API of other modules take RawRangeBoundary,
|
|
|
|
* e.g., methods of Selection and nsRange.
|
|
|
|
*/
|
|
|
|
operator const RawRangeBoundary() const
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
2017-11-22 22:09:00 +03:00
|
|
|
if (!IsSet() ||
|
|
|
|
NS_WARN_IF(!mIsChildInitialized && !mOffset.isSome())) {
|
2017-11-28 11:13:15 +03:00
|
|
|
return RawRangeBoundary();
|
2017-11-22 22:09:00 +03:00
|
|
|
}
|
|
|
|
if (!mParent->IsContainerNode()) {
|
|
|
|
// If the container is a data node like a text node, we need to create
|
|
|
|
// RangeBoundaryBase instance only with mOffset because mChild is always
|
|
|
|
// nullptr.
|
2017-11-28 11:13:15 +03:00
|
|
|
return RawRangeBoundary(mParent, mOffset.value());
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mIsChildInitialized && mOffset.isSome()) {
|
2017-12-04 10:28:50 +03:00
|
|
|
// If we've already set both child and offset, we should create
|
|
|
|
// RangeBoundary with offset after validation.
|
|
|
|
#ifdef DEBUG
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mChild) {
|
2017-12-04 10:28:50 +03:00
|
|
|
MOZ_ASSERT(mParent == mChild->GetParentNode());
|
|
|
|
MOZ_ASSERT(mParent->GetChildAt(mOffset.value()) == mChild);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mParent->Length() == mOffset.value());
|
2017-11-22 22:09:00 +03:00
|
|
|
}
|
2017-12-04 10:28:50 +03:00
|
|
|
#endif // #ifdef DEBUG
|
|
|
|
return RawRangeBoundary(mParent, mOffset.value());
|
2017-11-08 14:23:10 +03:00
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
// Otherwise, we should create RangeBoundaryBase only with available
|
|
|
|
// information.
|
|
|
|
if (mOffset.isSome()) {
|
2017-11-28 11:13:15 +03:00
|
|
|
return RawRangeBoundary(mParent, mOffset.value());
|
2017-11-08 14:23:10 +03:00
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mChild) {
|
2017-11-28 11:13:15 +03:00
|
|
|
return RawRangeBoundary(mParent, mChild->GetPreviousSibling());
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
2017-11-28 11:13:15 +03:00
|
|
|
return RawRangeBoundary(mParent, mParent->GetLastChild());
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
private:
|
2017-11-22 18:15:29 +03:00
|
|
|
void
|
2017-11-22 22:09:00 +03:00
|
|
|
EnsureChild()
|
2017-11-22 18:15:29 +03:00
|
|
|
{
|
2017-11-22 22:09:00 +03:00
|
|
|
if (mIsChildInitialized) {
|
2017-11-22 18:15:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!mParent) {
|
|
|
|
MOZ_ASSERT(!mOffset.isSome());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mOffset.isSome());
|
|
|
|
MOZ_ASSERT(mOffset.value() <= mParent->Length());
|
2017-11-22 22:09:00 +03:00
|
|
|
mIsChildInitialized = true;
|
|
|
|
if (!mParent->IsContainerNode()) {
|
2017-11-22 18:15:29 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-11-22 22:09:00 +03:00
|
|
|
mChild = mParent->GetChildAt(mOffset.value());
|
|
|
|
MOZ_ASSERT(mChild || mOffset.value() == mParent->Length());
|
2017-11-22 18:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ParentType mParent;
|
2017-11-22 22:09:00 +03:00
|
|
|
ChildType mChild;
|
|
|
|
|
|
|
|
mozilla::Maybe<uint32_t> mOffset;
|
2017-11-22 18:15:29 +03:00
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
bool mIsChildInitialized;
|
2017-11-22 18:15:29 +03:00
|
|
|
|
2017-11-22 22:09:00 +03:00
|
|
|
template<typename PT, typename CT>
|
2017-11-22 18:15:29 +03:00
|
|
|
friend class EditorDOMPointBase;
|
2017-11-28 11:13:15 +03:00
|
|
|
|
|
|
|
friend void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback&,
|
|
|
|
EditorDOMPoint&, const char*,
|
|
|
|
uint32_t);
|
|
|
|
friend void ImplCycleCollectionUnlink(EditorDOMPoint&);
|
2017-11-01 08:41:03 +03:00
|
|
|
};
|
|
|
|
|
2017-11-28 11:13:15 +03:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(EditorDOMPoint& aField)
|
|
|
|
{
|
|
|
|
ImplCycleCollectionUnlink(aField.mParent);
|
|
|
|
ImplCycleCollectionUnlink(aField.mChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
EditorDOMPoint& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
ImplCycleCollectionTraverse(aCallback, aField.mParent, "mParent", 0);
|
|
|
|
ImplCycleCollectionTraverse(aCallback, aField.mChild, "mChild", 0);
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:00:56 +03:00
|
|
|
/**
|
|
|
|
* AutoEditorDOMPointOffsetInvalidator is useful if DOM tree will be changed
|
|
|
|
* when EditorDOMPoint instance is available and keeps referring same child
|
|
|
|
* node.
|
|
|
|
*
|
|
|
|
* This class automatically guarantees that given EditorDOMPoint instance
|
|
|
|
* stores the child node and invalidates its offset when the instance is
|
|
|
|
* destroyed. Additionally, users of this class can invalidate the offset
|
|
|
|
* manually when they need.
|
|
|
|
*/
|
|
|
|
class MOZ_STACK_CLASS AutoEditorDOMPointOffsetInvalidator final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AutoEditorDOMPointOffsetInvalidator(EditorDOMPoint& aPoint)
|
|
|
|
: mPoint(aPoint)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPoint.IsSetAndValid());
|
2017-12-07 12:45:52 +03:00
|
|
|
MOZ_ASSERT(mPoint.CanContainerHaveChildren());
|
2017-12-07 13:08:56 +03:00
|
|
|
mChild = mPoint.GetChild();
|
2017-11-17 11:00:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoEditorDOMPointOffsetInvalidator()
|
|
|
|
{
|
|
|
|
InvalidateOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manually, invalidate offset of the given point.
|
|
|
|
*/
|
|
|
|
void InvalidateOffset()
|
|
|
|
{
|
|
|
|
if (mChild) {
|
|
|
|
mPoint.Set(mChild);
|
|
|
|
} else {
|
|
|
|
// If the point referred after the last child, let's keep referring
|
|
|
|
// after current last node of the old container.
|
2017-12-07 12:45:52 +03:00
|
|
|
mPoint.SetToEndOf(mPoint.GetContainer());
|
2017-11-17 11:00:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EditorDOMPoint& mPoint;
|
|
|
|
// Needs to store child node by ourselves because EditorDOMPoint stores
|
|
|
|
// child node with mRef which is previous sibling of current child node.
|
|
|
|
// Therefore, we cannot keep referring it if it's first child.
|
|
|
|
nsCOMPtr<nsIContent> mChild;
|
|
|
|
|
|
|
|
AutoEditorDOMPointOffsetInvalidator() = delete;
|
|
|
|
AutoEditorDOMPointOffsetInvalidator(
|
|
|
|
const AutoEditorDOMPointOffsetInvalidator& aOther) = delete;
|
|
|
|
const AutoEditorDOMPointOffsetInvalidator& operator=(
|
|
|
|
const AutoEditorDOMPointOffsetInvalidator& aOther) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AutoEditorDOMPointChildInvalidator is useful if DOM tree will be changed
|
|
|
|
* when EditorDOMPoint instance is available and keeps referring same container
|
|
|
|
* and offset in it.
|
|
|
|
*
|
|
|
|
* This class automatically guarantees that given EditorDOMPoint instance
|
|
|
|
* stores offset and invalidates its child node when the instance is destroyed.
|
|
|
|
* Additionally, users of this class can invalidate the child manually when
|
|
|
|
* they need.
|
|
|
|
*/
|
|
|
|
class MOZ_STACK_CLASS AutoEditorDOMPointChildInvalidator final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AutoEditorDOMPointChildInvalidator(EditorDOMPoint& aPoint)
|
|
|
|
: mPoint(aPoint)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPoint.IsSetAndValid());
|
|
|
|
Unused << mPoint.Offset();
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoEditorDOMPointChildInvalidator()
|
|
|
|
{
|
|
|
|
InvalidateChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manually, invalidate child of the given point.
|
|
|
|
*/
|
|
|
|
void InvalidateChild()
|
|
|
|
{
|
2017-12-07 12:45:52 +03:00
|
|
|
mPoint.Set(mPoint.GetContainer(), mPoint.Offset());
|
2017-11-17 11:00:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EditorDOMPoint& mPoint;
|
|
|
|
|
|
|
|
AutoEditorDOMPointChildInvalidator() = delete;
|
|
|
|
AutoEditorDOMPointChildInvalidator(
|
|
|
|
const AutoEditorDOMPointChildInvalidator& aOther) = delete;
|
|
|
|
const AutoEditorDOMPointChildInvalidator& operator=(
|
|
|
|
const AutoEditorDOMPointChildInvalidator& aOther) = delete;
|
|
|
|
};
|
|
|
|
|
2017-11-01 08:41:03 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // #ifndef mozilla_EditorDOMPoint_h
|