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-02-15 15:21:40 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2010-06-08 16:39:58 +00:00
|
|
|
|
2012-11-18 02:01:44 +00:00
|
|
|
using namespace mozilla::a11y;
|
|
|
|
|
2010-02-21 00:55:04 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WalkState
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-19 09:20:09 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace a11y {
|
|
|
|
|
2010-02-21 00:55:04 +00:00
|
|
|
struct WalkState
|
|
|
|
{
|
|
|
|
WalkState(nsIContent *aContent) :
|
2012-07-30 14:20:58 +00:00
|
|
|
content(aContent), childIdx(0), prevState(nullptr) {}
|
2010-02-21 00:55:04 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
nsCOMPtr<nsINodeList> childList;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t childIdx;
|
2010-02-21 00:55:04 +00:00
|
|
|
WalkState *prevState;
|
|
|
|
};
|
|
|
|
|
2012-11-19 09:20:09 +00:00
|
|
|
} // namespace a11y
|
|
|
|
} // namespace mozilla
|
|
|
|
|
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) :
|
2012-11-19 09:20:09 +00:00
|
|
|
mDoc(aContext->Document()), mContext(aContext),
|
2014-02-15 15:21:40 +00:00
|
|
|
mFlags(aFlags), mState(nullptr)
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent, "No node for the accessible tree walker!");
|
|
|
|
|
|
|
|
if (aContent)
|
|
|
|
mState = new WalkState(aContent);
|
|
|
|
|
2012-11-17 15:27:03 +00:00
|
|
|
mChildFilter = mContext->CanHaveAnonChildren() ?
|
|
|
|
nsIContent::eAllChildren : nsIContent::eAllButXBL;
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2010-10-15 15:34:35 +00:00
|
|
|
mChildFilter |= nsIContent::eSkipPlaceholderContent;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// Clear state stack from memory
|
|
|
|
while (mState)
|
|
|
|
PopState();
|
|
|
|
|
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*
|
2012-11-19 09:20:09 +00:00
|
|
|
TreeWalker::NextChildInternal(bool aNoWalkUp)
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
|
|
|
if (!mState || !mState->content)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-02-21 00:55:04 +00:00
|
|
|
|
|
|
|
if (!mState->childList)
|
2010-10-15 15:34:35 +00:00
|
|
|
mState->childList = mState->content->GetChildren(mChildFilter);
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = 0;
|
2010-02-21 00:55:04 +00:00
|
|
|
if (mState->childList)
|
|
|
|
mState->childList->GetLength(&length);
|
|
|
|
|
2010-02-21 01:31:07 +00:00
|
|
|
while (mState->childIdx < length) {
|
2012-10-13 12:50:24 +00:00
|
|
|
nsIContent* childNode = mState->childList->Item(mState->childIdx);
|
2010-02-21 00:55:04 +00:00
|
|
|
mState->childIdx++;
|
|
|
|
|
2010-11-13 17:49:26 +00:00
|
|
|
bool isSubtreeHidden = false;
|
2014-02-15 15:21:40 +00:00
|
|
|
Accessible* accessible = mFlags & eWalkCache ?
|
|
|
|
mDoc->GetAccessible(childNode) :
|
2012-11-30 07:33:01 +00:00
|
|
|
GetAccService()->GetOrCreateAccessible(childNode, mContext,
|
|
|
|
&isSubtreeHidden);
|
2010-02-21 00:55:04 +00:00
|
|
|
|
|
|
|
if (accessible)
|
2011-03-31 09:30:58 +00:00
|
|
|
return accessible;
|
2010-02-21 00:55:04 +00:00
|
|
|
|
|
|
|
// Walk down into subtree to find accessibles.
|
2010-11-13 17:49:26 +00:00
|
|
|
if (!isSubtreeHidden) {
|
2014-02-15 15:21:40 +00:00
|
|
|
PushState(childNode);
|
2011-03-31 09:30:58 +00:00
|
|
|
accessible = NextChildInternal(true);
|
2010-02-21 00:55:04 +00:00
|
|
|
if (accessible)
|
2011-03-31 09:30:58 +00:00
|
|
|
return accessible;
|
2010-02-21 00:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No more children, get back to the parent.
|
2014-02-15 15:21:40 +00:00
|
|
|
nsIContent* anchorNode = mState->content;
|
2010-02-21 00:55:04 +00:00
|
|
|
PopState();
|
2014-02-15 15:21:40 +00:00
|
|
|
if (aNoWalkUp)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (mState)
|
|
|
|
return NextChildInternal(false);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
while (anchorNode != mContext->GetNode()) {
|
|
|
|
nsINode* parentNode = anchorNode->GetFlattenedTreeParent();
|
|
|
|
if (!parentNode || !parentNode->IsElement())
|
|
|
|
return nullptr;
|
2010-02-21 00:55:04 +00:00
|
|
|
|
2014-02-15 15:21:40 +00:00
|
|
|
PushState(parentNode->AsElement());
|
|
|
|
mState->childList = mState->content->GetChildren(mChildFilter);
|
|
|
|
length = 0;
|
|
|
|
if (mState->childList)
|
|
|
|
mState->childList->GetLength(&length);
|
|
|
|
|
|
|
|
while (mState->childIdx < length) {
|
|
|
|
nsIContent* childNode = mState->childList->Item(mState->childIdx);
|
|
|
|
mState->childIdx++;
|
|
|
|
if (childNode == anchorNode)
|
|
|
|
return NextChildInternal(false);
|
|
|
|
}
|
|
|
|
PopState();
|
|
|
|
|
|
|
|
anchorNode = parentNode->AsElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2010-02-21 00:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-19 09:20:09 +00:00
|
|
|
TreeWalker::PopState()
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
|
|
|
WalkState* prevToLastState = mState->prevState;
|
|
|
|
delete mState;
|
|
|
|
mState = prevToLastState;
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:21:40 +00:00
|
|
|
void
|
2012-11-19 09:20:09 +00:00
|
|
|
TreeWalker::PushState(nsIContent* aContent)
|
2010-02-21 00:55:04 +00:00
|
|
|
{
|
|
|
|
WalkState* nextToLastState = new WalkState(aContent);
|
|
|
|
nextToLastState->prevState = mState;
|
|
|
|
mState = nextToLastState;
|
|
|
|
}
|