summaryrefslogtreecommitdiffstats
path: root/accessible/base/ARIAStateMap.cpp
blob: 6bf20cf1cce63f74ba654143e7b6fe73072f6c57 (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
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
/* -*- 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 "ARIAMap.h"
#include "nsAccUtils.h"
#include "States.h"

#include "mozilla/dom/Element.h"

using namespace mozilla;
using namespace mozilla::a11y;
using namespace mozilla::a11y::aria;

/**
 * Used to store state map rule data for ARIA attribute of enum type.
 */
struct EnumTypeData {
  // ARIA attribute name.
  nsStaticAtom* const mAttrName;

  // States if the attribute value is matched to the enum value. Used as
  // Element::AttrValuesArray, last item must be nullptr.
  nsStaticAtom* const mValues[4];

  // States applied if corresponding enum values are matched.
  const uint64_t mStates[3];

  // States to clear in case of match.
  const uint64_t mClearState;
};

enum ETokenType {
  eBoolType = 0,
  eMixedType = 1,       // can take 'mixed' value
  eDefinedIfAbsent = 2  // permanent and false state are applied if absent
};

/**
 * Used to store state map rule data for ARIA attribute of token type (including
 * mixed value).
 */
struct TokenTypeData {
  TokenTypeData(nsAtom* aAttrName, uint32_t aType, uint64_t aPermanentState,
                uint64_t aTrueState, uint64_t aFalseState = 0)
      : mAttrName(aAttrName),
        mType(aType),
        mPermanentState(aPermanentState),
        mTrueState(aTrueState),
        mFalseState(aFalseState) {}

  // ARIA attribute name.
  nsAtom* const mAttrName;

  // Type.
  const uint32_t mType;

  // State applied if the attribute is defined or mType doesn't have
  // eDefinedIfAbsent flag set.
  const uint64_t mPermanentState;

  // States applied if the attribute value is true/false.
  const uint64_t mTrueState;
  const uint64_t mFalseState;
};

/**
 * Map enum type attribute value to accessible state.
 */
static void MapEnumType(dom::Element* aElement, uint64_t* aState,
                        const EnumTypeData& aData);

/**
 * Map token type attribute value to states.
 */
static void MapTokenType(dom::Element* aContent, uint64_t* aState,
                         const TokenTypeData& aData);

bool aria::MapToState(EStateRule aRule, dom::Element* aElement,
                      uint64_t* aState) {
  switch (aRule) {
    case eARIAAutoComplete: {
      static const EnumTypeData data = {
          nsGkAtoms::aria_autocomplete,
          {nsGkAtoms::inlinevalue, nsGkAtoms::list_, nsGkAtoms::both, nullptr},
          {states::SUPPORTS_AUTOCOMPLETION,
           states::HASPOPUP | states::SUPPORTS_AUTOCOMPLETION,
           states::HASPOPUP | states::SUPPORTS_AUTOCOMPLETION},
          0};

      MapEnumType(aElement, aState, data);
      return true;
    }

    case eARIABusy: {
      static const EnumTypeData data = {
          nsGkAtoms::aria_busy,
          {nsGkAtoms::_true, nsGkAtoms::error, nullptr},
          {states::BUSY, states::INVALID},
          0};

      MapEnumType(aElement, aState, data);
      return true;
    }

    case eARIACheckableBool: {
      static const TokenTypeData data(nsGkAtoms::aria_checked,
                                      eBoolType | eDefinedIfAbsent,
                                      states::CHECKABLE, states::CHECKED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIACheckableMixed: {
      static const TokenTypeData data(nsGkAtoms::aria_checked,
                                      eMixedType | eDefinedIfAbsent,
                                      states::CHECKABLE, states::CHECKED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIACheckedMixed: {
      static const TokenTypeData data(nsGkAtoms::aria_checked, eMixedType,
                                      states::CHECKABLE, states::CHECKED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIACurrent: {
      static const TokenTypeData data(nsGkAtoms::aria_current, eBoolType, 0,
                                      states::CURRENT);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIADisabled: {
      static const TokenTypeData data(nsGkAtoms::aria_disabled, eBoolType, 0,
                                      states::UNAVAILABLE);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAExpanded: {
      static const TokenTypeData data(nsGkAtoms::aria_expanded, eBoolType, 0,
                                      states::EXPANDED, states::COLLAPSED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAHasPopup: {
      static const TokenTypeData data(nsGkAtoms::aria_haspopup, eBoolType, 0,
                                      states::HASPOPUP);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAInvalid: {
      static const TokenTypeData data(nsGkAtoms::aria_invalid, eBoolType, 0,
                                      states::INVALID);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAModal: {
      static const TokenTypeData data(nsGkAtoms::aria_modal, eBoolType, 0,
                                      states::MODAL);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAMultiline: {
      static const TokenTypeData data(nsGkAtoms::aria_multiline,
                                      eBoolType | eDefinedIfAbsent, 0,
                                      states::MULTI_LINE, states::SINGLE_LINE);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAMultiSelectable: {
      static const TokenTypeData data(
          nsGkAtoms::aria_multiselectable, eBoolType, 0,
          states::MULTISELECTABLE | states::EXTSELECTABLE);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAOrientation: {
      static const EnumTypeData data = {
          nsGkAtoms::aria_orientation,
          {nsGkAtoms::horizontal, nsGkAtoms::vertical, nullptr},
          {states::HORIZONTAL, states::VERTICAL},
          states::HORIZONTAL | states::VERTICAL};

      MapEnumType(aElement, aState, data);
      return true;
    }

    case eARIAPressed: {
      static const TokenTypeData data(nsGkAtoms::aria_pressed, eMixedType, 0,
                                      states::PRESSED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAReadonly: {
      static const TokenTypeData data(nsGkAtoms::aria_readonly, eBoolType, 0,
                                      states::READONLY);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIAReadonlyOrEditable: {
      static const TokenTypeData data(nsGkAtoms::aria_readonly,
                                      eBoolType | eDefinedIfAbsent, 0,
                                      states::READONLY, states::EDITABLE);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIARequired: {
      static const TokenTypeData data(nsGkAtoms::aria_required, eBoolType, 0,
                                      states::REQUIRED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIASelectable: {
      static const TokenTypeData data(nsGkAtoms::aria_selected,
                                      eBoolType | eDefinedIfAbsent,
                                      states::SELECTABLE, states::SELECTED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eARIASelectableIfDefined: {
      static const TokenTypeData data(nsGkAtoms::aria_selected, eBoolType,
                                      states::SELECTABLE, states::SELECTED);

      MapTokenType(aElement, aState, data);
      return true;
    }

    case eReadonlyUntilEditable: {
      if (!(*aState & states::EDITABLE)) *aState |= states::READONLY;

      return true;
    }

    case eIndeterminateIfNoValue: {
      if (!nsAccUtils::HasARIAAttr(aElement, nsGkAtoms::aria_valuenow) &&
          !nsAccUtils::HasARIAAttr(aElement, nsGkAtoms::aria_valuetext)) {
        *aState |= states::MIXED;
      }

      return true;
    }

    case eFocusableUntilDisabled: {
      if (!nsAccUtils::HasDefinedARIAToken(aElement,
                                           nsGkAtoms::aria_disabled) ||
          nsAccUtils::ARIAAttrValueIs(aElement, nsGkAtoms::aria_disabled,
                                      nsGkAtoms::_false, eCaseMatters)) {
        *aState |= states::FOCUSABLE;
      }

      return true;
    }

    default:
      return false;
  }
}

static void MapEnumType(dom::Element* aElement, uint64_t* aState,
                        const EnumTypeData& aData) {
  switch (nsAccUtils::FindARIAAttrValueIn(aElement, aData.mAttrName,
                                          aData.mValues, eCaseMatters)) {
    case 0:
      *aState = (*aState & ~aData.mClearState) | aData.mStates[0];
      return;
    case 1:
      *aState = (*aState & ~aData.mClearState) | aData.mStates[1];
      return;
    case 2:
      *aState = (*aState & ~aData.mClearState) | aData.mStates[2];
      return;
  }
}

static void MapTokenType(dom::Element* aElement, uint64_t* aState,
                         const TokenTypeData& aData) {
  if (nsAccUtils::HasDefinedARIAToken(aElement, aData.mAttrName)) {
    if (nsAccUtils::ARIAAttrValueIs(aElement, aData.mAttrName, nsGkAtoms::mixed,
                                    eCaseMatters)) {
      if (aData.mType & eMixedType) {
        *aState |= aData.mPermanentState | states::MIXED;
      } else {  // unsupported use of 'mixed' is an authoring error
        *aState |= aData.mPermanentState | aData.mFalseState;
      }
      return;
    }

    if (nsAccUtils::ARIAAttrValueIs(aElement, aData.mAttrName,
                                    nsGkAtoms::_false, eCaseMatters)) {
      *aState |= aData.mPermanentState | aData.mFalseState;
      return;
    }

    *aState |= aData.mPermanentState | aData.mTrueState;
    return;
  }

  if (aData.mType & eDefinedIfAbsent) {
    *aState |= aData.mPermanentState | aData.mFalseState;
  }
}