Created
July 20, 2011 11:49
-
-
Save shesek/1094819 to your computer and use it in GitHub Desktop.
Underscore.js templates escaping support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _.extend(_.templateSettings, { | |
| encode: /<%=([\s\S]+?)%>/g, | |
| interpolate : /<%==([\s\S]+?)%>/g | |
| }); | |
| _.extend(_, { | |
| // Taken from Backbone.js's escapeHTML() | |
| escape: function(string) { | |
| return (''+string).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/'); | |
| }, | |
| template: function(str, data) { | |
| var c = _.templateSettings; | |
| var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' + | |
| 'with(obj||{}){__p.push(\'' + | |
| str.replace(/\\/g, '\\\\') | |
| .replace(/'/g, "\\'") | |
| .replace(c.interpolate, function(match, code) { | |
| return "'," + code.replace(/\\'/g, "'") + ",'"; | |
| }) | |
| .replace(c.encode, function(match, code) { | |
| return "',_.escape(" + code.replace(/\\'/g, "'") + "),'"; | |
| }) | |
| .replace(c.evaluate || null, function(match, code) { | |
| return "');" + code.replace(/\\'/g, "'") | |
| .replace(/[\r\n\t]/g, ' ') + "__p.push('"; | |
| }) | |
| .replace(/\r/g, '\\r') | |
| .replace(/\n/g, '\\n') | |
| .replace(/\t/g, '\\t') | |
| + "');}return __p.join('');"; | |
| var func = new Function('obj', tmpl); | |
| return data ? func(data) : func; | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _.extend(_.templateSettings,{encode:/<%=([\s\S]+?)%>/g,interpolate:/<%==([\s\S]+?)%>/g}),_.extend(_,{escape:function(a){return(""+a).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/\//g,"/")},template:function(a,b){var c=_.templateSettings,d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+a.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(c.interpolate,function(a,b){return"',"+b.replace(/\\'/g,"'")+",'"}).replace(c.encode,function(a,b){return"',_.escape("+b.replace(/\\'/g,"'")+"),'"}).replace(c.evaluate||null,function(a,b){return"');"+b.replace(/\\'/g,"'").replace(/[\r\n\t]/g," ")+"__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');",e=new Function("obj",d);return b?e(b):e}}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _.templateSettings.encode = /<%==([\s\S]+?)%>/g; | |
| _.extend(_, { | |
| // Taken from Backbone.js's escapeHTML() | |
| escape: function(string) { | |
| return (''+string).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/'); | |
| }, | |
| template: function(str, data) { | |
| var c = _.templateSettings; | |
| var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' + | |
| 'with(obj||{}){__p.push(\'' + | |
| str.replace(/\\/g, '\\\\') | |
| .replace(/'/g, "\\'") | |
| .replace(c.encode, function(match, code) { | |
| return "',_.escape(" + code.replace(/\\'/g, "'") + "),'"; | |
| }) | |
| .replace(c.interpolate, function(match, code) { | |
| return "'," + code.replace(/\\'/g, "'") + ",'"; | |
| }) | |
| .replace(c.evaluate || null, function(match, code) { | |
| return "');" + code.replace(/\\'/g, "'") | |
| .replace(/[\r\n\t]/g, ' ') + "__p.push('"; | |
| }) | |
| .replace(/\r/g, '\\r') | |
| .replace(/\n/g, '\\n') | |
| .replace(/\t/g, '\\t') | |
| + "');}return __p.join('');"; | |
| var func = new Function('obj', tmpl); | |
| return data ? func(data) : func; | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _.templateSettings.encode=/<%==([\s\S]+?)%>/g,_.extend(_,{escape:function(a){return(""+a).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/\//g,"/")},template:function(a,b){var c=_.templateSettings,d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+a.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(c.encode,function(a,b){return"',_.escape("+b.replace(/\\'/g,"'")+"),'"}).replace(c.interpolate,function(a,b){return"',"+b.replace(/\\'/g,"'")+",'"}).replace(c.evaluate||null,function(a,b){return"');"+b.replace(/\\'/g,"'").replace(/[\r\n\t]/g," ")+"__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');",e=new Function("obj",d);return b?e(b):e}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment