has-content-virtual.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import isVisualContent from './is-visual-content';
  2. import labelVirtual from '../aria/label-virtual';
  3. const hiddenTextElms = [
  4. 'HEAD',
  5. 'TITLE',
  6. 'TEMPLATE',
  7. 'SCRIPT',
  8. 'STYLE',
  9. 'IFRAME',
  10. 'OBJECT',
  11. 'VIDEO',
  12. 'AUDIO',
  13. 'NOSCRIPT'
  14. ];
  15. function hasChildTextNodes(elm) {
  16. if (!hiddenTextElms.includes(elm.actualNode.nodeName.toUpperCase())) {
  17. return elm.children.some(
  18. ({ actualNode }) =>
  19. actualNode.nodeType === 3 && actualNode.nodeValue.trim()
  20. );
  21. }
  22. }
  23. /**
  24. * Check that the element has visible content in the form of either text,
  25. * an aria-label or visual content such as image
  26. * @method hasContentVirtual
  27. * @memberof axe.commons.dom
  28. * @instance
  29. * @param {VirtualNode} elm Virtual Node to search
  30. * @param {Boolean} noRecursion If true, only the element is checked, otherwise it will search all child nodes
  31. * @param {Boolean} ignoreAria if true, ignores `aria label` computation for content deduction
  32. * @return {Boolean}
  33. */
  34. function hasContentVirtual(elm, noRecursion, ignoreAria) {
  35. return (
  36. // It has text
  37. hasChildTextNodes(elm) ||
  38. // It is a graphical element
  39. isVisualContent(elm.actualNode) ||
  40. // It has an ARIA label
  41. (!ignoreAria && !!labelVirtual(elm)) ||
  42. // or one of it's descendants does
  43. (!noRecursion &&
  44. elm.children.some(
  45. child => child.actualNode.nodeType === 1 && hasContentVirtual(child)
  46. ))
  47. );
  48. }
  49. export default hasContentVirtual;