no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / base / StaticRange.h
blobaf7054f843c69eaacd57cad0975601417fa81c6b
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/RangeUtils.h"
12 #include "mozilla/dom/AbstractRange.h"
13 #include "mozilla/dom/StaticRangeBinding.h"
14 #include "nsTArray.h"
15 #include "nsWrapperCache.h"
17 namespace mozilla {
18 class ErrorResult;
20 namespace dom {
22 class StaticRange final : public AbstractRange {
23 public:
24 StaticRange() = delete;
25 explicit StaticRange(const StaticRange& aOther) = delete;
27 static already_AddRefed<StaticRange> Constructor(const GlobalObject& global,
28 const StaticRangeInit& init,
29 ErrorResult& aRv);
31 /**
32 * The following Create() returns `nsRange` instance which is initialized
33 * only with aNode. The result is never positioned.
35 static already_AddRefed<StaticRange> Create(nsINode* aNode);
37 /**
38 * Create() may return `StaticRange` instance which is initialized with
39 * given range or points. If it fails initializing new range with the
40 * arguments, returns `nullptr`. `ErrorResult` is set to an error only
41 * when this returns `nullptr`. The error code indicates the reason why
42 * it couldn't initialize the instance.
44 static already_AddRefed<StaticRange> Create(
45 const AbstractRange* aAbstractRange, ErrorResult& aRv) {
46 MOZ_ASSERT(aAbstractRange);
47 return StaticRange::Create(aAbstractRange->StartRef(),
48 aAbstractRange->EndRef(), aRv);
50 static already_AddRefed<StaticRange> Create(nsINode* aStartContainer,
51 uint32_t aStartOffset,
52 nsINode* aEndContainer,
53 uint32_t aEndOffset,
54 ErrorResult& aRv) {
55 return StaticRange::Create(
56 RawRangeBoundary(aStartContainer, aStartOffset,
57 RangeBoundaryIsMutationObserved::No),
58 RawRangeBoundary(aEndContainer, aEndOffset,
59 RangeBoundaryIsMutationObserved::No),
60 aRv);
62 template <typename SPT, typename SRT, typename EPT, typename ERT>
63 static already_AddRefed<StaticRange> Create(
64 const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
65 const RangeBoundaryBase<EPT, ERT>& aEndBoundary, ErrorResult& aRv);
67 /**
68 * Returns true if the range is valid.
70 * @see https://dom.spec.whatwg.org/#staticrange-valid
72 bool IsValid() const;
74 void NotifyNodeBecomesShadowHost(nsINode* aNode) {
75 if (aNode == mStart.Container()) {
76 mStart.NotifyParentBecomesShadowHost();
79 if (aNode == mEnd.Container()) {
80 mEnd.NotifyParentBecomesShadowHost();
84 private:
85 // Whether the start and end points are in the same tree.
86 // They could be in different trees, i.e, cross shadow boundaries.
87 bool mAreStartAndEndInSameTree = false;
89 protected:
90 explicit StaticRange(nsINode* aNode)
91 : AbstractRange(aNode, /* aIsDynamicRange = */ false) {}
92 virtual ~StaticRange();
94 public:
95 NS_DECL_ISUPPORTS_INHERITED
96 NS_IMETHODIMP_(void) DeleteCycleCollectable(void) override;
97 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(StaticRange,
98 AbstractRange)
100 JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final;
103 * SetStartAndEnd() works similar to call both SetStart() and SetEnd().
104 * Different from calls them separately, this does nothing if either
105 * the start point or the end point is invalid point.
106 * If the specified start point is after the end point, the range will be
107 * collapsed at the end point. Similarly, if they are in different root,
108 * the range will be collapsed at the end point.
110 nsresult SetStartAndEnd(nsINode* aStartContainer, uint32_t aStartOffset,
111 nsINode* aEndContainer, uint32_t aEndOffset) {
112 return SetStartAndEnd(RawRangeBoundary(aStartContainer, aStartOffset),
113 RawRangeBoundary(aEndContainer, aEndOffset));
115 template <typename SPT, typename SRT, typename EPT, typename ERT>
116 nsresult SetStartAndEnd(const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
117 const RangeBoundaryBase<EPT, ERT>& aEndBoundary) {
118 return AbstractRange::SetStartAndEndInternal(aStartBoundary, aEndBoundary,
119 this);
122 protected:
124 * DoSetRange() is called when `AbstractRange::SetStartAndEndInternal()` sets
125 * mStart and mEnd.
127 * @param aStartBoundary Computed start point. This must equals or be before
128 * aEndBoundary in the DOM tree order.
129 * @param aEndBoundary Computed end point.
130 * @param aRootNode The root node.
132 template <typename SPT, typename SRT, typename EPT, typename ERT>
133 void DoSetRange(const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
134 const RangeBoundaryBase<EPT, ERT>& aEndBoundary,
135 nsINode* aRootNode);
137 static nsTArray<RefPtr<StaticRange>>* sCachedRanges;
139 friend class AbstractRange;
142 inline StaticRange* AbstractRange::AsStaticRange() {
143 MOZ_ASSERT(IsStaticRange());
144 return static_cast<StaticRange*>(this);
146 inline const StaticRange* AbstractRange::AsStaticRange() const {
147 MOZ_ASSERT(IsStaticRange());
148 return static_cast<const StaticRange*>(this);
151 } // namespace dom
152 } // namespace mozilla
154 #endif // #ifndef mozilla_dom_StaticRange_h