source: frontend/node_modules/eslint-plugin-jsx-a11y/lib/util/getAccessibleChildText.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[9af201e]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = getAccessibleChildText;
7var _jsxAstUtils = require("jsx-ast-utils");
8var _isHiddenFromScreenReader = _interopRequireDefault(require("./isHiddenFromScreenReader"));
9function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
10/**
11 * Returns a new "standardized" string: all whitespace is collapsed to one space,
12 * and the string is lowercase
13 * @param {string} input
14 * @returns lowercase, single-spaced, punctuation-stripped, trimmed string
15 */
16function standardizeSpaceAndCase(input) {
17 return input.trim().replace(/[,.?¿!‽¡;:]/g, '') // strip punctuation
18 .replace(/\s\s+/g, ' ') // collapse spaces
19 .toLowerCase();
20}
21
22/**
23 * Returns the (recursively-defined) accessible child text of a node, which (in-order) is:
24 * 1. The element's aria-label
25 * 2. If the element is a direct literal, the literal value
26 * 3. Otherwise, merge all of its children
27 * @param {JSXElement} node - node to traverse
28 * @returns child text as a string
29 */
30function getAccessibleChildText(node, elementType) {
31 var ariaLabel = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.openingElement.attributes, 'aria-label'));
32 // early escape-hatch when aria-label is applied
33 if (ariaLabel) return standardizeSpaceAndCase(ariaLabel);
34
35 // early-return if alt prop exists and is an image
36 var altTag = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.openingElement.attributes, 'alt'));
37 if (elementType(node.openingElement) === 'img' && altTag) return standardizeSpaceAndCase(altTag);
38
39 // skip if aria-hidden is true
40 if ((0, _isHiddenFromScreenReader["default"])(elementType(node.openingElement), node.openingElement.attributes)) {
41 return '';
42 }
43 var rawChildText = node.children.map(function (currentNode) {
44 // $FlowFixMe JSXText is missing in ast-types-flow
45 if (currentNode.type === 'Literal' || currentNode.type === 'JSXText') {
46 return String(currentNode.value);
47 }
48 if (currentNode.type === 'JSXElement') {
49 return getAccessibleChildText(currentNode, elementType);
50 }
51 return '';
52 }).join(' ');
53 return standardizeSpaceAndCase(rawChildText);
54}
55module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.