web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.7 KiB

  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = getRole;
  4. var _util = require("./util");
  5. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  6. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  7. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  8. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  9. function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  10. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  11. var localNameToRoleMappings = {
  12. article: "article",
  13. aside: "complementary",
  14. body: "document",
  15. button: "button",
  16. datalist: "listbox",
  17. dd: "definition",
  18. details: "group",
  19. dialog: "dialog",
  20. dt: "term",
  21. fieldset: "group",
  22. figure: "figure",
  23. // WARNING: Only with an accessible name
  24. form: "form",
  25. footer: "contentinfo",
  26. h1: "heading",
  27. h2: "heading",
  28. h3: "heading",
  29. h4: "heading",
  30. h5: "heading",
  31. h6: "heading",
  32. header: "banner",
  33. hr: "separator",
  34. legend: "legend",
  35. li: "listitem",
  36. math: "math",
  37. main: "main",
  38. menu: "list",
  39. nav: "navigation",
  40. ol: "list",
  41. optgroup: "group",
  42. // WARNING: Only in certain context
  43. option: "option",
  44. output: "status",
  45. progress: "progressbar",
  46. // WARNING: Only with an accessible name
  47. section: "region",
  48. summary: "button",
  49. table: "table",
  50. tbody: "rowgroup",
  51. textarea: "textbox",
  52. tfoot: "rowgroup",
  53. // WARNING: Only in certain context
  54. td: "cell",
  55. th: "columnheader",
  56. thead: "rowgroup",
  57. tr: "row",
  58. ul: "list"
  59. };
  60. var prohibitedAttributes = {
  61. caption: new Set(["aria-label", "aria-labelledby"]),
  62. code: new Set(["aria-label", "aria-labelledby"]),
  63. deletion: new Set(["aria-label", "aria-labelledby"]),
  64. emphasis: new Set(["aria-label", "aria-labelledby"]),
  65. generic: new Set(["aria-label", "aria-labelledby", "aria-roledescription"]),
  66. insertion: new Set(["aria-label", "aria-labelledby"]),
  67. paragraph: new Set(["aria-label", "aria-labelledby"]),
  68. presentation: new Set(["aria-label", "aria-labelledby"]),
  69. strong: new Set(["aria-label", "aria-labelledby"]),
  70. subscript: new Set(["aria-label", "aria-labelledby"]),
  71. superscript: new Set(["aria-label", "aria-labelledby"])
  72. };
  73. /**
  74. *
  75. * @param element
  76. * @param role The role used for this element. This is specified to control whether you want to use the implicit or explicit role.
  77. */
  78. function hasGlobalAriaAttributes(element, role) {
  79. // https://rawgit.com/w3c/aria/stable/#global_states
  80. // commented attributes are deprecated
  81. return ["aria-atomic", "aria-busy", "aria-controls", "aria-current", "aria-describedby", "aria-details", // "disabled",
  82. "aria-dropeffect", // "errormessage",
  83. "aria-flowto", "aria-grabbed", // "haspopup",
  84. "aria-hidden", // "invalid",
  85. "aria-keyshortcuts", "aria-label", "aria-labelledby", "aria-live", "aria-owns", "aria-relevant", "aria-roledescription"].some(function (attributeName) {
  86. var _prohibitedAttributes;
  87. return element.hasAttribute(attributeName) && !((_prohibitedAttributes = prohibitedAttributes[role]) === null || _prohibitedAttributes === void 0 ? void 0 : _prohibitedAttributes.has(attributeName));
  88. });
  89. }
  90. function ignorePresentationalRole(element, implicitRole) {
  91. // https://rawgit.com/w3c/aria/stable/#conflict_resolution_presentation_none
  92. return hasGlobalAriaAttributes(element, implicitRole);
  93. }
  94. function getRole(element) {
  95. var explicitRole = getExplicitRole(element);
  96. if (explicitRole === null || explicitRole === "presentation") {
  97. var implicitRole = getImplicitRole(element);
  98. if (explicitRole !== "presentation" || ignorePresentationalRole(element, implicitRole || "")) {
  99. return implicitRole;
  100. }
  101. }
  102. return explicitRole;
  103. }
  104. function getImplicitRole(element) {
  105. var mappedByTag = localNameToRoleMappings[(0, _util.getLocalName)(element)];
  106. if (mappedByTag !== undefined) {
  107. return mappedByTag;
  108. }
  109. switch ((0, _util.getLocalName)(element)) {
  110. case "a":
  111. case "area":
  112. case "link":
  113. if (element.hasAttribute("href")) {
  114. return "link";
  115. }
  116. break;
  117. case "img":
  118. if (element.getAttribute("alt") === "" && !ignorePresentationalRole(element, "img")) {
  119. return "presentation";
  120. }
  121. return "img";
  122. case "input":
  123. {
  124. var _ref = element,
  125. type = _ref.type;
  126. switch (type) {
  127. case "button":
  128. case "image":
  129. case "reset":
  130. case "submit":
  131. return "button";
  132. case "checkbox":
  133. case "radio":
  134. return type;
  135. case "range":
  136. return "slider";
  137. case "email":
  138. case "tel":
  139. case "text":
  140. case "url":
  141. if (element.hasAttribute("list")) {
  142. return "combobox";
  143. }
  144. return "textbox";
  145. case "search":
  146. if (element.hasAttribute("list")) {
  147. return "combobox";
  148. }
  149. return "searchbox";
  150. default:
  151. return null;
  152. }
  153. }
  154. case "select":
  155. if (element.hasAttribute("multiple") || element.size > 1) {
  156. return "listbox";
  157. }
  158. return "combobox";
  159. }
  160. return null;
  161. }
  162. function getExplicitRole(element) {
  163. if (element.hasAttribute("role")) {
  164. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- safe due to hasAttribute check
  165. var _trim$split = element.getAttribute("role").trim().split(" "),
  166. _trim$split2 = _slicedToArray(_trim$split, 1),
  167. explicitRole = _trim$split2[0];
  168. if (explicitRole !== undefined && explicitRole.length > 0) {
  169. return explicitRole;
  170. }
  171. }
  172. return null;
  173. }
  174. //# sourceMappingURL=getRole.js.map