summaryrefslogtreecommitdiffstats
path: root/accessible/base/AccIterator.h
blob: 63b3f86ff1add98f4ee5b889fc61632a9c8afd58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_a11y_AccIterator_h__
#define mozilla_a11y_AccIterator_h__

#include "Filters.h"
#include "mozilla/a11y/DocAccessible.h"
#include "nsTArray.h"

#include <memory>

class nsITreeView;

namespace mozilla {
namespace a11y {
class DocAccessibleParent;

/**
 * AccIterable is a basic interface for iterators over accessibles.
 */
class AccIterable {
 public:
  virtual ~AccIterable() {}
  virtual Accessible* Next() = 0;

 private:
  friend class Relation;
  std::unique_ptr<AccIterable> mNextIter;
};

/**
 * Allows to iterate through accessible children or subtree complying with
 * filter function.
 */
class AccIterator : public AccIterable {
 public:
  AccIterator(const LocalAccessible* aRoot, filters::FilterFuncPtr aFilterFunc);
  virtual ~AccIterator();

  /**
   * Return next accessible complying with filter function. Return the first
   * accessible for the first time.
   */
  virtual LocalAccessible* Next() override;

 private:
  AccIterator();
  AccIterator(const AccIterator&);
  AccIterator& operator=(const AccIterator&);

  struct IteratorState {
    explicit IteratorState(const LocalAccessible* aParent,
                           IteratorState* mParentState = nullptr);

    const LocalAccessible* mParent;
    int32_t mIndex;
    IteratorState* mParentState;
  };

  filters::FilterFuncPtr mFilterFunc;
  IteratorState* mState;
};

/**
 * Allows to traverse through related accessibles that are pointing to the given
 * dependent accessible by relation attribute.
 */
class RelatedAccIterator : public AccIterable {
 public:
  /**
   * Constructor.
   *
   * @param aDocument         [in] the document accessible the related
   * &                         accessibles belong to.
   * @param aDependentContent [in] the content of dependent accessible that
   *                           relations were requested for
   * @param aRelAttr          [in] relation attribute that relations are
   *                           pointed by
   */
  RelatedAccIterator(DocAccessible* aDocument, nsIContent* aDependentContent,
                     nsAtom* aRelAttr);

  virtual ~RelatedAccIterator() {}

  /**
   * Return next related accessible for the given dependent accessible.
   */
  virtual LocalAccessible* Next() override;

 private:
  RelatedAccIterator();
  RelatedAccIterator(const RelatedAccIterator&);
  RelatedAccIterator& operator=(const RelatedAccIterator&);

  DocAccessible* mDocument;
  nsAtom* mRelAttr;
  DocAccessible::AttrRelProviders* mProviders;
  uint32_t mIndex;
};

/**
 * Used to iterate through HTML labels associated with the given accessible.
 */
class HTMLLabelIterator : public AccIterable {
 public:
  enum LabelFilter { eAllLabels, eSkipAncestorLabel };

  HTMLLabelIterator(DocAccessible* aDocument,
                    const LocalAccessible* aAccessible,
                    LabelFilter aFilter = eAllLabels);

  virtual ~HTMLLabelIterator() {}

  /**
   * Return next label accessible associated with the given element.
   */
  virtual LocalAccessible* Next() override;

 private:
  HTMLLabelIterator();
  HTMLLabelIterator(const HTMLLabelIterator&);
  HTMLLabelIterator& operator=(const HTMLLabelIterator&);

  bool IsLabel(LocalAccessible* aLabel);

  RelatedAccIterator mRelIter;
  // XXX: replace it on weak reference (bug 678429), it's safe to use raw
  // pointer now because iterators life cycle is short.
  const LocalAccessible* mAcc;
  LabelFilter mLabelFilter;
};

/**
 * Used to iterate through HTML outputs associated with the given element.
 */
class HTMLOutputIterator : public AccIterable {
 public:
  HTMLOutputIterator(DocAccessible* aDocument, nsIContent* aElement);
  virtual ~HTMLOutputIterator() {}

  /**
   * Return next output accessible associated with the given element.
   */
  virtual LocalAccessible* Next() override;

 private:
  HTMLOutputIterator();
  HTMLOutputIterator(const HTMLOutputIterator&);
  HTMLOutputIterator& operator=(const HTMLOutputIterator&);

  RelatedAccIterator mRelIter;
};

/**
 * Used to iterate through XUL labels associated with the given element.
 */
class XULLabelIterator : public AccIterable {
 public:
  XULLabelIterator(DocAccessible* aDocument, nsIContent* aElement);
  virtual ~XULLabelIterator() {}

  /**
   * Return next label accessible associated with the given element.
   */
  virtual LocalAccessible* Next() override;

 private:
  XULLabelIterator();
  XULLabelIterator(const XULLabelIterator&);
  XULLabelIterator& operator=(const XULLabelIterator&);

  RelatedAccIterator mRelIter;
};

/**
 * Used to iterate through XUL descriptions associated with the given element.
 */
class XULDescriptionIterator : public AccIterable {
 public:
  XULDescriptionIterator(DocAccessible* aDocument, nsIContent* aElement);
  virtual ~XULDescriptionIterator() {}

  /**
   * Return next description accessible associated with the given element.
   */
  virtual LocalAccessible* Next() override;

 private:
  XULDescriptionIterator();
  XULDescriptionIterator(const XULDescriptionIterator&);
  XULDescriptionIterator& operator=(const XULDescriptionIterator&);

  RelatedAccIterator mRelIter;
};

/**
 * Used to iterate through IDs, elements or accessibles pointed by IDRefs
 * attribute. Note, any method used to iterate through IDs, elements, or
 * accessibles moves iterator to next position.
 */
class IDRefsIterator : public AccIterable {
 public:
  IDRefsIterator(DocAccessible* aDoc, nsIContent* aContent,
                 nsAtom* aIDRefsAttr);
  virtual ~IDRefsIterator() {}

  /**
   * Return next ID.
   */
  const nsDependentSubstring NextID();

  /**
   * Return next element.
   */
  nsIContent* NextElem();

  /**
   * Return the element with the given ID.
   */
  static dom::Element* GetElem(nsIContent* aContent, const nsAString& aID);
  dom::Element* GetElem(const nsDependentSubstring& aID);

  // AccIterable
  virtual LocalAccessible* Next() override;

 private:
  IDRefsIterator();
  IDRefsIterator(const IDRefsIterator&);
  IDRefsIterator operator=(const IDRefsIterator&);

  nsString mIDs;
  nsIContent* mContent;
  DocAccessible* mDoc;
  nsAString::index_type mCurrIdx;
};

/**
 * Iterator that points to a single accessible returning it on the first call
 * to Next().
 */
class SingleAccIterator : public AccIterable {
 public:
  explicit SingleAccIterator(Accessible* aTarget) : mAcc(aTarget) {}
  virtual ~SingleAccIterator() {}

  virtual Accessible* Next() override;

 private:
  SingleAccIterator();
  SingleAccIterator(const SingleAccIterator&);
  SingleAccIterator& operator=(const SingleAccIterator&);

  Accessible* mAcc;
};

/**
 * Used to iterate items of the given item container.
 */
class ItemIterator : public AccIterable {
 public:
  explicit ItemIterator(const Accessible* aItemContainer)
      : mContainer(aItemContainer), mAnchor(nullptr) {}

  virtual Accessible* Next() override;

 private:
  ItemIterator() = delete;
  ItemIterator(const ItemIterator&) = delete;
  ItemIterator& operator=(const ItemIterator&) = delete;

  const Accessible* mContainer;
  Accessible* mAnchor;
};

/**
 * Used to iterate through XUL tree items of the same level.
 */
class XULTreeItemIterator : public AccIterable {
 public:
  XULTreeItemIterator(const XULTreeAccessible* aXULTree, nsITreeView* aTreeView,
                      int32_t aRowIdx);
  virtual ~XULTreeItemIterator() {}

  virtual LocalAccessible* Next() override;

 private:
  XULTreeItemIterator() = delete;
  XULTreeItemIterator(const XULTreeItemIterator&) = delete;
  XULTreeItemIterator& operator=(const XULTreeItemIterator&) = delete;

  const XULTreeAccessible* mXULTree;
  nsITreeView* mTreeView;
  int32_t mRowCount;
  int32_t mContainerLevel;
  int32_t mCurrRowIdx;
};

/**
 * Used to iterate through a sequence of RemoteAccessibles supplied as an array
 * of ids. Such id arrays are included in the RemoteAccessible cache.
 */
class RemoteAccIterator : public AccIterable {
 public:
  /**
   * Construct with a reference to an array owned somewhere else; e.g. a
   * RemoteAccessible cache.
   */
  RemoteAccIterator(const nsTArray<uint64_t>& aIds, DocAccessibleParent* aDoc)
      : mIds(aIds), mDoc(aDoc), mIndex(0) {}

  /**
   * Construct with an array moved from somewhere else. In this case, this
   * RemoteAccIterator takes ownership of the array. This should be used, for
   * example, when using sync IPC to retrieve relations.
   */
  RemoteAccIterator(nsTArray<uint64_t>&& aIds, DocAccessibleParent* aDoc);

  virtual ~RemoteAccIterator() = default;

  virtual Accessible* Next() override;

 private:
  // Used when ownership of the array is transferred to this instance.
  nsTArray<uint64_t> mOwnedIds;
  const nsTArray<uint64_t>& mIds;
  DocAccessibleParent* mDoc;
  uint32_t mIndex;
};

}  // namespace a11y
}  // namespace mozilla

#endif