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_StaticRange_h
8 #define mozilla_dom_StaticRange_h
10 #include "mozilla/RangeBoundary.h"
11 #include "mozilla/dom/AbstractRange.h"
12 #include "mozilla/dom/StaticRangeBinding.h"
14 #include "nsWrapperCache.h"
21 class StaticRange final
: public AbstractRange
{
23 StaticRange() = delete;
24 explicit StaticRange(const StaticRange
& aOther
) = delete;
26 static already_AddRefed
<StaticRange
> Constructor(const GlobalObject
& global
,
27 const StaticRangeInit
& init
,
31 * The following Create() returns `nsRange` instance which is initialized
32 * only with aNode. The result is never positioned.
34 static already_AddRefed
<StaticRange
> Create(nsINode
* aNode
);
37 * Create() may return `StaticRange` instance which is initialized with
38 * given range or points. If it fails initializing new range with the
39 * arguments, returns `nullptr`. `ErrorResult` is set to an error only
40 * when this returns `nullptr`. The error code indicates the reason why
41 * it couldn't initialize the instance.
43 static already_AddRefed
<StaticRange
> Create(
44 const AbstractRange
* aAbstractRange
, ErrorResult
& aRv
) {
45 MOZ_ASSERT(aAbstractRange
);
46 return StaticRange::Create(aAbstractRange
->StartRef(),
47 aAbstractRange
->EndRef(), aRv
);
49 static already_AddRefed
<StaticRange
> Create(nsINode
* aStartContainer
,
50 uint32_t aStartOffset
,
51 nsINode
* aEndContainer
,
54 return StaticRange::Create(
55 RawRangeBoundary(aStartContainer
, aStartOffset
,
56 RangeBoundaryIsMutationObserved::No
),
57 RawRangeBoundary(aEndContainer
, aEndOffset
,
58 RangeBoundaryIsMutationObserved::No
),
61 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
>
62 static already_AddRefed
<StaticRange
> Create(
63 const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
64 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
, ErrorResult
& aRv
);
67 * Returns true if the range is valid.
69 * @see https://dom.spec.whatwg.org/#staticrange-valid
71 bool IsValid() const {
72 return mStart
.IsSetAndValid() && mEnd
.IsSetAndValid();
76 explicit StaticRange(nsINode
* aNode
)
77 : AbstractRange(aNode
, /* aIsDynamicRange = */ false) {}
78 virtual ~StaticRange();
81 NS_DECL_ISUPPORTS_INHERITED
82 NS_IMETHODIMP_(void) DeleteCycleCollectable(void) override
;
83 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(StaticRange
,
86 JSObject
* WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) final
;
89 * SetStartAndEnd() works similar to call both SetStart() and SetEnd().
90 * Different from calls them separately, this does nothing if either
91 * the start point or the end point is invalid point.
92 * If the specified start point is after the end point, the range will be
93 * collapsed at the end point. Similarly, if they are in different root,
94 * the range will be collapsed at the end point.
96 nsresult
SetStartAndEnd(nsINode
* aStartContainer
, uint32_t aStartOffset
,
97 nsINode
* aEndContainer
, uint32_t aEndOffset
) {
98 return SetStartAndEnd(RawRangeBoundary(aStartContainer
, aStartOffset
),
99 RawRangeBoundary(aEndContainer
, aEndOffset
));
101 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
>
102 nsresult
SetStartAndEnd(const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
103 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
) {
104 return AbstractRange::SetStartAndEndInternal(aStartBoundary
, aEndBoundary
,
110 * DoSetRange() is called when `AbstractRange::SetStartAndEndInternal()` sets
113 * @param aStartBoundary Computed start point. This must equals or be before
114 * aEndBoundary in the DOM tree order.
115 * @param aEndBoundary Computed end point.
116 * @param aRootNode The root node.
118 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
>
119 void DoSetRange(const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
120 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
,
123 static nsTArray
<RefPtr
<StaticRange
>>* sCachedRanges
;
125 friend class AbstractRange
;
128 inline StaticRange
* AbstractRange::AsStaticRange() {
129 MOZ_ASSERT(IsStaticRange());
130 return static_cast<StaticRange
*>(this);
132 inline const StaticRange
* AbstractRange::AsStaticRange() const {
133 MOZ_ASSERT(IsStaticRange());
134 return static_cast<const StaticRange
*>(this);
138 } // namespace mozilla
140 #endif // #ifndef mozilla_dom_StaticRange_h