summaryrefslogtreecommitdiffstats
path: root/accessible/android/TraversalRule.cpp
blob: 4203fd48c6d754d3ff59cbb50a116fe11c251de8 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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 "TraversalRule.h"

#include "mozilla/ArrayUtils.h"
#include "mozilla/a11y/Accessible.h"

#include "mozilla/a11y/Role.h"
#include "HTMLListAccessible.h"
#include "SessionAccessibility.h"
#include "nsAccUtils.h"
#include "nsIAccessiblePivot.h"

using namespace mozilla;
using namespace mozilla::a11y;

TraversalRule::TraversalRule()
    : TraversalRule(java::SessionAccessibility::HTML_GRANULARITY_DEFAULT,
                    true) {}

TraversalRule::TraversalRule(int32_t aGranularity, bool aIsLocal)
    : mGranularity(aGranularity), mIsLocal(aIsLocal) {}

uint16_t TraversalRule::Match(Accessible* aAcc) {
  MOZ_ASSERT(aAcc);

  if (mIsLocal && aAcc->IsRemote()) {
    // If we encounter a remote accessible in a local rule, we should
    // ignore the subtree because we won't encounter anymore local accessibles
    // in it.
    return nsIAccessibleTraversalRule::FILTER_IGNORE |
           nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
  } else if (!mIsLocal && aAcc->IsLocal()) {
    // If we encounter a local accessible in a remote rule we are likely
    // traversing backwards/upwards, we don't ignore its subtree because it is
    // likely the outer doc root of the remote tree.
    return nsIAccessibleTraversalRule::FILTER_IGNORE;
  }

  uint16_t result = nsIAccessibleTraversalRule::FILTER_IGNORE;

  if (nsAccUtils::MustPrune(aAcc)) {
    result |= nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
  }

  uint64_t state = aAcc->State();

  if ((state & states::INVISIBLE) != 0) {
    return result;
  }

  if (aAcc->Opacity() == 0.0f) {
    return result | nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
  }

  switch (mGranularity) {
    case java::SessionAccessibility::HTML_GRANULARITY_LINK:
      result |= LinkMatch(aAcc);
      break;
    case java::SessionAccessibility::HTML_GRANULARITY_CONTROL:
      result |= ControlMatch(aAcc);
      break;
    case java::SessionAccessibility::HTML_GRANULARITY_SECTION:
      result |= SectionMatch(aAcc);
      break;
    case java::SessionAccessibility::HTML_GRANULARITY_HEADING:
      result |= HeadingMatch(aAcc);
      break;
    case java::SessionAccessibility::HTML_GRANULARITY_LANDMARK:
      result |= LandmarkMatch(aAcc);
      break;
    default:
      result |= DefaultMatch(aAcc);
      break;
  }

  return result;
}

bool TraversalRule::IsSingleLineage(Accessible* aAccessible) {
  Accessible* child = aAccessible;
  while (child) {
    switch (child->ChildCount()) {
      case 0:
        return true;
      case 1:
        child = child->FirstChild();
        break;
      case 2:
        if (IsListItemBullet(child->FirstChild())) {
          child = child->LastChild();
        } else {
          return false;
        }
        break;
      default:
        return false;
    }
  }

  return true;
}

bool TraversalRule::IsListItemBullet(const Accessible* aAccessible) {
  return aAccessible->Role() == roles::LISTITEM_MARKER;
}

bool TraversalRule::IsFlatSubtree(const Accessible* aAccessible) {
  for (auto child = aAccessible->FirstChild(); child;
       child = child->NextSibling()) {
    roles::Role role = child->Role();
    if (role == roles::TEXT_LEAF || role == roles::STATICTEXT) {
      continue;
    }

    if (child->ChildCount() > 0 || child->ActionCount() > 0) {
      return false;
    }
  }

  return true;
}

bool TraversalRule::HasName(const Accessible* aAccessible) {
  nsAutoString name;
  aAccessible->Name(name);
  name.CompressWhitespace();
  return !name.IsEmpty();
}

uint16_t TraversalRule::LinkMatch(Accessible* aAccessible) {
  if (aAccessible->Role() == roles::LINK &&
      (aAccessible->State() & states::LINKED) != 0) {
    return nsIAccessibleTraversalRule::FILTER_MATCH |
           nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
  }

  return nsIAccessibleTraversalRule::FILTER_IGNORE;
}

uint16_t TraversalRule::HeadingMatch(Accessible* aAccessible) {
  if (aAccessible->Role() == roles::HEADING && aAccessible->ChildCount()) {
    return nsIAccessibleTraversalRule::FILTER_MATCH;
  }

  return nsIAccessibleTraversalRule::FILTER_IGNORE;
}

uint16_t TraversalRule::SectionMatch(Accessible* aAccessible) {
  roles::Role role = aAccessible->Role();
  if (role == roles::HEADING || role == roles::LANDMARK ||
      aAccessible->LandmarkRole()) {
    return nsIAccessibleTraversalRule::FILTER_MATCH;
  }

  return nsIAccessibleTraversalRule::FILTER_IGNORE;
}

uint16_t TraversalRule::LandmarkMatch(Accessible* aAccessible) {
  if (aAccessible->LandmarkRole()) {
    return nsIAccessibleTraversalRule::FILTER_MATCH;
  }

  return nsIAccessibleTraversalRule::FILTER_IGNORE;
}

uint16_t TraversalRule::ControlMatch(Accessible* aAccessible) {
  switch (aAccessible->Role()) {
    case roles::PUSHBUTTON:
    case roles::SPINBUTTON:
    case roles::TOGGLE_BUTTON:
    case roles::BUTTONDROPDOWN:
    case roles::COMBOBOX:
    case roles::LISTBOX:
    case roles::ENTRY:
    case roles::PASSWORD_TEXT:
    case roles::PAGETAB:
    case roles::RADIOBUTTON:
    case roles::RADIO_MENU_ITEM:
    case roles::SLIDER:
    case roles::CHECKBUTTON:
    case roles::CHECK_MENU_ITEM:
    case roles::SWITCH:
    case roles::MENUITEM:
      return nsIAccessibleTraversalRule::FILTER_MATCH |
             nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
    case roles::LINK:
      return LinkMatch(aAccessible);
    case roles::EDITCOMBOBOX:
      if (aAccessible->State() & states::EDITABLE) {
        // Only match ARIA 1.0 comboboxes; i.e. where the combobox itself is
        // editable. If it's a 1.1 combobox, the combobox is just a container;
        // we want to stop on the textbox inside it, not the container.
        return nsIAccessibleTraversalRule::FILTER_MATCH |
               nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
      }
      break;
    default:
      break;
  }

  return nsIAccessibleTraversalRule::FILTER_IGNORE;
}

uint16_t TraversalRule::DefaultMatch(Accessible* aAccessible) {
  switch (aAccessible->Role()) {
    case roles::COMBOBOX:
      // We don't want to ignore the subtree because this is often
      // where the list box hangs out.
      return nsIAccessibleTraversalRule::FILTER_MATCH;
    case roles::EDITCOMBOBOX:
      if (aAccessible->State() & states::EDITABLE) {
        // Only match ARIA 1.0 comboboxes; i.e. where the combobox itself is
        // editable. If it's a 1.1 combobox, the combobox is just a container;
        // we want to stop on the textbox inside it.
        return nsIAccessibleTraversalRule::FILTER_MATCH;
      }
      break;
    case roles::TEXT_LEAF:
    case roles::GRAPHIC:
      // Nameless text leaves are boring, skip them.
      if (HasName(aAccessible)) {
        return nsIAccessibleTraversalRule::FILTER_MATCH;
      }
      break;
    case roles::STATICTEXT:
      // Ignore list bullets
      if (!IsListItemBullet(aAccessible)) {
        return nsIAccessibleTraversalRule::FILTER_MATCH;
      }
      break;
    case roles::HEADING:
    case roles::COLUMNHEADER:
    case roles::ROWHEADER:
    case roles::STATUSBAR:
      if ((aAccessible->ChildCount() > 0 || HasName(aAccessible)) &&
          (IsSingleLineage(aAccessible) || IsFlatSubtree(aAccessible))) {
        return nsIAccessibleTraversalRule::FILTER_MATCH |
               nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
      }
      break;
    case roles::GRID_CELL:
      if (IsSingleLineage(aAccessible) || IsFlatSubtree(aAccessible)) {
        return nsIAccessibleTraversalRule::FILTER_MATCH |
               nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
      }
      break;
    case roles::LABEL:
      if (IsFlatSubtree(aAccessible)) {
        // Match if this is a label with text but no nested controls.
        return nsIAccessibleTraversalRule::FILTER_MATCH |
               nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
      }
      break;
    case roles::MENUITEM:
    case roles::LINK:
    case roles::PAGETAB:
    case roles::PUSHBUTTON:
    case roles::CHECKBUTTON:
    case roles::RADIOBUTTON:
    case roles::PROGRESSBAR:
    case roles::BUTTONDROPDOWN:
    case roles::BUTTONMENU:
    case roles::CHECK_MENU_ITEM:
    case roles::PASSWORD_TEXT:
    case roles::RADIO_MENU_ITEM:
    case roles::TOGGLE_BUTTON:
    case roles::ENTRY:
    case roles::KEY:
    case roles::SLIDER:
    case roles::SPINBUTTON:
    case roles::OPTION:
    case roles::SWITCH:
    case roles::MATHML_MATH:
      // Ignore the subtree, if there is one. So that we don't land on
      // the same content that was already presented by its parent.
      return nsIAccessibleTraversalRule::FILTER_MATCH |
             nsIAccessibleTraversalRule::FILTER_IGNORE_SUBTREE;
    default:
      break;
  }

  return nsIAccessibleTraversalRule::FILTER_IGNORE;
}