1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_AbstractRange_h
8 #define mozilla_dom_AbstractRange_h
12 #include "ErrorList.h"
13 #include "js/RootingAPI.h"
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/RangeBoundary.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/WeakPtr.h"
19 #include "nsCycleCollectionParticipant.h"
20 #include "nsISupports.h"
21 #include "nsWrapperCache.h"
29 namespace mozilla::dom
{
34 class AbstractRange
: public nsISupports
,
35 public nsWrapperCache
,
36 // For linking together selection-associated ranges.
37 public mozilla::LinkedListElement
<AbstractRange
> {
39 explicit AbstractRange(nsINode
* aNode
, bool aIsDynamicRange
);
40 virtual ~AbstractRange();
43 AbstractRange() = delete;
44 explicit AbstractRange(const AbstractRange
& aOther
) = delete;
47 * Called when the process is shutting down.
49 static void Shutdown();
51 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
52 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(AbstractRange
)
54 const RangeBoundary
& StartRef() const { return mStart
; }
55 const RangeBoundary
& EndRef() const { return mEnd
; }
57 nsIContent
* GetChildAtStartOffset() const {
58 return mStart
.GetChildAtOffset();
60 nsIContent
* GetChildAtEndOffset() const { return mEnd
.GetChildAtOffset(); }
61 bool IsPositioned() const { return mIsPositioned
; }
63 * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
65 nsINode
* GetClosestCommonInclusiveAncestor() const;
69 // If Range is created from JS, it's initialized with Document.createRange()
70 // and it collaps the range to start of the Document. Therefore, the
71 // following WebIDL methods are called only when `mIsPositioned` is true.
72 // So, it does not make sense to take `ErrorResult` as their parameter
73 // since its destruction cost may appear in profile. If you create range
74 // object from C++ and needs to check whether it's positioned, should call
75 // `IsPositioned()` directly.
77 nsINode
* GetStartContainer() const { return mStart
.Container(); }
78 nsINode
* GetEndContainer() const { return mEnd
.Container(); }
80 Document
* GetComposedDocOfContainers() const {
81 return mStart
.Container() ? mStart
.Container()->GetComposedDoc() : nullptr;
84 // FYI: Returns 0 if it's not positioned.
85 uint32_t StartOffset() const {
86 return static_cast<uint32_t>(
87 *mStart
.Offset(RangeBoundary::OffsetFilter::kValidOrInvalidOffsets
));
90 // FYI: Returns 0 if it's not positioned.
91 uint32_t EndOffset() const {
92 return static_cast<uint32_t>(
93 *mEnd
.Offset(RangeBoundary::OffsetFilter::kValidOrInvalidOffsets
));
95 bool Collapsed() const {
96 return !mIsPositioned
|| (mStart
.Container() == mEnd
.Container() &&
97 StartOffset() == EndOffset());
100 nsINode
* GetParentObject() const;
101 virtual JSObject
* WrapObject(JSContext
* aCx
,
102 JS::Handle
<JSObject
*> aGivenProto
) override
;
104 bool HasEqualBoundaries(const AbstractRange
& aOther
) const {
105 return (mStart
== aOther
.mStart
) && (mEnd
== aOther
.mEnd
);
107 bool IsDynamicRange() const { return mIsDynamicRange
; }
108 bool IsStaticRange() const { return !mIsDynamicRange
; }
109 inline nsRange
* AsDynamicRange();
110 inline const nsRange
* AsDynamicRange() const;
111 inline StaticRange
* AsStaticRange();
112 inline const StaticRange
* AsStaticRange() const;
115 * Return true if this range is part of a Selection object
116 * and isn't detached.
118 bool IsInAnySelection() const { return !mSelections
.IsEmpty(); }
120 MOZ_CAN_RUN_SCRIPT
void RegisterSelection(
121 mozilla::dom::Selection
& aSelection
);
123 void UnregisterSelection(const mozilla::dom::Selection
& aSelection
);
126 * Returns a list of all Selections the range is associated with.
128 const nsTArray
<WeakPtr
<Selection
>>& GetSelections() const;
131 * Return true if this range is in |aSelection|.
133 bool IsInSelection(const mozilla::dom::Selection
& aSelection
) const;
136 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
,
138 static nsresult
SetStartAndEndInternal(
139 const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
140 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
, RangeType
* aRange
);
142 template <class RangeType
>
143 static bool MaybeCacheToReuse(RangeType
& aInstance
);
145 void Init(nsINode
* aNode
);
147 friend std::ostream
& operator<<(std::ostream
& aStream
,
148 const AbstractRange
& aRange
) {
149 if (aRange
.Collapsed()) {
150 aStream
<< "{ mStart=mEnd=" << aRange
.mStart
;
152 aStream
<< "{ mStart=" << aRange
.mStart
<< ", mEnd=" << aRange
.mEnd
;
154 return aStream
<< ", mIsGenerated="
155 << (aRange
.mIsGenerated
? "true" : "false")
157 << (aRange
.mIsPositioned
? "true" : "false")
158 << ", mIsDynamicRange="
159 << (aRange
.mIsDynamicRange
? "true" : "false") << " }";
163 * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
165 void RegisterClosestCommonInclusiveAncestor(nsINode
* aNode
);
167 * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
169 void UnregisterClosestCommonInclusiveAncestor(nsINode
* aNode
,
172 void UpdateCommonAncestorIfNecessary();
174 static void MarkDescendants(const nsINode
& aNode
);
175 static void UnmarkDescendants(const nsINode
& aNode
);
178 void ClearForReuse();
181 RefPtr
<Document
> mOwner
;
182 RangeBoundary mStart
;
185 // A Range can be part of multiple |Selection|s. This is a very rare use case.
186 AutoTArray
<WeakPtr
<Selection
>, 1> mSelections
;
187 // mRegisteredClosestCommonInclusiveAncestor is only non-null when the range
188 // IsInAnySelection().
189 nsCOMPtr
<nsINode
> mRegisteredClosestCommonInclusiveAncestor
;
191 // `true` if `mStart` and `mEnd` are set for StaticRange or set and valid
195 // Used by nsRange, but this should have this for minimizing the size.
197 // Used by nsRange, but this should have this for minimizing the size.
200 // true if this is an `nsRange` object.
201 const bool mIsDynamicRange
;
203 static bool sHasShutDown
;
206 } // namespace mozilla::dom
208 #endif // #ifndef mozilla_dom_AbstractRange_h