1999-01-14 21:02:45 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-01-14 21:02:45 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-01-14 21:02:45 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-01-14 21:02:45 +03:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-01-11 23:49:15 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-01-14 21:02:45 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DeleteRangeTxn.h"
|
|
|
|
#include "nsIDOMRange.h"
|
1999-01-21 04:51:09 +03:00
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
2000-09-14 15:45:01 +04:00
|
|
|
#include "nsISelection.h"
|
1999-01-21 04:51:09 +03:00
|
|
|
#include "DeleteTextTxn.h"
|
|
|
|
#include "DeleteElementTxn.h"
|
|
|
|
#include "TransactionFactory.h"
|
1999-03-09 22:22:48 +03:00
|
|
|
#include "nsIContentIterator.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsLayoutCID.h"
|
1999-01-21 04:51:09 +03:00
|
|
|
|
1999-01-21 05:29:52 +03:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#endif
|
|
|
|
|
2000-10-29 02:17:53 +04:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
static PRBool gNoisy = PR_FALSE;
|
|
|
|
#else
|
|
|
|
static const PRBool gNoisy = PR_FALSE;
|
|
|
|
#endif
|
1999-01-14 21:02:45 +03:00
|
|
|
|
|
|
|
// note that aEditor is not refcounted
|
1999-01-21 04:51:09 +03:00
|
|
|
DeleteRangeTxn::DeleteRangeTxn()
|
|
|
|
: EditAggregateTxn()
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTxn::Init(nsIEditor *aEditor, nsIDOMRange *aRange)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
1999-08-19 17:30:48 +04:00
|
|
|
NS_ASSERTION(aEditor && aRange, "bad state");
|
|
|
|
if (!aEditor || !aRange) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
mEditor = aEditor;
|
|
|
|
mRange = do_QueryInterface(aRange);
|
|
|
|
|
2000-08-24 07:54:30 +04:00
|
|
|
nsresult result = aRange->GetStartContainer(getter_AddRefs(mStartParent));
|
1999-08-19 17:30:48 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartParent failed.");
|
2000-08-24 07:54:30 +04:00
|
|
|
result = aRange->GetEndContainer(getter_AddRefs(mEndParent));
|
1999-08-19 17:30:48 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndParent failed.");
|
|
|
|
result = aRange->GetStartOffset(&mStartOffset);
|
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartOffset failed.");
|
|
|
|
result = aRange->GetEndOffset(&mEndOffset);
|
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndOffset failed.");
|
2000-08-24 07:54:30 +04:00
|
|
|
result = aRange->GetCommonAncestorContainer(getter_AddRefs(mCommonParent));
|
1999-08-19 17:30:48 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "GetCommonParent failed.");
|
1999-01-21 04:51:09 +03:00
|
|
|
|
|
|
|
#ifdef NS_DEBUG
|
1999-02-17 22:42:29 +03:00
|
|
|
{
|
1999-01-21 04:51:09 +03:00
|
|
|
PRUint32 count;
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> textNode;
|
1999-04-04 22:01:35 +04:00
|
|
|
textNode = do_QueryInterface(mStartParent);
|
1999-01-21 04:51:09 +03:00
|
|
|
if (textNode)
|
|
|
|
textNode->GetLength(&count);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNodeList> children;
|
|
|
|
result = mStartParent->GetChildNodes(getter_AddRefs(children));
|
|
|
|
NS_ASSERTION(((NS_SUCCEEDED(result)) && children), "bad start child list");
|
|
|
|
children->GetLength(&count);
|
|
|
|
}
|
1999-03-06 00:05:35 +03:00
|
|
|
NS_ASSERTION(mStartOffset<=(PRInt32)count, "bad start offset");
|
1999-01-21 04:51:09 +03:00
|
|
|
|
1999-04-04 22:01:35 +04:00
|
|
|
textNode = do_QueryInterface(mEndParent);
|
1999-01-21 04:51:09 +03:00
|
|
|
if (textNode)
|
|
|
|
textNode->GetLength(&count);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNodeList> children;
|
|
|
|
result = mEndParent->GetChildNodes(getter_AddRefs(children));
|
|
|
|
NS_ASSERTION(((NS_SUCCEEDED(result)) && children), "bad end child list");
|
|
|
|
children->GetLength(&count);
|
|
|
|
}
|
1999-03-06 00:05:35 +03:00
|
|
|
NS_ASSERTION(mEndOffset<=(PRInt32)count, "bad end offset");
|
2000-10-29 02:17:53 +04:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf ("DeleteRange: %d of %p to %d of %p\n",
|
|
|
|
mStartOffset, (void *)mStartParent, mEndOffset, (void *)mEndParent);
|
|
|
|
}
|
1999-02-17 22:42:29 +03:00
|
|
|
}
|
1999-01-21 04:51:09 +03:00
|
|
|
#endif
|
1999-08-19 17:30:48 +04:00
|
|
|
return result;
|
|
|
|
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DeleteRangeTxn::~DeleteRangeTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTxn::DoTransaction(void)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2000-10-29 02:17:53 +04:00
|
|
|
if (gNoisy) { printf("Do Delete Range\n"); }
|
1999-08-19 17:30:48 +04:00
|
|
|
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
|
2000-01-04 06:09:41 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1999-01-14 21:02:45 +03:00
|
|
|
|
|
|
|
nsresult result;
|
1999-01-21 04:51:09 +03:00
|
|
|
// build the child transactions
|
|
|
|
|
|
|
|
if (mStartParent==mEndParent)
|
|
|
|
{ // the selection begins and ends in the same node
|
|
|
|
result = CreateTxnsToDeleteBetween(mStartParent, mStartOffset, mEndOffset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // the selection ends in a different node from where it started
|
|
|
|
// delete the relevant content in the start node
|
1999-12-07 11:30:19 +03:00
|
|
|
result = CreateTxnsToDeleteContent(mStartParent, mStartOffset, nsIEditor::eNext);
|
1999-01-21 04:51:09 +03:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
// delete the intervening nodes
|
1999-03-09 22:22:48 +03:00
|
|
|
result = CreateTxnsToDeleteNodesBetween();
|
1999-01-21 04:51:09 +03:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
// delete the relevant content in the end node
|
1999-12-07 11:30:19 +03:00
|
|
|
result = CreateTxnsToDeleteContent(mEndParent, mEndOffset, nsIEditor::ePrevious);
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we've successfully built this aggregate transaction, then do it.
|
1999-02-17 22:42:29 +03:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2001-03-09 17:23:59 +03:00
|
|
|
result = EditAggregateTxn::DoTransaction();
|
1999-02-17 22:42:29 +03:00
|
|
|
}
|
|
|
|
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
|
|
|
// only set selection to deletion point if editor gives permission
|
|
|
|
PRBool bAdjustSelection;
|
|
|
|
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
|
|
|
|
if (bAdjustSelection)
|
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
1999-02-17 22:42:29 +03:00
|
|
|
result = mEditor->GetSelection(getter_AddRefs(selection));
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
result = selection->Collapse(mStartParent, mStartOffset);
|
1999-02-17 22:42:29 +03:00
|
|
|
}
|
2000-01-04 06:09:41 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing - dom range gravity will adjust selection
|
|
|
|
}
|
1999-01-21 04:51:09 +03:00
|
|
|
|
1999-01-14 21:02:45 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTxn::UndoTransaction(void)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2000-10-29 02:17:53 +04:00
|
|
|
if (gNoisy) { printf("Undo Delete Range\n"); }
|
1999-08-19 17:30:48 +04:00
|
|
|
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
|
2000-01-04 06:09:41 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
nsresult result = EditAggregateTxn::UndoTransaction();
|
1999-01-14 21:02:45 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTxn::RedoTransaction(void)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2000-10-29 02:17:53 +04:00
|
|
|
if (gNoisy) { printf("Redo Delete Range\n"); }
|
1999-08-19 17:30:48 +04:00
|
|
|
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
|
2000-01-04 06:09:41 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1999-01-14 21:02:45 +03:00
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
nsresult result = EditAggregateTxn::RedoTransaction();
|
1999-01-14 21:02:45 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTxn::Merge(nsITransaction *aTransaction, PRBool *aDidMerge)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
|
|
|
if (nsnull!=aDidMerge)
|
|
|
|
*aDidMerge=PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTxn::GetTxnDescription(nsAWritableString& aString)
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2001-03-09 17:23:59 +03:00
|
|
|
aString.Assign(NS_LITERAL_STRING("DeleteRangeTxn"));
|
1999-01-14 21:02:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-01-21 04:51:09 +03:00
|
|
|
|
1999-08-19 17:30:48 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent,
|
|
|
|
PRUint32 aStartOffset,
|
|
|
|
PRUint32 aEndOffset)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
// see what kind of node we have
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> textNode;
|
1999-03-03 01:27:46 +03:00
|
|
|
textNode = do_QueryInterface(aStartParent);
|
1999-01-21 04:51:09 +03:00
|
|
|
if (textNode)
|
|
|
|
{ // if the node is a text node, then delete text content
|
|
|
|
DeleteTextTxn *txn;
|
1999-07-15 23:13:46 +04:00
|
|
|
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!txn) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
PRInt32 numToDel;
|
|
|
|
if (aStartOffset==aEndOffset)
|
|
|
|
numToDel = 1;
|
|
|
|
else
|
|
|
|
numToDel = aEndOffset-aStartOffset;
|
|
|
|
txn->Init(mEditor, textNode, aStartOffset, numToDel);
|
|
|
|
AppendChild(txn);
|
2000-02-01 08:12:25 +03:00
|
|
|
NS_RELEASE(txn);
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PRUint32 childCount;
|
|
|
|
nsCOMPtr<nsIDOMNodeList> children;
|
|
|
|
result = aStartParent->GetChildNodes(getter_AddRefs(children));
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!children) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
children->GetLength(&childCount);
|
|
|
|
NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
|
|
|
|
PRUint32 i;
|
|
|
|
for (i=aStartOffset; i<aEndOffset; i++)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
1999-08-19 17:30:48 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
result = children->Item(i, getter_AddRefs(child));
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!child) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
DeleteElementTxn *txn;
|
|
|
|
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!txn) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
txn->Init(child);
|
|
|
|
AppendChild(txn);
|
2000-02-01 08:12:25 +03:00
|
|
|
NS_RELEASE(txn);
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteContent(nsIDOMNode *aParent,
|
1999-03-02 10:52:41 +03:00
|
|
|
PRUint32 aOffset,
|
1999-12-07 11:30:19 +03:00
|
|
|
nsIEditor::EDirection aAction)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
2000-11-10 17:53:40 +03:00
|
|
|
nsresult result = NS_OK;
|
1999-01-21 04:51:09 +03:00
|
|
|
// see what kind of node we have
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> textNode;
|
1999-03-03 01:27:46 +03:00
|
|
|
textNode = do_QueryInterface(aParent);
|
1999-01-21 04:51:09 +03:00
|
|
|
if (textNode)
|
|
|
|
{ // if the node is a text node, then delete text content
|
|
|
|
PRUint32 start, numToDelete;
|
1999-12-07 11:30:19 +03:00
|
|
|
if (nsIEditor::eNext == aAction)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
start=aOffset;
|
|
|
|
textNode->GetLength(&numToDelete);
|
1999-01-22 22:44:46 +03:00
|
|
|
numToDelete -= aOffset;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start=0;
|
|
|
|
numToDelete=aOffset;
|
|
|
|
}
|
1999-03-09 22:22:48 +03:00
|
|
|
|
|
|
|
if (numToDelete)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
1999-03-09 22:22:48 +03:00
|
|
|
DeleteTextTxn *txn;
|
1999-07-15 23:13:46 +04:00
|
|
|
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!txn) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
txn->Init(mEditor, textNode, start, numToDelete);
|
|
|
|
AppendChild(txn);
|
2000-03-23 04:14:49 +03:00
|
|
|
NS_RELEASE(txn);
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-03-09 22:22:48 +03:00
|
|
|
static NS_DEFINE_IID(kSubtreeIteratorCID, NS_SUBTREEITERATOR_CID);
|
|
|
|
static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID);
|
|
|
|
|
|
|
|
NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
1999-03-09 22:22:48 +03:00
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
1999-01-21 04:51:09 +03:00
|
|
|
|
1999-03-09 23:11:27 +03:00
|
|
|
result = nsComponentManager::CreateInstance(kSubtreeIteratorCID,
|
1999-03-09 22:22:48 +03:00
|
|
|
nsnull,
|
2000-01-11 23:49:15 +03:00
|
|
|
NS_GET_IID(nsIContentIterator),
|
1999-03-09 22:22:48 +03:00
|
|
|
getter_AddRefs(iter));
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!iter) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
1999-03-09 22:22:48 +03:00
|
|
|
result = iter->Init(mRange);
|
1999-08-19 17:30:48 +04:00
|
|
|
if (NS_FAILED(result)) return result;
|
1999-03-09 22:22:48 +03:00
|
|
|
|
1999-10-28 07:16:48 +04:00
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
1999-03-09 22:22:48 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
result = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
node = do_QueryInterface(content);
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!node) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
DeleteElementTxn *txn;
|
|
|
|
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
|
2000-01-04 06:09:41 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!txn) return NS_ERROR_NULL_POINTER;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
txn->Init(node);
|
|
|
|
AppendChild(txn);
|
2000-03-23 04:14:49 +03:00
|
|
|
NS_RELEASE(txn);
|
1999-03-09 22:22:48 +03:00
|
|
|
iter->Next();
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|