Katochimoto/xblocks

View on GitHub
src/dom/eachInnerFollowing.js

Summary

Maintainability
A
1 hr
Test Coverage
/**
 * Проход по всем потомкам в прямом порядке (от певой до последней)
 * @function xblocks.dom.eachInnerFollowing
 * @param {HTMLElement} node
 * @param {function} callback
 * @returns {boolean}
 */
export default function (node, callback) {
    var stack = [ node ];
    var item;
    var cbcall;
    var childsLength;

    while ((item = stack.pop())) {
        cbcall = callback && callback(item, stack);

        if (typeof cbcall !== 'undefined' && !cbcall) {
            return false;

        } else if (cbcall === 'next') {
            continue;
        }

        if (item.nodeType !== 1) {
            continue;
        }

        if (!item.hasChildNodes()) {
            continue;
        }

        childsLength = item.childNodes.length;

        while (childsLength--) {
            stack.push(item.childNodes[childsLength]);
        }
    }

    return true;
}