UNPKG

@jsxtools/eslint-plugin-jsx-a11y

Version:

Static AST checker for accessibility rules on JSX elements for flat ESLint Config.

33 lines (30 loc) 1.27 kB
const jsxAstUtils = require('./module/jsx-ast-utils.cjs'); const isHiddenFromScreenReader = require('./isHiddenFromScreenReader.cjs'); function standardizeSpaceAndCase(input) { return input.trim().replace(/[,.?¿!‽¡;:]/g, "").replace(/\s\s+/g, " ").toLowerCase(); } function getAccessibleChildText(node, elementType) { const ariaLabel = jsxAstUtils.getLiteralPropValue(jsxAstUtils.getProp(node.openingElement.attributes, "aria-label")); if (ariaLabel) return standardizeSpaceAndCase(ariaLabel); const altTag = jsxAstUtils.getLiteralPropValue(jsxAstUtils.getProp(node.openingElement.attributes, "alt")); if (elementType(node.openingElement) === "img" && altTag) return standardizeSpaceAndCase(altTag); if (isHiddenFromScreenReader( elementType(node.openingElement), node.openingElement.attributes )) { return ""; } const rawChildText = node.children.map((currentNode) => { if (currentNode.type === "Literal" || currentNode.type === "JSXText") { return String(currentNode.value); } if (currentNode.type === "JSXElement") { return getAccessibleChildText(currentNode, elementType); } return ""; }).join(" "); return standardizeSpaceAndCase(rawChildText); } module.exports = getAccessibleChildText;