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 338 339 340 341
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "TreeWalker.h"
#include "ARIAMap.h"
#include "nsAccessibilityService.h"
#include "DocAccessible.h"
#include "mozilla/dom/ChildIterator.h"
#include "mozilla/dom/Element.h"
namespace mozilla::a11y {
////////////////////////////////////////////////////////////////////////////////
// TreeWalker
////////////////////////////////////////////////////////////////////////////////
TreeWalker::TreeWalker(LocalAccessible* aContext)
: mDoc(aContext->Document()),
mContext(aContext),
mAnchorNode(nullptr),
mARIAOwnsIdx(0),
mChildFilter(nsIContent::eSkipPlaceholderContent),
mFlags(0),
mPhase(eAtStart) {
mChildFilter |= nsIContent::eAllChildren;
mAnchorNode = mContext->IsDoc() ? mDoc->DocumentNode()->GetRootElement()
: mContext->GetContent();
MOZ_COUNT_CTOR(TreeWalker);
}
TreeWalker::TreeWalker(LocalAccessible* aContext, nsIContent* aAnchorNode,
uint32_t aFlags)
: mDoc(aContext->Document()),
mContext(aContext),
mAnchorNode(aAnchorNode),
mARIAOwnsIdx(0),
mChildFilter(nsIContent::eSkipPlaceholderContent),
mFlags(aFlags),
mPhase(eAtStart) {
MOZ_ASSERT(mFlags & eWalkCache,
"This constructor cannot be used for tree creation");
MOZ_ASSERT(aAnchorNode, "No anchor node for the accessible tree walker");
mChildFilter |= nsIContent::eAllChildren;
MOZ_COUNT_CTOR(TreeWalker);
}
TreeWalker::TreeWalker(DocAccessible* aDocument, nsIContent* aAnchorNode)
: mDoc(aDocument),
mContext(nullptr),
mAnchorNode(aAnchorNode),
mARIAOwnsIdx(0),
mChildFilter(nsIContent::eSkipPlaceholderContent |
nsIContent::eAllChildren),
mFlags(eWalkCache),
mPhase(eAtStart) {
MOZ_ASSERT(aAnchorNode, "No anchor node for the accessible tree walker");
MOZ_COUNT_CTOR(TreeWalker);
}
TreeWalker::~TreeWalker() { MOZ_COUNT_DTOR(TreeWalker); }
LocalAccessible* TreeWalker::Scope(nsIContent* aAnchorNode) {
Reset();
mAnchorNode = aAnchorNode;
mFlags |= eScoped;
bool skipSubtree = false;
LocalAccessible* acc = AccessibleFor(aAnchorNode, 0, &skipSubtree);
if (acc) {
mPhase = eAtEnd;
return acc;
}
return skipSubtree ? nullptr : Next();
}
bool TreeWalker::Seek(nsIContent* aChildNode) {
MOZ_ASSERT(aChildNode, "Child cannot be null");
Reset();
if (mAnchorNode == aChildNode) {
return true;
}
nsIContent* childNode = nullptr;
nsINode* parentNode = aChildNode;
do {
childNode = parentNode->AsContent();
parentNode = childNode->GetFlattenedTreeParent();
// Handle the special case of XBL binding child under a shadow root.
if (parentNode && parentNode->IsShadowRoot()) {
parentNode = childNode->GetFlattenedTreeParent();
if (parentNode == mAnchorNode) {
return true;
}
continue;
}
if (!parentNode || !parentNode->IsElement()) {
return false;
}
// If ARIA owned child.
LocalAccessible* child = mDoc->GetAccessible(childNode);
if (child && child->IsRelocated()) {
MOZ_ASSERT(
!(mFlags & eScoped),
"Walker should not be scoped when seeking into relocated children");
if (child->LocalParent() != mContext) {
return false;
}
LocalAccessible* ownedChild = nullptr;
while ((ownedChild = mDoc->ARIAOwnedAt(mContext, mARIAOwnsIdx++)) &&
ownedChild != child) {
;
}
MOZ_ASSERT(ownedChild, "A child has to be in ARIA owned elements");
mPhase = eAtARIAOwns;
return true;
}
// Look in DOM.
dom::AllChildrenIterator* iter =
PrependState(parentNode->AsElement(), true);
if (!iter->Seek(childNode)) {
return false;
}
if (parentNode == mAnchorNode) {
mPhase = eAtDOM;
return true;
}
} while (true);
MOZ_ASSERT_UNREACHABLE("because the do-while loop never breaks");
}
LocalAccessible* TreeWalker::Next() {
if (mStateStack.IsEmpty()) {
if (mPhase == eAtEnd) {
return nullptr;
}
if (mPhase == eAtDOM || mPhase == eAtARIAOwns) {
if (!(mFlags & eScoped)) {
mPhase = eAtARIAOwns;
LocalAccessible* child = mDoc->ARIAOwnedAt(mContext, mARIAOwnsIdx);
if (child) {
mARIAOwnsIdx++;
return child;
}
}
MOZ_ASSERT(!(mFlags & eScoped) || mPhase != eAtARIAOwns,
"Don't walk relocated children in scoped mode");
mPhase = eAtEnd;
return nullptr;
}
if (!mAnchorNode) {
mPhase = eAtEnd;
return nullptr;
}
mPhase = eAtDOM;
PushState(mAnchorNode, true);
}
dom::AllChildrenIterator* top = &mStateStack[mStateStack.Length() - 1];
while (top) {
while (nsIContent* childNode = top->GetNextChild()) {
bool skipSubtree = false;
LocalAccessible* child = AccessibleFor(childNode, mFlags, &skipSubtree);
if (child) {
return child;
}
// Walk down the subtree if allowed.
if (!skipSubtree && childNode->IsElement()) {
top = PushState(childNode, true);
}
}
top = PopState();
}
// If we traversed the whole subtree of the anchor node. Move to next node
// relative anchor node within the context subtree if asked.
if (mFlags != eWalkContextTree) {
// eWalkCache flag presence indicates that the search is scoped to the
// anchor (no ARIA owns stuff).
if (mFlags & eWalkCache) {
mPhase = eAtEnd;
return nullptr;
}
return Next();
}
nsINode* contextNode = mContext->GetNode();
while (mAnchorNode != contextNode) {
nsINode* parentNode = mAnchorNode->GetFlattenedTreeParent();
if (!parentNode || !parentNode->IsElement()) return nullptr;
nsIContent* parent = parentNode->AsElement();
top = PushState(parent, true);
if (top->Seek(mAnchorNode)) {
mAnchorNode = parent;
return Next();
}
// XXX We really should never get here, it means we're trying to find an
// accessible for a dom node where iterating over its parent's children
// doesn't return it. However this sometimes happens when we're asked for
// the nearest accessible to place holder content which we ignore.
mAnchorNode = parent;
}
return Next();
}
LocalAccessible* TreeWalker::Prev() {
if (mStateStack.IsEmpty()) {
if (mPhase == eAtStart || mPhase == eAtDOM) {
mPhase = eAtStart;
return nullptr;
}
if (mPhase == eAtEnd) {
if (mFlags & eScoped) {
mPhase = eAtDOM;
} else {
mPhase = eAtARIAOwns;
mARIAOwnsIdx = mDoc->ARIAOwnedCount(mContext);
}
}
if (mPhase == eAtARIAOwns) {
MOZ_ASSERT(!(mFlags & eScoped),
"Should not walk relocated children in scoped mode");
if (mARIAOwnsIdx > 0) {
return mDoc->ARIAOwnedAt(mContext, --mARIAOwnsIdx);
}
if (!mAnchorNode) {
mPhase = eAtStart;
return nullptr;
}
mPhase = eAtDOM;
PushState(mAnchorNode, false);
}
}
dom::AllChildrenIterator* top = &mStateStack[mStateStack.Length() - 1];
while (top) {
while (nsIContent* childNode = top->GetPreviousChild()) {
// No accessible creation on the way back.
bool skipSubtree = false;
LocalAccessible* child =
AccessibleFor(childNode, eWalkCache, &skipSubtree);
if (child) {
return child;
}
// Walk down into subtree to find accessibles.
if (!skipSubtree && childNode->IsElement()) {
top = PushState(childNode, false);
}
}
top = PopState();
}
// Move to a previous node relative the anchor node within the context
// subtree if asked.
if (mFlags != eWalkContextTree) {
mPhase = eAtStart;
return nullptr;
}
nsINode* contextNode = mContext->GetNode();
while (mAnchorNode != contextNode) {
nsINode* parentNode = mAnchorNode->GetFlattenedTreeParent();
if (!parentNode || !parentNode->IsElement()) {
return nullptr;
}
nsIContent* parent = parentNode->AsElement();
top = PushState(parent, true);
if (top->Seek(mAnchorNode)) {
mAnchorNode = parent;
return Prev();
}
mAnchorNode = parent;
}
mPhase = eAtStart;
return nullptr;
}
LocalAccessible* TreeWalker::AccessibleFor(nsIContent* aNode, uint32_t aFlags,
bool* aSkipSubtree) {
// Ignore the accessible and its subtree if it was repositioned by means
// of aria-owns.
LocalAccessible* child = mDoc->GetAccessible(aNode);
if (child) {
if (child->IsRelocated()) {
*aSkipSubtree = true;
return nullptr;
}
return child;
}
// Create an accessible if allowed.
if (!(aFlags & eWalkCache) && mContext->IsAcceptableChild(aNode) &&
!aria::IsValidARIAHidden(mDoc)) {
mDoc->RelocateARIAOwnedIfNeeded(aNode);
return GetAccService()->CreateAccessible(aNode, mContext, aSkipSubtree);
}
return nullptr;
}
dom::AllChildrenIterator* TreeWalker::PopState() {
mStateStack.RemoveLastElement();
return mStateStack.IsEmpty() ? nullptr : &mStateStack.LastElement();
}
} // namespace mozilla::a11y
|