2010-02-20 16:55:04 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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-20 16:55:04 -08:00
|
|
|
|
2012-11-19 01:20:09 -08:00
|
|
|
#include "TreeWalker.h"
|
2010-02-20 16:55:04 -08:00
|
|
|
|
2012-05-28 18:18:45 -07:00
|
|
|
#include "Accessible.h"
|
2010-02-20 16:55:04 -08:00
|
|
|
#include "nsAccessibilityService.h"
|
2012-05-27 02:01:40 -07:00
|
|
|
#include "DocAccessible.h"
|
2010-02-20 16:55:04 -08:00
|
|
|
|
2014-07-16 11:41:57 -07:00
|
|
|
#include "mozilla/dom/ChildIterator.h"
|
2014-02-15 07:21:40 -08:00
|
|
|
#include "mozilla/dom/Element.h"
|
2010-06-08 09:39:58 -07:00
|
|
|
|
2012-11-17 18:01:44 -08:00
|
|
|
using namespace mozilla::a11y;
|
|
|
|
|
2014-09-01 05:31:35 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WalkState
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace a11y {
|
|
|
|
|
|
|
|
struct WalkState
|
|
|
|
{
|
|
|
|
WalkState(nsIContent *aContent, uint32_t aFilter) :
|
|
|
|
content(aContent), prevState(nullptr), iter(aContent, aFilter) {}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
WalkState *prevState;
|
|
|
|
dom::AllChildrenIterator iter;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace a11y
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-02-20 16:55:04 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-11-19 01:20:09 -08:00
|
|
|
// TreeWalker
|
2010-02-20 16:55:04 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-19 01:20:09 -08:00
|
|
|
TreeWalker::
|
2014-02-15 07:21:40 -08:00
|
|
|
TreeWalker(Accessible* aContext, nsIContent* aContent, uint32_t aFlags) :
|
2014-09-01 05:31:35 -07:00
|
|
|
mDoc(aContext->Document()), mContext(aContext),
|
|
|
|
mFlags(aFlags), mState(nullptr)
|
2010-02-20 16:55:04 -08:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent, "No node for the accessible tree walker!");
|
|
|
|
|
2012-11-17 07:27:03 -08:00
|
|
|
mChildFilter = mContext->CanHaveAnonChildren() ?
|
|
|
|
nsIContent::eAllChildren : nsIContent::eAllButXBL;
|
2010-10-15 08:34:35 -07:00
|
|
|
mChildFilter |= nsIContent::eSkipPlaceholderContent;
|
|
|
|
|
2014-07-16 11:41:57 -07:00
|
|
|
if (aContent)
|
2014-09-01 05:31:35 -07:00
|
|
|
mState = new WalkState(aContent, mChildFilter);
|
2014-07-16 11:41:57 -07:00
|
|
|
|
2012-11-19 01:20:09 -08:00
|
|
|
MOZ_COUNT_CTOR(TreeWalker);
|
2010-02-20 16:55:04 -08:00
|
|
|
}
|
|
|
|
|
2012-11-19 01:20:09 -08:00
|
|
|
TreeWalker::~TreeWalker()
|
2010-02-20 16:55:04 -08:00
|
|
|
{
|
2014-09-01 05:31:35 -07:00
|
|
|
// Clear state stack from memory
|
|
|
|
while (mState)
|
|
|
|
PopState();
|
|
|
|
|
2012-11-19 01:20:09 -08:00
|
|
|
MOZ_COUNT_DTOR(TreeWalker);
|
2010-02-20 16:55:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-11-19 01:20:09 -08:00
|
|
|
// TreeWalker: private
|
2010-02-20 16:55:04 -08:00
|
|
|
|
2012-05-28 18:18:45 -07:00
|
|
|
Accessible*
|
2014-09-01 05:31:35 -07:00
|
|
|
TreeWalker::NextChildInternal(bool aNoWalkUp)
|
2010-02-20 16:55:04 -08:00
|
|
|
{
|
2014-09-01 05:31:35 -07:00
|
|
|
if (!mState || !mState->content)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-02-20 16:55:04 -08:00
|
|
|
|
2014-09-01 05:31:35 -07:00
|
|
|
while (nsIContent* childNode = mState->iter.GetNextChild()) {
|
|
|
|
bool isSubtreeHidden = false;
|
|
|
|
Accessible* accessible = mFlags & eWalkCache ?
|
|
|
|
mDoc->GetAccessible(childNode) :
|
|
|
|
GetAccService()->GetOrCreateAccessible(childNode, mContext,
|
|
|
|
&isSubtreeHidden);
|
|
|
|
|
|
|
|
if (accessible)
|
|
|
|
return accessible;
|
2010-02-20 16:55:04 -08:00
|
|
|
|
2014-09-01 05:31:35 -07:00
|
|
|
// Walk down into subtree to find accessibles.
|
|
|
|
if (!isSubtreeHidden && childNode->IsElement()) {
|
|
|
|
PushState(childNode);
|
|
|
|
accessible = NextChildInternal(true);
|
2010-02-20 16:55:04 -08:00
|
|
|
if (accessible)
|
2011-03-31 02:30:58 -07:00
|
|
|
return accessible;
|
2014-08-11 23:02:28 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 07:21:40 -08:00
|
|
|
|
2014-09-01 05:31:35 -07:00
|
|
|
// No more children, get back to the parent.
|
|
|
|
nsIContent* anchorNode = mState->content;
|
|
|
|
PopState();
|
|
|
|
if (aNoWalkUp)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (mState)
|
|
|
|
return NextChildInternal(false);
|
|
|
|
|
2014-02-15 07:21:40 -08: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-09-01 05:31:35 -07:00
|
|
|
while (anchorNode != mContext->GetNode()) {
|
|
|
|
nsINode* parentNode = anchorNode->GetFlattenedTreeParent();
|
2014-02-15 07:21:40 -08:00
|
|
|
if (!parentNode || !parentNode->IsElement())
|
|
|
|
return nullptr;
|
2010-02-20 16:55:04 -08:00
|
|
|
|
2014-09-01 05:31:35 -07:00
|
|
|
PushState(parentNode->AsElement());
|
|
|
|
while (nsIContent* childNode = mState->iter.GetNextChild()) {
|
|
|
|
if (childNode == anchorNode)
|
|
|
|
return NextChildInternal(false);
|
2014-02-15 07:21:40 -08:00
|
|
|
}
|
2014-09-01 05:31:35 -07:00
|
|
|
PopState();
|
|
|
|
|
|
|
|
anchorNode = parentNode->AsElement();
|
2014-02-15 07:21:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2010-02-20 16:55:04 -08:00
|
|
|
}
|
|
|
|
|
2014-09-01 05:31:35 -07:00
|
|
|
void
|
2012-11-19 01:20:09 -08:00
|
|
|
TreeWalker::PopState()
|
2010-02-20 16:55:04 -08:00
|
|
|
{
|
2014-09-01 05:31:35 -07:00
|
|
|
WalkState* prevToLastState = mState->prevState;
|
|
|
|
delete mState;
|
|
|
|
mState = prevToLastState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TreeWalker::PushState(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
WalkState* nextToLastState = new WalkState(aContent, mChildFilter);
|
|
|
|
nextToLastState->prevState = mState;
|
|
|
|
mState = nextToLastState;
|
2010-02-20 16:55:04 -08:00
|
|
|
}
|