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(RawRangeBoundary(aStartContainer
, aStartOffset
),
55 RawRangeBoundary(aEndContainer
, aEndOffset
),
58 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
>
59 static already_AddRefed
<StaticRange
> Create(
60 const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
61 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
, ErrorResult
& aRv
);
64 explicit StaticRange(nsINode
* aNode
) : AbstractRange(aNode
) {}
65 virtual ~StaticRange() = default;
68 NS_DECL_ISUPPORTS_INHERITED
69 NS_IMETHODIMP_(void) DeleteCycleCollectable(void) override
;
70 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(StaticRange
,
73 JSObject
* WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) final
;
76 * SetStartAndEnd() works similar to call both SetStart() and SetEnd().
77 * Different from calls them separately, this does nothing if either
78 * the start point or the end point is invalid point.
79 * If the specified start point is after the end point, the range will be
80 * collapsed at the end point. Similarly, if they are in different root,
81 * the range will be collapsed at the end point.
83 nsresult
SetStartAndEnd(nsINode
* aStartContainer
, uint32_t aStartOffset
,
84 nsINode
* aEndContainer
, uint32_t aEndOffset
) {
85 return SetStartAndEnd(RawRangeBoundary(aStartContainer
, aStartOffset
),
86 RawRangeBoundary(aEndContainer
, aEndOffset
));
88 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
>
89 nsresult
SetStartAndEnd(const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
90 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
) {
91 return AbstractRange::SetStartAndEndInternal(aStartBoundary
, aEndBoundary
,
97 * DoSetRange() is called when `AbstractRange::SetStartAndEndInternal()` sets
100 * @param aStartBoundary Computed start point. This must equals or be before
101 * aEndBoundary in the DOM tree order.
102 * @param aEndBoundary Computed end point.
103 * @param aRootNode The root node.
105 template <typename SPT
, typename SRT
, typename EPT
, typename ERT
>
106 void DoSetRange(const RangeBoundaryBase
<SPT
, SRT
>& aStartBoundary
,
107 const RangeBoundaryBase
<EPT
, ERT
>& aEndBoundary
,
110 static nsTArray
<RefPtr
<StaticRange
>>* sCachedRanges
;
112 friend class AbstractRange
;
116 } // namespace mozilla
118 #endif // #ifndef mozilla_dom_StaticRange_h