getRole.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = getRole;
  4. var _util = require("./util");
  5. // https://w3c.github.io/html-aria/#document-conformance-requirements-for-use-of-aria-attributes-in-html
  6. var localNameToRoleMappings = {
  7. article: "article",
  8. aside: "complementary",
  9. body: "document",
  10. button: "button",
  11. datalist: "listbox",
  12. dd: "definition",
  13. details: "group",
  14. dialog: "dialog",
  15. dt: "term",
  16. fieldset: "group",
  17. figure: "figure",
  18. // WARNING: Only with an accessible name
  19. form: "form",
  20. footer: "contentinfo",
  21. h1: "heading",
  22. h2: "heading",
  23. h3: "heading",
  24. h4: "heading",
  25. h5: "heading",
  26. h6: "heading",
  27. header: "banner",
  28. hr: "separator",
  29. legend: "legend",
  30. li: "listitem",
  31. math: "math",
  32. main: "main",
  33. menu: "list",
  34. nav: "navigation",
  35. ol: "list",
  36. optgroup: "group",
  37. // WARNING: Only in certain context
  38. option: "option",
  39. output: "status",
  40. progress: "progressbar",
  41. // WARNING: Only with an accessible name
  42. section: "region",
  43. summary: "button",
  44. table: "table",
  45. tbody: "rowgroup",
  46. textarea: "textbox",
  47. tfoot: "rowgroup",
  48. // WARNING: Only in certain context
  49. td: "cell",
  50. th: "columnheader",
  51. thead: "rowgroup",
  52. tr: "row",
  53. ul: "list"
  54. };
  55. var prohibitedAttributes = {
  56. caption: new Set(["aria-label", "aria-labelledby"]),
  57. code: new Set(["aria-label", "aria-labelledby"]),
  58. deletion: new Set(["aria-label", "aria-labelledby"]),
  59. emphasis: new Set(["aria-label", "aria-labelledby"]),
  60. generic: new Set(["aria-label", "aria-labelledby", "aria-roledescription"]),
  61. insertion: new Set(["aria-label", "aria-labelledby"]),
  62. paragraph: new Set(["aria-label", "aria-labelledby"]),
  63. presentation: new Set(["aria-label", "aria-labelledby"]),
  64. strong: new Set(["aria-label", "aria-labelledby"]),
  65. subscript: new Set(["aria-label", "aria-labelledby"]),
  66. superscript: new Set(["aria-label", "aria-labelledby"])
  67. };
  68. /**
  69. *
  70. * @param element
  71. * @param role The role used for this element. This is specified to control whether you want to use the implicit or explicit role.
  72. */
  73. function hasGlobalAriaAttributes(element, role) {
  74. // https://rawgit.com/w3c/aria/stable/#global_states
  75. // commented attributes are deprecated
  76. return ["aria-atomic", "aria-busy", "aria-controls", "aria-current", "aria-describedby", "aria-details", // "disabled",
  77. "aria-dropeffect", // "errormessage",
  78. "aria-flowto", "aria-grabbed", // "haspopup",
  79. "aria-hidden", // "invalid",
  80. "aria-keyshortcuts", "aria-label", "aria-labelledby", "aria-live", "aria-owns", "aria-relevant", "aria-roledescription"].some(function (attributeName) {
  81. var _prohibitedAttributes;
  82. return element.hasAttribute(attributeName) && !((_prohibitedAttributes = prohibitedAttributes[role]) !== null && _prohibitedAttributes !== void 0 && _prohibitedAttributes.has(attributeName));
  83. });
  84. }
  85. function ignorePresentationalRole(element, implicitRole) {
  86. // https://rawgit.com/w3c/aria/stable/#conflict_resolution_presentation_none
  87. return hasGlobalAriaAttributes(element, implicitRole);
  88. }
  89. function getRole(element) {
  90. var explicitRole = getExplicitRole(element);
  91. if (explicitRole === null || explicitRole === "presentation") {
  92. var implicitRole = getImplicitRole(element);
  93. if (explicitRole !== "presentation" || ignorePresentationalRole(element, implicitRole || "")) {
  94. return implicitRole;
  95. }
  96. }
  97. return explicitRole;
  98. }
  99. function getImplicitRole(element) {
  100. var mappedByTag = localNameToRoleMappings[(0, _util.getLocalName)(element)];
  101. if (mappedByTag !== undefined) {
  102. return mappedByTag;
  103. }
  104. switch ((0, _util.getLocalName)(element)) {
  105. case "a":
  106. case "area":
  107. case "link":
  108. if (element.hasAttribute("href")) {
  109. return "link";
  110. }
  111. break;
  112. case "img":
  113. if (element.getAttribute("alt") === "" && !ignorePresentationalRole(element, "img")) {
  114. return "presentation";
  115. }
  116. return "img";
  117. case "input":
  118. {
  119. var _ref = element,
  120. type = _ref.type;
  121. switch (type) {
  122. case "button":
  123. case "image":
  124. case "reset":
  125. case "submit":
  126. return "button";
  127. case "checkbox":
  128. case "radio":
  129. return type;
  130. case "range":
  131. return "slider";
  132. case "email":
  133. case "tel":
  134. case "text":
  135. case "url":
  136. if (element.hasAttribute("list")) {
  137. return "combobox";
  138. }
  139. return "textbox";
  140. case "search":
  141. if (element.hasAttribute("list")) {
  142. return "combobox";
  143. }
  144. return "searchbox";
  145. default:
  146. return null;
  147. }
  148. }
  149. case "select":
  150. if (element.hasAttribute("multiple") || element.size > 1) {
  151. return "listbox";
  152. }
  153. return "combobox";
  154. }
  155. return null;
  156. }
  157. function getExplicitRole(element) {
  158. var role = element.getAttribute("role");
  159. if (role !== null) {
  160. var explicitRole = role.trim().split(" ")[0]; // String.prototype.split(sep, limit) will always return an array with at least one member
  161. // as long as limit is either undefined or > 0
  162. if (explicitRole.length > 0) {
  163. return explicitRole;
  164. }
  165. }
  166. return null;
  167. }
  168. //# sourceMappingURL=getRole.js.map