alt-text.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findByAltText = exports.findAllByAltText = exports.getAllByAltText = exports.getByAltText = exports.queryAllByAltText = exports.queryByAltText = void 0;
  6. var _queryHelpers = require("../query-helpers");
  7. var _helpers = require("../helpers");
  8. var _allUtils = require("./all-utils");
  9. const queryAllByAltText = (container, alt, {
  10. exact = true,
  11. collapseWhitespace,
  12. trim,
  13. normalizer
  14. } = {}) => {
  15. (0, _helpers.checkContainerType)(container);
  16. const matcher = exact ? _allUtils.matches : _allUtils.fuzzyMatches;
  17. const matchNormalizer = (0, _allUtils.makeNormalizer)({
  18. collapseWhitespace,
  19. trim,
  20. normalizer
  21. });
  22. return Array.from(container.querySelectorAll('img,input,area')).filter(node => matcher(node.getAttribute('alt'), node, alt, matchNormalizer));
  23. };
  24. const getMultipleError = (c, alt) => `Found multiple elements with the alt text: ${alt}`;
  25. const getMissingError = (c, alt) => `Unable to find an element with the alt text: ${alt}`;
  26. const queryAllByAltTextWithSuggestions = (0, _queryHelpers.wrapAllByQueryWithSuggestion)(queryAllByAltText, queryAllByAltText.name, 'queryAll');
  27. exports.queryAllByAltText = queryAllByAltTextWithSuggestions;
  28. const [queryByAltText, getAllByAltText, getByAltText, findAllByAltText, findByAltText] = (0, _allUtils.buildQueries)(queryAllByAltText, getMultipleError, getMissingError);
  29. exports.findByAltText = findByAltText;
  30. exports.findAllByAltText = findAllByAltText;
  31. exports.getByAltText = getByAltText;
  32. exports.getAllByAltText = getAllByAltText;
  33. exports.queryByAltText = queryByAltText;