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
|
/* -*- 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/. */
/*
* Implementation of the DOM nsIDOMRange object.
*/
#ifndef nsRange_h___
#define nsRange_h___
#include "nsIDOMRange.h"
#include "nsCOMPtr.h"
#include "nsINode.h"
#include "nsIDocument.h"
#include "nsIDOMNode.h"
#include "nsLayoutUtils.h"
#include "prmon.h"
#include "nsStubMutationObserver.h"
#include "nsWrapperCache.h"
#include "mozilla/Attributes.h"
namespace mozilla {
class ErrorResult;
namespace dom {
class DocumentFragment;
class DOMRect;
class DOMRectList;
}
}
class nsRange final : public nsIDOMRange,
public nsStubMutationObserver,
public nsWrapperCache
{
typedef mozilla::ErrorResult ErrorResult;
typedef mozilla::dom::DOMRect DOMRect;
typedef mozilla::dom::DOMRectList DOMRectList;
virtual ~nsRange();
public:
explicit nsRange(nsINode* aNode)
: mRoot(nullptr)
, mStartOffset(0)
, mEndOffset(0)
, mIsPositioned(false)
, mIsDetached(false)
, mMaySpanAnonymousSubtrees(false)
, mInSelection(false)
, mIsGenerated(false)
, mStartOffsetWasIncremented(false)
, mEndOffsetWasIncremented(false)
, mEnableGravitationOnElementRemoval(true)
#ifdef DEBUG
, mAssertNextInsertOrAppendIndex(-1)
, mAssertNextInsertOrAppendNode(nullptr)
#endif
{
MOZ_ASSERT(aNode, "range isn't in a document!");
mOwner = aNode->OwnerDoc();
}
static nsresult CreateRange(nsIDOMNode* aStartParent, int32_t aStartOffset,
nsIDOMNode* aEndParent, int32_t aEndOffset,
nsRange** aRange);
static nsresult CreateRange(nsIDOMNode* aStartParent, int32_t aStartOffset,
nsIDOMNode* aEndParent, int32_t aEndOffset,
nsIDOMRange** aRange);
static nsresult CreateRange(nsINode* aStartParent, int32_t aStartOffset,
nsINode* aEndParent, int32_t aEndOffset,
nsRange** aRange);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsRange, nsIDOMRange)
/**
* The DOM Range spec requires that when a node is removed from its parent,
* and the node's subtree contains the start or end point of a range, that
* start or end point is moved up to where the node was removed from its
* parent.
* For some internal uses of Ranges it's useful to disable that behavior,
* so that a range of children within a single parent is preserved even if
* that parent is removed from the document tree.
*/
void SetEnableGravitationOnElementRemoval(bool aEnable)
{
mEnableGravitationOnElementRemoval = aEnable;
}
// nsIDOMRange interface
NS_DECL_NSIDOMRANGE
nsINode* GetRoot() const
{
return mRoot;
}
nsINode* GetStartParent() const
{
return mStartParent;
}
nsINode* GetEndParent() const
{
return mEndParent;
}
int32_t StartOffset() const
{
return mStartOffset;
}
int32_t EndOffset() const
{
return mEndOffset;
}
bool IsPositioned() const
{
return mIsPositioned;
}
void SetMaySpanAnonymousSubtrees(bool aMaySpanAnonymousSubtrees)
{
mMaySpanAnonymousSubtrees = aMaySpanAnonymousSubtrees;
}
/**
* Return true iff this range is part of at least one Selection object
* and isn't detached.
*/
bool IsInSelection() const
{
return mInSelection;
}
/**
* Called when the range is added/removed from a Selection.
*/
void SetInSelection(bool aInSelection)
{
if (mInSelection == aInSelection) {
return;
}
mInSelection = aInSelection;
nsINode* commonAncestor = GetCommonAncestor();
NS_ASSERTION(commonAncestor, "unexpected disconnected nodes");
if (mInSelection) {
RegisterCommonAncestor(commonAncestor);
} else {
UnregisterCommonAncestor(commonAncestor);
}
}
/**
* Return true if this range was generated.
* @see SetIsGenerated
*/
bool IsGenerated() const
{
return mIsGenerated;
}
/**
* Mark this range as being generated or not.
* Currently it is used for marking ranges that are created when splitting up
* a range to exclude a -moz-user-select:none region.
* @see Selection::AddItem
* @see ExcludeNonSelectableNodes
*/
void SetIsGenerated(bool aIsGenerated)
{
mIsGenerated = aIsGenerated;
}
nsINode* GetCommonAncestor() const;
void Reset();
nsresult SetStart(nsINode* aParent, int32_t aOffset);
nsresult SetEnd(nsINode* aParent, int32_t aOffset);
already_AddRefed<nsRange> CloneRange() const;
nsresult Set(nsINode* aStartParent, int32_t aStartOffset,
nsINode* aEndParent, int32_t aEndOffset)
{
// If this starts being hot, we may be able to optimize this a bit,
// but for now just set start and end separately.
nsresult rv = SetStart(aStartParent, aStartOffset);
NS_ENSURE_SUCCESS(rv, rv);
return SetEnd(aEndParent, aEndOffset);
}
NS_IMETHOD GetUsedFontFaces(nsIDOMFontFaceList** aResult);
// nsIMutationObserver methods
NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
NS_DECL_NSIMUTATIONOBSERVER_PARENTCHAINCHANGED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
// WebIDL
static already_AddRefed<nsRange>
Constructor(const mozilla::dom::GlobalObject& global,
mozilla::ErrorResult& aRv);
bool Collapsed() const
{
return mIsPositioned && mStartParent == mEndParent &&
mStartOffset == mEndOffset;
}
already_AddRefed<mozilla::dom::DocumentFragment>
CreateContextualFragment(const nsAString& aString, ErrorResult& aError);
already_AddRefed<mozilla::dom::DocumentFragment>
CloneContents(ErrorResult& aErr);
int16_t CompareBoundaryPoints(uint16_t aHow, nsRange& aOther,
ErrorResult& aErr);
int16_t ComparePoint(nsINode& aParent, uint32_t aOffset, ErrorResult& aErr);
void DeleteContents(ErrorResult& aRv);
already_AddRefed<mozilla::dom::DocumentFragment>
ExtractContents(ErrorResult& aErr);
nsINode* GetCommonAncestorContainer(ErrorResult& aRv) const;
nsINode* GetStartContainer(ErrorResult& aRv) const;
uint32_t GetStartOffset(ErrorResult& aRv) const;
nsINode* GetEndContainer(ErrorResult& aRv) const;
uint32_t GetEndOffset(ErrorResult& aRv) const;
void InsertNode(nsINode& aNode, ErrorResult& aErr);
bool IntersectsNode(nsINode& aNode, ErrorResult& aRv);
bool IsPointInRange(nsINode& aParent, uint32_t aOffset, ErrorResult& aErr);
void SelectNode(nsINode& aNode, ErrorResult& aErr);
void SelectNodeContents(nsINode& aNode, ErrorResult& aErr);
void SetEnd(nsINode& aNode, uint32_t aOffset, ErrorResult& aErr);
void SetEndAfter(nsINode& aNode, ErrorResult& aErr);
void SetEndBefore(nsINode& aNode, ErrorResult& aErr);
void SetStart(nsINode& aNode, uint32_t aOffset, ErrorResult& aErr);
void SetStartAfter(nsINode& aNode, ErrorResult& aErr);
void SetStartBefore(nsINode& aNode, ErrorResult& aErr);
void SurroundContents(nsINode& aNode, ErrorResult& aErr);
already_AddRefed<DOMRect> GetBoundingClientRect(bool aClampToEdge = true,
bool aFlushLayout = true);
already_AddRefed<DOMRectList> GetClientRects(bool aClampToEdge = true,
bool aFlushLayout = true);
static void GetInnerTextNoFlush(mozilla::dom::DOMString& aValue,
mozilla::ErrorResult& aError,
nsIContent* aStartParent,
uint32_t aStartOffset,
nsIContent* aEndParent,
uint32_t aEndOffset);
nsINode* GetParentObject() const { return mOwner; }
virtual JSObject* WrapObject(JSContext* cx) override final;
private:
// no copy's or assigns
nsRange(const nsRange&);
nsRange& operator=(const nsRange&);
/**
* Cut or delete the range's contents.
*
* @param aFragment nsIDOMDocumentFragment containing the nodes.
* May be null to indicate the caller doesn't want a fragment.
*/
nsresult CutContents(mozilla::dom::DocumentFragment** frag);
static nsresult CloneParentsBetween(nsINode* aAncestor,
nsINode* aNode,
nsINode** aClosestAncestor,
nsINode** aFarthestAncestor);
public:
/******************************************************************************
* Utility routine to detect if a content node starts before a range and/or
* ends after a range. If neither it is contained inside the range.
*
* XXX - callers responsibility to ensure node in same doc as range!
*
*****************************************************************************/
static nsresult CompareNodeToRange(nsINode* aNode, nsRange* aRange,
bool *outNodeBefore,
bool *outNodeAfter);
static bool IsNodeSelected(nsINode* aNode, uint32_t aStartOffset,
uint32_t aEndOffset);
static void CollectClientRects(nsLayoutUtils::RectCallback* aCollector,
nsRange* aRange,
nsINode* aStartParent, int32_t aStartOffset,
nsINode* aEndParent, int32_t aEndOffset,
bool aClampToEdge, bool aFlushLayout);
/**
* Scan this range for -moz-user-select:none nodes and split it up into
* multiple ranges to exclude those nodes. The resulting ranges are put
* in aOutRanges. If no -moz-user-select:none node is found in the range
* then |this| is unmodified and is the only range in aOutRanges.
* Otherwise, |this| will be modified so that it ends before the first
* -moz-user-select:none node and additional ranges may also be created.
* If all nodes in the range are -moz-user-select:none then aOutRanges
* will be empty.
* @param aOutRanges the resulting set of ranges
*/
void ExcludeNonSelectableNodes(nsTArray<nsRefPtr<nsRange>>* aOutRanges);
typedef nsTHashtable<nsPtrHashKey<nsRange> > RangeHashTable;
protected:
void RegisterCommonAncestor(nsINode* aNode);
void UnregisterCommonAncestor(nsINode* aNode);
nsINode* IsValidBoundary(nsINode* aNode);
// CharacterDataChanged set aNotInsertedYet to true to disable an assertion
// and suppress re-registering a range common ancestor node since
// the new text node of a splitText hasn't been inserted yet.
// CharacterDataChanged does the re-registering when needed.
void DoSetRange(nsINode* aStartN, int32_t aStartOffset,
nsINode* aEndN, int32_t aEndOffset,
nsINode* aRoot, bool aNotInsertedYet = false);
/**
* For a range for which IsInSelection() is true, return the common
* ancestor for the range. This method uses the selection bits and
* nsGkAtoms::range property on the nodes to quickly find the ancestor.
* That is, it's a faster version of GetCommonAncestor that only works
* for ranges in a Selection. The method will assert and the behavior
* is undefined if called on a range where IsInSelection() is false.
*/
nsINode* GetRegisteredCommonAncestor();
struct MOZ_STACK_CLASS AutoInvalidateSelection
{
explicit AutoInvalidateSelection(nsRange* aRange) : mRange(aRange)
{
#ifdef DEBUG
mWasInSelection = mRange->IsInSelection();
#endif
if (!mRange->IsInSelection() || mIsNested) {
return;
}
mIsNested = true;
mCommonAncestor = mRange->GetRegisteredCommonAncestor();
}
~AutoInvalidateSelection();
nsRange* mRange;
nsRefPtr<nsINode> mCommonAncestor;
#ifdef DEBUG
bool mWasInSelection;
#endif
static bool mIsNested;
};
nsCOMPtr<nsIDocument> mOwner;
nsCOMPtr<nsINode> mRoot;
nsCOMPtr<nsINode> mStartParent;
nsCOMPtr<nsINode> mEndParent;
int32_t mStartOffset;
int32_t mEndOffset;
bool mIsPositioned;
bool mIsDetached;
bool mMaySpanAnonymousSubtrees;
bool mInSelection;
bool mIsGenerated;
bool mStartOffsetWasIncremented;
bool mEndOffsetWasIncremented;
bool mEnableGravitationOnElementRemoval;
#ifdef DEBUG
int32_t mAssertNextInsertOrAppendIndex;
nsINode* mAssertNextInsertOrAppendNode;
#endif
};
inline nsISupports*
ToCanonicalSupports(nsRange* aRange)
{
return static_cast<nsIDOMRange*>(aRange);
}
inline nsISupports*
ToSupports(nsRange* aRange)
{
return static_cast<nsIDOMRange*>(aRange);
}
#endif /* nsRange_h___ */
|