mayHaveAccessibleLabel.js 2.7 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 _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 hasLabellingProp(openingElement) {
  19. var additionalLabellingProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  20. var labellingProps = [].concat('alt', // Assume alt is used correctly on an image
  21. 'aria-label', 'aria-labelledby', additionalLabellingProps);
  22. return openingElement.attributes.some(function (attribute) {
  23. // We must assume that a spread value contains a labelling prop.
  24. if (attribute.type !== 'JSXAttribute') {
  25. return true;
  26. } // Attribute matches.
  27. if ((0, _arrayIncludes["default"])(labellingProps, (0, _jsxAstUtils.propName)(attribute)) && !!(0, _jsxAstUtils.getPropValue)(attribute)) {
  28. return true;
  29. }
  30. return false;
  31. });
  32. }
  33. function mayHaveAccessibleLabel(root) {
  34. var maxDepth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  35. var additionalLabellingProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  36. function checkElement(node, depth) {
  37. // Bail when maxDepth is exceeded.
  38. if (depth > maxDepth) {
  39. return false;
  40. } // Check for literal text.
  41. if (node.type === 'Literal' && !!node.value) {
  42. return true;
  43. } // Assume an expression container renders a label. It is the best we can
  44. // do in this case.
  45. if (node.type === 'JSXExpressionContainer') {
  46. return true;
  47. } // Check for JSXText.
  48. // $FlowFixMe Remove after updating ast-types-flow
  49. if (node.type === 'JSXText' && !!node.value) {
  50. return true;
  51. } // Check for labelling props.
  52. if (node.openingElement
  53. /* $FlowFixMe */
  54. && hasLabellingProp(node.openingElement, additionalLabellingProps)) {
  55. return true;
  56. } // Recurse into the child element nodes.
  57. if (node.children) {
  58. /* $FlowFixMe */
  59. for (var i = 0; i < node.children.length; i += 1) {
  60. /* $FlowFixMe */
  61. if (checkElement(node.children[i], depth + 1)) {
  62. return true;
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. return checkElement(root, 0);
  69. }
  70. module.exports = exports.default;