1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "DeleteRangeTransaction.h"
#include "DeleteContentTransactionBase.h"
#include "DeleteNodeTransaction.h"
#include "DeleteTextTransaction.h"
#include "EditTransactionBase.h"
#include "EditorBase.h"
#include "EditorDOMPoint.h"
#include "EditorUtils.h"
#include "HTMLEditUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/Logging.h"
#include "mozilla/mozalloc.h"
#include "mozilla/RangeBoundary.h"
#include "mozilla/StaticPrefs_editor.h"
#include "mozilla/ToString.h"
#include "mozilla/dom/Selection.h"
#include "nsAtom.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsINode.h"
#include "nsAString.h"
namespace mozilla {
using namespace dom;
using EditorType = EditorUtils::EditorType;
DeleteRangeTransaction::DeleteRangeTransaction(EditorBase& aEditorBase,
const nsRange& aRangeToDelete)
: mEditorBase(&aEditorBase), mRangeToDelete(aRangeToDelete.CloneRange()) {}
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeleteRangeTransaction,
EditAggregateTransaction, mEditorBase,
mRangeToDelete, mPointToPutCaret)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeleteRangeTransaction)
NS_INTERFACE_MAP_END_INHERITING(EditAggregateTransaction)
void DeleteRangeTransaction::AppendChild(
DeleteContentTransactionBase& aTransaction) {
mChildren.AppendElement(aTransaction);
}
nsresult
DeleteRangeTransaction::MaybeExtendDeletingRangeWithSurroundingWhitespace(
nsRange& aRange) const {
if (!mEditorBase->mEditActionData->SelectionCreatedByDoubleclick() ||
!StaticPrefs::
editor_word_select_delete_space_after_doubleclick_selection()) {
return NS_OK;
}
EditorRawDOMPoint startPoint(aRange.StartRef());
EditorRawDOMPoint endPoint(aRange.EndRef());
const bool maybeRangeStartsAfterWhiteSpace =
startPoint.IsInTextNode() && !startPoint.IsStartOfContainer();
const bool maybeRangeEndsAtWhiteSpace =
endPoint.IsInTextNode() && !endPoint.IsEndOfContainer();
if (!maybeRangeStartsAfterWhiteSpace && !maybeRangeEndsAtWhiteSpace) {
// no whitespace before or after word => nothing to do here.
return NS_OK;
}
const bool precedingCharIsWhitespace =
maybeRangeStartsAfterWhiteSpace
? startPoint.IsPreviousCharASCIISpaceOrNBSP()
: false;
const bool trailingCharIsWhitespace =
maybeRangeEndsAtWhiteSpace ? endPoint.IsCharASCIISpaceOrNBSP() : false;
// if possible, try to remove the preceding whitespace
// so the caret is at the end of the previous word.
if (precedingCharIsWhitespace) {
// "one [two]", "one [two] three" or "one [two], three"
ErrorResult err;
aRange.SetStart(startPoint.PreviousPoint(), err);
if (auto rv = err.StealNSResult(); NS_FAILED(rv)) {
NS_WARNING(
"DeleteRangeTransaction::"
"MaybeExtendDeletingRangeWithSurroundingWhitespace"
" failed to update the start of the deleting range");
return rv;
}
} else if (trailingCharIsWhitespace) {
// "[one] two"
ErrorResult err;
aRange.SetEnd(endPoint.NextPoint(), err);
if (auto rv = err.StealNSResult(); NS_FAILED(rv)) {
NS_WARNING(
"DeleteRangeTransaction::"
"MaybeExtendDeletingRangeWithSurroundingWhitespace"
" failed to update the end of the deleting range");
return rv;
}
}
return NS_OK;
}
NS_IMETHODIMP DeleteRangeTransaction::DoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteRangeTransaction::%s this={ mName=%s } "
"Start==============================",
this, __FUNCTION__,
nsAtomCString(mName ? mName.get() : nsGkAtoms::_empty).get()));
if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mRangeToDelete)) {
return NS_ERROR_NOT_AVAILABLE;
}
// 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);
MaybeExtendDeletingRangeWithSurroundingWhitespace(*rangeToDelete);
// build the child transactions
// XXX We should move this to the constructor. Then, we don't need to make
// this class has mRangeToDelete with nullptr since transaction instances
// may be created a lot and live long.
{
EditorRawDOMRange extendedRange(*rangeToDelete);
MOZ_ASSERT(extendedRange.IsPositionedAndValid());
if (extendedRange.InSameContainer()) {
// the selection begins and ends in the same node
nsresult rv = AppendTransactionsToDeleteIn(extendedRange);
if (NS_FAILED(rv)) {
NS_WARNING(
"DeleteRangeTransaction::AppendTransactionsToDeleteIn() failed");
return rv;
}
} else {
// If the range ends at end of a node, we may need to extend the range to
// make ContentSubtreeIterator iterates close tag of the unnecessary nodes
// in AppendTransactionsToDeleteNodesEntirelyIn.
for (EditorRawDOMPoint endOfRange = extendedRange.EndRef();
endOfRange.IsInContentNode() && endOfRange.IsEndOfContainer() &&
endOfRange.GetContainer() != extendedRange.StartRef().GetContainer();
endOfRange = extendedRange.EndRef()) {
extendedRange.SetEnd(
EditorRawDOMPoint::After(*endOfRange.ContainerAs<nsIContent>()));
}
// the selection ends in a different node from where it started. delete
// the relevant content in the start node
nsresult rv = AppendTransactionToDeleteText(extendedRange.StartRef(),
nsIEditor::eNext);
if (NS_FAILED(rv)) {
NS_WARNING(
"DeleteRangeTransaction::AppendTransactionToDeleteText() failed");
return rv;
}
// delete the intervening nodes
rv = AppendTransactionsToDeleteNodesWhoseEndBoundaryIn(extendedRange);
if (NS_FAILED(rv)) {
NS_WARNING(
"DeleteRangeTransaction::"
"AppendTransactionsToDeleteNodesWhoseEndBoundaryIn() failed");
return rv;
}
// delete the relevant content in the end node
rv = AppendTransactionToDeleteText(extendedRange.EndRef(),
nsIEditor::ePrevious);
if (NS_FAILED(rv)) {
NS_WARNING(
"DeleteRangeTransaction::AppendTransactionToDeleteText() failed");
return rv;
}
}
}
// if we've successfully built this aggregate transaction, then do it.
nsresult rv = EditAggregateTransaction::DoTransaction();
if (NS_FAILED(rv)) {
NS_WARNING("EditAggregateTransaction::DoTransaction() failed");
return rv;
}
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteRangeTransaction::%s this={ mName=%s } "
"End==============================",
this, __FUNCTION__,
nsAtomCString(mName ? mName.get() : nsGkAtoms::_empty).get()));
mPointToPutCaret = rangeToDelete->StartRef();
if (MOZ_UNLIKELY(!mPointToPutCaret.IsSetAndValid())) {
for (const OwningNonNull<EditTransactionBase>& transaction :
Reversed(mChildren)) {
if (const DeleteContentTransactionBase* deleteContentTransaction =
transaction->GetAsDeleteContentTransactionBase()) {
mPointToPutCaret = deleteContentTransaction->SuggestPointToPutCaret();
if (mPointToPutCaret.IsSetAndValid()) {
break;
}
continue;
}
MOZ_ASSERT_UNREACHABLE(
"Child transactions must be DeleteContentTransactionBase");
}
}
return NS_OK;
}
NS_IMETHODIMP DeleteRangeTransaction::UndoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteRangeTransaction::%s this={ mName=%s } "
"Start==============================",
this, __FUNCTION__,
nsAtomCString(mName ? mName.get() : nsGkAtoms::_empty).get()));
nsresult rv = EditAggregateTransaction::UndoTransaction();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditAggregateTransaction::UndoTransaction() failed");
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteRangeTransaction::%s this={ mName=%s } "
"End==============================",
this, __FUNCTION__,
nsAtomCString(mName ? mName.get() : nsGkAtoms::_empty).get()));
return rv;
}
NS_IMETHODIMP DeleteRangeTransaction::RedoTransaction() {
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteRangeTransaction::%s this={ mName=%s } "
"Start==============================",
this, __FUNCTION__,
nsAtomCString(mName ? mName.get() : nsGkAtoms::_empty).get()));
nsresult rv = EditAggregateTransaction::RedoTransaction();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"EditAggregateTransaction::RedoTransaction() failed");
MOZ_LOG(GetLogModule(), LogLevel::Info,
("%p DeleteRangeTransaction::%s this={ mName=%s } "
"End==============================",
this, __FUNCTION__,
nsAtomCString(mName ? mName.get() : nsGkAtoms::_empty).get()));
return rv;
}
nsresult DeleteRangeTransaction::AppendTransactionsToDeleteIn(
const EditorRawDOMRange& aRangeToDelete) {
if (NS_WARN_IF(!aRangeToDelete.IsPositionedAndValid())) {
return NS_ERROR_INVALID_ARG;
}
MOZ_ASSERT(aRangeToDelete.InSameContainer());
if (NS_WARN_IF(!mEditorBase)) {
return NS_ERROR_NOT_AVAILABLE;
}
// see what kind of node we have
if (Text* textNode = aRangeToDelete.StartRef().GetContainerAs<Text>()) {
if (mEditorBase->IsHTMLEditor() &&
NS_WARN_IF(
!EditorUtils::IsEditableContent(*textNode, EditorType::HTML))) {
// Just ignore to append the transaction for non-editable node.
return NS_OK;
}
// if the node is a chardata node, then delete chardata content
uint32_t textLengthToDelete;
if (aRangeToDelete.Collapsed()) {
textLengthToDelete = 1;
} else {
textLengthToDelete =
aRangeToDelete.EndRef().Offset() - aRangeToDelete.StartRef().Offset();
MOZ_DIAGNOSTIC_ASSERT(textLengthToDelete > 0);
}
RefPtr<DeleteTextTransaction> deleteTextTransaction =
DeleteTextTransaction::MaybeCreate(*mEditorBase, *textNode,
aRangeToDelete.StartRef().Offset(),
textLengthToDelete);
// If the text node isn't editable, it should be never undone/redone.
// So, the transaction shouldn't be recorded.
if (!deleteTextTransaction) {
NS_WARNING("DeleteTextTransaction::MaybeCreate() failed");
return NS_ERROR_FAILURE;
}
AppendChild(*deleteTextTransaction);
return NS_OK;
}
MOZ_ASSERT(mEditorBase->IsHTMLEditor());
// Even if we detect invalid range, we should ignore it for removing
// specified range's nodes as far as possible.
// XXX This is super expensive. Probably, we should make
// DeleteNodeTransaction() can treat multiple siblings.
for (nsIContent* child = aRangeToDelete.StartRef().GetChild();
child && child != aRangeToDelete.EndRef().GetChild();
child = child->GetNextSibling()) {
if (NS_WARN_IF(!HTMLEditUtils::IsRemovableNode(*child))) {
continue; // Should we abort?
}
RefPtr<DeleteNodeTransaction> deleteNodeTransaction =
DeleteNodeTransaction::MaybeCreate(*mEditorBase, *child);
if (deleteNodeTransaction) {
AppendChild(*deleteNodeTransaction);
}
}
return NS_OK;
}
nsresult DeleteRangeTransaction::AppendTransactionToDeleteText(
const EditorRawDOMPoint& aMaybePointInText, nsIEditor::EDirection aAction) {
if (NS_WARN_IF(!aMaybePointInText.IsSetAndValid())) {
return NS_ERROR_INVALID_ARG;
}
if (NS_WARN_IF(!mEditorBase)) {
return NS_ERROR_NOT_AVAILABLE;
}
if (!aMaybePointInText.IsInTextNode()) {
return NS_OK;
}
// If the node is a text node, then delete text before or after the point.
Text& textNode = *aMaybePointInText.ContainerAs<Text>();
uint32_t startOffset, numToDelete;
if (nsIEditor::eNext == aAction) {
startOffset = aMaybePointInText.Offset();
numToDelete = textNode.TextDataLength() - startOffset;
} else {
startOffset = 0;
numToDelete = aMaybePointInText.Offset();
}
if (!numToDelete) {
return NS_OK;
}
RefPtr<DeleteTextTransaction> deleteTextTransaction =
DeleteTextTransaction::MaybeCreate(*mEditorBase, textNode, startOffset,
numToDelete);
// If the text node isn't editable, it should be never undone/redone.
// So, the transaction shouldn't be recorded.
if (MOZ_UNLIKELY(!deleteTextTransaction)) {
NS_WARNING("DeleteTextTransaction::MaybeCreate() failed");
return NS_ERROR_FAILURE;
}
AppendChild(*deleteTextTransaction);
return NS_OK;
}
nsresult
DeleteRangeTransaction::AppendTransactionsToDeleteNodesWhoseEndBoundaryIn(
const EditorRawDOMRange& aRangeToDelete) {
if (NS_WARN_IF(!mEditorBase)) {
return NS_ERROR_NOT_AVAILABLE;
}
ContentSubtreeIterator subtreeIter;
nsresult rv = subtreeIter.Init(aRangeToDelete.StartRef().ToRawRangeBoundary(),
aRangeToDelete.EndRef().ToRawRangeBoundary());
if (NS_FAILED(rv)) {
NS_WARNING("ContentSubtreeIterator::Init() failed");
return rv;
}
for (; !subtreeIter.IsDone(); subtreeIter.Next()) {
nsINode* node = subtreeIter.GetCurrentNode();
if (NS_WARN_IF(!node) || NS_WARN_IF(!node->IsContent())) {
return NS_ERROR_FAILURE;
}
if (NS_WARN_IF(!HTMLEditUtils::IsRemovableNode(*node->AsContent()))) {
continue;
}
RefPtr<DeleteNodeTransaction> deleteNodeTransaction =
DeleteNodeTransaction::MaybeCreate(*mEditorBase, *node->AsContent());
if (deleteNodeTransaction) {
AppendChild(*deleteNodeTransaction);
}
}
return NS_OK;
}
} // namespace mozilla
|