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
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
#include "DeleteRangeTransaction.h"
|
2014-12-02 08:07:42 +03:00
|
|
|
|
2016-07-07 09:08:33 +03:00
|
|
|
#include "DeleteNodeTransaction.h"
|
2016-07-07 09:20:22 +03:00
|
|
|
#include "DeleteTextTransaction.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2016-07-08 07:10:13 +03:00
|
|
|
#include "mozilla/EditorBase.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"
|
2017-11-08 09:01:23 +03:00
|
|
|
#include "mozilla/RangeBoundary.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.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 "nsINode.h"
|
|
|
|
#include "nsAString.h"
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2012-06-06 14:34:46 +04:00
|
|
|
|
2017-03-09 05:19:39 +03:00
|
|
|
DeleteRangeTransaction::DeleteRangeTransaction(EditorBase& aEditorBase,
|
2017-12-18 12:08:43 +03:00
|
|
|
nsRange& aRangeToDelete)
|
2017-03-21 13:00:36 +03:00
|
|
|
: mEditorBase(&aEditorBase)
|
2017-03-09 05:19:39 +03:00
|
|
|
, mRangeToDelete(aRangeToDelete.CloneRange())
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 11:11:30 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteRangeTransaction,
|
|
|
|
EditAggregateTransaction,
|
2017-03-21 13:00:36 +03:00
|
|
|
mEditorBase,
|
2017-03-09 05:19:39 +03:00
|
|
|
mRangeToDelete)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-07 08:56:53 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteRangeTransaction)
|
2016-07-07 11:11:30 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditAggregateTransaction)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2012-06-25 14:22:49 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 08:56:53 +03:00
|
|
|
DeleteRangeTransaction::DoTransaction()
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mRangeToDelete)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2012-06-25 15:44:40 +04:00
|
|
|
|
2017-03-24 23:07:11 +03:00
|
|
|
// Swap mRangeToDelete out into a stack variable, so we make sure to null it
|
|
|
|
// out on return from this function. Once this function returns, we no longer
|
|
|
|
// need mRangeToDelete, and keeping it alive in the long term slows down all
|
|
|
|
// DOM mutations because it's observing them.
|
|
|
|
RefPtr<nsRange> rangeToDelete;
|
|
|
|
rangeToDelete.swap(mRangeToDelete);
|
|
|
|
|
1999-01-21 04:51:09 +03:00
|
|
|
// build the child transactions
|
2017-11-08 09:01:23 +03:00
|
|
|
const RangeBoundary& startRef = rangeToDelete->StartRef();
|
|
|
|
const RangeBoundary& endRef = rangeToDelete->EndRef();
|
|
|
|
MOZ_ASSERT(startRef.IsSetAndValid());
|
|
|
|
MOZ_ASSERT(endRef.IsSetAndValid());
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2017-11-08 09:01:23 +03:00
|
|
|
if (startRef.Container() == endRef.Container()) {
|
2012-06-25 14:22:49 +04:00
|
|
|
// the selection begins and ends in the same node
|
2017-11-08 09:01:23 +03:00
|
|
|
nsresult rv = CreateTxnsToDeleteBetween(startRef.AsRaw(), endRef.AsRaw());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-06-25 14:22:49 +04:00
|
|
|
} else {
|
|
|
|
// the selection ends in a different node from where it started. delete
|
|
|
|
// the relevant content in the start node
|
2017-11-08 09:01:23 +03:00
|
|
|
nsresult rv = CreateTxnsToDeleteContent(startRef.AsRaw(), nsIEditor::eNext);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-06-25 14:22:49 +04:00
|
|
|
// delete the intervening nodes
|
2017-03-24 23:07:11 +03:00
|
|
|
rv = CreateTxnsToDeleteNodesBetween(rangeToDelete);
|
2017-11-08 09:01:23 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-06-25 14:22:49 +04:00
|
|
|
// delete the relevant content in the end node
|
2017-11-08 09:01:23 +03:00
|
|
|
rv = CreateTxnsToDeleteContent(endRef.AsRaw(), nsIEditor::ePrevious);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we've successfully built this aggregate transaction, then do it.
|
2016-10-19 12:09:33 +03:00
|
|
|
nsresult rv = EditAggregateTransaction::DoTransaction();
|
2017-11-08 09:01:23 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
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
|
2018-08-01 14:16:30 +03:00
|
|
|
if (mEditorBase->GetShouldTxnSetSelection()) {
|
2017-03-21 13:00:36 +03:00
|
|
|
RefPtr<Selection> selection = mEditorBase->GetSelection();
|
2017-11-08 09:01:23 +03:00
|
|
|
if (NS_WARN_IF(!selection)) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
rv = selection->Collapse(startRef.AsRaw());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
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
|
2016-07-07 08:56:53 +03:00
|
|
|
DeleteRangeTransaction::UndoTransaction()
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2016-07-07 11:11:30 +03:00
|
|
|
return EditAggregateTransaction::UndoTransaction();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2012-06-25 14:22:49 +04:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 08:56:53 +03:00
|
|
|
DeleteRangeTransaction::RedoTransaction()
|
1999-01-14 21:02:45 +03:00
|
|
|
{
|
2016-07-07 11:11:30 +03:00
|
|
|
return EditAggregateTransaction::RedoTransaction();
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2012-06-25 14:22:49 +04:00
|
|
|
nsresult
|
2017-11-08 09:01:23 +03:00
|
|
|
DeleteRangeTransaction::CreateTxnsToDeleteBetween(
|
|
|
|
const RawRangeBoundary& aStart,
|
|
|
|
const RawRangeBoundary& aEnd)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
2017-11-08 09:01:23 +03:00
|
|
|
if (NS_WARN_IF(!aStart.IsSetAndValid()) ||
|
|
|
|
NS_WARN_IF(!aEnd.IsSetAndValid()) ||
|
|
|
|
NS_WARN_IF(aStart.Container() != aEnd.Container())) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
1999-01-21 04:51:09 +03:00
|
|
|
// see what kind of node we have
|
2018-04-15 13:46:24 +03:00
|
|
|
if (RefPtr<CharacterData> charDataNode =
|
|
|
|
CharacterData::FromNode(aStart.Container())) {
|
2012-06-25 14:22:49 +04:00
|
|
|
// if the node is a chardata node, then delete chardata content
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numToDel;
|
2017-11-08 09:01:23 +03:00
|
|
|
if (aStart == aEnd) {
|
1999-08-19 17:30:48 +04:00
|
|
|
numToDel = 1;
|
2012-06-25 14:22:49 +04:00
|
|
|
} else {
|
2017-11-08 09:01:23 +03:00
|
|
|
numToDel = aEnd.Offset() - aStart.Offset();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(numToDel > 0);
|
2012-06-25 14:22:49 +04:00
|
|
|
}
|
2012-06-06 11:41:41 +04:00
|
|
|
|
2017-03-10 07:38:28 +03:00
|
|
|
RefPtr<DeleteTextTransaction> deleteTextTransaction =
|
2017-12-15 14:43:26 +03:00
|
|
|
DeleteTextTransaction::MaybeCreate(*mEditorBase, *charDataNode,
|
|
|
|
aStart.Offset(), numToDel);
|
2017-03-10 07:38:28 +03:00
|
|
|
// If the text node isn't editable, it should be never undone/redone.
|
|
|
|
// So, the transaction shouldn't be recorded.
|
2017-12-15 14:43:26 +03:00
|
|
|
if (NS_WARN_IF(!deleteTextTransaction)) {
|
2017-03-10 07:38:28 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
AppendChild(deleteTextTransaction);
|
2012-06-06 11:41:41 +04:00
|
|
|
return NS_OK;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2017-11-08 09:01:23 +03:00
|
|
|
// Even if we detect invalid range, we should ignore it for removing
|
|
|
|
// specified range's nodes as far as possible.
|
|
|
|
for (nsIContent* child = aStart.GetChildAtOffset();
|
|
|
|
child && child != aEnd.GetChildAtOffset();
|
|
|
|
child = child->GetNextSibling()) {
|
2017-03-10 07:23:40 +03:00
|
|
|
RefPtr<DeleteNodeTransaction> deleteNodeTransaction =
|
2017-12-15 15:24:33 +03:00
|
|
|
DeleteNodeTransaction::MaybeCreate(*mEditorBase, *child);
|
2017-03-10 07:23:40 +03:00
|
|
|
// XXX This is odd handling. Even if some children are not editable,
|
|
|
|
// editor should append transactions because they could be editable
|
|
|
|
// at undoing/redoing. Additionally, if the transaction needs to
|
|
|
|
// delete/restore all nodes, it should at undoing/redoing.
|
2017-12-15 15:24:33 +03:00
|
|
|
if (deleteNodeTransaction) {
|
2017-03-10 07:23:40 +03:00
|
|
|
AppendChild(deleteNodeTransaction);
|
2012-06-25 14:22:49 +04:00
|
|
|
}
|
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
|
2017-11-08 09:01:23 +03:00
|
|
|
DeleteRangeTransaction::CreateTxnsToDeleteContent(
|
|
|
|
const RawRangeBoundary& aPoint,
|
|
|
|
nsIEditor::EDirection aAction)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
2017-11-08 09:01:23 +03:00
|
|
|
if (NS_WARN_IF(!aPoint.IsSetAndValid())) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2018-04-15 13:46:24 +03:00
|
|
|
RefPtr<CharacterData> dataNode = CharacterData::FromNode(aPoint.Container());
|
|
|
|
if (!dataNode) {
|
2017-11-08 09:01:23 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-25 14:22:49 +04:00
|
|
|
|
2017-11-08 09:01:23 +03:00
|
|
|
// If the node is a chardata node, then delete chardata content
|
|
|
|
uint32_t startOffset, numToDelete;
|
|
|
|
if (nsIEditor::eNext == aAction) {
|
|
|
|
startOffset = aPoint.Offset();
|
|
|
|
numToDelete = aPoint.Container()->Length() - aPoint.Offset();
|
|
|
|
} else {
|
|
|
|
startOffset = 0;
|
|
|
|
numToDelete = aPoint.Offset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!numToDelete) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DeleteTextTransaction> deleteTextTransaction =
|
2017-12-15 14:43:26 +03:00
|
|
|
DeleteTextTransaction::MaybeCreate(*mEditorBase, *dataNode,
|
|
|
|
startOffset, numToDelete);
|
2017-11-08 09:01:23 +03:00
|
|
|
// If the text node isn't editable, it should be never undone/redone.
|
|
|
|
// So, the transaction shouldn't be recorded.
|
2017-12-15 14:43:26 +03:00
|
|
|
if (NS_WARN_IF(!deleteTextTransaction)) {
|
2017-11-08 09:01:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
2017-11-08 09:01:23 +03:00
|
|
|
AppendChild(deleteTextTransaction);
|
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
|
2017-03-24 23:07:11 +03:00
|
|
|
DeleteRangeTransaction::CreateTxnsToDeleteNodesBetween(nsRange* aRangeToDelete)
|
1999-01-21 04:51:09 +03:00
|
|
|
{
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:22:49 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewContentSubtreeIterator();
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2017-03-24 23:07:11 +03:00
|
|
|
nsresult rv = iter->Init(aRangeToDelete);
|
2016-10-19 12:09:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-25 14:22:49 +04:00
|
|
|
while (!iter->IsDone()) {
|
|
|
|
nsCOMPtr<nsINode> node = iter->GetCurrentNode();
|
2017-03-10 07:23:40 +03:00
|
|
|
if (NS_WARN_IF(!node)) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2017-03-10 07:23:40 +03:00
|
|
|
RefPtr<DeleteNodeTransaction> deleteNodeTransaction =
|
2017-12-15 15:24:33 +03:00
|
|
|
DeleteNodeTransaction::MaybeCreate(*mEditorBase, *node);
|
2017-03-10 07:23:40 +03:00
|
|
|
// XXX This is odd handling. Even if some nodes in the range are not
|
|
|
|
// editable, editor should append transactions because they could
|
|
|
|
// at undoing/redoing. Additionally, if the transaction needs to
|
|
|
|
// delete/restore all nodes, it should at undoing/redoing.
|
2017-12-15 15:24:33 +03:00
|
|
|
if (NS_WARN_IF(!deleteNodeTransaction)) {
|
2017-03-10 07:23:40 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
AppendChild(deleteNodeTransaction);
|
2012-06-25 14:22:49 +04:00
|
|
|
|
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
|
|
|
}
|
2016-07-07 08:56:53 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|