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