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.24 kB
import { getLiteralPropValue, getProp } from './module/jsx-ast-utils.js'; import isHiddenFromScreenReader from './isHiddenFromScreenReader.js'; function standardizeSpaceAndCase(input) { return input.trim().replace(/[,.?¿!‽¡;:]/g, "").replace(/\s\s+/g, " ").toLowerCase(); } function getAccessibleChildText(node, elementType) { const ariaLabel = getLiteralPropValue(getProp(node.openingElement.attributes, "aria-label")); if (ariaLabel) return standardizeSpaceAndCase(ariaLabel); const altTag = getLiteralPropValue(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); } export { getAccessibleChildText as default };