<commit_before>define(["sprout/util", "sprout/dom"], function (_, $) {
    "use strict";

    var foreachExpando = "__cid__",
        // IE 9 cannot reliably read the "nodeValue" property of a comment node (see https://github.com/SteveSanderson/knockout/issues/186)
        // but it does give them a nonstandard alternative property called "text" that it can read reliably. Other browsers don't have that property.
        // So, use node.text where available, and node.nodeValue elsewhere
        commentNodesHaveTextProperty = document.createComment("test").text === "<!--test-->",
        startCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*data-bind\s+(.*\:.*)\s*-->$/ : /^\s*data-bind\s+(.*\:.*)\s*$/,
        endCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*\/data-bind\s*-->$/ : /^\s*\/data-bind\s*$/;

    // TODO: the comment regex and comment functions are defined in the databind module as well. Best to have just one version of each defined.
    // TODO: maybe define them in databindings.util and databind can access them from there.
    function isStartComment (element)
    {
        return (element.nodeType == 8) && (commentNodesHaveTextProperty ? element.text : element.nodeValue).match(startCommentRegex);
    }

    function isEndComment (element)
    {
        return (element.nodeType == 8) && (commentNodesHaveTextProperty ? element.text : element.nodeValue).match(endCommentRegex);
    }

    function removeCommentContent (startCommentElement, endCommentElement)
    {
        var sibling = startCommentElement.nextSibling,
            nextSibling;

        while (sibling && sibling !== endCommentElement) {
            nextSibling = sibling.nextSibling;
            databindings.databind.removeBindings(sibling);
            $(sibling).remove();
            sibling = nextSibling;
        }
    }

    /**
     * Renders an individual item from a foreach binder's collection.
     * @private
     * @param {Object} element The dom element the foreach binder is bound to.
     * @param {Object} model The model this item is bound to.
     * @param {Object} template The markup template for the item.
     * @param {Object} viewModel The model the foreach binder is bound to.
     * @param {Object} at The index to insert the item at.
     * @param {Object} info The binding info for the foreach binding.
     */
    function renderItem (element, model, template, viewModel, at, info)
    {
        var itemElements = $("<div></div>").html(template).contents(),
            cid = model.get("cid");

        viewModel.fire('foreach-render', { nodes: itemElements, model: model, at: at }, function () {
            // If this is a comment element then render items as siblings
            if (info.isComment) {
                if (_.isNumber(at)) {
                    if (at === 0) {
                        itemElements.insertAfter(element);
                    }
                    else {
                        $(element).nextUntil(info.endCommentElement).eq(at - 1).after(itemElements);
                    }
                }
                else {
                    itemElements.insertBefore(info.endCommentElement);
                }
            }
            // Else render items as children
            else {
                if (_.isNumber(at)) {
                    if (at === 0) {
                        itemElements.prependTo(element);
                    }
                    else {
                        $('> tr', element).eq(at - 1).after(itemElements);
                    }
                }
                else {
                    itemElements.appendTo(element);
                }
            }

            itemElements.each(function () {
                this[foreachExpando] = cid;
                databindings.databind.applyBindings(model, this, info.context);
            });
        });
    }
<commit_msg>Changed the foreach-render event's info.at value to be set to the index that an item was inserted at when the at value was not specified.<commit_after>define(["sprout/util", "sprout/dom"], function (_, $) {
    "use strict";

    var foreachExpando = "__cid__",
        // IE 9 cannot reliably read the "nodeValue" property of a comment node (see https://github.com/SteveSanderson/knockout/issues/186)
        // but it does give them a nonstandard alternative property called "text" that it can read reliably. Other browsers don't have that property.
        // So, use node.text where available, and node.nodeValue elsewhere
        commentNodesHaveTextProperty = document.createComment("test").text === "<!--test-->",
        startCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*data-bind\s+(.*\:.*)\s*-->$/ : /^\s*data-bind\s+(.*\:.*)\s*$/,
        endCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*\/data-bind\s*-->$/ : /^\s*\/data-bind\s*$/;

    // TODO: the comment regex and comment functions are defined in the databind module as well. Best to have just one version of each defined.
    // TODO: maybe define them in databindings.util and databind can access them from there.
    function isStartComment (element)
    {
        return (element.nodeType == 8) && (commentNodesHaveTextProperty ? element.text : element.nodeValue).match(startCommentRegex);
    }

    function isEndComment (element)
    {
        return (element.nodeType == 8) && (commentNodesHaveTextProperty ? element.text : element.nodeValue).match(endCommentRegex);
    }

    function removeCommentContent (startCommentElement, endCommentElement)
    {
        var sibling = startCommentElement.nextSibling,
            nextSibling;

        while (sibling && sibling !== endCommentElement) {
            nextSibling = sibling.nextSibling;
            databindings.databind.removeBindings(sibling);
            $(sibling).remove();
            sibling = nextSibling;
        }
    }

    /**
     * Renders an individual item from a foreach binder's collection.
     * @private
     * @param {Object} element The dom element the foreach binder is bound to.
     * @param {Object} model The model this item is bound to.
     * @param {Object} template The markup template for the item.
     * @param {Object} viewModel The model the foreach binder is bound to.
     * @param {Object} at The index to insert the item at.
     * @param {Object} info The binding info for the foreach binding.
     */
    function renderItem (element, model, template, viewModel, at, info)
    {
        var itemElements = $("<div></div>").html(template).contents(),
            cid = model.get("cid");

        viewModel.fire('foreach-render', { nodes: itemElements, model: model, at: at }, function (e) {
            // If this is a comment element then render items as siblings
            if (info.isComment) {
                if (_.isNumber(at)) {
                    if (at === 0) {
                        itemElements.insertAfter(element);
                    }
                    else {
                        $(element).nextUntil(info.endCommentElement).eq(at - 1).after(itemElements);
                    }
                }
                else {
                    itemElements.insertBefore(info.endCommentElement);
                }
            }
            // Else render items as children
            else {
                if (_.isNumber(at)) {
                    if (at === 0) {
                        itemElements.prependTo(element);
                    }
                    else {
                        $('> tr', element).eq(at - 1).after(itemElements);
                    }
                }
                else {
                    itemElements.appendTo(element);
                }
            }

            // If the at value was not defined
            if (!_.isNumber(at)) {
                // Then update the at value with the index that the item was rendered at
                if (info.isComment) {
                    e.info.at = itemElements.index() - $(element).index();
                }
                else {
                    e.info.at = itemElements.index();
                }
            }

            itemElements.each(function () {
                this[foreachExpando] = cid;
                databindings.databind.applyBindings(model, this, info.context);
            });
        });
    }

{'fraction_non_alphanumeric': 0.11372123840501144, 'fraction_numerical': 0.0024093482712926155, 'mean_word_length': 1.8286201022146507, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '5282997', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2100, 'n_words': 612}