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