reduce-to-elements-below-floating.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Reduce an array of elements to only those that are below a 'floating' element.
  3. * @method reduceToElementsBelowFloating
  4. * @memberof axe.commons.dom
  5. * @instance
  6. * @param {Array} elements
  7. * @param {Element} targetNode
  8. * @returns {Array}
  9. */
  10. function reduceToElementsBelowFloating(elements, targetNode) {
  11. const floatingPositions = ['fixed', 'sticky'];
  12. let finalElements = [];
  13. let targetFound = false;
  14. // Filter out elements that are temporarily floating above the target
  15. for (let index = 0; index < elements.length; ++index) {
  16. const currentNode = elements[index];
  17. if (currentNode === targetNode) {
  18. targetFound = true;
  19. }
  20. const style = window.getComputedStyle(currentNode);
  21. if (!targetFound && floatingPositions.indexOf(style.position) !== -1) {
  22. //Target was not found yet, so it must be under this floating thing (and will not always be under it)
  23. finalElements = [];
  24. continue;
  25. }
  26. finalElements.push(currentNode);
  27. }
  28. return finalElements;
  29. }
  30. export default reduceToElementsBelowFloating;