ARIAStateMap.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "ARIAMap.h"
  6. #include "nsAccUtils.h"
  7. #include "States.h"
  8. #include "mozilla/dom/Element.h"
  9. using namespace mozilla;
  10. using namespace mozilla::a11y;
  11. using namespace mozilla::a11y::aria;
  12. /**
  13. * Used to store state map rule data for ARIA attribute of enum type.
  14. */
  15. struct EnumTypeData
  16. {
  17. // ARIA attribute name.
  18. nsIAtom* const mAttrName;
  19. // States if the attribute value is matched to the enum value. Used as
  20. // nsIContent::AttrValuesArray, last item must be nullptr.
  21. nsIAtom* const* const mValues[4];
  22. // States applied if corresponding enum values are matched.
  23. const uint64_t mStates[3];
  24. // States to clear in case of match.
  25. const uint64_t mClearState;
  26. };
  27. enum ETokenType
  28. {
  29. eBoolType = 0,
  30. eMixedType = 1, // can take 'mixed' value
  31. eDefinedIfAbsent = 2 // permanent and false state are applied if absent
  32. };
  33. /**
  34. * Used to store state map rule data for ARIA attribute of token type (including
  35. * mixed value).
  36. */
  37. struct TokenTypeData
  38. {
  39. TokenTypeData(nsIAtom* aAttrName, uint32_t aType,
  40. uint64_t aPermanentState,
  41. uint64_t aTrueState,
  42. uint64_t aFalseState = 0) :
  43. mAttrName(aAttrName), mType(aType), mPermanentState(aPermanentState),
  44. mTrueState(aTrueState), mFalseState(aFalseState)
  45. { }
  46. // ARIA attribute name.
  47. nsIAtom* const mAttrName;
  48. // Type.
  49. const uint32_t mType;
  50. // State applied if the attribute is defined or mType doesn't have
  51. // eDefinedIfAbsent flag set.
  52. const uint64_t mPermanentState;
  53. // States applied if the attribute value is true/false.
  54. const uint64_t mTrueState;
  55. const uint64_t mFalseState;
  56. };
  57. /**
  58. * Map enum type attribute value to accessible state.
  59. */
  60. static void MapEnumType(dom::Element* aElement, uint64_t* aState,
  61. const EnumTypeData& aData);
  62. /**
  63. * Map token type attribute value to states.
  64. */
  65. static void MapTokenType(dom::Element* aContent, uint64_t* aState,
  66. const TokenTypeData& aData);
  67. bool
  68. aria::MapToState(EStateRule aRule, dom::Element* aElement, uint64_t* aState)
  69. {
  70. switch (aRule) {
  71. case eARIAAutoComplete:
  72. {
  73. static const EnumTypeData data = {
  74. nsGkAtoms::aria_autocomplete,
  75. { &nsGkAtoms::inlinevalue,
  76. &nsGkAtoms::list,
  77. &nsGkAtoms::both, nullptr },
  78. { states::SUPPORTS_AUTOCOMPLETION,
  79. states::HASPOPUP | states::SUPPORTS_AUTOCOMPLETION,
  80. states::HASPOPUP | states::SUPPORTS_AUTOCOMPLETION }, 0
  81. };
  82. MapEnumType(aElement, aState, data);
  83. return true;
  84. }
  85. case eARIABusy:
  86. {
  87. static const EnumTypeData data = {
  88. nsGkAtoms::aria_busy,
  89. { &nsGkAtoms::_true,
  90. &nsGkAtoms::error, nullptr },
  91. { states::BUSY,
  92. states::INVALID }, 0
  93. };
  94. MapEnumType(aElement, aState, data);
  95. return true;
  96. }
  97. case eARIACheckableBool:
  98. {
  99. static const TokenTypeData data(
  100. nsGkAtoms::aria_checked, eBoolType | eDefinedIfAbsent,
  101. states::CHECKABLE, states::CHECKED);
  102. MapTokenType(aElement, aState, data);
  103. return true;
  104. }
  105. case eARIACheckableMixed:
  106. {
  107. static const TokenTypeData data(
  108. nsGkAtoms::aria_checked, eMixedType | eDefinedIfAbsent,
  109. states::CHECKABLE, states::CHECKED);
  110. MapTokenType(aElement, aState, data);
  111. return true;
  112. }
  113. case eARIACheckedMixed:
  114. {
  115. static const TokenTypeData data(
  116. nsGkAtoms::aria_checked, eMixedType,
  117. states::CHECKABLE, states::CHECKED);
  118. MapTokenType(aElement, aState, data);
  119. return true;
  120. }
  121. case eARIADisabled:
  122. {
  123. static const TokenTypeData data(
  124. nsGkAtoms::aria_disabled, eBoolType,
  125. 0, states::UNAVAILABLE);
  126. MapTokenType(aElement, aState, data);
  127. return true;
  128. }
  129. case eARIAExpanded:
  130. {
  131. static const TokenTypeData data(
  132. nsGkAtoms::aria_expanded, eBoolType,
  133. 0, states::EXPANDED, states::COLLAPSED);
  134. MapTokenType(aElement, aState, data);
  135. return true;
  136. }
  137. case eARIAHasPopup:
  138. {
  139. static const TokenTypeData data(
  140. nsGkAtoms::aria_haspopup, eBoolType,
  141. 0, states::HASPOPUP);
  142. MapTokenType(aElement, aState, data);
  143. return true;
  144. }
  145. case eARIAInvalid:
  146. {
  147. static const TokenTypeData data(
  148. nsGkAtoms::aria_invalid, eBoolType,
  149. 0, states::INVALID);
  150. MapTokenType(aElement, aState, data);
  151. return true;
  152. }
  153. case eARIAModal:
  154. {
  155. static const TokenTypeData data(
  156. nsGkAtoms::aria_modal, eBoolType,
  157. 0, states::MODAL);
  158. MapTokenType(aElement, aState, data);
  159. return true;
  160. }
  161. case eARIAMultiline:
  162. {
  163. static const TokenTypeData data(
  164. nsGkAtoms::aria_multiline, eBoolType | eDefinedIfAbsent,
  165. 0, states::MULTI_LINE, states::SINGLE_LINE);
  166. MapTokenType(aElement, aState, data);
  167. return true;
  168. }
  169. case eARIAMultiSelectable:
  170. {
  171. static const TokenTypeData data(
  172. nsGkAtoms::aria_multiselectable, eBoolType,
  173. 0, states::MULTISELECTABLE | states::EXTSELECTABLE);
  174. MapTokenType(aElement, aState, data);
  175. return true;
  176. }
  177. case eARIAOrientation:
  178. {
  179. static const EnumTypeData data = {
  180. nsGkAtoms::aria_orientation,
  181. { &nsGkAtoms::horizontal,
  182. &nsGkAtoms::vertical, nullptr },
  183. { states::HORIZONTAL,
  184. states::VERTICAL },
  185. states::HORIZONTAL | states::VERTICAL
  186. };
  187. MapEnumType(aElement, aState, data);
  188. return true;
  189. }
  190. case eARIAPressed:
  191. {
  192. static const TokenTypeData data(
  193. nsGkAtoms::aria_pressed, eMixedType,
  194. 0, states::PRESSED);
  195. MapTokenType(aElement, aState, data);
  196. return true;
  197. }
  198. case eARIAReadonly:
  199. {
  200. static const TokenTypeData data(
  201. nsGkAtoms::aria_readonly, eBoolType,
  202. 0, states::READONLY);
  203. MapTokenType(aElement, aState, data);
  204. return true;
  205. }
  206. case eARIAReadonlyOrEditable:
  207. {
  208. static const TokenTypeData data(
  209. nsGkAtoms::aria_readonly, eBoolType | eDefinedIfAbsent,
  210. 0, states::READONLY, states::EDITABLE);
  211. MapTokenType(aElement, aState, data);
  212. return true;
  213. }
  214. case eARIAReadonlyOrEditableIfDefined:
  215. {
  216. static const TokenTypeData data(
  217. nsGkAtoms::aria_readonly, eBoolType,
  218. 0, states::READONLY, states::EDITABLE);
  219. MapTokenType(aElement, aState, data);
  220. return true;
  221. }
  222. case eARIARequired:
  223. {
  224. static const TokenTypeData data(
  225. nsGkAtoms::aria_required, eBoolType,
  226. 0, states::REQUIRED);
  227. MapTokenType(aElement, aState, data);
  228. return true;
  229. }
  230. case eARIASelectable:
  231. {
  232. static const TokenTypeData data(
  233. nsGkAtoms::aria_selected, eBoolType | eDefinedIfAbsent,
  234. states::SELECTABLE, states::SELECTED);
  235. MapTokenType(aElement, aState, data);
  236. return true;
  237. }
  238. case eARIASelectableIfDefined:
  239. {
  240. static const TokenTypeData data(
  241. nsGkAtoms::aria_selected, eBoolType,
  242. states::SELECTABLE, states::SELECTED);
  243. MapTokenType(aElement, aState, data);
  244. return true;
  245. }
  246. case eReadonlyUntilEditable:
  247. {
  248. if (!(*aState & states::EDITABLE))
  249. *aState |= states::READONLY;
  250. return true;
  251. }
  252. case eIndeterminateIfNoValue:
  253. {
  254. if (!aElement->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_valuenow) &&
  255. !aElement->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_valuetext))
  256. *aState |= states::MIXED;
  257. return true;
  258. }
  259. case eFocusableUntilDisabled:
  260. {
  261. if (!nsAccUtils::HasDefinedARIAToken(aElement, nsGkAtoms::aria_disabled) ||
  262. aElement->AttrValueIs(kNameSpaceID_None, nsGkAtoms::aria_disabled,
  263. nsGkAtoms::_false, eCaseMatters))
  264. *aState |= states::FOCUSABLE;
  265. return true;
  266. }
  267. default:
  268. return false;
  269. }
  270. }
  271. static void
  272. MapEnumType(dom::Element* aElement, uint64_t* aState, const EnumTypeData& aData)
  273. {
  274. switch (aElement->FindAttrValueIn(kNameSpaceID_None, aData.mAttrName,
  275. aData.mValues, eCaseMatters)) {
  276. case 0:
  277. *aState = (*aState & ~aData.mClearState) | aData.mStates[0];
  278. return;
  279. case 1:
  280. *aState = (*aState & ~aData.mClearState) | aData.mStates[1];
  281. return;
  282. case 2:
  283. *aState = (*aState & ~aData.mClearState) | aData.mStates[2];
  284. return;
  285. }
  286. }
  287. static void
  288. MapTokenType(dom::Element* aElement, uint64_t* aState,
  289. const TokenTypeData& aData)
  290. {
  291. if (nsAccUtils::HasDefinedARIAToken(aElement, aData.mAttrName)) {
  292. if ((aData.mType & eMixedType) &&
  293. aElement->AttrValueIs(kNameSpaceID_None, aData.mAttrName,
  294. nsGkAtoms::mixed, eCaseMatters)) {
  295. *aState |= aData.mPermanentState | states::MIXED;
  296. return;
  297. }
  298. if (aElement->AttrValueIs(kNameSpaceID_None, aData.mAttrName,
  299. nsGkAtoms::_false, eCaseMatters)) {
  300. *aState |= aData.mPermanentState | aData.mFalseState;
  301. return;
  302. }
  303. *aState |= aData.mPermanentState | aData.mTrueState;
  304. return;
  305. }
  306. if (aData.mType & eDefinedIfAbsent)
  307. *aState |= aData.mPermanentState | aData.mFalseState;
  308. }