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"
|
2019-01-11 04:49:00 +03:00
|
|
|
#include "mozilla/ContentIterator.h"
|
2019-01-10 12:42:27 +03:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2019-01-11 04:49:00 +03:00
|
|
|
#include "mozilla/EditorBase.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"
|
|
|
|
#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,
|
2020-05-03 02:44:46 +03:00
|
|
|
const nsRange& aRangeToDelete)
|
2017-03-09 05:19:39 +03:00
|
|
|
: mEditorBase(&aEditorBase), mRangeToDelete(aRangeToDelete.CloneRange()) {}
|
1999-01-21 04:51:09 +03:00
|
|
|
|
2016-07-07 11:11:30 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteRangeTransaction,
|
|
|
|
EditAggregateTransaction, 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
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTransaction::DoTransaction() {
|
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());
|
2020-03-09 17:57:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteBetween() failed");
|
2017-11-08 09:01:23 +03:00
|
|
|
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);
|
2020-03-09 17:57:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteContent() failed");
|
2017-11-08 09:01:23 +03:00
|
|
|
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);
|
2020-03-09 17:57:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"DeleteRangeTransaction::CreateTxnsToDeleteNodesBetween() failed");
|
2017-11-08 09:01:23 +03:00
|
|
|
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);
|
2020-03-09 17:57:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteContent() failed");
|
2017-11-08 09:01:23 +03:00
|
|
|
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();
|
2020-03-09 17:57:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("EditAggregateTransaction::DoTransaction() failed");
|
2017-11-08 09:01:23 +03:00
|
|
|
return rv;
|
|
|
|
}
|
1999-02-17 22:42:29 +03:00
|
|
|
|
2018-08-01 14:53:57 +03:00
|
|
|
if (!mEditorBase->AllowsTransactionsToChangeSelection()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Selection> selection = mEditorBase->GetSelection();
|
|
|
|
if (NS_WARN_IF(!selection)) {
|
2020-03-09 17:57:23 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2018-08-01 14:53:57 +03:00
|
|
|
}
|
|
|
|
rv = selection->Collapse(startRef.AsRaw());
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Selection::Collapsed() failed");
|
|
|
|
return rv;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTransaction::UndoTransaction() {
|
|
|
|
nsresult rv = EditAggregateTransaction::UndoTransaction();
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"EditAggregateTransaction::UndoTransaction() failed");
|
|
|
|
return rv;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_IMETHODIMP DeleteRangeTransaction::RedoTransaction() {
|
|
|
|
nsresult rv = EditAggregateTransaction::RedoTransaction();
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"EditAggregateTransaction::RedoTransaction() failed");
|
|
|
|
return rv;
|
1999-01-14 21:02:45 +03:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:01:23 +03:00
|
|
|
nsresult DeleteRangeTransaction::CreateTxnsToDeleteBetween(
|
|
|
|
const RawRangeBoundary& aStart, const RawRangeBoundary& aEnd) {
|
|
|
|
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
|
2020-04-03 11:30:37 +03:00
|
|
|
if (Text* textNode = Text::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 {
|
2019-12-16 14:31:50 +03:00
|
|
|
numToDel = *aEnd.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets) -
|
|
|
|
*aStart.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets);
|
2017-11-08 09:01:23 +03:00
|
|
|
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 =
|
2019-12-16 14:31:50 +03:00
|
|
|
DeleteTextTransaction::MaybeCreate(
|
|
|
|
*mEditorBase, *textNode,
|
|
|
|
*aStart.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets),
|
|
|
|
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.
|
2020-03-09 17:57:23 +03:00
|
|
|
if (!deleteTextTransaction) {
|
|
|
|
NS_WARNING("DeleteTextTransaction::MaybeCreate() failed");
|
2017-03-10 07:38:28 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2020-03-09 17:57:23 +03:00
|
|
|
DebugOnly<nsresult> rvIgnored = AppendChild(deleteTextTransaction);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rvIgnored),
|
|
|
|
"DeleteRangeTransaction::AppendChild() failed, but ignored");
|
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.
|
2020-03-09 17:57:23 +03:00
|
|
|
// XXX This is super expensive. Probably, we should make
|
|
|
|
// DeleteNodeTransaction() can treat multiple siblings.
|
2017-11-08 09:01:23 +03:00
|
|
|
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) {
|
2020-03-09 17:57:23 +03:00
|
|
|
DebugOnly<nsresult> rvIgnored = AppendChild(deleteNodeTransaction);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rvIgnored),
|
|
|
|
"DeleteRangeTransaction::AppendChild() failed, but ignored");
|
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
|
|
|
}
|
|
|
|
|
2017-11-08 09:01:23 +03:00
|
|
|
nsresult DeleteRangeTransaction::CreateTxnsToDeleteContent(
|
|
|
|
const RawRangeBoundary& aPoint, nsIEditor::EDirection aAction) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-03 11:30:37 +03:00
|
|
|
Text* textNode = Text::FromNode(aPoint.Container());
|
2019-07-22 06:53:29 +03:00
|
|
|
if (!textNode) {
|
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) {
|
2019-12-16 14:31:50 +03:00
|
|
|
startOffset = *aPoint.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets);
|
|
|
|
numToDelete = aPoint.Container()->Length() - startOffset;
|
2017-11-08 09:01:23 +03:00
|
|
|
} else {
|
|
|
|
startOffset = 0;
|
2019-12-16 14:31:50 +03:00
|
|
|
numToDelete = *aPoint.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets);
|
2017-11-08 09:01:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!numToDelete) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DeleteTextTransaction> deleteTextTransaction =
|
2019-07-22 06:53:29 +03:00
|
|
|
DeleteTextTransaction::MaybeCreate(*mEditorBase, *textNode, startOffset,
|
2017-12-15 14:43:26 +03:00
|
|
|
numToDelete);
|
2020-03-09 17:57:23 +03:00
|
|
|
NS_WARNING_ASSERTION(deleteTextTransaction,
|
|
|
|
"DeleteTextTransaction::MaybeCreate() failed");
|
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.
|
2020-03-09 17:57:23 +03:00
|
|
|
if (!deleteTextTransaction) {
|
2017-11-08 09:01:23 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
2020-03-09 17:57:23 +03:00
|
|
|
DebugOnly<nsresult> rvIgnored = AppendChild(deleteTextTransaction);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rvIgnored),
|
|
|
|
"DeleteRangeTransaction::AppendChild() failed, but ignored");
|
2012-06-25 14:22:49 +04:00
|
|
|
return NS_OK;
|
1999-01-21 04:51:09 +03:00
|
|
|
}
|
|
|
|
|
2017-03-24 23:07:11 +03:00
|
|
|
nsresult DeleteRangeTransaction::CreateTxnsToDeleteNodesBetween(
|
|
|
|
nsRange* aRangeToDelete) {
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:52:26 +03:00
|
|
|
ContentSubtreeIterator subtreeIter;
|
|
|
|
nsresult rv = subtreeIter.Init(aRangeToDelete);
|
2020-03-09 17:57:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("ContentSubtreeIterator::Init() failed");
|
|
|
|
return rv;
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2020-03-09 17:57:23 +03:00
|
|
|
for (; !subtreeIter.IsDone(); subtreeIter.Next()) {
|
2020-04-03 11:30:37 +03:00
|
|
|
nsINode* node = subtreeIter.GetCurrentNode();
|
|
|
|
if (NS_WARN_IF(!node) || NS_WARN_IF(!node->IsContent())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2017-03-10 07:23:40 +03:00
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2017-03-10 07:23:40 +03:00
|
|
|
RefPtr<DeleteNodeTransaction> deleteNodeTransaction =
|
2020-04-03 11:30:37 +03:00
|
|
|
DeleteNodeTransaction::MaybeCreate(*mEditorBase, *node->AsContent());
|
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.
|
2020-03-09 17:57:23 +03:00
|
|
|
if (!deleteNodeTransaction) {
|
|
|
|
NS_WARNING("DeleteNodeTransaction::MaybeCreate() failed");
|
2017-03-10 07:23:40 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2020-03-09 17:57:23 +03:00
|
|
|
DebugOnly<nsresult> rvIgnored = AppendChild(deleteNodeTransaction);
|
|
|
|
NS_WARNING_ASSERTION(
|
|
|
|
NS_SUCCEEDED(rvIgnored),
|
|
|
|
"DeleteRangeTransaction::AppendChild() failed, but ignored");
|
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
|