explicit-evaluate.js 915 B

12345678910111213141516171819202122232425262728293031323334
  1. import { getRootNode, isVisible } from '../../commons/dom';
  2. import { accessibleText } from '../../commons/text';
  3. import { escapeSelector } from '../../core/utils';
  4. function explicitEvaluate(node, options, virtualNode) {
  5. if (virtualNode.attr('id')) {
  6. if (!virtualNode.actualNode) {
  7. return undefined;
  8. }
  9. const root = getRootNode(virtualNode.actualNode);
  10. const id = escapeSelector(virtualNode.attr('id'));
  11. const labels = Array.from(root.querySelectorAll(`label[for="${id}"]`));
  12. if (labels.length) {
  13. try {
  14. return labels.some(label => {
  15. // defer to hidden-explicit-label check for better messaging
  16. if (!isVisible(label)) {
  17. return true;
  18. } else {
  19. return !!accessibleText(label);
  20. }
  21. });
  22. } catch (e) {
  23. return undefined;
  24. }
  25. }
  26. }
  27. return false;
  28. }
  29. export default explicitEvaluate;