2010-02-21 03:55:04 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2012-11-19 13:20:09 +04:00
|
|
|
#include "TreeWalker.h"
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2012-05-29 05:18:45 +04:00
|
|
|
#include "Accessible.h"
|
2015-09-15 19:01:51 +03:00
|
|
|
#include "AccIterator.h"
|
2010-02-21 03:55:04 +03:00
|
|
|
#include "nsAccessibilityService.h"
|
2012-05-27 13:01:40 +04:00
|
|
|
#include "DocAccessible.h"
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
#include "mozilla/dom/ChildIterator.h"
|
2014-02-15 19:21:40 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2010-06-08 20:39:58 +04:00
|
|
|
|
2014-08-12 10:02:28 +04:00
|
|
|
using namespace mozilla;
|
2012-11-18 06:01:44 +04:00
|
|
|
using namespace mozilla::a11y;
|
|
|
|
|
2010-02-21 03:55:04 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-11-19 13:20:09 +04:00
|
|
|
// TreeWalker
|
2010-02-21 03:55:04 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-02-26 21:29:27 +03:00
|
|
|
TreeWalker::TreeWalker(Accessible* aContext)
|
|
|
|
: mDoc(aContext->Document()),
|
|
|
|
mContext(aContext),
|
|
|
|
mAnchorNode(nullptr),
|
2016-02-28 19:30:46 +03:00
|
|
|
mARIAOwnsIdx(0),
|
2016-03-08 00:43:27 +03:00
|
|
|
mChildFilter(nsIContent::eSkipPlaceholderContent),
|
|
|
|
mFlags(0),
|
|
|
|
mPhase(eAtStart) {
|
2016-02-26 21:29:27 +03:00
|
|
|
mChildFilter |=
|
|
|
|
mContext->NoXBLKids() ? nsIContent::eAllButXBL : nsIContent::eAllChildren;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-02-26 21:29:27 +03:00
|
|
|
mAnchorNode = mContext->IsDoc() ? mDoc->DocumentNode()->GetRootElement()
|
|
|
|
: mContext->GetContent();
|
|
|
|
|
|
|
|
MOZ_COUNT_CTOR(TreeWalker);
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeWalker::TreeWalker(Accessible* aContext, nsIContent* aAnchorNode,
|
|
|
|
uint32_t aFlags)
|
|
|
|
: mDoc(aContext->Document()),
|
|
|
|
mContext(aContext),
|
|
|
|
mAnchorNode(aAnchorNode),
|
2016-02-28 19:30:46 +03:00
|
|
|
mARIAOwnsIdx(0),
|
2016-03-08 00:43:27 +03:00
|
|
|
mChildFilter(nsIContent::eSkipPlaceholderContent),
|
|
|
|
mFlags(aFlags),
|
|
|
|
mPhase(eAtStart) {
|
2016-03-10 23:46:44 +03:00
|
|
|
MOZ_ASSERT(mFlags & eWalkCache,
|
|
|
|
"This constructor cannot be used for tree creation");
|
2016-02-26 21:29:27 +03:00
|
|
|
MOZ_ASSERT(aAnchorNode, "No anchor node for the accessible tree walker");
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2016-02-26 21:29:27 +03:00
|
|
|
mChildFilter |=
|
2016-02-26 19:04:05 +03:00
|
|
|
mContext->NoXBLKids() ? nsIContent::eAllButXBL : nsIContent::eAllChildren;
|
2010-10-15 19:34:35 +04:00
|
|
|
|
2012-11-19 13:20:09 +04:00
|
|
|
MOZ_COUNT_CTOR(TreeWalker);
|
2010-02-21 03:55:04 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 21:02:41 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-11-19 13:20:09 +04:00
|
|
|
TreeWalker::~TreeWalker() { MOZ_COUNT_DTOR(TreeWalker); }
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2016-08-05 17:20:58 +03:00
|
|
|
Accessible* TreeWalker::Scope(nsIContent* aAnchorNode) {
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
mAnchorNode = aAnchorNode;
|
|
|
|
|
2017-12-22 19:25:00 +03:00
|
|
|
mFlags |= eScoped;
|
|
|
|
|
2016-08-05 17:20:58 +03:00
|
|
|
bool skipSubtree = false;
|
|
|
|
Accessible* acc = AccessibleFor(aAnchorNode, 0, &skipSubtree);
|
|
|
|
if (acc) {
|
|
|
|
mPhase = eAtEnd;
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return skipSubtree ? nullptr : Next();
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:27:59 +03:00
|
|
|
bool TreeWalker::Seek(nsIContent* aChildNode) {
|
|
|
|
MOZ_ASSERT(aChildNode, "Child cannot be null");
|
|
|
|
|
2016-08-05 17:20:58 +03:00
|
|
|
Reset();
|
2016-03-08 16:27:59 +03:00
|
|
|
|
2016-09-21 21:15:20 +03:00
|
|
|
if (mAnchorNode == aChildNode) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:27:59 +03:00
|
|
|
nsIContent* childNode = nullptr;
|
|
|
|
nsINode* parentNode = aChildNode;
|
|
|
|
do {
|
|
|
|
childNode = parentNode->AsContent();
|
|
|
|
parentNode = childNode->HasFlag(NODE_MAY_BE_IN_BINDING_MNGR) &&
|
|
|
|
(mChildFilter & nsIContent::eAllButXBL)
|
|
|
|
? childNode->GetParentNode()
|
|
|
|
: childNode->GetFlattenedTreeParent();
|
|
|
|
|
|
|
|
if (!parentNode || !parentNode->IsElement()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If ARIA owned child.
|
|
|
|
Accessible* child = mDoc->GetAccessible(childNode);
|
|
|
|
if (child && child->IsRelocated()) {
|
2017-12-22 19:25:00 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
!(mFlags & eScoped),
|
|
|
|
"Walker should not be scoped when seeking into relocated children");
|
2016-03-08 16:27:59 +03:00
|
|
|
if (child->Parent() != mContext) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Accessible* 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);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2016-08-05 17:20:58 +03:00
|
|
|
Accessible* TreeWalker::Next() {
|
2016-02-28 19:30:46 +03:00
|
|
|
if (mStateStack.IsEmpty()) {
|
2016-03-08 00:43:27 +03:00
|
|
|
if (mPhase == eAtEnd) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPhase == eAtDOM || mPhase == eAtARIAOwns) {
|
2017-12-22 19:25:00 +03:00
|
|
|
if (!(mFlags & eScoped)) {
|
|
|
|
mPhase = eAtARIAOwns;
|
|
|
|
Accessible* child = mDoc->ARIAOwnedAt(mContext, mARIAOwnsIdx);
|
|
|
|
if (child) {
|
|
|
|
mARIAOwnsIdx++;
|
|
|
|
return child;
|
|
|
|
}
|
2016-03-08 00:43:27 +03:00
|
|
|
}
|
2017-12-22 19:25:00 +03:00
|
|
|
MOZ_ASSERT(!(mFlags & eScoped) || mPhase != eAtARIAOwns,
|
|
|
|
"Don't walk relocated children in scoped mode");
|
2016-03-08 00:43:27 +03:00
|
|
|
mPhase = eAtEnd;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mAnchorNode) {
|
|
|
|
mPhase = eAtEnd;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPhase = eAtDOM;
|
|
|
|
PushState(mAnchorNode, true);
|
2016-02-28 19:30:46 +03:00
|
|
|
}
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2016-02-28 19:30:46 +03:00
|
|
|
dom::AllChildrenIterator* top = &mStateStack[mStateStack.Length() - 1];
|
2014-08-12 10:02:28 +04:00
|
|
|
while (top) {
|
2016-02-28 19:30:46 +03:00
|
|
|
while (nsIContent* childNode = top->GetNextChild()) {
|
|
|
|
bool skipSubtree = false;
|
2016-03-10 23:50:37 +03:00
|
|
|
Accessible* child = AccessibleFor(childNode, mFlags, &skipSubtree);
|
2016-02-28 19:30:46 +03:00
|
|
|
if (child) {
|
2015-09-15 19:01:51 +03:00
|
|
|
return child;
|
2016-02-28 19:30:46 +03:00
|
|
|
}
|
2014-02-15 19:21:40 +04:00
|
|
|
|
2016-08-05 17:20:58 +03:00
|
|
|
// Walk down the subtree if allowed.
|
2016-02-28 19:30:46 +03:00
|
|
|
if (!skipSubtree && childNode->IsElement()) {
|
2016-03-08 00:43:27 +03:00
|
|
|
top = PushState(childNode, true);
|
2016-02-28 19:30:46 +03:00
|
|
|
}
|
2014-08-12 10:02:28 +04:00
|
|
|
}
|
|
|
|
top = PopState();
|
|
|
|
}
|
2014-09-01 16:31:35 +04:00
|
|
|
|
2014-02-15 19:21:40 +04:00
|
|
|
// If we traversed the whole subtree of the anchor node. Move to next node
|
2016-03-10 23:46:44 +03:00
|
|
|
// 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;
|
|
|
|
}
|
2016-08-05 17:20:58 +03:00
|
|
|
return Next();
|
2016-03-10 23:46:44 +03:00
|
|
|
}
|
2014-02-15 19:21:40 +04:00
|
|
|
|
2014-08-12 10:02:28 +04:00
|
|
|
nsINode* contextNode = mContext->GetNode();
|
|
|
|
while (mAnchorNode != contextNode) {
|
|
|
|
nsINode* parentNode = mAnchorNode->GetFlattenedTreeParent();
|
2014-02-15 19:21:40 +04:00
|
|
|
if (!parentNode || !parentNode->IsElement()) return nullptr;
|
2010-02-21 03:55:04 +03:00
|
|
|
|
2014-08-12 10:02:28 +04:00
|
|
|
nsIContent* parent = parentNode->AsElement();
|
2016-03-08 00:43:27 +03:00
|
|
|
top = PushState(parent, true);
|
2016-02-28 19:30:46 +03:00
|
|
|
if (top->Seek(mAnchorNode)) {
|
2016-02-11 16:24:38 +03:00
|
|
|
mAnchorNode = parent;
|
2016-08-05 17:20:58 +03:00
|
|
|
return Next();
|
2014-02-15 19:21:40 +04:00
|
|
|
}
|
2014-09-01 16:31:35 +04:00
|
|
|
|
2014-08-12 10:02:28 +04:00
|
|
|
// 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;
|
2014-02-15 19:21:40 +04:00
|
|
|
}
|
|
|
|
|
2016-08-05 17:20:58 +03:00
|
|
|
return Next();
|
2015-09-15 19:01:51 +03:00
|
|
|
}
|
|
|
|
|
2016-03-08 00:43:27 +03:00
|
|
|
Accessible* TreeWalker::Prev() {
|
|
|
|
if (mStateStack.IsEmpty()) {
|
|
|
|
if (mPhase == eAtStart || mPhase == eAtDOM) {
|
|
|
|
mPhase = eAtStart;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPhase == eAtEnd) {
|
2017-12-22 19:25:00 +03:00
|
|
|
if (mFlags & eScoped) {
|
|
|
|
mPhase = eAtDOM;
|
|
|
|
} else {
|
|
|
|
mPhase = eAtARIAOwns;
|
|
|
|
mARIAOwnsIdx = mDoc->ARIAOwnedCount(mContext);
|
|
|
|
}
|
2016-03-08 00:43:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPhase == eAtARIAOwns) {
|
2017-12-22 19:25:00 +03:00
|
|
|
MOZ_ASSERT(!(mFlags & eScoped),
|
|
|
|
"Should not walk relocated children in scoped mode");
|
2016-03-08 00:43:27 +03:00
|
|
|
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.
|
2016-03-10 23:50:37 +03:00
|
|
|
bool skipSubtree = false;
|
|
|
|
Accessible* child = AccessibleFor(childNode, eWalkCache, &skipSubtree);
|
2016-03-08 00:43:27 +03:00
|
|
|
if (child) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk down into subtree to find accessibles.
|
|
|
|
if (!skipSubtree && childNode->IsElement()) {
|
2016-03-11 19:14:29 +03:00
|
|
|
top = PushState(childNode, false);
|
2016-03-08 00:43:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-10 23:50:37 +03:00
|
|
|
Accessible* TreeWalker::AccessibleFor(nsIContent* aNode, uint32_t aFlags,
|
|
|
|
bool* aSkipSubtree) {
|
|
|
|
// Ignore the accessible and its subtree if it was repositioned by means
|
|
|
|
// of aria-owns.
|
2016-04-06 14:23:41 +03:00
|
|
|
Accessible* 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)) {
|
2016-07-08 18:56:30 +03:00
|
|
|
// We may have ARIA owned element in the dependent attributes map, but the
|
|
|
|
// element may be not allowed for this ARIA owns relation, if the relation
|
|
|
|
// crosses out XBL anonymous content boundaries. In this case we won't
|
|
|
|
// create an accessible object for it, when aria-owns is processed, which
|
|
|
|
// may make the element subtree inaccessible. To avoid that let's create
|
|
|
|
// an accessible object now, and later, if allowed, move it in the tree,
|
|
|
|
// when aria-owns relation is processed.
|
|
|
|
if (mDoc->RelocateARIAOwnedIfNeeded(aNode) && !aNode->IsXULElement()) {
|
2016-04-06 14:58:58 +03:00
|
|
|
*aSkipSubtree = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-06 14:23:41 +03:00
|
|
|
return GetAccService()->CreateAccessible(aNode, mContext, aSkipSubtree);
|
2016-03-10 23:50:37 +03:00
|
|
|
}
|
|
|
|
|
2016-04-06 14:23:41 +03:00
|
|
|
return nullptr;
|
2016-03-10 23:50:37 +03:00
|
|
|
}
|
|
|
|
|
2016-02-28 19:30:46 +03:00
|
|
|
dom::AllChildrenIterator* TreeWalker::PopState() {
|
2018-03-13 16:51:33 +03:00
|
|
|
mStateStack.RemoveLastElement();
|
2016-03-10 23:50:37 +03:00
|
|
|
return mStateStack.IsEmpty() ? nullptr : &mStateStack.LastElement();
|
2010-02-21 03:55:04 +03:00
|
|
|
}
|