<commit_before>Kwf.enableOnReadyConsoleProfile = false;

/* fn: fn,
 * scope: scope,
 * options: options || {},
 * type: [jquery|ext],
 * num: unique number, //used to mark in initDone
 * selector: selector, // null if onContentReady
 * */
Kwf._readyHandlers = [];


if (!Kwf.isApp) {
    $(document).ready(function() {
        if (!document.body) {
            //this happens if a redirect by changing location in JS is done
            //in that case no contentReady needs to be called
            return;
        }
        var t = Kwf.Utils.BenchmarkBox.now();
        if (Kwf.enableOnReadyConsoleProfile) console.profile("callOnContentReady body");
        Kwf._skipDeferred = true;
        Kwf.callOnContentReady(document.body, { action: 'render' });
        delete Kwf._skipDeferred;
        if (Kwf.enableOnReadyConsoleProfile) console.profileEnd();
        Kwf.Utils.BenchmarkBox.time('time', Kwf.Utils.BenchmarkBox.now()-t);
        Kwf.Utils.BenchmarkBox.create({
            counters: Kwf._onReadyStats,
            type: 'onReady'
        });
        (function() {
            Kwf._deferredStart = Kwf.Utils.BenchmarkBox.now();
            if (Kwf.enableOnReadyConsoleProfile) console.profile("callOnContentReady body deferred");
            Kwf._skipDeferred = false;
            Kwf.callOnContentReady(document.body, { action: 'render' });
            delete Kwf._skipDeferred;
        }).defer(10);

        Ext.fly(window).on('resize', function() {
            Kwf.callOnContentReady(document.body, { action: 'widthChange' } );
        }, this, { buffer: 100 });
    });
}

Kwf._skipDeferred;
Kwf._deferredStart = null;
Kwf._onReadyIsCalling = false;
Kwf._onReadyCallQueue = [];
Kwf._onReadyElQueue = [];
Kwf._elQueueNum = 0;
Kwf._onReadyElSortCache = {};
Kwf._elCacheBySelector = {};

Kwf._addReadyHandler = function(type, onAction, selector, fn, options)
{
    Kwf._readyHandlers.push({
        selector: selector,
        fn: fn,
        options: options || {},
        num: Kwf._readyHandlers.length, //unique number
        type: type,
        onAction: onAction
    });
};

/**
 * @param element the added/changed dom element
 * @param options newRender (bool): if new elements have been added to the dom or just visiblity/width changed
 */
Kwf.callOnContentReady = function(renderedEl, options)
{
    Kwf.Utils.BenchmarkBox.count('callOnContentReady');
    if (!options) options = {};
    if (options.newRender) {
        delete options.newRender;
        options.action = 'render';
    }
    if (!options.action) {
        if (console && console.warn) console.warn('Please set option action on callOnContentReady');
        options.action = 'render';
    }
    if (!renderedEl) {
        if (console && console && console.warn) console.warn('Please pass element argument on callOnContentReady');
        renderedEl = document.body;
    }
    if (Ext.Element && renderedEl instanceof Ext.Element) renderedEl = renderedEl.dom;
    if (jQuery && renderedEl instanceof jQuery) renderedEl = renderedEl.get();

    Kwf._onReadyCallQueue.push({
        renderedEl: renderedEl,
        options: options
    });

    if (options.action == 'render') {
        //new elements rendered, clear cache
        Kwf._elCacheBySelector = {};

        var t = Kwf.Utils.BenchmarkBox.now();
        var html = renderedEl.innerHTML;
        Kwf.Utils.BenchmarkBox.time('innerHTML', Kwf.Utils.BenchmarkBox.now()-t);
    }


    var addToQueue = function(onActions) {


        for (var i = 0; i < Kwf._readyHandlers.length; i++) {
            var hndl = Kwf._readyHandlers[i];


            //Kwf._skipDeferred gets set before callOnContentReady(body)
            //can not be part of options as it would be missing in recursive calls
            if (Kwf._skipDeferred === true) {
                if (hndl.options.defer) {
                    continue;
                }
            } else if (Kwf._skipDeferred === false) {
                if (!hndl.options.defer) {
                    continue;
                }
            }
            if (onActions.indexOf(hndl.onAction) != -1) {

                if (options.action == 'render' && !Kwf._elCacheBySelector[hndl.selector]) {
                    var t = Kwf.Utils.BenchmarkBox.now();
                    var m = hndl.selector.match(/^[a-z]*\.([a-z]+)/i)
                    if (m) {
                        //do a stupid text search on the selector, using that we can skip query for many selectors that don't exist in the current el
                        if (html.indexOf(m[1]) == -1) {
                            Kwf._elCacheBySelector[hndl.selector] = [];
                        }
                    }
                    Kwf.Utils.BenchmarkBox.time('checkInnerHtml', Kwf.Utils.BenchmarkBox.now()-t);
                }

                if (Kwf._elCacheBySelector[hndl.selector]) {
                    if (Kwf._elCacheBySelector[hndl.selector].length === 0) {
                        //Optimize: if we never got element by that selector, skip query
                        Kwf.Utils.BenchmarkBox.count('querySkip');
                        continue;
                    }
                    Kwf.Utils.BenchmarkBox.count('queryCache');
                    var els = [];
                    for (var j=0; j<Kwf._elCacheBySelector[hndl.selector].length; j++) {
                        if (renderedEl == document.body || $.contains(renderedEl, Kwf._elCacheBySelector[hndl.selector][j])) {
                            els.push(Kwf._elCacheBySelector[hndl.selector][j]);
                        }
                    }
                } else {
                    var t = Kwf.Utils.BenchmarkBox.now();
                    var els = $.makeArray($(renderedEl).find(hndl.selector));
                    Kwf.Utils.BenchmarkBox.time('query', Kwf.Utils.BenchmarkBox.now() - t);
                    if (options.action == 'render' && renderedEl == document.body) {
                        Kwf._elCacheBySelector[hndl.selector] = els;
                    }
                }
                for (var j = 0; j< els.length; ++j) {
                    var alreadyInQueue = false;
                    Kwf._onReadyElQueue.each(function(q) {
                        if (q.num == hndl.num && q.el === els[j]) {
                            alreadyInQueue = true;
                            return true;
                        }
                    }, this);
                    if (!alreadyInQueue) {
                        var parentsCount = 0;
                        var n = els[j];
                        while (n = n.parentNode) {
                            parentsCount++;
                        }
                        Kwf.Utils.BenchmarkBox.count('readyEl');
                        Kwf._onReadyElQueue.push({
                            el: els[j],
                            fn: hndl.fn,
                            options: hndl.options,
                            num: hndl.num,
                            type: hndl.type,
                            onAction: hndl.onAction,
                            selector: hndl.selector,
                            queueNum: Kwf._elQueueNum++,
                            priority: hndl.options.priority || 0,
                            parentsCount: parentsCount
                        });
                    }
                }
            }
        }
    };

    if (options.action == 'render') {
        addToQueue(['render', 'show', 'widthChange']);
    } else if (options.action == 'show') {
        addToQueue(['show', 'widthChange']);
    } else if (options.action == 'hide') {
        addToQueue(['hide']);
    } else if (options.action == 'widthChange') {
        addToQueue(['widthChange']);
    }

    var t = Kwf.Utils.BenchmarkBox.now();
    Kwf._onReadyElQueue.sort(function sortOnReadyElQueue(a, b) {
        if (a.priority != b.priority) {
            return a.priority - b.priority;
        } else {
            if (a.parentsCount != b.parentsCount) {
                return a.parentsCount - b.parentsCount;
            } else {
                return 1;<commit_msg>simplify code: use continue statement<commit_after>Kwf.enableOnReadyConsoleProfile = false;

/* fn: fn,
 * scope: scope,
 * options: options || {},
 * type: [jquery|ext],
 * num: unique number, //used to mark in initDone
 * selector: selector, // null if onContentReady
 * */
Kwf._readyHandlers = [];


if (!Kwf.isApp) {
    $(document).ready(function() {
        if (!document.body) {
            //this happens if a redirect by changing location in JS is done
            //in that case no contentReady needs to be called
            return;
        }
        var t = Kwf.Utils.BenchmarkBox.now();
        if (Kwf.enableOnReadyConsoleProfile) console.profile("callOnContentReady body");
        Kwf._skipDeferred = true;
        Kwf.callOnContentReady(document.body, { action: 'render' });
        delete Kwf._skipDeferred;
        if (Kwf.enableOnReadyConsoleProfile) console.profileEnd();
        Kwf.Utils.BenchmarkBox.time('time', Kwf.Utils.BenchmarkBox.now()-t);
        Kwf.Utils.BenchmarkBox.create({
            counters: Kwf._onReadyStats,
            type: 'onReady'
        });
        (function() {
            Kwf._deferredStart = Kwf.Utils.BenchmarkBox.now();
            if (Kwf.enableOnReadyConsoleProfile) console.profile("callOnContentReady body deferred");
            Kwf._skipDeferred = false;
            Kwf.callOnContentReady(document.body, { action: 'render' });
            delete Kwf._skipDeferred;
        }).defer(10);

        Ext.fly(window).on('resize', function() {
            Kwf.callOnContentReady(document.body, { action: 'widthChange' } );
        }, this, { buffer: 100 });
    });
}

Kwf._skipDeferred;
Kwf._deferredStart = null;
Kwf._onReadyIsCalling = false;
Kwf._onReadyCallQueue = [];
Kwf._onReadyElQueue = [];
Kwf._elQueueNum = 0;
Kwf._onReadyElSortCache = {};
Kwf._elCacheBySelector = {};

Kwf._addReadyHandler = function(type, onAction, selector, fn, options)
{
    Kwf._readyHandlers.push({
        selector: selector,
        fn: fn,
        options: options || {},
        num: Kwf._readyHandlers.length, //unique number
        type: type,
        onAction: onAction
    });
};

/**
 * @param element the added/changed dom element
 * @param options newRender (bool): if new elements have been added to the dom or just visiblity/width changed
 */
Kwf.callOnContentReady = function(renderedEl, options)
{
    Kwf.Utils.BenchmarkBox.count('callOnContentReady');
    if (!options) options = {};
    if (options.newRender) {
        delete options.newRender;
        options.action = 'render';
    }
    if (!options.action) {
        if (console && console.warn) console.warn('Please set option action on callOnContentReady');
        options.action = 'render';
    }
    if (!renderedEl) {
        if (console && console && console.warn) console.warn('Please pass element argument on callOnContentReady');
        renderedEl = document.body;
    }
    if (Ext.Element && renderedEl instanceof Ext.Element) renderedEl = renderedEl.dom;
    if (jQuery && renderedEl instanceof jQuery) renderedEl = renderedEl.get();

    Kwf._onReadyCallQueue.push({
        renderedEl: renderedEl,
        options: options
    });

    if (options.action == 'render') {
        //new elements rendered, clear cache
        Kwf._elCacheBySelector = {};

        var t = Kwf.Utils.BenchmarkBox.now();
        var html = renderedEl.innerHTML;
        Kwf.Utils.BenchmarkBox.time('innerHTML', Kwf.Utils.BenchmarkBox.now()-t);
    }


    var addToQueue = function(onActions) {


        for (var i = 0; i < Kwf._readyHandlers.length; i++) {
            var hndl = Kwf._readyHandlers[i];


            //Kwf._skipDeferred gets set before callOnContentReady(body)
            //can not be part of options as it would be missing in recursive calls
            if (Kwf._skipDeferred === true) {
                if (hndl.options.defer) {
                    continue;
                }
            } else if (Kwf._skipDeferred === false) {
                if (!hndl.options.defer) {
                    continue;
                }
            }
            if (onActions.indexOf(hndl.onAction) == -1) {
                continue;
            }

            if (options.action == 'render' && !Kwf._elCacheBySelector[hndl.selector]) {
                var t = Kwf.Utils.BenchmarkBox.now();
                var m = hndl.selector.match(/^[a-z]*\.([a-z]+)/i)
                if (m) {
                    //do a stupid text search on the selector, using that we can skip query for many selectors that don't exist in the current el
                    if (html.indexOf(m[1]) == -1) {
                        Kwf._elCacheBySelector[hndl.selector] = [];
                    }
                }
                Kwf.Utils.BenchmarkBox.time('checkInnerHtml', Kwf.Utils.BenchmarkBox.now()-t);
            }

            if (Kwf._elCacheBySelector[hndl.selector]) {
                if (Kwf._elCacheBySelector[hndl.selector].length === 0) {
                    //Optimize: if we never got element by that selector, skip query
                    Kwf.Utils.BenchmarkBox.count('querySkip');
                    continue;
                }
                Kwf.Utils.BenchmarkBox.count('queryCache');
                var els = [];
                for (var j=0; j<Kwf._elCacheBySelector[hndl.selector].length; j++) {
                    if (renderedEl == document.body || $.contains(renderedEl, Kwf._elCacheBySelector[hndl.selector][j])) {
                        els.push(Kwf._elCacheBySelector[hndl.selector][j]);
                    }
                }
            } else {
                var t = Kwf.Utils.BenchmarkBox.now();
                var els = $.makeArray($(renderedEl).find(hndl.selector));
                Kwf.Utils.BenchmarkBox.time('query', Kwf.Utils.BenchmarkBox.now() - t);
                if (options.action == 'render' && renderedEl == document.body) {
                    Kwf._elCacheBySelector[hndl.selector] = els;
                }
            }
            for (var j = 0; j< els.length; ++j) {
                var alreadyInQueue = false;
                Kwf._onReadyElQueue.each(function(q) {
                    if (q.num == hndl.num && q.el === els[j]) {
                        alreadyInQueue = true;
                        return true;
                    }
                }, this);
                if (!alreadyInQueue) {
                    var parentsCount = 0;
                    var n = els[j];
                    while (n = n.parentNode) {
                        parentsCount++;
                    }
                    Kwf.Utils.BenchmarkBox.count('readyEl');
                    Kwf._onReadyElQueue.push({
                        el: els[j],
                        fn: hndl.fn,
                        options: hndl.options,
                        num: hndl.num,
                        type: hndl.type,
                        onAction: hndl.onAction,
                        selector: hndl.selector,
                        queueNum: Kwf._elQueueNum++,
                        priority: hndl.options.priority || 0,
                        parentsCount: parentsCount
                    });
                }
            }
        }
    };

    if (options.action == 'render') {
        addToQueue(['render', 'show', 'widthChange']);
    } else if (options.action == 'show') {
        addToQueue(['show', 'widthChange']);
    } else if (options.action == 'hide') {
        addToQueue(['hide']);
    } else if (options.action == 'widthChange') {
        addToQueue(['widthChange']);
    }

    var t = Kwf.Utils.BenchmarkBox.now();
    Kwf._onReadyElQueue.sort(function sortOnReadyElQueue(a, b) {
        if (a.priority != b.priority) {
            return a.priority - b.priority;
        } else {
            if (a.parentsCount != b.parentsCount) {
                return a.parentsCount - b.parentsCount;
            } else {
                return 1;
{'fraction_non_alphanumeric': 0.12156078039019509, 'fraction_numerical': 0.002001000500250125, 'mean_word_length': 1.6364985163204748, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3280891', 'n_tokens_mistral': 4742, 'n_tokens_neox': 4430, 'n_words': 942}