no bug - Correct some typos in the comments. a=typo-fix
[gecko.git] / accessible / base / TreeWalker.h
blob9093d8187f87370038af80f6efa0a886873f5875
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/. */
6 #ifndef mozilla_a11y_TreeWalker_h_
7 #define mozilla_a11y_TreeWalker_h_
9 #include "mozilla/Attributes.h"
10 #include <stdint.h>
11 #include "mozilla/dom/ChildIterator.h"
12 #include "nsCOMPtr.h"
14 class nsIContent;
16 namespace mozilla {
17 namespace a11y {
19 class LocalAccessible;
20 class DocAccessible;
22 /**
23 * This class is used to walk the DOM tree to create accessible tree.
25 class TreeWalker final {
26 public:
27 enum {
28 // used to walk the existing tree of the given node
29 eWalkCache = 1,
30 // used to walk the context tree starting from given node
31 eWalkContextTree = 2 | eWalkCache,
32 eScoped = 4
35 /**
36 * Used to navigate and create if needed the accessible children.
38 explicit TreeWalker(LocalAccessible* aContext);
40 /**
41 * Used to navigate the accessible children relative to the anchor.
43 * @param aContext [in] container accessible for the given node, used to
44 * define accessible context
45 * @param aAnchorNode [in] the node the search will be prepared relative to
46 * @param aFlags [in] flags (see enum above)
48 TreeWalker(LocalAccessible* aContext, nsIContent* aAnchorNode,
49 uint32_t aFlags = eWalkCache);
51 /**
52 * Navigates the accessible children within the anchor node subtree.
54 TreeWalker(DocAccessible* aDocument, nsIContent* aAnchorNode);
56 ~TreeWalker();
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.
62 LocalAccessible* Scope(nsIContent* aAnchorNode);
64 /**
65 * Resets the walker state.
67 void Reset() {
68 mPhase = eAtStart;
69 mStateStack.Clear();
70 mARIAOwnsIdx = 0;
73 /**
74 * Sets the walker state to the given child node if it's within the anchor.
76 bool Seek(nsIContent* aChildNode);
78 /**
79 * Return the next/prev accessible.
81 * @note Returned accessible is bound to the document, if the accessible is
82 * rejected during tree creation then the caller should be unbind it
83 * from the document.
85 LocalAccessible* Next();
86 LocalAccessible* Prev();
88 LocalAccessible* Context() const { return mContext; }
89 DocAccessible* Document() const { return mDoc; }
91 private:
92 TreeWalker();
93 TreeWalker(const TreeWalker&);
94 TreeWalker& operator=(const TreeWalker&);
96 /**
97 * Return an accessible for the given node if any.
99 LocalAccessible* AccessibleFor(nsIContent* aNode, uint32_t aFlags,
100 bool* aSkipSubtree);
103 * Create new state for the given node and push it on top of stack / at bottom
104 * of stack.
106 * @note State stack is used to navigate up/down the DOM subtree during
107 * accessible children search.
109 dom::AllChildrenIterator* PushState(nsIContent* aContent,
110 bool aStartAtBeginning) {
111 return mStateStack.AppendElement(
112 dom::AllChildrenIterator(aContent, mChildFilter, aStartAtBeginning));
114 dom::AllChildrenIterator* PrependState(nsIContent* aContent,
115 bool aStartAtBeginning) {
116 return mStateStack.InsertElementAt(
117 0, dom::AllChildrenIterator(aContent, mChildFilter, aStartAtBeginning));
121 * Pop state from stack.
123 dom::AllChildrenIterator* PopState();
125 DocAccessible* mDoc;
126 LocalAccessible* mContext;
127 nsIContent* mAnchorNode;
129 AutoTArray<dom::AllChildrenIterator, 20> mStateStack;
130 uint32_t mARIAOwnsIdx;
132 int32_t mChildFilter;
133 uint32_t mFlags;
135 enum Phase { eAtStart, eAtDOM, eAtARIAOwns, eAtEnd };
136 Phase mPhase;
139 } // namespace a11y
140 } // namespace mozilla
142 #endif // mozilla_a11y_TreeWalker_h_