suggestions.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getSuggestedQuery = getSuggestedQuery;
  6. var _domAccessibilityApi = require("dom-accessibility-api");
  7. var _matches = require("./matches");
  8. var _getNodeText = require("./get-node-text");
  9. var _config = require("./config");
  10. var _roleHelpers = require("./role-helpers");
  11. var _labelHelpers = require("./label-helpers");
  12. const normalize = (0, _matches.getDefaultNormalizer)();
  13. function escapeRegExp(string) {
  14. return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  15. }
  16. function getRegExpMatcher(string) {
  17. return new RegExp(escapeRegExp(string.toLowerCase()), 'i');
  18. }
  19. function makeSuggestion(queryName, element, content, {
  20. variant,
  21. name
  22. }) {
  23. let warning = '';
  24. const queryOptions = {};
  25. const queryArgs = [['Role', 'TestId'].includes(queryName) ? content : getRegExpMatcher(content)];
  26. if (name) {
  27. queryOptions.name = getRegExpMatcher(name);
  28. }
  29. if (queryName === 'Role' && (0, _roleHelpers.isInaccessible)(element)) {
  30. queryOptions.hidden = true;
  31. warning = `Element is inaccessible. This means that the element and all its children are invisible to screen readers.
  32. If you are using the aria-hidden prop, make sure this is the right choice for your case.
  33. `;
  34. }
  35. if (Object.keys(queryOptions).length > 0) {
  36. queryArgs.push(queryOptions);
  37. }
  38. const queryMethod = `${variant}By${queryName}`;
  39. return {
  40. queryName,
  41. queryMethod,
  42. queryArgs,
  43. variant,
  44. warning,
  45. toString() {
  46. if (warning) {
  47. console.warn(warning);
  48. }
  49. let [text, options] = queryArgs;
  50. text = typeof text === 'string' ? `'${text}'` : text;
  51. options = options ? `, { ${Object.entries(options).map(([k, v]) => `${k}: ${v}`).join(', ')} }` : '';
  52. return `${queryMethod}(${text}${options})`;
  53. }
  54. };
  55. }
  56. function canSuggest(currentMethod, requestedMethod, data) {
  57. return data && (!requestedMethod || requestedMethod.toLowerCase() === currentMethod.toLowerCase());
  58. }
  59. function getSuggestedQuery(element, variant = 'get', method) {
  60. var _element$getAttribute, _getImplicitAriaRoles;
  61. // don't create suggestions for script and style elements
  62. if (element.matches(_config.DEFAULT_IGNORE_TAGS)) {
  63. return undefined;
  64. } //We prefer to suggest something else if the role is generic
  65. const role = (_element$getAttribute = element.getAttribute('role')) != null ? _element$getAttribute : (_getImplicitAriaRoles = (0, _roleHelpers.getImplicitAriaRoles)(element)) == null ? void 0 : _getImplicitAriaRoles[0];
  66. if (role !== 'generic' && canSuggest('Role', method, role)) {
  67. return makeSuggestion('Role', element, role, {
  68. variant,
  69. name: (0, _domAccessibilityApi.computeAccessibleName)(element, {
  70. computedStyleSupportsPseudoElements: (0, _config.getConfig)().computedStyleSupportsPseudoElements
  71. })
  72. });
  73. }
  74. const labelText = (0, _labelHelpers.getLabels)(document, element).map(label => label.content).join(' ');
  75. if (canSuggest('LabelText', method, labelText)) {
  76. return makeSuggestion('LabelText', element, labelText, {
  77. variant
  78. });
  79. }
  80. const placeholderText = element.getAttribute('placeholder');
  81. if (canSuggest('PlaceholderText', method, placeholderText)) {
  82. return makeSuggestion('PlaceholderText', element, placeholderText, {
  83. variant
  84. });
  85. }
  86. const textContent = normalize((0, _getNodeText.getNodeText)(element));
  87. if (canSuggest('Text', method, textContent)) {
  88. return makeSuggestion('Text', element, textContent, {
  89. variant
  90. });
  91. }
  92. if (canSuggest('DisplayValue', method, element.value)) {
  93. return makeSuggestion('DisplayValue', element, normalize(element.value), {
  94. variant
  95. });
  96. }
  97. const alt = element.getAttribute('alt');
  98. if (canSuggest('AltText', method, alt)) {
  99. return makeSuggestion('AltText', element, alt, {
  100. variant
  101. });
  102. }
  103. const title = element.getAttribute('title');
  104. if (canSuggest('Title', method, title)) {
  105. return makeSuggestion('Title', element, title, {
  106. variant
  107. });
  108. }
  109. const testId = element.getAttribute((0, _config.getConfig)().testIdAttribute);
  110. if (canSuggest('TestId', method, testId)) {
  111. return makeSuggestion('TestId', element, testId, {
  112. variant
  113. });
  114. }
  115. return undefined;
  116. }