no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / base / Highlight.h
blob7c542ff4f32406a6ea4859434ef47cdaf2897662
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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_Highlight_h
8 #define mozilla_dom_Highlight_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/BindingDeclarations.h"
12 #include "mozilla/dom/HighlightBinding.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsAtomHashKeys.h"
16 #include "nsTHashSet.h"
17 #include "nsTArray.h"
18 #include "nsWrapperCache.h"
20 class nsFrameSelection;
21 class nsPIDOMWindowInner;
22 namespace mozilla {
23 class ErrorResult;
26 namespace mozilla::dom {
27 class AbstractRange;
28 class Document;
29 class HighlightRegistry;
30 class Selection;
32 /**
33 * @brief Collection of all data of a highlight instance.
35 * This struct is intended to be allocated on the stack and passed on
36 * to the `nsFrameSelection` and layout code.
38 struct HighlightSelectionData {
39 RefPtr<nsAtom> mHighlightName;
40 RefPtr<Highlight> mHighlight;
43 /**
44 * @brief Representation of a custom `Highlight`.
46 * A `Highlight` is defined in JS as a collection of `AbstractRange`s.
47 * Furthermore, a custom highlight contains the highlight type and priority.
49 * A highlight is added to a document using the `HighlightRegistry` interface.
50 * A highlight can be added to a document using different names as well as to
51 * multiple `HighlightRegistries`.
52 * To propagate runtime changes of the highlight to its registries, an
53 * observer pattern is implemented.
55 * The spec defines this class as a `setlike`. To allow access and iteration
56 * of the setlike contents from C++, the insertion and deletion operations are
57 * overridden and the Ranges are also stored internally in an Array.
59 * @see https://drafts.csswg.org/css-highlight-api-1/#creation
61 class Highlight final : public nsISupports, public nsWrapperCache {
62 public:
63 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
64 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Highlight)
66 protected:
67 MOZ_CAN_RUN_SCRIPT Highlight(
68 const Sequence<OwningNonNull<AbstractRange>>& aInitialRanges,
69 nsPIDOMWindowInner* aWindow, ErrorResult& aRv);
70 ~Highlight() = default;
72 public:
73 /**
74 * @brief Adds `this` to `aHighlightRegistry`.
76 * Highlights must know of all registry objects which contain them, so that
77 * the registries can be notified when a property of the Highlight changes.
79 * Since a Highlight can be part of a registry using different names,
80 * the name has to be provided as well.
82 void AddToHighlightRegistry(HighlightRegistry& aHighlightRegistry,
83 nsAtom& aHighlightName);
85 /**
86 * @brief Removes `this` from `aHighlightRegistry`.
88 void RemoveFromHighlightRegistry(HighlightRegistry& aHighlightRegistry,
89 nsAtom& aHighlightName);
91 /**
92 * @brief Creates a Highlight Selection using the given ranges.
94 MOZ_CAN_RUN_SCRIPT already_AddRefed<Selection> CreateHighlightSelection(
95 nsAtom* aHighlightName, nsFrameSelection* aFrameSelection);
97 // WebIDL interface
98 nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
100 JSObject* WrapObject(JSContext* aCx,
101 JS::Handle<JSObject*> aGivenProto) override;
103 static MOZ_CAN_RUN_SCRIPT_BOUNDARY already_AddRefed<Highlight> Constructor(
104 const GlobalObject& aGlobal,
105 const Sequence<OwningNonNull<AbstractRange>>& aInitialRanges,
106 ErrorResult& aRv);
109 * @brief Priority of this highlight.
111 * Priority is used to stack overlapping highlights.
113 int32_t Priority() const { return mPriority; }
116 * @brief Set the priority of this Highlight.
118 * Priority is used to stack overlapping highlights.
120 void SetPriority(int32_t aPriority) { mPriority = aPriority; }
123 * @brief The HighlightType of this Highlight (Highlight, Spelling Error,
124 * Grammar Error)
126 HighlightType Type() const { return mHighlightType; }
129 * @brief Sets the HighlightType (Highlight, Spelling Error, Grammar Error)
131 void SetType(HighlightType aHighlightType) {
132 mHighlightType = aHighlightType;
136 * @brief This mirrors the `size` property in JS world (_not_ exposed via
137 * webIDL)
139 uint32_t Size() const { return mRanges.Length(); }
142 * @brief Adds a `Range` to this highlight.
144 * This adds `aRange` both to the setlike data storage and the internal one
145 * needed for iteration, if it is not yet present.
147 * Also notifies all `HighlightRegistry` instances.
149 MOZ_CAN_RUN_SCRIPT void Add(AbstractRange& aRange, ErrorResult& aRv);
152 * @brief Removes all ranges from this highlight.
154 * This removes all highlights from the setlike data structure as well as from
155 * the internal one.
157 * Also notifies all `HighlightRegistry` instances.
159 MOZ_CAN_RUN_SCRIPT void Clear(ErrorResult& aRv);
162 * @brief Removes `aRange` from this highlight.
164 * This removes `aRange` from the setlike data structure as well as from the
165 * internal one.
167 * Also notifies all `HighlightRegistry` instances.
169 * @return As per spec, returns true if the range was deleted.
171 MOZ_CAN_RUN_SCRIPT bool Delete(AbstractRange& aRange, ErrorResult& aRv);
173 private:
174 RefPtr<nsPIDOMWindowInner> mWindow;
177 * All Range objects contained in this highlight.
179 nsTArray<RefPtr<AbstractRange>> mRanges;
182 * Type of this highlight.
183 * @see HighlightType
185 HighlightType mHighlightType{HighlightType::Highlight};
188 * Priority of this highlight.
190 * If highlights are overlapping, the priority can
191 * be used to prioritize. If the priorities of all
192 * Highlights involved are equal, the highlights are
193 * stacked in order of ther insertion into the
194 * `HighlightRegistry`.
196 int32_t mPriority{0};
199 * All highlight registries that contain this Highlight.
201 * A highlight can be included in several registries
202 * using several names.
204 * Note: Storing `HighlightRegistry` as raw pointer is safe here
205 * because it unregisters itself from `this` when it is destroyed/CC'd
207 nsTHashMap<nsPtrHashKey<HighlightRegistry>, nsTHashSet<RefPtr<nsAtom>>>
208 mHighlightRegistries;
211 } // namespace mozilla::dom
213 #endif // mozilla_dom_Highlight_h