mayHaveAccessibleLabel.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = mayHaveAccessibleLabel;
  7. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  8. var _jsxAstUtils = require("jsx-ast-utils");
  9. /**
  10. * Returns true if a labelling element is found or if it cannot determine if
  11. * a label is present because of expression containers or spread attributes.
  12. * A false return value means that the node definitely does not have a label,
  13. * but a true return return value means that the node may or may not have a
  14. * label.
  15. *
  16. *
  17. */
  18. function tryTrim(value) {
  19. return typeof value === 'string' ? value.trim() : value;
  20. }
  21. function hasLabellingProp(openingElement) {
  22. var additionalLabellingProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  23. var labellingProps = [].concat('alt',
  24. // Assume alt is used correctly on an image
  25. 'aria-label', 'aria-labelledby', additionalLabellingProps);
  26. return openingElement.attributes.some(function (attribute) {
  27. // We must assume that a spread value contains a labelling prop.
  28. if (attribute.type !== 'JSXAttribute') {
  29. return true;
  30. }
  31. // Attribute matches.
  32. if ((0, _arrayIncludes["default"])(labellingProps, (0, _jsxAstUtils.propName)(attribute)) && !!tryTrim((0, _jsxAstUtils.getPropValue)(attribute))) {
  33. return true;
  34. }
  35. return false;
  36. });
  37. }
  38. function mayHaveAccessibleLabel(root) {
  39. var maxDepth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  40. var additionalLabellingProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  41. function checkElement(node, depth) {
  42. // Bail when maxDepth is exceeded.
  43. if (depth > maxDepth) {
  44. return false;
  45. }
  46. // Check for literal text.
  47. if (node.type === 'Literal' && !!tryTrim(node.value)) {
  48. return true;
  49. }
  50. // Assume an expression container renders a label. It is the best we can
  51. // do in this case.
  52. if (node.type === 'JSXExpressionContainer') {
  53. return true;
  54. }
  55. // Check for JSXText.
  56. // $FlowFixMe Remove after updating ast-types-flow
  57. if (node.type === 'JSXText' && !!tryTrim(node.value)) {
  58. return true;
  59. }
  60. // Check for labelling props.
  61. if (node.openingElement
  62. /* $FlowFixMe */ && hasLabellingProp(node.openingElement, additionalLabellingProps)) {
  63. return true;
  64. }
  65. // Recurse into the child element nodes.
  66. if (node.children) {
  67. /* $FlowFixMe */
  68. for (var i = 0; i < node.children.length; i += 1) {
  69. /* $FlowFixMe */
  70. if (checkElement(node.children[i], depth + 1)) {
  71. return true;
  72. }
  73. }
  74. }
  75. return false;
  76. }
  77. return checkElement(root, 0);
  78. }
  79. module.exports = exports.default;