find-elms-in-context.js 937 B

12345678910111213141516171819202122232425262728293031
  1. import getRootNode from './get-root-node';
  2. import { escapeSelector } from '../../core/utils';
  3. /**
  4. * Find elements referenced from a given context
  5. * @method findElmsInContext
  6. * @memberof axe.commons.dom
  7. * @instance
  8. * @param {Object} element
  9. * @param {String} element.context Element in the same context
  10. * @param {String} element.value Attribute value to search for
  11. * @param {String} element.attr Attribute name to search for
  12. * @param {String} element.elm NodeName to search for (optional)
  13. * @return {Array<Node>}
  14. */
  15. function findElmsInContext({ context, value, attr, elm = '' }) {
  16. let root;
  17. const escapedValue = escapeSelector(value);
  18. if (context.nodeType === 9 || context.nodeType === 11) {
  19. // It's already root
  20. root = context;
  21. } else {
  22. root = getRootNode(context);
  23. }
  24. return Array.from(
  25. root.querySelectorAll(elm + '[' + attr + '=' + escapedValue + ']')
  26. );
  27. }
  28. export default findElmsInContext;