mayHaveAccessibleLabel.js 2.8 KB

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