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 "DeleteNodeTxn.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "DeleteRangeTxn.h"
|
|
|
|
#include "DeleteTextTxn.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Selection.h"
|
|
|
|
#include "mozilla/mozalloc.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsEditor.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIContent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIContentIterator.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsAString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
class nsIDOMRange;
|
2012-06-06 03:34:46 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// note that aEditor is not refcounted
|
|
|
|
DeleteRangeTxn::DeleteRangeTxn()
|
2012-06-25 03:22:49 -07:00
|
|
|
: EditAggregateTxn(),
|
|
|
|
mRange(),
|
2012-07-30 07:20:58 -07:00
|
|
|
mEditor(nullptr),
|
|
|
|
mRangeUpdater(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:23:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(DeleteRangeTxn, EditAggregateTxn,
|
|
|
|
mRange)
|
2009-05-08 21:59:25 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteRangeTxn)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditAggregateTxn)
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsresult
|
|
|
|
DeleteRangeTxn::Init(nsEditor* aEditor,
|
|
|
|
nsRange* aRange,
|
|
|
|
nsRangeUpdater* aRangeUpdater)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-25 03:22:49 -07:00
|
|
|
MOZ_ASSERT(aEditor && aRange);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mEditor = aEditor;
|
2012-06-25 04:44:40 -07:00
|
|
|
mRange = aRange->CloneRange();
|
2007-03-22 10:30:00 -07:00
|
|
|
mRangeUpdater = aRangeUpdater;
|
2007-06-27 19:48:16 -07:00
|
|
|
|
2012-06-25 04:44:40 -07:00
|
|
|
NS_ENSURE_TRUE(mEditor->IsModifiableNode(mRange->GetStartParent()),
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
NS_ENSURE_TRUE(mEditor->IsModifiableNode(mRange->GetEndParent()),
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
NS_ENSURE_TRUE(mEditor->IsModifiableNode(mRange->GetCommonAncestor()),
|
|
|
|
NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-06 00:41:35 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteRangeTxn::DoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-25 04:44:40 -07:00
|
|
|
MOZ_ASSERT(mRange && mEditor);
|
2012-06-25 03:22:49 -07:00
|
|
|
nsresult res;
|
2012-06-25 04:44:40 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// build the child transactions
|
2012-06-25 04:44:40 -07:00
|
|
|
nsCOMPtr<nsINode> startParent = mRange->GetStartParent();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startOffset = mRange->StartOffset();
|
2012-06-25 04:44:40 -07:00
|
|
|
nsCOMPtr<nsINode> endParent = mRange->GetEndParent();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t endOffset = mRange->EndOffset();
|
2012-06-25 04:44:40 -07:00
|
|
|
MOZ_ASSERT(startParent && endParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-25 04:44:40 -07:00
|
|
|
if (startParent == endParent) {
|
2012-06-25 03:22:49 -07:00
|
|
|
// the selection begins and ends in the same node
|
2012-06-25 04:44:40 -07:00
|
|
|
res = CreateTxnsToDeleteBetween(startParent, startOffset, endOffset);
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
} else {
|
|
|
|
// the selection ends in a different node from where it started. delete
|
|
|
|
// the relevant content in the start node
|
2012-06-25 04:44:40 -07:00
|
|
|
res = CreateTxnsToDeleteContent(startParent, startOffset, nsIEditor::eNext);
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
// delete the intervening nodes
|
|
|
|
res = CreateTxnsToDeleteNodesBetween();
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
// delete the relevant content in the end node
|
2012-06-25 04:44:40 -07:00
|
|
|
res = CreateTxnsToDeleteContent(endParent, endOffset, nsIEditor::ePrevious);
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we've successfully built this aggregate transaction, then do it.
|
2012-06-25 03:22:49 -07:00
|
|
|
res = EditAggregateTxn::DoTransaction();
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// only set selection to deletion point if editor gives permission
|
2011-09-28 23:19:26 -07:00
|
|
|
bool bAdjustSelection;
|
2007-03-22 10:30:00 -07:00
|
|
|
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
|
2012-06-25 03:22:49 -07:00
|
|
|
if (bAdjustSelection) {
|
|
|
|
nsRefPtr<Selection> selection = mEditor->GetSelection();
|
2010-06-17 12:41:16 -07:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-06-25 04:44:40 -07:00
|
|
|
res = selection->Collapse(startParent, startOffset);
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-25 03:22:49 -07:00
|
|
|
// else do nothing - dom range gravity will adjust selection
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteRangeTxn::UndoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-25 04:44:40 -07:00
|
|
|
MOZ_ASSERT(mRange && mEditor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return EditAggregateTxn::UndoTransaction();
|
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteRangeTxn::RedoTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-25 04:44:40 -07:00
|
|
|
MOZ_ASSERT(mRange && mEditor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return EditAggregateTxn::RedoTransaction();
|
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteRangeTxn::GetTxnDescription(nsAString& aString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aString.AssignLiteral("DeleteRangeTxn");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsresult
|
|
|
|
DeleteRangeTxn::CreateTxnsToDeleteBetween(nsINode* aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// see what kind of node we have
|
2012-06-25 03:22:49 -07:00
|
|
|
if (aNode->IsNodeOfType(nsINode::eDATA_NODE)) {
|
|
|
|
// if the node is a chardata node, then delete chardata content
|
2009-04-24 15:45:34 -07:00
|
|
|
nsRefPtr<DeleteTextTxn> txn = new DeleteTextTxn();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numToDel;
|
2012-06-25 03:22:49 -07:00
|
|
|
if (aStartOffset == aEndOffset) {
|
2007-03-22 10:30:00 -07:00
|
|
|
numToDel = 1;
|
2012-06-25 03:22:49 -07:00
|
|
|
} else {
|
|
|
|
numToDel = aEndOffset - aStartOffset;
|
|
|
|
}
|
2012-06-06 00:41:41 -07:00
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> charDataNode = do_QueryInterface(aNode);
|
|
|
|
nsresult res = txn->Init(mEditor, charDataNode, aStartOffset, numToDel,
|
|
|
|
mRangeUpdater);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-06-06 00:41:41 -07:00
|
|
|
|
|
|
|
AppendChild(txn);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsCOMPtr<nsIContent> child = aNode->GetChildAt(aStartOffset);
|
2012-06-06 00:41:41 -07:00
|
|
|
NS_ENSURE_STATE(child);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsresult res = NS_OK;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = aStartOffset; i < aEndOffset; ++i) {
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<DeleteNodeTxn> txn = new DeleteNodeTxn();
|
2012-06-19 06:23:36 -07:00
|
|
|
res = txn->Init(mEditor, child, mRangeUpdater);
|
2012-06-25 03:22:49 -07:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
2012-06-06 00:41:41 -07:00
|
|
|
AppendChild(txn);
|
2012-06-25 03:22:49 -07:00
|
|
|
}
|
2012-06-06 00:41:41 -07:00
|
|
|
|
|
|
|
child = child->GetNextSibling();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-25 03:22:49 -07:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsresult
|
|
|
|
DeleteRangeTxn::CreateTxnsToDeleteContent(nsINode* aNode,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aOffset,
|
2012-06-25 03:22:49 -07:00
|
|
|
nsIEditor::EDirection aAction)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// see what kind of node we have
|
2012-06-25 03:22:49 -07:00
|
|
|
if (aNode->IsNodeOfType(nsINode::eDATA_NODE)) {
|
|
|
|
// if the node is a chardata node, then delete chardata content
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t start, numToDelete;
|
2012-06-25 03:22:49 -07:00
|
|
|
if (nsIEditor::eNext == aAction) {
|
|
|
|
start = aOffset;
|
|
|
|
numToDelete = aNode->Length() - aOffset;
|
|
|
|
} else {
|
|
|
|
start = 0;
|
|
|
|
numToDelete = aOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-25 03:22:49 -07:00
|
|
|
|
|
|
|
if (numToDelete) {
|
2009-04-24 15:45:34 -07:00
|
|
|
nsRefPtr<DeleteTextTxn> txn = new DeleteTextTxn();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> charDataNode = do_QueryInterface(aNode);
|
|
|
|
nsresult res = txn->Init(mEditor, charDataNode, start, numToDelete,
|
|
|
|
mRangeUpdater);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
AppendChild(txn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsresult
|
|
|
|
DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-25 03:22:49 -07:00
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewContentSubtreeIterator();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
nsresult res = iter->Init(mRange);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-25 03:22:49 -07:00
|
|
|
while (!iter->IsDone()) {
|
|
|
|
nsCOMPtr<nsINode> node = iter->GetCurrentNode();
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsRefPtr<DeleteNodeTxn> txn = new DeleteNodeTxn();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
res = txn->Init(mEditor, node, mRangeUpdater);
|
2012-06-25 03:22:49 -07:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
AppendChild(txn);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
iter->Next();
|
|
|
|
}
|
2012-06-25 03:22:49 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|