1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "DeleteRangeTransaction.h"
8 #include "DeleteNodeTransaction.h"
9 #include "DeleteTextTransaction.h"
10 #include "mozilla/Assertions.h"
11 #include "mozilla/ContentIterator.h"
12 #include "mozilla/dom/Selection.h"
13 #include "mozilla/EditorBase.h"
14 #include "mozilla/mozalloc.h"
15 #include "mozilla/RangeBoundary.h"
19 #include "nsIContent.h"
21 #include "nsAString.h"
27 DeleteRangeTransaction::DeleteRangeTransaction(EditorBase
& aEditorBase
,
28 const nsRange
& aRangeToDelete
)
29 : mEditorBase(&aEditorBase
), mRangeToDelete(aRangeToDelete
.CloneRange()) {}
31 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteRangeTransaction
,
32 EditAggregateTransaction
, mEditorBase
,
35 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteRangeTransaction
)
36 NS_INTERFACE_MAP_END_INHERITING(EditAggregateTransaction
)
38 NS_IMETHODIMP
DeleteRangeTransaction::DoTransaction() {
39 if (NS_WARN_IF(!mEditorBase
) || NS_WARN_IF(!mRangeToDelete
)) {
40 return NS_ERROR_NOT_AVAILABLE
;
43 // Swap mRangeToDelete out into a stack variable, so we make sure to null it
44 // out on return from this function. Once this function returns, we no longer
45 // need mRangeToDelete, and keeping it alive in the long term slows down all
46 // DOM mutations because it's observing them.
47 RefPtr
<nsRange
> rangeToDelete
;
48 rangeToDelete
.swap(mRangeToDelete
);
50 // build the child transactions
51 const RangeBoundary
& startRef
= rangeToDelete
->StartRef();
52 const RangeBoundary
& endRef
= rangeToDelete
->EndRef();
53 MOZ_ASSERT(startRef
.IsSetAndValid());
54 MOZ_ASSERT(endRef
.IsSetAndValid());
56 if (startRef
.Container() == endRef
.Container()) {
57 // the selection begins and ends in the same node
58 nsresult rv
= CreateTxnsToDeleteBetween(startRef
.AsRaw(), endRef
.AsRaw());
60 NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteBetween() failed");
64 // the selection ends in a different node from where it started. delete
65 // the relevant content in the start node
66 nsresult rv
= CreateTxnsToDeleteContent(startRef
.AsRaw(), nsIEditor::eNext
);
68 NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteContent() failed");
71 // delete the intervening nodes
72 rv
= CreateTxnsToDeleteNodesBetween(rangeToDelete
);
75 "DeleteRangeTransaction::CreateTxnsToDeleteNodesBetween() failed");
78 // delete the relevant content in the end node
79 rv
= CreateTxnsToDeleteContent(endRef
.AsRaw(), nsIEditor::ePrevious
);
81 NS_WARNING("DeleteRangeTransaction::CreateTxnsToDeleteContent() failed");
86 // if we've successfully built this aggregate transaction, then do it.
87 nsresult rv
= EditAggregateTransaction::DoTransaction();
89 NS_WARNING("EditAggregateTransaction::DoTransaction() failed");
93 if (!mEditorBase
->AllowsTransactionsToChangeSelection()) {
97 RefPtr
<Selection
> selection
= mEditorBase
->GetSelection();
98 if (NS_WARN_IF(!selection
)) {
99 return NS_ERROR_NOT_INITIALIZED
;
101 rv
= selection
->CollapseInLimiter(startRef
.AsRaw());
102 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
),
103 "Selection::CollapseInLimiter() failed");
107 NS_IMETHODIMP
DeleteRangeTransaction::UndoTransaction() {
108 nsresult rv
= EditAggregateTransaction::UndoTransaction();
109 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
),
110 "EditAggregateTransaction::UndoTransaction() failed");
114 NS_IMETHODIMP
DeleteRangeTransaction::RedoTransaction() {
115 nsresult rv
= EditAggregateTransaction::RedoTransaction();
116 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv
),
117 "EditAggregateTransaction::RedoTransaction() failed");
121 nsresult
DeleteRangeTransaction::CreateTxnsToDeleteBetween(
122 const RawRangeBoundary
& aStart
, const RawRangeBoundary
& aEnd
) {
123 if (NS_WARN_IF(!aStart
.IsSetAndValid()) ||
124 NS_WARN_IF(!aEnd
.IsSetAndValid()) ||
125 NS_WARN_IF(aStart
.Container() != aEnd
.Container())) {
126 return NS_ERROR_INVALID_ARG
;
129 if (NS_WARN_IF(!mEditorBase
)) {
130 return NS_ERROR_NOT_AVAILABLE
;
133 // see what kind of node we have
134 if (Text
* textNode
= Text::FromNode(aStart
.Container())) {
135 // if the node is a chardata node, then delete chardata content
137 if (aStart
== aEnd
) {
140 numToDel
= *aEnd
.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets
) -
141 *aStart
.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets
);
142 MOZ_DIAGNOSTIC_ASSERT(numToDel
> 0);
145 RefPtr
<DeleteTextTransaction
> deleteTextTransaction
=
146 DeleteTextTransaction::MaybeCreate(
147 *mEditorBase
, *textNode
,
148 *aStart
.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets
),
150 // If the text node isn't editable, it should be never undone/redone.
151 // So, the transaction shouldn't be recorded.
152 if (!deleteTextTransaction
) {
153 NS_WARNING("DeleteTextTransaction::MaybeCreate() failed");
154 return NS_ERROR_FAILURE
;
156 DebugOnly
<nsresult
> rvIgnored
= AppendChild(deleteTextTransaction
);
157 NS_WARNING_ASSERTION(
158 NS_SUCCEEDED(rvIgnored
),
159 "DeleteRangeTransaction::AppendChild() failed, but ignored");
163 // Even if we detect invalid range, we should ignore it for removing
164 // specified range's nodes as far as possible.
165 // XXX This is super expensive. Probably, we should make
166 // DeleteNodeTransaction() can treat multiple siblings.
167 for (nsIContent
* child
= aStart
.GetChildAtOffset();
168 child
&& child
!= aEnd
.GetChildAtOffset();
169 child
= child
->GetNextSibling()) {
170 RefPtr
<DeleteNodeTransaction
> deleteNodeTransaction
=
171 DeleteNodeTransaction::MaybeCreate(*mEditorBase
, *child
);
172 // XXX This is odd handling. Even if some children are not editable,
173 // editor should append transactions because they could be editable
174 // at undoing/redoing. Additionally, if the transaction needs to
175 // delete/restore all nodes, it should at undoing/redoing.
176 if (deleteNodeTransaction
) {
177 DebugOnly
<nsresult
> rvIgnored
= AppendChild(deleteNodeTransaction
);
178 NS_WARNING_ASSERTION(
179 NS_SUCCEEDED(rvIgnored
),
180 "DeleteRangeTransaction::AppendChild() failed, but ignored");
187 nsresult
DeleteRangeTransaction::CreateTxnsToDeleteContent(
188 const RawRangeBoundary
& aPoint
, nsIEditor::EDirection aAction
) {
189 if (NS_WARN_IF(!aPoint
.IsSetAndValid())) {
190 return NS_ERROR_INVALID_ARG
;
193 if (NS_WARN_IF(!mEditorBase
)) {
194 return NS_ERROR_NOT_AVAILABLE
;
197 Text
* textNode
= Text::FromNode(aPoint
.Container());
202 // If the node is a chardata node, then delete chardata content
203 uint32_t startOffset
, numToDelete
;
204 if (nsIEditor::eNext
== aAction
) {
205 startOffset
= *aPoint
.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets
);
206 numToDelete
= aPoint
.Container()->Length() - startOffset
;
209 numToDelete
= *aPoint
.Offset(RawRangeBoundary::OffsetFilter::kValidOffsets
);
216 RefPtr
<DeleteTextTransaction
> deleteTextTransaction
=
217 DeleteTextTransaction::MaybeCreate(*mEditorBase
, *textNode
, startOffset
,
219 NS_WARNING_ASSERTION(deleteTextTransaction
,
220 "DeleteTextTransaction::MaybeCreate() failed");
221 // If the text node isn't editable, it should be never undone/redone.
222 // So, the transaction shouldn't be recorded.
223 if (!deleteTextTransaction
) {
224 return NS_ERROR_FAILURE
;
226 DebugOnly
<nsresult
> rvIgnored
= AppendChild(deleteTextTransaction
);
227 NS_WARNING_ASSERTION(
228 NS_SUCCEEDED(rvIgnored
),
229 "DeleteRangeTransaction::AppendChild() failed, but ignored");
233 nsresult
DeleteRangeTransaction::CreateTxnsToDeleteNodesBetween(
234 nsRange
* aRangeToDelete
) {
235 if (NS_WARN_IF(!mEditorBase
)) {
236 return NS_ERROR_NOT_AVAILABLE
;
239 ContentSubtreeIterator subtreeIter
;
240 nsresult rv
= subtreeIter
.Init(aRangeToDelete
);
242 NS_WARNING("ContentSubtreeIterator::Init() failed");
246 for (; !subtreeIter
.IsDone(); subtreeIter
.Next()) {
247 nsINode
* node
= subtreeIter
.GetCurrentNode();
248 if (NS_WARN_IF(!node
) || NS_WARN_IF(!node
->IsContent())) {
249 return NS_ERROR_FAILURE
;
252 RefPtr
<DeleteNodeTransaction
> deleteNodeTransaction
=
253 DeleteNodeTransaction::MaybeCreate(*mEditorBase
, *node
->AsContent());
254 // XXX This is odd handling. Even if some nodes in the range are not
255 // editable, editor should append transactions because they could
256 // at undoing/redoing. Additionally, if the transaction needs to
257 // delete/restore all nodes, it should at undoing/redoing.
258 if (!deleteNodeTransaction
) {
259 NS_WARNING("DeleteNodeTransaction::MaybeCreate() failed");
260 return NS_ERROR_FAILURE
;
262 DebugOnly
<nsresult
> rvIgnored
= AppendChild(deleteNodeTransaction
);
263 NS_WARNING_ASSERTION(
264 NS_SUCCEEDED(rvIgnored
),
265 "DeleteRangeTransaction::AppendChild() failed, but ignored");
270 } // namespace mozilla