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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/* -*- 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/. */
#ifndef AutoRangeArray_h
#define AutoRangeArray_h
#include "EditAction.h" // for EditSubAction
#include "EditorBase.h" // for EditorBase
#include "EditorDOMPoint.h" // for EditorDOMPoint, EditorDOMRange, etc
#include "EditorForwards.h"
#include "SelectionState.h" // for SelectionState
#include "mozilla/ErrorResult.h" // for ErrorResult
#include "mozilla/IntegerRange.h" // for IntegerRange
#include "mozilla/Maybe.h" // for Maybe
#include "mozilla/RangeBoundary.h" // for RangeBoundary
#include "mozilla/Result.h" // for Result<>
#include "mozilla/dom/Element.h" // for dom::Element
#include "mozilla/dom/HTMLBRElement.h" // for dom::HTMLBRElement
#include "mozilla/dom/Selection.h" // for dom::Selection
#include "mozilla/dom/Text.h" // for dom::Text
#include "nsDebug.h" // for NS_WARNING, etc
#include "nsDirection.h" // for nsDirection
#include "nsError.h" // for NS_SUCCESS_* and NS_ERROR_*
#include "nsRange.h" // for nsRange
namespace mozilla {
/******************************************************************************
* AutoRangeArray stores ranges which do no belong any `Selection`.
* So, different from `AutoSelectionRangeArray`, this can be used for
* ranges which may need to be modified before touching the DOM tree,
* but does not want to modify `Selection` for the performance.
*****************************************************************************/
class MOZ_STACK_CLASS AutoRangeArray final {
public:
explicit AutoRangeArray(const dom::Selection& aSelection);
template <typename PointType>
explicit AutoRangeArray(const EditorDOMRangeBase<PointType>& aRange);
template <typename PT, typename CT>
explicit AutoRangeArray(const EditorDOMPointBase<PT, CT>& aPoint);
// The copy constructor copies everything except saved ranges.
explicit AutoRangeArray(const AutoRangeArray& aOther);
~AutoRangeArray();
void Initialize(const dom::Selection& aSelection) {
ClearSavedRanges();
mDirection = aSelection.GetDirection();
mRanges.Clear();
for (const uint32_t i : IntegerRange(aSelection.RangeCount())) {
MOZ_ASSERT(aSelection.GetRangeAt(i));
mRanges.AppendElement(aSelection.GetRangeAt(i)->CloneRange());
if (aSelection.GetRangeAt(i) == aSelection.GetAnchorFocusRange()) {
mAnchorFocusRange = mRanges.LastElement();
}
}
}
/**
* Check whether all ranges in content nodes or not. If the ranges is empty,
* this returns false.
*/
[[nodiscard]] bool IsInContent() const {
if (mRanges.IsEmpty()) {
return false;
}
for (const OwningNonNull<nsRange>& range : mRanges) {
if (MOZ_UNLIKELY(!range->IsPositioned() || !range->GetStartContainer() ||
!range->GetStartContainer()->IsContent() ||
!range->GetEndContainer() ||
!range->GetEndContainer()->IsContent())) {
return false;
}
}
return true;
}
/**
* EnsureOnlyEditableRanges() removes ranges which cannot modify.
* Note that this is designed only for `HTMLEditor` because this must not
* be required by `TextEditor`.
*/
void EnsureOnlyEditableRanges(const dom::Element& aEditingHost);
/**
* EnsureRangesInTextNode() is designed for TextEditor to guarantee that
* all ranges are in its text node which is first child of the anonymous <div>
* element and is first child.
*/
void EnsureRangesInTextNode(const dom::Text& aTextNode);
/**
* Extend ranges to wrap lines to handle block level edit actions such as
* updating the block parent or indent/outdent around the selection.
*/
void ExtendRangesToWrapLinesToHandleBlockLevelEditAction(
EditSubAction aEditSubAction, const dom::Element& aEditingHost);
/**
* Check whether the range is in aEditingHost and both containers of start and
* end boundaries of the range are editable.
*/
[[nodiscard]] static bool IsEditableRange(const dom::AbstractRange& aRange,
const dom::Element& aEditingHost);
/**
* Check whether the first range is in aEditingHost and both containers of
* start and end boundaries of the first range are editable.
*/
[[nodiscard]] bool IsFirstRangeEditable(
const dom::Element& aEditingHost) const {
return IsEditableRange(FirstRangeRef(), aEditingHost);
}
/**
* IsAtLeastOneContainerOfRangeBoundariesInclusiveDescendantOf() returns true
* if at least one of the containers of the range boundaries is an inclusive
* descendant of aContent.
*/
[[nodiscard]] bool
IsAtLeastOneContainerOfRangeBoundariesInclusiveDescendantOf(
const nsIContent& aContent) const {
for (const OwningNonNull<nsRange>& range : mRanges) {
nsINode* startContainer = range->GetStartContainer();
if (startContainer &&
startContainer->IsInclusiveDescendantOf(&aContent)) {
return true;
}
nsINode* endContainer = range->GetEndContainer();
if (startContainer == endContainer) {
continue;
}
if (endContainer && endContainer->IsInclusiveDescendantOf(&aContent)) {
return true;
}
}
return false;
}
[[nodiscard]] auto& Ranges() { return mRanges; }
[[nodiscard]] const auto& Ranges() const { return mRanges; }
[[nodiscard]] OwningNonNull<nsRange>& FirstRangeRef() { return mRanges[0]; }
[[nodiscard]] const OwningNonNull<nsRange>& FirstRangeRef() const {
return mRanges[0];
}
template <template <typename> typename StrongPtrType>
[[nodiscard]] AutoTArray<StrongPtrType<nsRange>, 8> CloneRanges() const {
AutoTArray<StrongPtrType<nsRange>, 8> ranges;
for (const auto& range : mRanges) {
ranges.AppendElement(range->CloneRange());
}
return ranges;
}
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType GetFirstRangeStartPoint() const {
if (mRanges.IsEmpty() || !mRanges[0]->IsPositioned()) {
return EditorDOMPointType();
}
return EditorDOMPointType(mRanges[0]->StartRef());
}
template <typename EditorDOMPointType>
[[nodiscard]] EditorDOMPointType GetFirstRangeEndPoint() const {
if (mRanges.IsEmpty() || !mRanges[0]->IsPositioned()) {
return EditorDOMPointType();
}
return EditorDOMPointType(mRanges[0]->EndRef());
}
nsresult SelectNode(nsINode& aNode) {
mRanges.Clear();
if (!mAnchorFocusRange) {
mAnchorFocusRange = nsRange::Create(&aNode);
if (!mAnchorFocusRange) {
return NS_ERROR_FAILURE;
}
}
ErrorResult error;
mAnchorFocusRange->SelectNode(aNode, error);
if (error.Failed()) {
mAnchorFocusRange = nullptr;
return error.StealNSResult();
}
mRanges.AppendElement(*mAnchorFocusRange);
return NS_OK;
}
/**
* ExtendAnchorFocusRangeFor() extends the anchor-focus range for deleting
* content for aDirectionAndAmount. The range won't be extended to outer of
* selection limiter. Note that if a range is extened, the range is
* recreated. Therefore, caller cannot cache pointer of any ranges before
* calling this.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<nsIEditor::EDirection, nsresult>
ExtendAnchorFocusRangeFor(const EditorBase& aEditorBase,
nsIEditor::EDirection aDirectionAndAmount);
/**
* For compatiblity with the other browsers, we should shrink ranges to
* start from an atomic content and/or end after one instead of start
* from end of a preceding text node and end by start of a follwing text
* node. Returns true if this modifies a range.
*/
enum class IfSelectingOnlyOneAtomicContent {
Collapse, // Collapse to the range selecting only one atomic content to
// start or after of it. Whether to collapse start or after
// it depends on aDirectionAndAmount. This is ignored if
// there are multiple ranges.
KeepSelecting, // Won't collapse the range.
};
Result<bool, nsresult> ShrinkRangesIfStartFromOrEndAfterAtomicContent(
const HTMLEditor& aHTMLEditor, nsIEditor::EDirection aDirectionAndAmount,
IfSelectingOnlyOneAtomicContent aIfSelectingOnlyOneAtomicContent,
const dom::Element* aEditingHost);
/**
* The following methods are same as `Selection`'s methods.
*/
[[nodiscard]] bool IsCollapsed() const {
return mRanges.IsEmpty() ||
(mRanges.Length() == 1 && mRanges[0]->Collapsed());
}
template <typename PT, typename CT>
nsresult Collapse(const EditorDOMPointBase<PT, CT>& aPoint) {
mRanges.Clear();
if (!mAnchorFocusRange) {
ErrorResult error;
mAnchorFocusRange = nsRange::Create(aPoint.ToRawRangeBoundary(),
aPoint.ToRawRangeBoundary(), error);
if (error.Failed()) {
mAnchorFocusRange = nullptr;
return error.StealNSResult();
}
} else {
nsresult rv = mAnchorFocusRange->CollapseTo(aPoint.ToRawRangeBoundary());
if (NS_FAILED(rv)) {
mAnchorFocusRange = nullptr;
return rv;
}
}
mRanges.AppendElement(*mAnchorFocusRange);
return NS_OK;
}
template <typename SPT, typename SCT, typename EPT, typename ECT>
nsresult SetStartAndEnd(const EditorDOMPointBase<SPT, SCT>& aStart,
const EditorDOMPointBase<EPT, ECT>& aEnd) {
mRanges.Clear();
if (!mAnchorFocusRange) {
ErrorResult error;
mAnchorFocusRange = nsRange::Create(aStart.ToRawRangeBoundary(),
aEnd.ToRawRangeBoundary(), error);
if (error.Failed()) {
mAnchorFocusRange = nullptr;
return error.StealNSResult();
}
} else {
nsresult rv = mAnchorFocusRange->SetStartAndEnd(
aStart.ToRawRangeBoundary(), aEnd.ToRawRangeBoundary());
if (NS_FAILED(rv)) {
mAnchorFocusRange = nullptr;
return rv;
}
}
mRanges.AppendElement(*mAnchorFocusRange);
return NS_OK;
}
template <typename SPT, typename SCT, typename EPT, typename ECT>
nsresult SetBaseAndExtent(const EditorDOMPointBase<SPT, SCT>& aAnchor,
const EditorDOMPointBase<EPT, ECT>& aFocus) {
if (MOZ_UNLIKELY(!aAnchor.IsSet()) || MOZ_UNLIKELY(!aFocus.IsSet())) {
mRanges.Clear();
mAnchorFocusRange = nullptr;
return NS_ERROR_INVALID_ARG;
}
return aAnchor.EqualsOrIsBefore(aFocus) ? SetStartAndEnd(aAnchor, aFocus)
: SetStartAndEnd(aFocus, aAnchor);
}
[[nodiscard]] const nsRange* GetAnchorFocusRange() const {
return mAnchorFocusRange;
}
[[nodiscard]] nsDirection GetDirection() const { return mDirection; }
void SetDirection(nsDirection aDirection) { mDirection = aDirection; }
[[nodiscard]] const RangeBoundary& AnchorRef() const {
if (!mAnchorFocusRange) {
static RangeBoundary sEmptyRangeBoundary;
return sEmptyRangeBoundary;
}
return mDirection == nsDirection::eDirNext ? mAnchorFocusRange->StartRef()
: mAnchorFocusRange->EndRef();
}
[[nodiscard]] nsINode* GetAnchorNode() const {
return AnchorRef().IsSet() ? AnchorRef().Container() : nullptr;
}
[[nodiscard]] uint32_t GetAnchorOffset() const {
return AnchorRef().IsSet()
? AnchorRef()
.Offset(RangeBoundary::OffsetFilter::kValidOffsets)
.valueOr(0)
: 0;
}
[[nodiscard]] nsIContent* GetChildAtAnchorOffset() const {
return AnchorRef().IsSet() ? AnchorRef().GetChildAtOffset() : nullptr;
}
[[nodiscard]] const RangeBoundary& FocusRef() const {
if (!mAnchorFocusRange) {
static RangeBoundary sEmptyRangeBoundary;
return sEmptyRangeBoundary;
}
return mDirection == nsDirection::eDirNext ? mAnchorFocusRange->EndRef()
: mAnchorFocusRange->StartRef();
}
[[nodiscard]] nsINode* GetFocusNode() const {
return FocusRef().IsSet() ? FocusRef().Container() : nullptr;
}
[[nodiscard]] uint32_t FocusOffset() const {
return FocusRef().IsSet()
? FocusRef()
.Offset(RangeBoundary::OffsetFilter::kValidOffsets)
.valueOr(0)
: 0;
}
[[nodiscard]] nsIContent* GetChildAtFocusOffset() const {
return FocusRef().IsSet() ? FocusRef().GetChildAtOffset() : nullptr;
}
void RemoveAllRanges() {
mRanges.Clear();
mAnchorFocusRange = nullptr;
mDirection = nsDirection::eDirNext;
}
/**
* APIs to store ranges with only container node and offset in it, and track
* them with RangeUpdater.
*/
[[nodiscard]] bool SaveAndTrackRanges(HTMLEditor& aHTMLEditor);
[[nodiscard]] bool HasSavedRanges() const { return mSavedRanges.isSome(); }
void ClearSavedRanges();
void RestoreFromSavedRanges() {
MOZ_DIAGNOSTIC_ASSERT(mSavedRanges.isSome());
if (mSavedRanges.isNothing()) {
return;
}
mSavedRanges->ApplyTo(*this);
ClearSavedRanges();
}
/**
* Apply mRanges and mDirection to aSelection.
*/
MOZ_CAN_RUN_SCRIPT nsresult ApplyTo(dom::Selection& aSelection) {
dom::SelectionBatcher selectionBatcher(aSelection, __FUNCTION__);
aSelection.RemoveAllRanges(IgnoreErrors());
MOZ_ASSERT(!aSelection.RangeCount());
aSelection.SetDirection(mDirection);
IgnoredErrorResult error;
for (const OwningNonNull<nsRange>& range : mRanges) {
// MOZ_KnownLive(range) due to bug 1622253
aSelection.AddRangeAndSelectFramesAndNotifyListeners(MOZ_KnownLive(range),
error);
if (error.Failed()) {
return error.StealNSResult();
}
}
return NS_OK;
}
/**
* If the points are same (i.e., mean a collapsed range) and in an empty block
* element except the padding <br> element, this makes aStartPoint and
* aEndPoint contain the padding <br> element.
*/
static void UpdatePointsToSelectAllChildrenIfCollapsedInEmptyBlockElement(
EditorDOMPoint& aStartPoint, EditorDOMPoint& aEndPoint,
const dom::Element& aEditingHost);
/**
* CreateRangeExtendedToHardLineStartAndEnd() creates an nsRange instance
* which may be expanded to start/end of hard line at both edges of the given
* range. If this fails handling something, returns nullptr.
*/
static already_AddRefed<nsRange>
CreateRangeWrappingStartAndEndLinesContainingBoundaries(
const EditorDOMRange& aRange, EditSubAction aEditSubAction,
const dom::Element& aEditingHost) {
if (!aRange.IsPositioned()) {
return nullptr;
}
return CreateRangeWrappingStartAndEndLinesContainingBoundaries(
aRange.StartRef(), aRange.EndRef(), aEditSubAction, aEditingHost);
}
static already_AddRefed<nsRange>
CreateRangeWrappingStartAndEndLinesContainingBoundaries(
const EditorDOMPoint& aStartPoint, const EditorDOMPoint& aEndPoint,
EditSubAction aEditSubAction, const dom::Element& aEditingHost) {
RefPtr<nsRange> range =
nsRange::Create(aStartPoint.ToRawRangeBoundary(),
aEndPoint.ToRawRangeBoundary(), IgnoreErrors());
if (MOZ_UNLIKELY(!range)) {
return nullptr;
}
if (NS_FAILED(ExtendRangeToWrapStartAndEndLinesContainingBoundaries(
*range, aEditSubAction, aEditingHost)) ||
MOZ_UNLIKELY(!range->IsPositioned())) {
return nullptr;
}
return range.forget();
}
/**
* Splits text nodes if each range end is in middle of a text node, then,
* calls HTMLEditor::SplitParentInlineElementsAtRangeEdges(RangeItem&) for
* each range. Finally, updates ranges to keep edit target ranges as
* expected.
*
* @param aHTMLEditor The HTMLEditor which will handle the splittings.
* @return A suggest point to put caret if succeeded, but it may be
* unset.
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT Result<EditorDOMPoint, nsresult>
SplitTextNodesAtEndBoundariesAndParentInlineElementsAtBoundaries(
HTMLEditor& aHTMLEditor);
/**
* CollectEditTargetNodes() collects edit target nodes the ranges.
* First, this collects all nodes in given ranges, then, modifies the
* result for specific edit sub-actions.
*/
enum class CollectNonEditableNodes { No, Yes };
nsresult CollectEditTargetNodes(
const HTMLEditor& aHTMLEditor,
nsTArray<OwningNonNull<nsIContent>>& aOutArrayOfContents,
EditSubAction aEditSubAction,
CollectNonEditableNodes aCollectNonEditableNodes) const;
/**
* Retrieve a closest ancestor list element of a common ancestor of _A_ range
* of the ranges. This tries to retrieve it from the first range to the last
* range.
*/
dom::Element* GetClosestAncestorAnyListElementOfRange() const;
private:
static nsresult ExtendRangeToWrapStartAndEndLinesContainingBoundaries(
nsRange& aRange, EditSubAction aEditSubAction,
const dom::Element& aEditingHost);
AutoTArray<mozilla::OwningNonNull<nsRange>, 8> mRanges;
RefPtr<nsRange> mAnchorFocusRange;
nsDirection mDirection = nsDirection::eDirNext;
Maybe<SelectionState> mSavedRanges;
RefPtr<HTMLEditor> mTrackingHTMLEditor;
};
} // namespace mozilla
#endif // #ifndef AutoRangeArray_h
|