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
10 #include "mozilla/RangeBoundary.h"
11 #include "mozilla/dom/Document.h"
12 #include "nsIContent.h"
14 #include "nsWrapperCache.h"
19 class AbstractRange
: public nsISupports
, public nsWrapperCache
{
21 explicit AbstractRange(nsINode
* aNode
);
22 virtual ~AbstractRange() = default;
25 AbstractRange() = delete;
26 explicit AbstractRange(const AbstractRange
& aOther
) = delete;
29 * Called when the process is shutting down.
31 static void Shutdown();
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AbstractRange
)
36 const RangeBoundary
& StartRef() const { return mStart
; }
37 const RangeBoundary
& EndRef() const { return mEnd
; }
39 nsIContent
* GetChildAtStartOffset() const {
40 return mStart
.GetChildAtOffset();
42 nsIContent
* GetChildAtEndOffset() const { return mEnd
.GetChildAtOffset(); }
43 bool IsPositioned() const { return mIsPositioned
; }
45 * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
47 nsINode
* GetClosestCommonInclusiveAncestor() const;
51 // If Range is created from JS, it's initialized with Document.createRange()
52 // and it collaps the range to start of the Document. Therefore, the
53 // following WebIDL methods are called only when `mIsPositioned` is true.
54 // So, it does not make sense to take `ErrorResult` as their parameter
55 // since its destruction cost may appear in profile. If you create range
56 // object from C++ and needs to check whether it's positioned, should call
57 // `IsPositioned()` directly.
59 nsINode
* GetStartContainer() const { return mStart
.Container(); }
60 nsINode
* GetEndContainer() const { return mEnd
.Container(); }
62 // FYI: Returns 0 if it's not positioned.
63 uint32_t StartOffset() const {
64 return static_cast<uint32_t>(
65 *mStart
.Offset(RangeBoundary::OffsetFilter::kValidOrInvalidOffsets
));
68 // FYI: Returns 0 if it's not positioned.
69 uint32_t EndOffset() const {
70 return static_cast<uint32_t>(
71 *mEnd
.Offset(RangeBoundary::OffsetFilter::kValidOrInvalidOffsets
));
73 bool Collapsed() const {
74 return !mIsPositioned
|| (mStart
.Container() == mEnd
.Container() &&
75 StartOffset() == EndOffset());
78 nsINode
* GetParentObject() const { return mOwner
; }
79 virtual JSObject
* WrapObject(JSContext
* aCx
,
80 JS::Handle
<JSObject
*> aGivenProto
) override
;
82 bool HasEqualBoundaries(const AbstractRange
& aOther
) const {
83 return (mStart
== aOther
.mStart
) && (mEnd
== aOther
.mEnd
);
87 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
,
89 static nsresult
SetStartAndEndInternal(
90 const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
91 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
, RangeType
* aRange
);
93 template <class RangeType
>
94 static bool MaybeCacheToReuse(RangeType
& aInstance
);
96 void Init(nsINode
* aNode
);
99 void ClearForReuse() {
101 mStart
= RangeBoundary();
102 mEnd
= RangeBoundary();
103 mIsPositioned
= false;
104 mIsGenerated
= false;
109 RefPtr
<Document
> mOwner
;
110 RangeBoundary mStart
;
112 // `true` if `mStart` and `mEnd` are set for StaticRange or set and valid
116 // Used by nsRange, but this should have this for minimizing the size.
118 // Used by nsRange, but this should have this for minimizing the size.
121 static bool sHasShutDown
;
125 } // namespace mozilla
127 #endif // #ifndef mozilla_dom_AbstractRange_h