accessible/src/base/TreeWalker.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "TreeWalker.h"
michael@0 7
michael@0 8 #include "Accessible.h"
michael@0 9 #include "nsAccessibilityService.h"
michael@0 10 #include "DocAccessible.h"
michael@0 11
michael@0 12 #include "mozilla/dom/Element.h"
michael@0 13
michael@0 14 using namespace mozilla::a11y;
michael@0 15
michael@0 16 ////////////////////////////////////////////////////////////////////////////////
michael@0 17 // WalkState
michael@0 18 ////////////////////////////////////////////////////////////////////////////////
michael@0 19
michael@0 20 namespace mozilla {
michael@0 21 namespace a11y {
michael@0 22
michael@0 23 struct WalkState
michael@0 24 {
michael@0 25 WalkState(nsIContent *aContent) :
michael@0 26 content(aContent), childIdx(0), prevState(nullptr) {}
michael@0 27
michael@0 28 nsCOMPtr<nsIContent> content;
michael@0 29 nsCOMPtr<nsINodeList> childList;
michael@0 30 uint32_t childIdx;
michael@0 31 WalkState *prevState;
michael@0 32 };
michael@0 33
michael@0 34 } // namespace a11y
michael@0 35 } // namespace mozilla
michael@0 36
michael@0 37 ////////////////////////////////////////////////////////////////////////////////
michael@0 38 // TreeWalker
michael@0 39 ////////////////////////////////////////////////////////////////////////////////
michael@0 40
michael@0 41 TreeWalker::
michael@0 42 TreeWalker(Accessible* aContext, nsIContent* aContent, uint32_t aFlags) :
michael@0 43 mDoc(aContext->Document()), mContext(aContext),
michael@0 44 mFlags(aFlags), mState(nullptr)
michael@0 45 {
michael@0 46 NS_ASSERTION(aContent, "No node for the accessible tree walker!");
michael@0 47
michael@0 48 if (aContent)
michael@0 49 mState = new WalkState(aContent);
michael@0 50
michael@0 51 mChildFilter = mContext->CanHaveAnonChildren() ?
michael@0 52 nsIContent::eAllChildren : nsIContent::eAllButXBL;
michael@0 53
michael@0 54 mChildFilter |= nsIContent::eSkipPlaceholderContent;
michael@0 55
michael@0 56 MOZ_COUNT_CTOR(TreeWalker);
michael@0 57 }
michael@0 58
michael@0 59 TreeWalker::~TreeWalker()
michael@0 60 {
michael@0 61 // Clear state stack from memory
michael@0 62 while (mState)
michael@0 63 PopState();
michael@0 64
michael@0 65 MOZ_COUNT_DTOR(TreeWalker);
michael@0 66 }
michael@0 67
michael@0 68 ////////////////////////////////////////////////////////////////////////////////
michael@0 69 // TreeWalker: private
michael@0 70
michael@0 71 Accessible*
michael@0 72 TreeWalker::NextChildInternal(bool aNoWalkUp)
michael@0 73 {
michael@0 74 if (!mState || !mState->content)
michael@0 75 return nullptr;
michael@0 76
michael@0 77 if (!mState->childList)
michael@0 78 mState->childList = mState->content->GetChildren(mChildFilter);
michael@0 79
michael@0 80 uint32_t length = 0;
michael@0 81 if (mState->childList)
michael@0 82 mState->childList->GetLength(&length);
michael@0 83
michael@0 84 while (mState->childIdx < length) {
michael@0 85 nsIContent* childNode = mState->childList->Item(mState->childIdx);
michael@0 86 mState->childIdx++;
michael@0 87
michael@0 88 bool isSubtreeHidden = false;
michael@0 89 Accessible* accessible = mFlags & eWalkCache ?
michael@0 90 mDoc->GetAccessible(childNode) :
michael@0 91 GetAccService()->GetOrCreateAccessible(childNode, mContext,
michael@0 92 &isSubtreeHidden);
michael@0 93
michael@0 94 if (accessible)
michael@0 95 return accessible;
michael@0 96
michael@0 97 // Walk down into subtree to find accessibles.
michael@0 98 if (!isSubtreeHidden) {
michael@0 99 PushState(childNode);
michael@0 100 accessible = NextChildInternal(true);
michael@0 101 if (accessible)
michael@0 102 return accessible;
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 // No more children, get back to the parent.
michael@0 107 nsIContent* anchorNode = mState->content;
michael@0 108 PopState();
michael@0 109 if (aNoWalkUp)
michael@0 110 return nullptr;
michael@0 111
michael@0 112 if (mState)
michael@0 113 return NextChildInternal(false);
michael@0 114
michael@0 115 // If we traversed the whole subtree of the anchor node. Move to next node
michael@0 116 // relative anchor node within the context subtree if possible.
michael@0 117 if (mFlags != eWalkContextTree)
michael@0 118 return nullptr;
michael@0 119
michael@0 120 while (anchorNode != mContext->GetNode()) {
michael@0 121 nsINode* parentNode = anchorNode->GetFlattenedTreeParent();
michael@0 122 if (!parentNode || !parentNode->IsElement())
michael@0 123 return nullptr;
michael@0 124
michael@0 125 PushState(parentNode->AsElement());
michael@0 126 mState->childList = mState->content->GetChildren(mChildFilter);
michael@0 127 length = 0;
michael@0 128 if (mState->childList)
michael@0 129 mState->childList->GetLength(&length);
michael@0 130
michael@0 131 while (mState->childIdx < length) {
michael@0 132 nsIContent* childNode = mState->childList->Item(mState->childIdx);
michael@0 133 mState->childIdx++;
michael@0 134 if (childNode == anchorNode)
michael@0 135 return NextChildInternal(false);
michael@0 136 }
michael@0 137 PopState();
michael@0 138
michael@0 139 anchorNode = parentNode->AsElement();
michael@0 140 }
michael@0 141
michael@0 142 return nullptr;
michael@0 143 }
michael@0 144
michael@0 145 void
michael@0 146 TreeWalker::PopState()
michael@0 147 {
michael@0 148 WalkState* prevToLastState = mState->prevState;
michael@0 149 delete mState;
michael@0 150 mState = prevToLastState;
michael@0 151 }
michael@0 152
michael@0 153 void
michael@0 154 TreeWalker::PushState(nsIContent* aContent)
michael@0 155 {
michael@0 156 WalkState* nextToLastState = new WalkState(aContent);
michael@0 157 nextToLastState->prevState = mState;
michael@0 158 mState = nextToLastState;
michael@0 159 }

mercurial