2010-02-21 00:55:04 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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 00:55:04 +00:00
|
|
|
|
2012-11-19 09:20:09 +00:00
|
|
|
#include "TreeWalker.h"
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2012-05-29 01:18:45 +00:00
|
|
|
#include "Accessible.h"
|
2010-02-21 00:55:04 +00:00
|
|
|
#include "nsAccessibilityService.h"
|
2012-05-27 09:01:40 +00:00
|
|
|
#include "DocAccessible.h"
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
#include "mozilla/dom/ChildIterator.h"
|
2014-02-15 15:21:40 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2010-06-08 16:39:58 +00:00
|
|
|
|
2014-08-12 06:02:28 +00:00
|
|
|
using namespace mozilla;
|
2012-11-18 02:01:44 +00:00
|
|
|
using namespace mozilla::a11y;
|
|
|
|
|
2010-02-21 00:55:04 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-11-19 09:20:09 +00:00
|
|
|
// TreeWalker
|
2010-02-21 00:55:04 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-19 09:20:09 +00:00
|
|
|
TreeWalker::
|
2014-02-15 15:21:40 +00:00
|
|
|
TreeWalker(Accessible* aContext, nsIContent* aContent, uint32_t aFlags) :
|
2014-08-12 06:02:28 +00:00
|
|
|
mDoc(aContext->Document()), mContext(aContext), mAnchorNode(aContent),
|
|
|
|
mFlags(aFlags)
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent, "No node for the accessible tree walker!");
|
|
|
|
|
2012-11-17 15:27:03 +00:00
|
|
|
mChildFilter = mContext->CanHaveAnonChildren() ?
|
|
|
|
nsIContent::eAllChildren : nsIContent::eAllButXBL;
|
2010-10-15 15:34:35 +00:00
|
|
|
mChildFilter |= nsIContent::eSkipPlaceholderContent;
|
|
|
|
|
2014-07-16 18:41:57 +00:00
|
|
|
if (aContent)
|
2014-08-12 06:02:28 +00:00
|
|
|
PushState(aContent);
|
2014-07-16 18:41:57 +00:00
|
|
|
|
2012-11-19 09:20:09 +00:00
|
|
|
MOZ_COUNT_CTOR(TreeWalker);
|
2010-02-21 00:55:04 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 09:20:09 +00:00
|
|
|
TreeWalker::~TreeWalker()
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
2012-11-19 09:20:09 +00:00
|
|
|
MOZ_COUNT_DTOR(TreeWalker);
|
2010-02-21 00:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-11-19 09:20:09 +00:00
|
|
|
// TreeWalker: private
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2012-05-29 01:18:45 +00:00
|
|
|
Accessible*
|
2014-08-12 06:02:28 +00:00
|
|
|
TreeWalker::NextChild()
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
2014-08-12 06:02:28 +00:00
|
|
|
if (mStateStack.IsEmpty())
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2014-08-12 06:02:28 +00:00
|
|
|
dom::AllChildrenIterator* top = &mStateStack[mStateStack.Length() - 1];
|
|
|
|
while (top) {
|
|
|
|
while (nsIContent* childNode = top->GetNextChild()) {
|
|
|
|
bool isSubtreeHidden = false;
|
|
|
|
Accessible* accessible = mFlags & eWalkCache ?
|
|
|
|
mDoc->GetAccessible(childNode) :
|
|
|
|
GetAccService()->GetOrCreateAccessible(childNode, mContext,
|
|
|
|
&isSubtreeHidden);
|
2014-09-01 12:31:35 +00:00
|
|
|
|
2010-02-21 00:55:04 +00:00
|
|
|
if (accessible)
|
2011-03-31 09:30:58 +00:00
|
|
|
return accessible;
|
2014-02-15 15:21:40 +00:00
|
|
|
|
2014-08-12 06:02:28 +00:00
|
|
|
// Walk down into subtree to find accessibles.
|
|
|
|
if (!isSubtreeHidden && childNode->IsElement())
|
|
|
|
top = PushState(childNode);
|
|
|
|
}
|
2014-09-01 12:31:35 +00:00
|
|
|
|
2014-08-12 06:02:28 +00:00
|
|
|
top = PopState();
|
|
|
|
}
|
2014-09-01 12:31:35 +00:00
|
|
|
|
2014-02-15 15:21:40 +00:00
|
|
|
// If we traversed the whole subtree of the anchor node. Move to next node
|
|
|
|
// relative anchor node within the context subtree if possible.
|
|
|
|
if (mFlags != eWalkContextTree)
|
|
|
|
return nullptr;
|
|
|
|
|
2014-08-12 06:02:28 +00:00
|
|
|
nsINode* contextNode = mContext->GetNode();
|
|
|
|
while (mAnchorNode != contextNode) {
|
|
|
|
nsINode* parentNode = mAnchorNode->GetFlattenedTreeParent();
|
2014-02-15 15:21:40 +00:00
|
|
|
if (!parentNode || !parentNode->IsElement())
|
|
|
|
return nullptr;
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2014-08-12 06:02:28 +00:00
|
|
|
nsIContent* parent = parentNode->AsElement();
|
|
|
|
top = mStateStack.AppendElement(dom::AllChildrenIterator(parent,
|
|
|
|
mChildFilter));
|
|
|
|
while (nsIContent* childNode = top->GetNextChild()) {
|
|
|
|
if (childNode == mAnchorNode) {
|
|
|
|
mAnchorNode = parent;
|
|
|
|
return NextChild();
|
|
|
|
}
|
2014-02-15 15:21:40 +00:00
|
|
|
}
|
2014-09-01 12:31:35 +00:00
|
|
|
|
2014-08-12 06:02:28 +00: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 15:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2010-02-21 00:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-12 06:02:28 +00:00
|
|
|
dom::AllChildrenIterator*
|
2012-11-19 09:20:09 +00:00
|
|
|
TreeWalker::PopState()
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
2014-08-12 06:02:28 +00:00
|
|
|
size_t length = mStateStack.Length();
|
|
|
|
mStateStack.RemoveElementAt(length - 1);
|
|
|
|
return mStateStack.IsEmpty() ? nullptr : &mStateStack[mStateStack.Length() - 1];
|
2010-02-21 00:55:04 +00:00
|
|
|
}
|