title.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.queryByTitle = exports.queryAllByTitle = exports.getByTitle = exports.getAllByTitle = exports.findByTitle = exports.findAllByTitle = void 0;
  6. var _queryHelpers = require("../query-helpers");
  7. var _helpers = require("../helpers");
  8. var _allUtils = require("./all-utils");
  9. const isSvgTitle = node => node.tagName.toLowerCase() === 'title' && node.parentElement?.tagName.toLowerCase() === 'svg';
  10. const queryAllByTitle = (container, text, {
  11. exact = true,
  12. collapseWhitespace,
  13. trim,
  14. normalizer
  15. } = {}) => {
  16. (0, _helpers.checkContainerType)(container);
  17. const matcher = exact ? _allUtils.matches : _allUtils.fuzzyMatches;
  18. const matchNormalizer = (0, _allUtils.makeNormalizer)({
  19. collapseWhitespace,
  20. trim,
  21. normalizer
  22. });
  23. return Array.from(container.querySelectorAll('[title], svg > title')).filter(node => matcher(node.getAttribute('title'), node, text, matchNormalizer) || isSvgTitle(node) && matcher((0, _allUtils.getNodeText)(node), node, text, matchNormalizer));
  24. };
  25. const getMultipleError = (c, title) => `Found multiple elements with the title: ${title}.`;
  26. const getMissingError = (c, title) => `Unable to find an element with the title: ${title}.`;
  27. const queryAllByTitleWithSuggestions = exports.queryAllByTitle = (0, _queryHelpers.wrapAllByQueryWithSuggestion)(queryAllByTitle, queryAllByTitle.name, 'queryAll');
  28. const [queryByTitle, getAllByTitle, getByTitle, findAllByTitle, findByTitle] = (0, _allUtils.buildQueries)(queryAllByTitle, getMultipleError, getMissingError);
  29. exports.findByTitle = findByTitle;
  30. exports.findAllByTitle = findAllByTitle;
  31. exports.getByTitle = getByTitle;
  32. exports.getAllByTitle = getAllByTitle;
  33. exports.queryByTitle = queryByTitle;