Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/accessible/base/TreeWalker.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef mozilla_a11y_TreeWalker_h_
7
#define mozilla_a11y_TreeWalker_h_
8
9
#include "mozilla/Attributes.h"
10
#include <stdint.h>
11
#include "mozilla/dom/ChildIterator.h"
12
#include "nsCOMPtr.h"
13
14
class nsIContent;
15
16
namespace mozilla {
17
namespace a11y {
18
19
class Accessible;
20
class DocAccessible;
21
22
/**
23
 * This class is used to walk the DOM tree to create accessible tree.
24
 */
25
class TreeWalker final
26
{
27
public:
28
  enum {
29
    // used to walk the existing tree of the given node
30
    eWalkCache = 1,
31
    // used to walk the context tree starting from given node
32
    eWalkContextTree = 2 | eWalkCache,
33
    eScoped = 4
34
  };
35
36
  /**
37
   * Used to navigate and create if needed the accessible children.
38
   */
39
  explicit TreeWalker(Accessible* aContext);
40
41
  /**
42
   * Used to navigate the accessible children relative to the anchor.
43
   *
44
   * @param aContext [in] container accessible for the given node, used to
45
   *                   define accessible context
46
   * @param aAnchorNode [in] the node the search will be prepared relative to
47
   * @param aFlags   [in] flags (see enum above)
48
   */
49
  TreeWalker(Accessible* aContext, nsIContent* aAnchorNode, uint32_t aFlags = eWalkCache);
50
51
  /**
52
   * Navigates the accessible children within the anchor node subtree.
53
   */
54
  TreeWalker(DocAccessible* aDocument, nsIContent* aAnchorNode);
55
56
  ~TreeWalker();
57
58
  /**
59
   * Resets the walker state, and sets the given node as an anchor. Returns a
60
   * first accessible element within the node including the node itself.
61
   */
62
  Accessible* Scope(nsIContent* aAnchorNode);
63
64
  /**
65
   * Resets the walker state.
66
   */
67
  void Reset()
68
0
  {
69
0
    mPhase = eAtStart;
70
0
    mStateStack.Clear();
71
0
    mARIAOwnsIdx = 0;
72
0
  }
73
74
  /**
75
   * Sets the walker state to the given child node if it's within the anchor.
76
   */
77
  bool Seek(nsIContent* aChildNode);
78
79
  /**
80
   * Return the next/prev accessible.
81
   *
82
   * @note Returned accessible is bound to the document, if the accessible is
83
   *       rejected during tree creation then the caller should be unbind it
84
   *       from the document.
85
   */
86
  Accessible* Next();
87
  Accessible* Prev();
88
89
0
  Accessible* Context() const { return mContext; }
90
0
  DocAccessible* Document() const { return mDoc; }
91
92
private:
93
  TreeWalker();
94
  TreeWalker(const TreeWalker&);
95
  TreeWalker& operator =(const TreeWalker&);
96
97
  /**
98
   * Return an accessible for the given node if any.
99
   */
100
  Accessible* AccessibleFor(nsIContent* aNode, uint32_t aFlags,
101
                            bool* aSkipSubtree);
102
103
  /**
104
   * Create new state for the given node and push it on top of stack / at bottom
105
   * of stack.
106
   *
107
   * @note State stack is used to navigate up/down the DOM subtree during
108
   *        accessible children search.
109
   */
110
  dom::AllChildrenIterator* PushState(nsIContent* aContent,
111
                                      bool aStartAtBeginning)
112
0
  {
113
0
    return mStateStack.AppendElement(
114
0
      dom::AllChildrenIterator(aContent, mChildFilter, aStartAtBeginning));
115
0
  }
116
  dom::AllChildrenIterator* PrependState(nsIContent* aContent,
117
                                         bool aStartAtBeginning)
118
0
  {
119
0
    return mStateStack.InsertElementAt(0,
120
0
      dom::AllChildrenIterator(aContent, mChildFilter, aStartAtBeginning));
121
0
  }
122
123
  /**
124
   * Pop state from stack.
125
   */
126
  dom::AllChildrenIterator* PopState();
127
128
  DocAccessible* mDoc;
129
  Accessible* mContext;
130
  nsIContent* mAnchorNode;
131
132
  AutoTArray<dom::AllChildrenIterator, 20> mStateStack;
133
  uint32_t mARIAOwnsIdx;
134
135
  int32_t mChildFilter;
136
  uint32_t mFlags;
137
138
  enum Phase {
139
    eAtStart,
140
    eAtDOM,
141
    eAtARIAOwns,
142
    eAtEnd
143
  };
144
  Phase mPhase;
145
};
146
147
} // namespace a11y
148
} // namespace mozilla
149
150
#endif // mozilla_a11y_TreeWalker_h_