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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/* 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 "AccIterator.h"
#include "AccGroupInfo.h"
#include "ARIAMap.h"
#include "DocAccessible-inl.h"
#include "LocalAccessible-inl.h"
#include "nsAccUtils.h"
#include "XULTreeAccessible.h"
#include "mozilla/a11y/DocAccessibleParent.h"
#include "mozilla/dom/DocumentOrShadowRoot.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLLabelElement.h"
using namespace mozilla;
using namespace mozilla::a11y;
////////////////////////////////////////////////////////////////////////////////
// AccIterator
////////////////////////////////////////////////////////////////////////////////
AccIterator::AccIterator(const LocalAccessible* aAccessible,
filters::FilterFuncPtr aFilterFunc)
: mFilterFunc(aFilterFunc) {
mState = new IteratorState(aAccessible);
}
AccIterator::~AccIterator() {
while (mState) {
IteratorState* tmp = mState;
mState = tmp->mParentState;
delete tmp;
}
}
LocalAccessible* AccIterator::Next() {
while (mState) {
LocalAccessible* child = mState->mParent->LocalChildAt(mState->mIndex++);
if (!child) {
IteratorState* tmp = mState;
mState = mState->mParentState;
delete tmp;
continue;
}
uint32_t result = mFilterFunc(child);
if (result & filters::eMatch) return child;
if (!(result & filters::eSkipSubtree)) {
IteratorState* childState = new IteratorState(child, mState);
mState = childState;
}
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// nsAccIterator::IteratorState
AccIterator::IteratorState::IteratorState(const LocalAccessible* aParent,
IteratorState* mParentState)
: mParent(aParent), mIndex(0), mParentState(mParentState) {}
////////////////////////////////////////////////////////////////////////////////
// RelatedAccIterator
////////////////////////////////////////////////////////////////////////////////
RelatedAccIterator::RelatedAccIterator(DocAccessible* aDocument,
nsIContent* aDependentContent,
nsAtom* aRelAttr)
: mDocument(aDocument),
mDependentContent(aDependentContent),
mRelAttr(aRelAttr),
mProviders(nullptr),
mIndex(0),
mIsWalkingDependentElements(false) {
nsAutoString id;
if (aDependentContent->IsElement() &&
aDependentContent->AsElement()->GetAttr(nsGkAtoms::id, id)) {
mProviders = mDocument->GetRelProviders(aDependentContent->AsElement(), id);
}
}
LocalAccessible* RelatedAccIterator::Next() {
if (!mProviders || mIndex == mProviders->Length()) {
if (mIsWalkingDependentElements) {
// We've walked both dependent ids and dependent elements, so there are
// no more targets.
return nullptr;
}
// We've returned all dependent ids, but there might be dependent elements
// too. Walk those next.
mIsWalkingDependentElements = true;
mIndex = 0;
if (auto providers =
mDocument->mDependentElementsMap.Lookup(mDependentContent)) {
mProviders = &providers.Data();
} else {
mProviders = nullptr;
return nullptr;
}
}
while (mIndex < mProviders->Length()) {
const auto& provider = (*mProviders)[mIndex++];
// Return related accessible for the given attribute.
if (mRelAttr && provider->mRelAttr != mRelAttr) {
continue;
}
// If we're walking elements (not ids), the explicitly set attr-element
// `mDependentContent` must be a descendant of any of the refering element
// `mProvider->mContent`'s shadow-including ancestors.
if (mIsWalkingDependentElements &&
!nsCoreUtils::IsDescendantOfAnyShadowIncludingAncestor(
mDependentContent, provider->mContent)) {
continue;
}
LocalAccessible* related = mDocument->GetAccessible(provider->mContent);
if (related) {
return related;
}
// If the document content is pointed by relation then return the
// document itself.
if (provider->mContent == mDocument->GetContent()) {
return mDocument;
}
}
// We exhausted mProviders without returning anything.
if (!mIsWalkingDependentElements) {
// Call this function again to start walking the dependent elements.
return Next();
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// HTMLLabelIterator
////////////////////////////////////////////////////////////////////////////////
HTMLLabelIterator::HTMLLabelIterator(DocAccessible* aDocument,
const LocalAccessible* aAccessible,
LabelFilter aFilter)
: mRelIter(aDocument, aAccessible->GetContent(), nsGkAtoms::_for),
mAcc(aAccessible),
mLabelFilter(aFilter) {}
bool HTMLLabelIterator::IsLabel(LocalAccessible* aLabel) {
dom::HTMLLabelElement* labelEl =
dom::HTMLLabelElement::FromNode(aLabel->GetContent());
return labelEl && labelEl->GetControl() == mAcc->GetContent();
}
LocalAccessible* HTMLLabelIterator::Next() {
// Get either <label for="[id]"> element which explicitly points to given
// element, or <label> ancestor which implicitly point to it.
LocalAccessible* label = nullptr;
while ((label = mRelIter.Next())) {
if (IsLabel(label)) {
return label;
}
}
// Ignore ancestor label on not widget accessible.
if (mLabelFilter == eSkipAncestorLabel || !mAcc->IsWidget()) return nullptr;
// Go up tree to get a name of ancestor label if there is one (an ancestor
// <label> implicitly points to us). Don't go up farther than form or
// document.
LocalAccessible* walkUp = mAcc->LocalParent();
while (walkUp && !walkUp->IsDoc()) {
nsIContent* walkUpEl = walkUp->GetContent();
if (IsLabel(walkUp) && !walkUpEl->AsElement()->HasAttr(nsGkAtoms::_for)) {
mLabelFilter = eSkipAncestorLabel; // prevent infinite loop
return walkUp;
}
if (walkUpEl->IsHTMLElement(nsGkAtoms::form)) break;
walkUp = walkUp->LocalParent();
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// HTMLOutputIterator
////////////////////////////////////////////////////////////////////////////////
HTMLOutputIterator::HTMLOutputIterator(DocAccessible* aDocument,
nsIContent* aElement)
: mRelIter(aDocument, aElement, nsGkAtoms::_for) {}
LocalAccessible* HTMLOutputIterator::Next() {
LocalAccessible* output = nullptr;
while ((output = mRelIter.Next())) {
if (output->GetContent()->IsHTMLElement(nsGkAtoms::output)) return output;
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// XULLabelIterator
////////////////////////////////////////////////////////////////////////////////
XULLabelIterator::XULLabelIterator(DocAccessible* aDocument,
nsIContent* aElement)
: mRelIter(aDocument, aElement, nsGkAtoms::control) {}
LocalAccessible* XULLabelIterator::Next() {
LocalAccessible* label = nullptr;
while ((label = mRelIter.Next())) {
if (label->GetContent()->IsXULElement(nsGkAtoms::label)) return label;
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// XULDescriptionIterator
////////////////////////////////////////////////////////////////////////////////
XULDescriptionIterator::XULDescriptionIterator(DocAccessible* aDocument,
nsIContent* aElement)
: mRelIter(aDocument, aElement, nsGkAtoms::control) {}
LocalAccessible* XULDescriptionIterator::Next() {
LocalAccessible* descr = nullptr;
while ((descr = mRelIter.Next())) {
if (descr->GetContent()->IsXULElement(nsGkAtoms::description)) return descr;
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// AssociatedElementsIterator
////////////////////////////////////////////////////////////////////////////////
AssociatedElementsIterator::AssociatedElementsIterator(DocAccessible* aDoc,
nsIContent* aContent,
nsAtom* aIDRefsAttr)
: mContent(aContent), mDoc(aDoc), mCurrIdx(0), mElemIdx(0) {
if (mContent->IsElement()) {
mContent->AsElement()->GetAttr(aIDRefsAttr, mIDs);
if (mIDs.IsEmpty() &&
(aria::AttrCharacteristicsFor(aIDRefsAttr) & ATTR_REFLECT_ELEMENTS)) {
nsAccUtils::GetARIAElementsAttr(mContent->AsElement(), aIDRefsAttr,
mElements);
}
}
}
const nsDependentSubstring AssociatedElementsIterator::NextID() {
for (; mCurrIdx < mIDs.Length(); mCurrIdx++) {
if (!NS_IsAsciiWhitespace(mIDs[mCurrIdx])) break;
}
if (mCurrIdx >= mIDs.Length()) return nsDependentSubstring();
nsAString::index_type idStartIdx = mCurrIdx;
while (++mCurrIdx < mIDs.Length()) {
if (NS_IsAsciiWhitespace(mIDs[mCurrIdx])) break;
}
return Substring(mIDs, idStartIdx, mCurrIdx++ - idStartIdx);
}
dom::Element* AssociatedElementsIterator::NextElem() {
while (true) {
const nsDependentSubstring id = NextID();
if (id.IsEmpty()) break;
dom::Element* refContent = GetElem(id);
if (refContent) return refContent;
}
while (dom::Element* element = mElements.SafeElementAt(mElemIdx++)) {
if (nsCoreUtils::IsDescendantOfAnyShadowIncludingAncestor(element,
mContent)) {
return element;
}
}
return nullptr;
}
dom::Element* AssociatedElementsIterator::GetElem(nsIContent* aContent,
const nsAString& aID) {
// Get elements in DOM tree by ID attribute if this is an explicit content.
// In case of bound element check its anonymous subtree.
if (!aContent->IsInNativeAnonymousSubtree()) {
dom::DocumentOrShadowRoot* docOrShadowRoot =
aContent->GetUncomposedDocOrConnectedShadowRoot();
if (docOrShadowRoot) {
dom::Element* refElm = docOrShadowRoot->GetElementById(aID);
if (refElm) {
return refElm;
}
}
}
return nullptr;
}
dom::Element* AssociatedElementsIterator::GetElem(
const nsDependentSubstring& aID) {
return GetElem(mContent, aID);
}
LocalAccessible* AssociatedElementsIterator::Next() {
dom::Element* nextEl = nullptr;
while ((nextEl = NextElem())) {
LocalAccessible* acc = mDoc->GetAccessible(nextEl);
if (acc) {
return acc;
}
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// SingleAccIterator
////////////////////////////////////////////////////////////////////////////////
Accessible* SingleAccIterator::Next() {
Accessible* nextAcc = mAcc;
mAcc = nullptr;
if (!nextAcc) {
return nullptr;
}
MOZ_ASSERT(!nextAcc->IsLocal() || !nextAcc->AsLocal()->IsDefunct(),
"Iterator references defunct accessible?");
return nextAcc;
}
////////////////////////////////////////////////////////////////////////////////
// ItemIterator
////////////////////////////////////////////////////////////////////////////////
Accessible* ItemIterator::Next() {
if (mContainer) {
mAnchor = AccGroupInfo::FirstItemOf(mContainer);
mContainer = nullptr;
return mAnchor;
}
if (mAnchor) {
mAnchor = AccGroupInfo::NextItemTo(mAnchor);
}
return mAnchor;
}
////////////////////////////////////////////////////////////////////////////////
// XULTreeItemIterator
////////////////////////////////////////////////////////////////////////////////
XULTreeItemIterator::XULTreeItemIterator(const XULTreeAccessible* aXULTree,
nsITreeView* aTreeView,
int32_t aRowIdx)
: mXULTree(aXULTree),
mTreeView(aTreeView),
mRowCount(-1),
mContainerLevel(-1),
mCurrRowIdx(aRowIdx + 1) {
mTreeView->GetRowCount(&mRowCount);
if (aRowIdx != -1) mTreeView->GetLevel(aRowIdx, &mContainerLevel);
}
LocalAccessible* XULTreeItemIterator::Next() {
while (mCurrRowIdx < mRowCount) {
int32_t level = 0;
mTreeView->GetLevel(mCurrRowIdx, &level);
if (level == mContainerLevel + 1) {
return mXULTree->GetTreeItemAccessible(mCurrRowIdx++);
}
if (level <= mContainerLevel) { // got level up
mCurrRowIdx = mRowCount;
break;
}
mCurrRowIdx++;
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// RemoteAccIterator
////////////////////////////////////////////////////////////////////////////////
Accessible* RemoteAccIterator::Next() {
while (mIndex < mIds.Length()) {
uint64_t id = mIds[mIndex++];
Accessible* acc = mDoc->GetAccessible(id);
if (acc) {
return acc;
}
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
// ArrayAccIterator
////////////////////////////////////////////////////////////////////////////////
Accessible* ArrayAccIterator::Next() {
if (mIndex < mAccs.Length()) {
return mAccs[mIndex++];
}
return nullptr;
}
|