mayHaveAccessibleLabel.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = mayHaveAccessibleLabel;
  6. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  7. var _jsxAstUtils = require("jsx-ast-utils");
  8. var _minimatch = _interopRequireDefault(require("minimatch"));
  9. function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
  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 tryTrim(value) {
  20. return typeof value === 'string' ? value.trim() : value;
  21. }
  22. function hasLabellingProp(openingElement) {
  23. var additionalLabellingProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  24. var labellingProps = [].concat('alt',
  25. // Assume alt is used correctly on an image
  26. 'aria-label', 'aria-labelledby', additionalLabellingProps);
  27. return openingElement.attributes.some(function (attribute) {
  28. // We must assume that a spread value contains a labelling prop.
  29. if (attribute.type !== 'JSXAttribute') {
  30. return true;
  31. }
  32. // Attribute matches.
  33. if ((0, _arrayIncludes["default"])(labellingProps, (0, _jsxAstUtils.propName)(attribute)) && !!tryTrim((0, _jsxAstUtils.getPropValue)(attribute))) {
  34. return true;
  35. }
  36. return false;
  37. });
  38. }
  39. function mayHaveAccessibleLabel(root) {
  40. var maxDepth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  41. var additionalLabellingProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  42. var getElementType = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _jsxAstUtils.elementType;
  43. var controlComponents = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
  44. function checkElement(node, depth) {
  45. // Bail when maxDepth is exceeded.
  46. if (depth > maxDepth) {
  47. return false;
  48. }
  49. // Check for literal text.
  50. if (node.type === 'Literal' && !!tryTrim(node.value)) {
  51. return true;
  52. }
  53. // Assume an expression container renders a label. It is the best we can
  54. // do in this case.
  55. if (node.type === 'JSXExpressionContainer') {
  56. return true;
  57. }
  58. // Check for JSXText.
  59. // $FlowFixMe Remove after updating ast-types-flow
  60. if (node.type === 'JSXText' && !!tryTrim(node.value)) {
  61. return true;
  62. }
  63. // Check for labelling props.
  64. if (node.openingElement
  65. /* $FlowFixMe */ && hasLabellingProp(node.openingElement, additionalLabellingProps)) {
  66. return true;
  67. }
  68. if (node.type === 'JSXElement' && node.children.length === 0 && node.openingElement) {
  69. // $FlowFixMe `node.openingElement` has `unknown` type
  70. var name = getElementType(node.openingElement);
  71. var isReactComponent = name.length > 0 && name[0] === name[0].toUpperCase();
  72. if (isReactComponent && !controlComponents.some(function (control) {
  73. return (0, _minimatch["default"])(name, control);
  74. })) {
  75. return true;
  76. }
  77. }
  78. // Recurse into the child element nodes.
  79. if (node.children) {
  80. /* $FlowFixMe */
  81. for (var i = 0; i < node.children.length; i += 1) {
  82. /* $FlowFixMe */
  83. if (checkElement(node.children[i], depth + 1)) {
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. return checkElement(root, 0);
  91. }
  92. module.exports = exports.default;