Bug 1858921 - Part 1: Remove unnecessary Allocator.h includes r=sfink
[gecko.git] / accessible / base / TextRange.h
blob121dbe8399e900941f13dea807caa73c857cda8a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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_a11y_TextRange_h__
8 #define mozilla_a11y_TextRange_h__
10 #include <utility>
12 #include "nsTArray.h"
14 class nsRange;
16 namespace mozilla {
17 namespace dom {
18 class Selection;
19 } // namespace dom
20 namespace a11y {
22 class Accessible;
23 class LocalAccessible;
25 /**
26 * A text point (HyperText + offset), represents a boundary of text range.
27 * In new code, This should only be used when you explicitly need to deal with
28 * HyperText containers and offsets, including embedded objects; e.g. for
29 * IAccessible2 and ATK. Otherwise, use TextLeafPoint instead.
31 struct TextPoint final {
32 TextPoint(Accessible* aContainer, int32_t aOffset)
33 : mContainer(aContainer), mOffset(aOffset) {}
34 TextPoint(const TextPoint& aPoint)
35 : mContainer(aPoint.mContainer), mOffset(aPoint.mOffset) {}
37 Accessible* mContainer;
38 int32_t mOffset;
40 bool operator==(const TextPoint& aPoint) const {
41 return mContainer == aPoint.mContainer && mOffset == aPoint.mOffset;
43 bool operator<(const TextPoint& aPoint) const;
46 /**
47 * Represents a HyperText range within the text control or document.
48 * In new code, This should only be used when you explicitly need to deal with
49 * HyperText containers and offsets, including embedded objects; e.g. for
50 * IAccessible2 and ATK. Otherwise, use TextLeafRange instead.
52 class TextRange final {
53 public:
54 TextRange(Accessible* aRoot, Accessible* aStartContainer,
55 int32_t aStartOffset, Accessible* aEndContainer,
56 int32_t aEndOffset);
57 TextRange() : mStartOffset{0}, mEndOffset{0} {}
58 TextRange(TextRange&& aRange)
59 : mRoot(std::move(aRange.mRoot)),
60 mStartContainer(std::move(aRange.mStartContainer)),
61 mEndContainer(std::move(aRange.mEndContainer)),
62 mStartOffset(aRange.mStartOffset),
63 mEndOffset(aRange.mEndOffset) {}
65 TextRange& operator=(TextRange&& aRange) {
66 mRoot = std::move(aRange.mRoot);
67 mStartContainer = std::move(aRange.mStartContainer);
68 mEndContainer = std::move(aRange.mEndContainer);
69 mStartOffset = aRange.mStartOffset;
70 mEndOffset = aRange.mEndOffset;
71 return *this;
74 Accessible* Root() { return mRoot; }
75 Accessible* StartContainer() const { return mStartContainer; }
76 int32_t StartOffset() const { return mStartOffset; }
77 Accessible* EndContainer() const { return mEndContainer; }
78 int32_t EndOffset() const { return mEndOffset; }
80 bool operator==(const TextRange& aRange) const {
81 return mStartContainer == aRange.mStartContainer &&
82 mStartOffset == aRange.mStartOffset &&
83 mEndContainer == aRange.mEndContainer &&
84 mEndOffset == aRange.mEndOffset;
87 TextPoint StartPoint() const {
88 return TextPoint(mStartContainer, mStartOffset);
90 TextPoint EndPoint() const { return TextPoint(mEndContainer, mEndOffset); }
92 /**
93 * Return a container containing both start and end points.
95 Accessible* Container() const;
97 /**
98 * Crops the range if it overlaps the given accessible element boundaries,
99 * returns true if the range was cropped successfully.
101 bool Crop(Accessible* aContainer);
104 * Convert stored hypertext offsets into DOM offsets and assign it to DOM
105 * range.
107 * Note that if start and/or end accessible offsets are in generated content
108 * such as ::before or
109 * ::after, the result range excludes the generated content. See also
110 * ClosestNotGeneratedDOMPoint() for more information.
112 * @param aRange [in, out] the range whose bounds to set
113 * @param aReversed [out] whether the start/end offsets were reversed.
114 * @return true if conversion was successful
116 bool AssignDOMRange(nsRange* aRange, bool* aReversed = nullptr) const;
119 * Return true if this TextRange object represents an actual range of text.
121 bool IsValid() const { return mRoot; }
123 void SetStartPoint(Accessible* aContainer, int32_t aOffset) {
124 mStartContainer = aContainer;
125 mStartOffset = aOffset;
127 void SetEndPoint(Accessible* aContainer, int32_t aOffset) {
128 mStartContainer = aContainer;
129 mStartOffset = aOffset;
132 static void TextRangesFromSelection(dom::Selection* aSelection,
133 nsTArray<TextRange>* aRanges);
135 private:
136 TextRange(const TextRange& aRange) = delete;
137 TextRange& operator=(const TextRange& aRange) = delete;
139 friend class HyperTextAccessible;
140 friend class xpcAccessibleTextRange;
142 void Set(Accessible* aRoot, Accessible* aStartContainer, int32_t aStartOffset,
143 Accessible* aEndContainer, int32_t aEndOffset);
146 * A helper method returning a common parent for two given accessible
147 * elements.
149 Accessible* CommonParent(Accessible* aAcc1, Accessible* aAcc2,
150 nsTArray<Accessible*>* aParents1, uint32_t* aPos1,
151 nsTArray<Accessible*>* aParents2,
152 uint32_t* aPos2) const;
154 Accessible* mRoot;
155 Accessible* mStartContainer;
156 Accessible* mEndContainer;
157 int32_t mStartOffset;
158 int32_t mEndOffset;
161 } // namespace a11y
162 } // namespace mozilla
164 #endif