diff --git a/src/js/dom-utils.js b/src/js/dom-utils.js index 1ab64a2f6..0a94933c5 100644 --- a/src/js/dom-utils.js +++ b/src/js/dom-utils.js @@ -11,7 +11,7 @@ * @return {Array} Array of sibling nodes plus target element. */ function getSiblingElementsAndSelf( element, selector ) { - return _getSiblingElements( element, selector, [ element ] ); + return _getSiblingElements( element, selector, true ); } /** @@ -52,29 +52,21 @@ function getSiblingElement( element, selector = '*' ){ * * @param {Node} element - Target element. * @param {string} [selector] - A CSS selector. - * @param {Array} [startArray] - Array of nodes to start with. + * @param {boolean} [includeSelf] - Whether to include self. * @return {Array} Array of sibling nodes. */ -function _getSiblingElements( element, selector = '*', startArray = [] ) { - const siblings = startArray; - let prev = element.previousElementSibling; - let next = element.nextElementSibling; - // TODO: check if iteration approach used by getSiblingElement is faster. It would be more elegant. - while ( prev ) { - if ( prev.matches( selector ) ) { - siblings.unshift( prev ); - } - prev = prev.previousElementSibling; - } +function _getSiblingElements( element, selector = '*', includeSelf = false ) { + const results = []; + let current = element.parentElement.firstElementChild; - while ( next ) { - if ( next.matches( selector ) ) { - siblings.push( next ); + while ( current ) { + if ( ( current === element && includeSelf ) || ( current !== element && current.matches( selector ) ) ){ + results.push( current ); } - next = next.nextElementSibling; + current = current.nextElementSibling; } - return siblings; + return results; } /**