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.

225 lines
6.9 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findByRole = exports.findAllByRole = exports.getByRole = exports.getAllByRole = exports.queryAllByRole = exports.queryByRole = void 0;
  6. var _domAccessibilityApi = require("dom-accessibility-api");
  7. var _ariaQuery = require("aria-query");
  8. var _roleHelpers = require("../role-helpers");
  9. var _queryHelpers = require("../query-helpers");
  10. var _helpers = require("../helpers");
  11. var _allUtils = require("./all-utils");
  12. function queryAllByRole(container, role, {
  13. exact = true,
  14. collapseWhitespace,
  15. hidden = (0, _allUtils.getConfig)().defaultHidden,
  16. name,
  17. trim,
  18. normalizer,
  19. queryFallbacks = false,
  20. selected,
  21. checked,
  22. pressed,
  23. level,
  24. expanded
  25. } = {}) {
  26. (0, _helpers.checkContainerType)(container);
  27. const matcher = exact ? _allUtils.matches : _allUtils.fuzzyMatches;
  28. const matchNormalizer = (0, _allUtils.makeNormalizer)({
  29. collapseWhitespace,
  30. trim,
  31. normalizer
  32. });
  33. if (selected !== undefined) {
  34. var _allRoles$get;
  35. // guard against unknown roles
  36. if (((_allRoles$get = _ariaQuery.roles.get(role)) == null ? void 0 : _allRoles$get.props['aria-selected']) === undefined) {
  37. throw new Error(`"aria-selected" is not supported on role "${role}".`);
  38. }
  39. }
  40. if (checked !== undefined) {
  41. var _allRoles$get2;
  42. // guard against unknown roles
  43. if (((_allRoles$get2 = _ariaQuery.roles.get(role)) == null ? void 0 : _allRoles$get2.props['aria-checked']) === undefined) {
  44. throw new Error(`"aria-checked" is not supported on role "${role}".`);
  45. }
  46. }
  47. if (pressed !== undefined) {
  48. var _allRoles$get3;
  49. // guard against unknown roles
  50. if (((_allRoles$get3 = _ariaQuery.roles.get(role)) == null ? void 0 : _allRoles$get3.props['aria-pressed']) === undefined) {
  51. throw new Error(`"aria-pressed" is not supported on role "${role}".`);
  52. }
  53. }
  54. if (level !== undefined) {
  55. // guard against using `level` option with any role other than `heading`
  56. if (role !== 'heading') {
  57. throw new Error(`Role "${role}" cannot have "level" property.`);
  58. }
  59. }
  60. if (expanded !== undefined) {
  61. var _allRoles$get4;
  62. // guard against unknown roles
  63. if (((_allRoles$get4 = _ariaQuery.roles.get(role)) == null ? void 0 : _allRoles$get4.props['aria-expanded']) === undefined) {
  64. throw new Error(`"aria-expanded" is not supported on role "${role}".`);
  65. }
  66. }
  67. const subtreeIsInaccessibleCache = new WeakMap();
  68. function cachedIsSubtreeInaccessible(element) {
  69. if (!subtreeIsInaccessibleCache.has(element)) {
  70. subtreeIsInaccessibleCache.set(element, (0, _roleHelpers.isSubtreeInaccessible)(element));
  71. }
  72. return subtreeIsInaccessibleCache.get(element);
  73. }
  74. return Array.from(container.querySelectorAll('*')).filter(node => {
  75. const isRoleSpecifiedExplicitly = node.hasAttribute('role');
  76. if (isRoleSpecifiedExplicitly) {
  77. const roleValue = node.getAttribute('role');
  78. if (queryFallbacks) {
  79. return roleValue.split(' ').filter(Boolean).some(text => matcher(text, node, role, matchNormalizer));
  80. } // if a custom normalizer is passed then let normalizer handle the role value
  81. if (normalizer) {
  82. return matcher(roleValue, node, role, matchNormalizer);
  83. } // other wise only send the first word to match
  84. const [firstWord] = roleValue.split(' ');
  85. return matcher(firstWord, node, role, matchNormalizer);
  86. }
  87. const implicitRoles = (0, _roleHelpers.getImplicitAriaRoles)(node);
  88. return implicitRoles.some(implicitRole => matcher(implicitRole, node, role, matchNormalizer));
  89. }).filter(element => {
  90. if (selected !== undefined) {
  91. return selected === (0, _roleHelpers.computeAriaSelected)(element);
  92. }
  93. if (checked !== undefined) {
  94. return checked === (0, _roleHelpers.computeAriaChecked)(element);
  95. }
  96. if (pressed !== undefined) {
  97. return pressed === (0, _roleHelpers.computeAriaPressed)(element);
  98. }
  99. if (expanded !== undefined) {
  100. return expanded === (0, _roleHelpers.computeAriaExpanded)(element);
  101. }
  102. if (level !== undefined) {
  103. return level === (0, _roleHelpers.computeHeadingLevel)(element);
  104. } // don't care if aria attributes are unspecified
  105. return true;
  106. }).filter(element => {
  107. return hidden === false ? (0, _roleHelpers.isInaccessible)(element, {
  108. isSubtreeInaccessible: cachedIsSubtreeInaccessible
  109. }) === false : true;
  110. }).filter(element => {
  111. if (name === undefined) {
  112. // Don't care
  113. return true;
  114. }
  115. return (0, _allUtils.matches)((0, _domAccessibilityApi.computeAccessibleName)(element, {
  116. computedStyleSupportsPseudoElements: (0, _allUtils.getConfig)().computedStyleSupportsPseudoElements
  117. }), element, name, text => text);
  118. });
  119. }
  120. const getMultipleError = (c, role, {
  121. name
  122. } = {}) => {
  123. let nameHint = '';
  124. if (name === undefined) {
  125. nameHint = '';
  126. } else if (typeof name === 'string') {
  127. nameHint = ` and name "${name}"`;
  128. } else {
  129. nameHint = ` and name \`${name}\``;
  130. }
  131. return `Found multiple elements with the role "${role}"${nameHint}`;
  132. };
  133. const getMissingError = (container, role, {
  134. hidden = (0, _allUtils.getConfig)().defaultHidden,
  135. name
  136. } = {}) => {
  137. if ((0, _allUtils.getConfig)()._disableExpensiveErrorDiagnostics) {
  138. return `Unable to find role="${role}"`;
  139. }
  140. let roles = '';
  141. Array.from(container.children).forEach(childElement => {
  142. roles += (0, _roleHelpers.prettyRoles)(childElement, {
  143. hidden,
  144. includeName: name !== undefined
  145. });
  146. });
  147. let roleMessage;
  148. if (roles.length === 0) {
  149. if (hidden === false) {
  150. roleMessage = 'There are no accessible roles. But there might be some inaccessible roles. ' + 'If you wish to access them, then set the `hidden` option to `true`. ' + 'Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole';
  151. } else {
  152. roleMessage = 'There are no available roles.';
  153. }
  154. } else {
  155. roleMessage = `
  156. Here are the ${hidden === false ? 'accessible' : 'available'} roles:
  157. ${roles.replace(/\n/g, '\n ').replace(/\n\s\s\n/g, '\n\n')}
  158. `.trim();
  159. }
  160. let nameHint = '';
  161. if (name === undefined) {
  162. nameHint = '';
  163. } else if (typeof name === 'string') {
  164. nameHint = ` and name "${name}"`;
  165. } else {
  166. nameHint = ` and name \`${name}\``;
  167. }
  168. return `
  169. Unable to find an ${hidden === false ? 'accessible ' : ''}element with the role "${role}"${nameHint}
  170. ${roleMessage}`.trim();
  171. };
  172. const queryAllByRoleWithSuggestions = (0, _queryHelpers.wrapAllByQueryWithSuggestion)(queryAllByRole, queryAllByRole.name, 'queryAll');
  173. exports.queryAllByRole = queryAllByRoleWithSuggestions;
  174. const [queryByRole, getAllByRole, getByRole, findAllByRole, findByRole] = (0, _allUtils.buildQueries)(queryAllByRole, getMultipleError, getMissingError);
  175. exports.findByRole = findByRole;
  176. exports.findAllByRole = findAllByRole;
  177. exports.getByRole = getByRole;
  178. exports.getAllByRole = getAllByRole;
  179. exports.queryByRole = queryByRole;