2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
|
2014-04-10 09:09:40 -07:00
|
|
|
#include "mozilla/dom/Selection.h" // for Selection
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsAString.h" // for nsAString_internal::Length
|
|
|
|
#include "nsAutoPtr.h" // for nsRefPtr, getter_AddRefs, etc
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsDebug.h" // for NS_ENSURE_TRUE, etc
|
|
|
|
#include "nsEditor.h" // for nsEditor
|
|
|
|
#include "nsEditorUtils.h" // for nsEditorUtils
|
|
|
|
#include "nsError.h" // for NS_OK, etc
|
2014-04-28 08:34:05 -07:00
|
|
|
#include "nsIContent.h" // for nsIContent
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
|
|
|
|
#include "nsIDOMNode.h" // for nsIDOMNode
|
|
|
|
#include "nsISupportsImpl.h" // for nsRange::Release
|
|
|
|
#include "nsRange.h" // for nsRange
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsSelectionState.h"
|
|
|
|
|
2012-07-23 03:27:22 -07:00
|
|
|
using namespace mozilla;
|
2014-04-10 09:09:40 -07:00
|
|
|
using namespace mozilla::dom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* class for recording selection info. stores selection as collection of
|
|
|
|
* { {startnode, startoffset} , {endnode, endoffset} } tuples. Can't store
|
|
|
|
* ranges since dom gravity will possibly change the ranges.
|
|
|
|
*/
|
|
|
|
nsSelectionState::nsSelectionState() : mArray(){}
|
|
|
|
|
|
|
|
nsSelectionState::~nsSelectionState()
|
|
|
|
{
|
|
|
|
MakeEmpty();
|
|
|
|
}
|
|
|
|
|
2009-05-08 21:59:25 -07:00
|
|
|
void
|
|
|
|
nsSelectionState::DoTraverse(nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0, iEnd = mArray.Length(); i < iEnd; ++i)
|
2009-05-08 21:59:25 -07:00
|
|
|
{
|
2012-07-12 23:31:15 -07:00
|
|
|
nsRangeStore* item = mArray[i];
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
|
|
|
|
"selection state mArray[i].startNode");
|
2012-07-12 23:31:15 -07:00
|
|
|
cb.NoteXPCOMChild(item->startNode);
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
|
|
|
|
"selection state mArray[i].endNode");
|
2012-07-12 23:31:15 -07:00
|
|
|
cb.NoteXPCOMChild(item->endNode);
|
2009-05-08 21:59:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 03:27:22 -07:00
|
|
|
void
|
|
|
|
nsSelectionState::SaveSelection(Selection* aSel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-23 03:27:22 -07:00
|
|
|
MOZ_ASSERT(aSel);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t arrayCount = mArray.Length();
|
|
|
|
int32_t rangeCount = aSel->GetRangeCount();
|
2012-07-23 03:27:22 -07:00
|
|
|
|
2009-06-16 05:38:51 -07:00
|
|
|
// if we need more items in the array, new them
|
2012-07-23 03:27:22 -07:00
|
|
|
if (arrayCount < rangeCount) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = arrayCount; i < rangeCount; i++) {
|
2009-06-16 05:38:51 -07:00
|
|
|
mArray.AppendElement();
|
2012-07-12 23:31:15 -07:00
|
|
|
mArray[i] = new nsRangeStore();
|
2009-06-16 05:38:51 -07:00
|
|
|
}
|
2012-07-23 03:27:22 -07:00
|
|
|
} else if (arrayCount > rangeCount) {
|
|
|
|
// else if we have too many, delete them
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = arrayCount - 1; i >= rangeCount; i--) {
|
2009-06-16 05:38:51 -07:00
|
|
|
mArray.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
2012-07-23 03:27:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// now store the selection ranges
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < rangeCount; i++) {
|
2012-07-23 03:27:22 -07:00
|
|
|
mArray[i]->StoreRange(aSel->GetRangeAt(i));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-11-02 04:04:13 -08:00
|
|
|
nsSelectionState::RestoreSelection(Selection* aSel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aSel, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult res;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, arrayCount = mArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// clear out selection
|
|
|
|
aSel->RemoveAllRanges();
|
|
|
|
|
|
|
|
// set the selection ranges anew
|
|
|
|
for (i=0; i<arrayCount; i++)
|
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRefPtr<nsRange> range = mArray[i]->GetRange();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_UNEXPECTED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
res = aSel->AddRange(range);
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSelectionState::IsCollapsed()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
if (1 != mArray.Length()) return false;
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRefPtr<nsRange> range = mArray[0]->GetRange();
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(range, false);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bIsCollapsed = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
range->GetCollapsed(&bIsCollapsed);
|
|
|
|
return bIsCollapsed;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSelectionState::IsEqual(nsSelectionState *aSelState)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(aSelState, false);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, myCount = mArray.Length(), itsCount = aSelState->mArray.Length();
|
2011-10-17 07:59:28 -07:00
|
|
|
if (myCount != itsCount) return false;
|
|
|
|
if (myCount < 1) return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (i=0; i<myCount; i++)
|
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRefPtr<nsRange> myRange = mArray[i]->GetRange();
|
|
|
|
nsRefPtr<nsRange> itsRange = aSelState->mArray[i]->GetRange();
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_TRUE(myRange && itsRange, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t compResult;
|
2010-08-05 19:13:07 -07:00
|
|
|
nsresult rv;
|
|
|
|
rv = myRange->CompareBoundaryPoints(nsIDOMRange::START_TO_START, itsRange, &compResult);
|
2011-10-17 07:59:28 -07:00
|
|
|
if (NS_FAILED(rv) || compResult) return false;
|
2010-08-05 19:13:07 -07:00
|
|
|
rv = myRange->CompareBoundaryPoints(nsIDOMRange::END_TO_END, itsRange, &compResult);
|
2011-10-17 07:59:28 -07:00
|
|
|
if (NS_FAILED(rv) || compResult) return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// if we got here, they are equal
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSelectionState::MakeEmpty()
|
|
|
|
{
|
|
|
|
// free any items in the array
|
|
|
|
mArray.Clear();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSelectionState::IsEmpty()
|
|
|
|
{
|
2009-04-22 01:43:15 -07:00
|
|
|
return mArray.IsEmpty();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************
|
2014-11-02 04:04:13 -08:00
|
|
|
* nsRangeUpdater: class for updating nsRanges in response to editor actions.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsRangeUpdater::nsRangeUpdater() : mArray(), mLock(false) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRangeUpdater::~nsRangeUpdater()
|
|
|
|
{
|
|
|
|
// nothing to do, we don't own the items in our array.
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRangeUpdater::RegisterRangeItem(nsRangeStore *aRangeItem)
|
|
|
|
{
|
|
|
|
if (!aRangeItem) return;
|
2009-04-22 01:43:15 -07:00
|
|
|
if (mArray.Contains(aRangeItem))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ERROR("tried to register an already registered range");
|
|
|
|
return; // don't register it again. It would get doubly adjusted.
|
|
|
|
}
|
|
|
|
mArray.AppendElement(aRangeItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRangeUpdater::DropRangeItem(nsRangeStore *aRangeItem)
|
|
|
|
{
|
|
|
|
if (!aRangeItem) return;
|
|
|
|
mArray.RemoveElement(aRangeItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::RegisterSelectionState(nsSelectionState &aSelState)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, theCount = aSelState.mArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (theCount < 1) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
for (i=0; i<theCount; i++)
|
|
|
|
{
|
2012-07-12 23:31:15 -07:00
|
|
|
RegisterRangeItem(aSelState.mArray[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::DropSelectionState(nsSelectionState &aSelState)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, theCount = aSelState.mArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (theCount < 1) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
for (i=0; i<theCount; i++)
|
|
|
|
{
|
2012-07-12 23:31:15 -07:00
|
|
|
DropRangeItem(aSelState.mArray[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gravity methods:
|
|
|
|
|
|
|
|
nsresult
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRangeUpdater::SelAdjCreateNode(nsINode* aParent, int32_t aPosition)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
if (mLock) {
|
|
|
|
// lock set by Will/DidReplaceParent, etc...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aParent, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
uint32_t count = mArray.Length();
|
2010-06-28 16:15:27 -07:00
|
|
|
if (!count) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
|
|
|
|
if (item->startNode == aParent && item->startOffset > aPosition) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset++;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
|
|
|
if (item->endNode == aParent && item->endOffset > aPosition) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset++;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::SelAdjCreateNode(nsIDOMNode* aParent, int32_t aPosition)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
|
|
|
|
return SelAdjCreateNode(parent, aPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::SelAdjInsertNode(nsINode* aParent, int32_t aPosition)
|
|
|
|
{
|
|
|
|
return SelAdjCreateNode(aParent, aPosition);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsRangeUpdater::SelAdjInsertNode(nsIDOMNode *aParent, int32_t aPosition)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return SelAdjCreateNode(aParent, aPosition);
|
|
|
|
}
|
|
|
|
|
2012-07-02 05:07:57 -07:00
|
|
|
void
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRangeUpdater::SelAdjDeleteNode(nsINode* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-02 05:07:57 -07:00
|
|
|
if (mLock) {
|
|
|
|
// lock set by Will/DidReplaceParent, etc...
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(aNode);
|
2014-04-28 08:34:05 -07:00
|
|
|
uint32_t count = mArray.Length();
|
2010-06-29 11:14:54 -07:00
|
|
|
if (!count) {
|
2012-07-02 05:07:57 -07:00
|
|
|
return;
|
2010-06-29 11:14:54 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
nsCOMPtr<nsINode> parent = aNode->GetParentNode();
|
|
|
|
int32_t offset = parent ? parent->IndexOf(aNode) : -1;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// check for range endpoints that are after aNode and in the same parent
|
2014-04-28 08:34:05 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2012-07-02 05:07:57 -07:00
|
|
|
MOZ_ASSERT(item);
|
2014-04-28 08:34:05 -07:00
|
|
|
|
|
|
|
if (item->startNode == parent && item->startOffset > offset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset--;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
|
|
|
if (item->endNode == parent && item->endOffset > offset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset--;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// check for range endpoints that are in aNode
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startNode == aNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startNode = parent;
|
|
|
|
item->startOffset = offset;
|
|
|
|
}
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endNode == aNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endNode = parent;
|
|
|
|
item->endOffset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for range endpoints that are in descendants of aNode
|
2014-04-28 08:34:05 -07:00
|
|
|
nsCOMPtr<nsINode> oldStart;
|
|
|
|
if (nsEditorUtils::IsDescendantOf(item->startNode, aNode)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
oldStart = item->startNode; // save for efficiency hack below.
|
|
|
|
item->startNode = parent;
|
|
|
|
item->startOffset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// avoid having to call IsDescendantOf() for common case of range startnode == range endnode.
|
|
|
|
if ((item->endNode == oldStart) || nsEditorUtils::IsDescendantOf(item->endNode, aNode))
|
|
|
|
{
|
|
|
|
item->endNode = parent;
|
|
|
|
item->endOffset = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
void
|
|
|
|
nsRangeUpdater::SelAdjDeleteNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, );
|
|
|
|
return SelAdjDeleteNode(node);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
2014-11-02 04:04:13 -08:00
|
|
|
nsRangeUpdater::SelAdjSplitNode(nsIContent& aOldRightNode, int32_t aOffset,
|
|
|
|
nsIContent* aNewLeftNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
if (mLock) {
|
|
|
|
// lock set by Will/DidReplaceParent, etc...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-11-02 04:04:13 -08:00
|
|
|
NS_ENSURE_TRUE(aNewLeftNode, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
uint32_t count = mArray.Length();
|
2010-06-29 11:14:54 -07:00
|
|
|
if (!count) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
nsCOMPtr<nsINode> parent = aOldRightNode.GetParentNode();
|
|
|
|
int32_t offset = parent ? parent->IndexOf(&aOldRightNode) : -1;
|
2014-04-28 08:34:05 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// first part is same as inserting aNewLeftnode
|
2014-04-28 08:34:05 -07:00
|
|
|
nsresult result = SelAdjInsertNode(parent, offset - 1);
|
2010-06-17 12:27:24 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// next step is to check for range enpoints inside aOldRightNode
|
2014-04-28 08:34:05 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
|
2014-11-02 04:04:13 -08:00
|
|
|
if (item->startNode == &aOldRightNode) {
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset -= aOffset;
|
2014-04-28 08:34:05 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startNode = aNewLeftNode;
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 04:04:13 -08:00
|
|
|
if (item->endNode == &aOldRightNode) {
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset -= aOffset;
|
2014-04-28 08:34:05 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endNode = aNewLeftNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-11-02 04:04:12 -08:00
|
|
|
nsRangeUpdater::SelAdjJoinNodes(nsINode& aLeftNode,
|
|
|
|
nsINode& aRightNode,
|
|
|
|
nsINode& aParent,
|
2014-04-28 08:34:05 -07:00
|
|
|
int32_t aOffset,
|
|
|
|
int32_t aOldLeftNodeLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
if (mLock) {
|
|
|
|
// lock set by Will/DidReplaceParent, etc...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
uint32_t count = mArray.Length();
|
2010-06-29 11:14:54 -07:00
|
|
|
if (!count) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
|
2014-11-02 04:04:12 -08:00
|
|
|
if (item->startNode == &aParent) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// adjust start point in aParent
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset--;
|
2014-04-28 08:34:05 -07:00
|
|
|
} else if (item->startOffset == aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// join keeps right hand node
|
2014-11-02 04:04:12 -08:00
|
|
|
item->startNode = &aRightNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset = aOldLeftNodeLength;
|
|
|
|
}
|
2014-11-02 04:04:12 -08:00
|
|
|
} else if (item->startNode == &aRightNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// adjust start point in aRightNode
|
|
|
|
item->startOffset += aOldLeftNodeLength;
|
2014-11-02 04:04:12 -08:00
|
|
|
} else if (item->startNode == &aLeftNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// adjust start point in aLeftNode
|
2014-11-02 04:04:12 -08:00
|
|
|
item->startNode = &aRightNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-11-02 04:04:12 -08:00
|
|
|
if (item->endNode == &aParent) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// adjust end point in aParent
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset--;
|
2014-04-28 08:34:05 -07:00
|
|
|
} else if (item->endOffset == aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// join keeps right hand node
|
2014-11-02 04:04:12 -08:00
|
|
|
item->endNode = &aRightNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset = aOldLeftNodeLength;
|
|
|
|
}
|
2014-11-02 04:04:12 -08:00
|
|
|
} else if (item->endNode == &aRightNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// adjust end point in aRightNode
|
|
|
|
item->endOffset += aOldLeftNodeLength;
|
2014-11-02 04:04:12 -08:00
|
|
|
} else if (item->endNode == &aLeftNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// adjust end point in aLeftNode
|
2014-11-02 04:04:12 -08:00
|
|
|
item->endNode = &aRightNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-28 08:34:05 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-29 04:43:23 -07:00
|
|
|
void
|
|
|
|
nsRangeUpdater::SelAdjInsertText(Text& aTextNode, int32_t aOffset,
|
|
|
|
const nsAString& aString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
if (mLock) {
|
|
|
|
// lock set by Will/DidReplaceParent, etc...
|
2014-08-29 04:43:23 -07:00
|
|
|
return;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mArray.Length();
|
2010-06-29 11:14:54 -07:00
|
|
|
if (!count) {
|
2014-08-29 04:43:23 -07:00
|
|
|
return;
|
2010-06-29 11:14:54 -07:00
|
|
|
}
|
2014-04-28 08:34:05 -07:00
|
|
|
|
|
|
|
uint32_t len = aString.Length();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2014-08-29 04:43:23 -07:00
|
|
|
MOZ_ASSERT(item);
|
2014-04-28 08:34:05 -07:00
|
|
|
|
2014-08-29 04:43:23 -07:00
|
|
|
if (item->startNode == &aTextNode && item->startOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset += len;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
2014-08-29 04:43:23 -07:00
|
|
|
if (item->endNode == &aTextNode && item->endOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset += len;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-08-29 04:43:23 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRangeUpdater::SelAdjDeleteText(nsIContent* aTextNode, int32_t aOffset,
|
|
|
|
int32_t aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
if (mLock) {
|
|
|
|
// lock set by Will/DidReplaceParent, etc...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
uint32_t count = mArray.Length();
|
2010-06-29 11:14:54 -07:00
|
|
|
if (!count) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-04-28 08:34:05 -07:00
|
|
|
NS_ENSURE_TRUE(aTextNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
|
|
|
|
if (item->startNode == aTextNode && item->startOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset -= aLength;
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startOffset < 0) {
|
|
|
|
item->startOffset = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endNode == aTextNode && item->endOffset > aOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset -= aLength;
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endOffset < 0) {
|
|
|
|
item->endOffset = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::SelAdjDeleteText(nsIDOMCharacterData* aTextNode,
|
|
|
|
int32_t aOffset, int32_t aLength)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> textNode = do_QueryInterface(aTextNode);
|
|
|
|
return SelAdjDeleteText(textNode, aOffset, aLength);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::WillReplaceContainer()
|
|
|
|
{
|
|
|
|
if (mLock) return NS_ERROR_UNEXPECTED;
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2014-08-20 05:25:16 -07:00
|
|
|
nsRangeUpdater::DidReplaceContainer(Element* aOriginalNode, Element* aNewNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
NS_ENSURE_TRUE(mLock, NS_ERROR_UNEXPECTED);
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aOriginalNode && aNewNode, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
uint32_t count = mArray.Length();
|
2010-06-29 11:14:54 -07:00
|
|
|
if (!count) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startNode == aOriginalNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startNode = aNewNode;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
|
|
|
if (item->endNode == aOriginalNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endNode = aNewNode;
|
2014-04-28 08:34:05 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::WillRemoveContainer()
|
|
|
|
{
|
|
|
|
if (mLock) return NS_ERROR_UNEXPECTED;
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRangeUpdater::DidRemoveContainer(nsINode* aNode, nsINode* aParent,
|
|
|
|
int32_t aOffset, uint32_t aNodeOrigLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
NS_ENSURE_TRUE(mLock, NS_ERROR_UNEXPECTED);
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(aNode && aParent, NS_ERROR_NULL_POINTER);
|
2014-04-28 08:34:05 -07:00
|
|
|
uint32_t count = mArray.Length();
|
2010-06-29 11:14:54 -07:00
|
|
|
if (!count) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(item, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startNode == aNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startNode = aParent;
|
|
|
|
item->startOffset += aOffset;
|
2014-04-28 08:34:05 -07:00
|
|
|
} else if (item->startNode == aParent && item->startOffset > aOffset) {
|
|
|
|
item->startOffset += (int32_t)aNodeOrigLen - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endNode == aNode) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endNode = aParent;
|
|
|
|
item->endOffset += aOffset;
|
2014-04-28 08:34:05 -07:00
|
|
|
} else if (item->endNode == aParent && item->endOffset > aOffset) {
|
|
|
|
item->endOffset += (int32_t)aNodeOrigLen - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::DidRemoveContainer(nsIDOMNode* aNode, nsIDOMNode* aParent,
|
|
|
|
int32_t aOffset, uint32_t aNodeOrigLen)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
|
|
|
|
return DidRemoveContainer(node, parent, aOffset, aNodeOrigLen);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::WillInsertContainer()
|
|
|
|
{
|
|
|
|
if (mLock) return NS_ERROR_UNEXPECTED;
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRangeUpdater::DidInsertContainer()
|
|
|
|
{
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(mLock, NS_ERROR_UNEXPECTED);
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRangeUpdater::WillMoveNode()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
void
|
|
|
|
nsRangeUpdater::DidMoveNode(nsINode* aOldParent, int32_t aOldOffset,
|
|
|
|
nsINode* aNewParent, int32_t aNewOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-10 03:03:38 -07:00
|
|
|
MOZ_ASSERT(aOldParent);
|
|
|
|
MOZ_ASSERT(aNewParent);
|
|
|
|
NS_ENSURE_TRUE_VOID(mLock);
|
2011-10-17 07:59:28 -07:00
|
|
|
mLock = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-10 03:03:38 -07:00
|
|
|
for (uint32_t i = 0, count = mArray.Length(); i < count; ++i) {
|
|
|
|
nsRangeStore* item = mArray[i];
|
|
|
|
NS_ENSURE_TRUE_VOID(item);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// like a delete in aOldParent
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startNode == aOldParent && item->startOffset > aOldOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset--;
|
2013-07-10 03:03:38 -07:00
|
|
|
}
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endNode == aOldParent && item->endOffset > aOldOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset--;
|
2013-07-10 03:03:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// and like an insert in aNewParent
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->startNode == aNewParent && item->startOffset > aNewOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->startOffset++;
|
2013-07-10 03:03:38 -07:00
|
|
|
}
|
2014-04-28 08:34:05 -07:00
|
|
|
if (item->endNode == aNewParent && item->endOffset > aNewOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
item->endOffset++;
|
2013-07-10 03:03:38 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* helper class for nsSelectionState. nsRangeStore stores range endpoints.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsRangeStore::nsRangeStore()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
nsRangeStore::~nsRangeStore()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
void
|
|
|
|
nsRangeStore::StoreRange(nsRange* aRange)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
MOZ_ASSERT(aRange);
|
|
|
|
startNode = aRange->GetStartParent();
|
|
|
|
startOffset = aRange->StartOffset();
|
|
|
|
endNode = aRange->GetEndParent();
|
|
|
|
endOffset = aRange->EndOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-28 08:34:05 -07:00
|
|
|
already_AddRefed<nsRange>
|
|
|
|
nsRangeStore::GetRange()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-28 08:34:05 -07:00
|
|
|
nsRefPtr<nsRange> range = new nsRange(startNode);
|
|
|
|
nsresult res = range->Set(startNode, startOffset, endNode, endOffset);
|
|
|
|
NS_ENSURE_SUCCESS(res, nullptr);
|
|
|
|
return range.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|