duplicate-id-evaluate.js 650 B

123456789101112131415161718192021222324
  1. import { getRootNode } from '../../commons/dom';
  2. import { escapeSelector } from '../../core/utils';
  3. function duplicateIdEvaluate(node) {
  4. const id = node.getAttribute('id').trim();
  5. // Since empty ID's are not meaningful and are ignored by Edge, we'll
  6. // let those pass.
  7. if (!id) {
  8. return true;
  9. }
  10. const root = getRootNode(node);
  11. const matchingNodes = Array.from(
  12. root.querySelectorAll(`[id="${escapeSelector(id)}"]`)
  13. ).filter(foundNode => foundNode !== node);
  14. if (matchingNodes.length) {
  15. this.relatedNodes(matchingNodes);
  16. }
  17. this.data(id);
  18. return matchingNodes.length === 0;
  19. }
  20. export default duplicateIdEvaluate;