<commit_before>/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var defaulttags =
    djang10.defaulttags = 
    {};

register = new djang10.Library();


var AutoEscapeControlNode =
    defaulttags.AutoEscapeControlNode =
    function(setting, nodelist) {

    this.setting = setting;
    this.nodelist = nodelist;
};
AutoEscapeControlNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<AutoEscapeControlNode: " + this.setting + ">";
    },
    __render: function(context, printer) {
        var has_setting = "autoescape" in context;
        var old_setting = context.autoescape;
        context.autoescape = this.setting;
        this.nodelist.__render(context, printer);
        if(has_setting)
            context.autoescape = old_setting;
    }
};

var CommentNode =
    defaulttags.CommentNode =
    function() {
};

CommentNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<Comment Node>"
    },
    __render: function(context, printer) {
        //noop
    }
};


var CycleNode =
    defaulttags.CycleNode =
    function(cyclevars, variable_name) {
        
    this.cyclevars = cyclevars;
    this.i = 0;
    this.variable_name = variable_name;
};
CycleNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {        
        return "<Cycle Node: cycleVars: " + this.cyclevars + ", name: " + this.variable_name + ">"; 
    },
    __render: function(context, printer) {
        var template_vars = context["__render_vars"];
        if(template_vars == null)
            template_vars = context["__render_vars"] = new Map();
        
        var i = template_vars.get(this) || 0;

        
        var value = this.cyclevars[i].resolve(context);

        if(++i >= this.cyclevars.length)
            i = 0;
        template_vars.set(this, i);

        if(this.variable_name)
            context[this.variable_name] = value;
        
        printer(value);
    }
};

var FilterNode =
    defaulttags.FilterNode =
    function(filter_expr, nodelist) {
        
        this.filter_expr = filter_expr;
        this.nodelist = nodelist;
};
FilterNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<Filter Node: " + filter_expr + ">";
    },
    
    __render: function(context, printer) {
        context.push();
        var output = this.nodelist.render(context);
        context["temp"] = output;
        var filtered = this.filter_expr.resolve(context);
        context.pop();
        
        printer(filtered);
    }
};

var FirstOfNode =
    defaulttags.FirstOfNode =
    function(exprs) {
        
    this.exprs = exprs;
};
FirstOfNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<FirstOf Node: " + this.exprs.join(",").substring(0, 25) + "...>";
    },
    
    __render: function(context, printer) {
        for(var i=0; i<this.exprs.length; i++) {
            var expr = this.exprs[i];
            var value = expr.resolve(context);
            if(djang10.Expression.is_true(value)) {
                printer(value);
                return;
            }
        }
    }
}

var ForNode =
    defaulttags.ForNode =
    function(loopvars, sequence, is_reversed, nodelist_loop) {
    
    this.loopvars = loopvars;
    this.sequence = sequence;
    this.is_reversed = is_reversed;
    this.nodelist_loop = nodelist_loop;
    
};

ForNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        var rev_text = this.is_reversed? " reversed" : "";
        return "<For Node: for " + tojson(this.loopvars) 
            + " in " + this.sequence 
            + ", tail_len: " + this.nodelist_loop.length 
            + rev_text + ">"; 
    },
    get_nodes_by_type: function(constructor) {
        var nodes = [];
        if(isinstance(this, constructor))
            nodes.push(this);
        
        nodes.addAll(this.nodelist_loop.get_nodes_by_type(constructor));
        
        return nodes;
    },
    __render: function(context, printer) {
        var parentloop;
        
        parentloop = ("forloop" in context)? parentloop = context["forloop"] : {};
        context.push();
        
        var values = this.sequence.resolve(context);

        if(!djang10.Expression.is_true(values))
            values = [];

        if ( values.getClass().getName() == "ed.db.DBCursor" ){
            // TODO: use iteration method instead of toArray
            values = values.toArray();
        }

        if( this.is_reversed )
            values.reverse();
        
        var loop_dict = context["forloop"] = {parentloop: parentloop};
        for(var i=0; i<values.length; i++) {
            var item = values[i];
            
            loop_dict['counter0'] = i;
            loop_dict['counter'] = i+1;
            loop_dict['revcounter'] = values.length - i;
            loop_dict['revcounter0'] = values.length - i - 1;
            loop_dict['first'] = (i==0);
            loop_dict['last'] = (i== (values.length -1));
            
            if(this.loopvars.length == 1)
                context[this.loopvars[0]] = item;
            else
                for(var j=0;j<this.loopvars.length; j++)
                    context[this.loopvars[j]] = item[j];
            
            this.nodelist_loop.__render(context, printer);
        }
        context.pop();
    }
};

var IfChangedNode =
    defaulttags.IfChangedNode =
    function(nodelist, exprs) {

    this.nodelist = nodelist;
    this._varlist = exprs;    
};
IfChangedNode.are_equal = function(a, b){
    if(typeof(a) != typeof(b))
        return false;

    if(typeof(a) != "object" || typeof(b) != "object")
        return a == b;

    if((a instanceof Array) && (b instanceof Array)) {
        if(a.length != b.length) return false;
        for(var i=0; i<a.length; i++)<commit_msg>fixed cursor handling in foprloop tags<commit_after>/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var defaulttags =
    djang10.defaulttags = 
    {};

register = new djang10.Library();


var AutoEscapeControlNode =
    defaulttags.AutoEscapeControlNode =
    function(setting, nodelist) {

    this.setting = setting;
    this.nodelist = nodelist;
};
AutoEscapeControlNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<AutoEscapeControlNode: " + this.setting + ">";
    },
    __render: function(context, printer) {
        var has_setting = "autoescape" in context;
        var old_setting = context.autoescape;
        context.autoescape = this.setting;
        this.nodelist.__render(context, printer);
        if(has_setting)
            context.autoescape = old_setting;
    }
};

var CommentNode =
    defaulttags.CommentNode =
    function() {
};

CommentNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<Comment Node>"
    },
    __render: function(context, printer) {
        //noop
    }
};


var CycleNode =
    defaulttags.CycleNode =
    function(cyclevars, variable_name) {
        
    this.cyclevars = cyclevars;
    this.i = 0;
    this.variable_name = variable_name;
};
CycleNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {        
        return "<Cycle Node: cycleVars: " + this.cyclevars + ", name: " + this.variable_name + ">"; 
    },
    __render: function(context, printer) {
        var template_vars = context["__render_vars"];
        if(template_vars == null)
            template_vars = context["__render_vars"] = new Map();
        
        var i = template_vars.get(this) || 0;

        
        var value = this.cyclevars[i].resolve(context);

        if(++i >= this.cyclevars.length)
            i = 0;
        template_vars.set(this, i);

        if(this.variable_name)
            context[this.variable_name] = value;
        
        printer(value);
    }
};

var FilterNode =
    defaulttags.FilterNode =
    function(filter_expr, nodelist) {
        
        this.filter_expr = filter_expr;
        this.nodelist = nodelist;
};
FilterNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<Filter Node: " + filter_expr + ">";
    },
    
    __render: function(context, printer) {
        context.push();
        var output = this.nodelist.render(context);
        context["temp"] = output;
        var filtered = this.filter_expr.resolve(context);
        context.pop();
        
        printer(filtered);
    }
};

var FirstOfNode =
    defaulttags.FirstOfNode =
    function(exprs) {
        
    this.exprs = exprs;
};
FirstOfNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        return "<FirstOf Node: " + this.exprs.join(",").substring(0, 25) + "...>";
    },
    
    __render: function(context, printer) {
        for(var i=0; i<this.exprs.length; i++) {
            var expr = this.exprs[i];
            var value = expr.resolve(context);
            if(djang10.Expression.is_true(value)) {
                printer(value);
                return;
            }
        }
    }
}

var ForNode =
    defaulttags.ForNode =
    function(loopvars, sequence, is_reversed, nodelist_loop) {
    
    this.loopvars = loopvars;
    this.sequence = sequence;
    this.is_reversed = is_reversed;
    this.nodelist_loop = nodelist_loop;
    
};

ForNode.prototype = {
    __proto__: djang10.Node.prototype,
    
    toString: function() {
        var rev_text = this.is_reversed? " reversed" : "";
        return "<For Node: for " + tojson(this.loopvars) 
            + " in " + this.sequence 
            + ", tail_len: " + this.nodelist_loop.length 
            + rev_text + ">"; 
    },
    get_nodes_by_type: function(constructor) {
        var nodes = [];
        if(isinstance(this, constructor))
            nodes.push(this);
        
        nodes.addAll(this.nodelist_loop.get_nodes_by_type(constructor));
        
        return nodes;
    },
    __render: function(context, printer) {
        var parentloop;
        
        parentloop = ("forloop" in context)? parentloop = context["forloop"] : {};
        context.push();
        
        var values = this.sequence.resolve(context);

        if(!djang10.Expression.is_true(values))
            values = [];

        var next_fn;
        var count;
        
        if ( values.getClass().getName() == "ed.db.DBCursor" ){
            if(this.is_reversed) {
                throw "Can't reverse a DBCursor, use array mode, if you need to do this";
            }
            next_fn = function() {
                return values.next();
            };
            count = values.count();
        }
        else {
            next_fn = function() {
                return  values[next_fn.i++];
            };
            next_fn.i=0;

            count = values.length;
        }

        if( this.is_reversed )
            values.reverse();
        
        var loop_dict = context["forloop"] = {parentloop: parentloop};
        for(var i=0; i<count; i++) {
            var item = next_fn();
            
            loop_dict['counter0'] = i;
            loop_dict['counter'] = i+1;
            loop_dict['revcounter'] = count - i;
            loop_dict['revcounter0'] = count - i - 1;
            loop_dict['first'] = (i==0);
            loop_dict['last'] = (i== (count - 1));
            
            if(this.loopvars.length == 1)
                context[this.loopvars[0]] = item;
            else
                for(var j=0;j<this.loopvars.length; j++)
                    context[this.loopvars[j]] = item[j];
            
            this.nodelist_loop.__render(context, printer);
        }
        context.pop();
    }
};

var IfChangedNode =
    defaulttags.IfChangedNode =
    function(nodelist, exprs) {

    this.nodelist = nodelist;
    this._varlist = exprs;    
};
IfChangedNode.are_equal = function(a, b){
    if(typeof(a) != typeof(b))
        return false;

    if(typeof(a) != "object" || typeof(b) != "object")
        return a == b;

    if((a instanceof Array) && (b instanceof Array)) {
        if(a.length != b.length) return false;
        for(var i=0; i<a.length; i++)
{'fraction_non_alphanumeric': 0.12270210409745294, 'fraction_numerical': 0.006718346253229974, 'mean_word_length': 2.068176670441676, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1788886', 'n_tokens_mistral': 4190, 'n_tokens_neox': 4060, 'n_words': 878}