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