Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / base / StaticRange.h
bloba6f677130da9e80162f3953a21f1205738d0442b
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"
13 #include "nsTArray.h"
14 #include "nsWrapperCache.h"
16 namespace mozilla {
17 class ErrorResult;
19 namespace dom {
21 class StaticRange final : public AbstractRange {
22 public:
23 StaticRange() = delete;
24 explicit StaticRange(const StaticRange& aOther) = delete;
26 static already_AddRefed<StaticRange> Constructor(const GlobalObject& global,
27 const StaticRangeInit& init,
28 ErrorResult& aRv);
30 /**
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);
36 /**
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,
52 uint32_t aEndOffset,
53 ErrorResult& aRv) {
54 return StaticRange::Create(
55 RawRangeBoundary(aStartContainer, aStartOffset,
56 RangeBoundaryIsMutationObserved::No),
57 RawRangeBoundary(aEndContainer, aEndOffset,
58 RangeBoundaryIsMutationObserved::No),
59 aRv);
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);
66 /**
67 * Returns true if the range is valid.
69 * @see https://dom.spec.whatwg.org/#staticrange-valid
71 bool IsValid() const;
73 protected:
74 explicit StaticRange(nsINode* aNode)
75 : AbstractRange(aNode, /* aIsDynamicRange = */ false) {}
76 virtual ~StaticRange();
78 public:
79 NS_DECL_ISUPPORTS_INHERITED
80 NS_IMETHODIMP_(void) DeleteCycleCollectable(void) override;
81 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(StaticRange,
82 AbstractRange)
84 JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final;
86 /**
87 * SetStartAndEnd() works similar to call both SetStart() and SetEnd().
88 * Different from calls them separately, this does nothing if either
89 * the start point or the end point is invalid point.
90 * If the specified start point is after the end point, the range will be
91 * collapsed at the end point. Similarly, if they are in different root,
92 * the range will be collapsed at the end point.
94 nsresult SetStartAndEnd(nsINode* aStartContainer, uint32_t aStartOffset,
95 nsINode* aEndContainer, uint32_t aEndOffset) {
96 return SetStartAndEnd(RawRangeBoundary(aStartContainer, aStartOffset),
97 RawRangeBoundary(aEndContainer, aEndOffset));
99 template <typename SPT, typename SRT, typename EPT, typename ERT>
100 nsresult SetStartAndEnd(const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
101 const RangeBoundaryBase<EPT, ERT>& aEndBoundary) {
102 return AbstractRange::SetStartAndEndInternal(aStartBoundary, aEndBoundary,
103 this);
106 protected:
108 * DoSetRange() is called when `AbstractRange::SetStartAndEndInternal()` sets
109 * mStart and mEnd.
111 * @param aStartBoundary Computed start point. This must equals or be before
112 * aEndBoundary in the DOM tree order.
113 * @param aEndBoundary Computed end point.
114 * @param aRootNode The root node.
116 template <typename SPT, typename SRT, typename EPT, typename ERT>
117 void DoSetRange(const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
118 const RangeBoundaryBase<EPT, ERT>& aEndBoundary,
119 nsINode* aRootNode);
121 static nsTArray<RefPtr<StaticRange>>* sCachedRanges;
123 friend class AbstractRange;
126 inline StaticRange* AbstractRange::AsStaticRange() {
127 MOZ_ASSERT(IsStaticRange());
128 return static_cast<StaticRange*>(this);
130 inline const StaticRange* AbstractRange::AsStaticRange() const {
131 MOZ_ASSERT(IsStaticRange());
132 return static_cast<const StaticRange*>(this);
135 } // namespace dom
136 } // namespace mozilla
138 #endif // #ifndef mozilla_dom_StaticRange_h