Bug 1883888 [wpt PR 44943] - Name unhandledrejection event correctly, a=testonly
[gecko.git] / accessible / base / AccIterator.h
blob61b126c81294925c7b30352a21c8d1613823ea7b
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_AccIterator_h__
8 #define mozilla_a11y_AccIterator_h__
10 #include "Filters.h"
11 #include "mozilla/a11y/DocAccessible.h"
12 #include "nsTArray.h"
14 #include <memory>
16 class nsITreeView;
18 namespace mozilla {
19 namespace a11y {
20 class DocAccessibleParent;
22 /**
23 * AccIterable is a basic interface for iterators over accessibles.
25 class AccIterable {
26 public:
27 virtual ~AccIterable() {}
28 virtual Accessible* Next() = 0;
30 private:
31 friend class Relation;
32 std::unique_ptr<AccIterable> mNextIter;
35 /**
36 * Allows to iterate through accessible children or subtree complying with
37 * filter function.
39 class AccIterator : public AccIterable {
40 public:
41 AccIterator(const LocalAccessible* aRoot, filters::FilterFuncPtr aFilterFunc);
42 virtual ~AccIterator();
44 /**
45 * Return next accessible complying with filter function. Return the first
46 * accessible for the first time.
48 virtual LocalAccessible* Next() override;
50 private:
51 AccIterator();
52 AccIterator(const AccIterator&);
53 AccIterator& operator=(const AccIterator&);
55 struct IteratorState {
56 explicit IteratorState(const LocalAccessible* aParent,
57 IteratorState* mParentState = nullptr);
59 const LocalAccessible* mParent;
60 int32_t mIndex;
61 IteratorState* mParentState;
64 filters::FilterFuncPtr mFilterFunc;
65 IteratorState* mState;
68 /**
69 * Allows to traverse through related accessibles that are pointing to the given
70 * dependent accessible by relation attribute. This is typically used to query
71 * implicit reverse relations; e.g. calculating the LABEL_FOR relation for a
72 * label where that label was referenced using aria-labelledby.
74 class RelatedAccIterator : public AccIterable {
75 public:
76 /**
77 * Constructor.
79 * @param aDocument [in] the document accessible the related
80 * & accessibles belong to.
81 * @param aDependentContent [in] the content of dependent accessible that
82 * relations were requested for
83 * @param aRelAttr [in] relation attribute that relations are
84 * pointed by, null for all relations
86 RelatedAccIterator(DocAccessible* aDocument, nsIContent* aDependentContent,
87 nsAtom* aRelAttr);
89 virtual ~RelatedAccIterator() {}
91 /**
92 * Return next related accessible for the given dependent accessible.
94 virtual LocalAccessible* Next() override;
96 private:
97 RelatedAccIterator();
98 RelatedAccIterator(const RelatedAccIterator&);
99 RelatedAccIterator& operator=(const RelatedAccIterator&);
101 DocAccessible* mDocument;
102 nsIContent* mDependentContent;
103 nsAtom* mRelAttr;
104 DocAccessible::AttrRelProviders* mProviders;
105 uint32_t mIndex;
106 bool mIsWalkingDependentElements;
110 * Used to iterate through HTML labels associated with the given accessible.
112 class HTMLLabelIterator : public AccIterable {
113 public:
114 enum LabelFilter { eAllLabels, eSkipAncestorLabel };
116 HTMLLabelIterator(DocAccessible* aDocument,
117 const LocalAccessible* aAccessible,
118 LabelFilter aFilter = eAllLabels);
120 virtual ~HTMLLabelIterator() {}
123 * Return next label accessible associated with the given element.
125 virtual LocalAccessible* Next() override;
127 private:
128 HTMLLabelIterator();
129 HTMLLabelIterator(const HTMLLabelIterator&);
130 HTMLLabelIterator& operator=(const HTMLLabelIterator&);
132 bool IsLabel(LocalAccessible* aLabel);
134 RelatedAccIterator mRelIter;
135 // XXX: replace it on weak reference (bug 678429), it's safe to use raw
136 // pointer now because iterators life cycle is short.
137 const LocalAccessible* mAcc;
138 LabelFilter mLabelFilter;
142 * Used to iterate through HTML outputs associated with the given element.
144 class HTMLOutputIterator : public AccIterable {
145 public:
146 HTMLOutputIterator(DocAccessible* aDocument, nsIContent* aElement);
147 virtual ~HTMLOutputIterator() {}
150 * Return next output accessible associated with the given element.
152 virtual LocalAccessible* Next() override;
154 private:
155 HTMLOutputIterator();
156 HTMLOutputIterator(const HTMLOutputIterator&);
157 HTMLOutputIterator& operator=(const HTMLOutputIterator&);
159 RelatedAccIterator mRelIter;
163 * Used to iterate through XUL labels associated with the given element.
165 class XULLabelIterator : public AccIterable {
166 public:
167 XULLabelIterator(DocAccessible* aDocument, nsIContent* aElement);
168 virtual ~XULLabelIterator() {}
171 * Return next label accessible associated with the given element.
173 virtual LocalAccessible* Next() override;
175 private:
176 XULLabelIterator();
177 XULLabelIterator(const XULLabelIterator&);
178 XULLabelIterator& operator=(const XULLabelIterator&);
180 RelatedAccIterator mRelIter;
184 * Used to iterate through XUL descriptions associated with the given element.
186 class XULDescriptionIterator : public AccIterable {
187 public:
188 XULDescriptionIterator(DocAccessible* aDocument, nsIContent* aElement);
189 virtual ~XULDescriptionIterator() {}
192 * Return next description accessible associated with the given element.
194 virtual LocalAccessible* Next() override;
196 private:
197 XULDescriptionIterator();
198 XULDescriptionIterator(const XULDescriptionIterator&);
199 XULDescriptionIterator& operator=(const XULDescriptionIterator&);
201 RelatedAccIterator mRelIter;
205 * Used to iterate through IDs, elements or accessibles pointed by IDRefs
206 * attribute. Note, any method used to iterate through IDs, elements, or
207 * accessibles moves iterator to next position.
209 class IDRefsIterator : public AccIterable {
210 public:
211 IDRefsIterator(DocAccessible* aDoc, nsIContent* aContent,
212 nsAtom* aIDRefsAttr);
213 virtual ~IDRefsIterator() {}
216 * Return next ID.
218 const nsDependentSubstring NextID();
221 * Return next element.
223 nsIContent* NextElem();
226 * Return the element with the given ID.
228 static dom::Element* GetElem(nsIContent* aContent, const nsAString& aID);
229 dom::Element* GetElem(const nsDependentSubstring& aID);
231 // AccIterable
232 virtual LocalAccessible* Next() override;
234 private:
235 IDRefsIterator();
236 IDRefsIterator(const IDRefsIterator&);
237 IDRefsIterator operator=(const IDRefsIterator&);
239 nsString mIDs;
240 nsIContent* mContent;
241 DocAccessible* mDoc;
242 nsAString::index_type mCurrIdx;
246 * Iterator that points to a single accessible returning it on the first call
247 * to Next().
249 class SingleAccIterator : public AccIterable {
250 public:
251 explicit SingleAccIterator(Accessible* aTarget) : mAcc(aTarget) {}
252 virtual ~SingleAccIterator() {}
254 virtual Accessible* Next() override;
256 private:
257 SingleAccIterator();
258 SingleAccIterator(const SingleAccIterator&);
259 SingleAccIterator& operator=(const SingleAccIterator&);
261 Accessible* mAcc;
265 * Used to iterate items of the given item container.
267 class ItemIterator : public AccIterable {
268 public:
269 explicit ItemIterator(const Accessible* aItemContainer)
270 : mContainer(aItemContainer), mAnchor(nullptr) {}
272 virtual Accessible* Next() override;
274 private:
275 ItemIterator() = delete;
276 ItemIterator(const ItemIterator&) = delete;
277 ItemIterator& operator=(const ItemIterator&) = delete;
279 const Accessible* mContainer;
280 Accessible* mAnchor;
284 * Used to iterate through XUL tree items of the same level.
286 class XULTreeItemIterator : public AccIterable {
287 public:
288 XULTreeItemIterator(const XULTreeAccessible* aXULTree, nsITreeView* aTreeView,
289 int32_t aRowIdx);
290 virtual ~XULTreeItemIterator() {}
292 virtual LocalAccessible* Next() override;
294 private:
295 XULTreeItemIterator() = delete;
296 XULTreeItemIterator(const XULTreeItemIterator&) = delete;
297 XULTreeItemIterator& operator=(const XULTreeItemIterator&) = delete;
299 const XULTreeAccessible* mXULTree;
300 nsITreeView* mTreeView;
301 int32_t mRowCount;
302 int32_t mContainerLevel;
303 int32_t mCurrRowIdx;
307 * Used to iterate through a sequence of RemoteAccessibles supplied as an array
308 * of ids. Such id arrays are included in the RemoteAccessible cache.
310 class RemoteAccIterator : public AccIterable {
311 public:
313 * Construct with a reference to an array owned somewhere else; e.g. a
314 * RemoteAccessible cache.
316 RemoteAccIterator(const nsTArray<uint64_t>& aIds, DocAccessibleParent* aDoc)
317 : mIds(aIds), mDoc(aDoc), mIndex(0) {}
319 virtual ~RemoteAccIterator() = default;
321 virtual Accessible* Next() override;
323 private:
324 const nsTArray<uint64_t>& mIds;
325 DocAccessibleParent* mDoc;
326 uint32_t mIndex;
329 } // namespace a11y
330 } // namespace mozilla
332 #endif