Comparing version 0.5.0 to 0.6.0
72
ejs.js
@@ -32,3 +32,3 @@ | ||
return function(p){ | ||
if ('.' != p[0]) return require(p); | ||
if ('.' != p.substr(0, 1)) return require(p); | ||
@@ -68,3 +68,3 @@ var path = parent.split('/') | ||
exports.version = '0.4.2'; | ||
exports.version = '0.5.0'; | ||
@@ -176,3 +176,3 @@ /** | ||
var prefix, postfix, line = '__stack.lineno=' + lineno; | ||
switch (str[i]) { | ||
switch (str.substr(i, 1)) { | ||
case '=': | ||
@@ -193,25 +193,23 @@ prefix = "', escape((" + line + ', '; | ||
var start = i; | ||
var end = str.indexOf(close, i); | ||
var js = str.substring(i, end); | ||
var n = 0; | ||
while ((n = js.indexOf("\n", n)) > -1) { | ||
n++; | ||
lineno++; | ||
} | ||
if (js[0] == ':') js = filtered(js); | ||
var end = str.indexOf(close, i) | ||
, js = str.substring(i, end) | ||
, start = i | ||
, n = 0; | ||
while (~(n = js.indexOf("\n", n))) n++, lineno++; | ||
if (js.substr(0, 1) == ':') js = filtered(js); | ||
buf.push(prefix, js, postfix); | ||
i += end - start + close.length - 1; | ||
} else if (str[i] == "\\") { | ||
} else if (str.substr(i, 1) == "\\") { | ||
buf.push("\\\\"); | ||
} else if (str[i] == "'") { | ||
} else if (str.substr(i, 1) == "'") { | ||
buf.push("\\'"); | ||
} else if (str[i] == "\r") { | ||
} else if (str.substr(i, 1) == "\r") { | ||
buf.push(" "); | ||
} else if (str[i] == "\n") { | ||
} else if (str.substr(i, 1) == "\n") { | ||
buf.push("\\n"); | ||
lineno++; | ||
} else { | ||
buf.push(str[i]); | ||
buf.push(str.substr(i, 1)); | ||
} | ||
@@ -281,2 +279,3 @@ } | ||
, options = options || {}; | ||
if (options.cache) { | ||
@@ -291,6 +290,41 @@ if (options.filename) { | ||
} | ||
return fn.call(options.scope, options.locals || {}); | ||
options.__proto__ = options.locals; | ||
return fn.call(options.scope, options); | ||
}; | ||
/** | ||
* Render an EJS file at the given `path` and callback `fn(err, str)`. | ||
* | ||
* @param {String} path | ||
* @param {Object|Function} options or callback | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
exports.renderFile = function(path, options, fn){ | ||
var key = path + ':string'; | ||
if ('function' == typeof options) { | ||
fn = options, options = {}; | ||
} | ||
options.filename = path; | ||
try { | ||
var str = options.cache | ||
? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8')) | ||
: fs.readFileSync(path, 'utf8'); | ||
fn(null, exports.render(str, options)); | ||
} catch (err) { | ||
fn(err); | ||
} | ||
}; | ||
// express support | ||
exports.__express = exports.renderFile; | ||
/** | ||
* Expose to require(). | ||
@@ -303,3 +337,3 @@ */ | ||
module._compile(compile(source, {}), filename); | ||
}; | ||
}; | ||
} else if (require.registerExtension) { | ||
@@ -306,0 +340,0 @@ require.registerExtension('.ejs', function(src) { |
// CommonJS require() | ||
function require(p){var path=require.resolve(p),mod=require.modules[path];if(!mod)throw new Error('failed to require "'+p+'"');mod.exports||(mod.exports={},mod.call(mod.exports,mod,mod.exports,require.relative(path)));return mod.exports}require.modules={},require.resolve=function(path){var orig=path,reg=path+".js",index=path+"/index.js";return require.modules[reg]&®||require.modules[index]&&index||orig},require.register=function(path,fn){require.modules[path]=fn},require.relative=function(parent){return function(p){if("."!=p[0])return require(p);var path=parent.split("/"),segs=p.split("/");path.pop();for(var i=0;i<segs.length;i++){var seg=segs[i];".."==seg?path.pop():"."!=seg&&path.push(seg)}return require(path.join("/"))}},require.register("ejs.js",function(module,exports,require){var utils=require("./utils");exports.version="0.4.2";var filters=exports.filters=require("./filters"),cache={};exports.clearCache=function(){cache={}};function filtered(js){return js.substr(1).split("|").reduce(function(js,filter){var parts=filter.split(":"),name=parts.shift(),args=parts.shift()||"";args&&(args=", "+args);return"filters."+name+"("+js+args+")"})}function rethrow(err,str,filename,lineno){var lines=str.split("\n"),start=Math.max(lineno-3,0),end=Math.min(lines.length,lineno+3),context=lines.slice(start,end).map(function(line,i){var curr=i+start+1;return(curr==lineno?" >> ":" ")+curr+"| "+line}).join("\n");err.path=filename,err.message=(filename||"ejs")+":"+lineno+"\n"+context+"\n\n"+err.message;throw err}var parse=exports.parse=function(str,options){var options=options||{},open=options.open||exports.open||"<%",close=options.close||exports.close||"%>",buf=["var buf = [];","\nwith (locals) {","\n buf.push('"],lineno=1;for(var i=0,len=str.length;i<len;++i)if(str.slice(i,open.length+i)==open){i+=open.length;var prefix,postfix,line="__stack.lineno="+lineno;switch(str[i]){case"=":prefix="', escape(("+line+", ",postfix=")), '",++i;break;case"-":prefix="', ("+line+", ",postfix="), '",++i;break;default:prefix="');"+line+";",postfix="; buf.push('"}var start=i,end=str.indexOf(close,i),js=str.substring(i,end);js[0]==":"&&(js=filtered(js)),buf.push(prefix,js,postfix),i+=end-start+close.length-1}else str[i]=="\\"?buf.push("\\\\"):str[i]=="'"?buf.push("\\'"):str[i]=="\r"?buf.push(" "):str[i]=="\n"?(buf.push("\\n"),lineno++):buf.push(str[i]);buf.push("');\n}\nreturn buf.join('');");return buf.join("")},compile=exports.compile=function(str,options){options=options||{};var input=JSON.stringify(str),filename=options.filename?JSON.stringify(options.filename):"undefined";str=["var __stack = { lineno: 1, input: "+input+", filename: "+filename+" };",rethrow.toString(),"try {",exports.parse(str,options),"} catch (err) {"," rethrow(err, __stack.input, __stack.filename, __stack.lineno);","}"].join("\n"),options.debug&&console.log(str);var fn=new Function("locals, filters, escape",str);return function(locals){return fn.call(this,locals,filters,utils.escape)}};exports.render=function(str,options){var fn,options=options||{};if(options.cache)if(options.filename)fn=cache[options.filename]||(cache[options.filename]=compile(str,options));else throw new Error('"cache" option requires "filename".');else fn=compile(str,options);return fn.call(options.scope,options.locals||{})},require.extensions?require.extensions[".ejs"]=function(module,filename){source=require("fs").readFileSync(filename,"utf-8"),module._compile(compile(source,{}),filename)}:require.registerExtension&&require.registerExtension(".ejs",function(src){return compile(src,{})})}),require.register("filters.js",function(module,exports,require){exports.first=function(obj){return obj[0]},exports.last=function(obj){return obj[obj.length-1]},exports.capitalize=function(str){str=String(str);return str[0].toUpperCase()+str.substr(1,str.length)},exports.downcase=function(str){return String(str).toLowerCase()},exports.upcase=function(str){return String(str).toUpperCase()},exports.sort=function(obj){return Object.create(obj).sort()},exports.sort_by=function(obj,prop){return Object.create(obj).sort(function(a,b){a=a[prop],b=b[prop];if(a>b)return 1;if(a<b)return-1;return 0})},exports.size=exports.length=function(obj){return obj.length},exports.plus=function(a,b){return Number(a)+Number(b)},exports.minus=function(a,b){return Number(a)-Number(b)},exports.times=function(a,b){return Number(a)*Number(b)},exports.divided_by=function(a,b){return Number(a)/Number(b)},exports.join=function(obj,str){return obj.join(str||", ")},exports.truncate=function(str,len){str=String(str);return str.substr(0,len)},exports.truncate_words=function(str,n){var str=String(str),words=str.split(/ +/);return words.slice(0,n).join(" ")},exports.replace=function(str,pattern,substitution){return String(str).replace(pattern,substitution||"")},exports.prepend=function(obj,val){return Array.isArray(obj)?[val].concat(obj):val+obj},exports.append=function(obj,val){return Array.isArray(obj)?obj.concat(val):obj+val},exports.map=function(arr,prop){return arr.map(function(obj){return obj[prop]})},exports.reverse=function(obj){return Array.isArray(obj)?obj.reverse():String(obj).split("").reverse().join("")},exports.get=function(obj,prop){return obj[prop]},exports.json=function(obj){return JSON.stringify(obj)}}),require.register("utils.js",function(module,exports,require){exports.escape=function(html){return String(html).replace(/&(?!\w+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}}) | ||
function require(p){var path=require.resolve(p),mod=require.modules[path];if(!mod)throw new Error('failed to require "'+p+'"');mod.exports||(mod.exports={},mod.call(mod.exports,mod,mod.exports,require.relative(path)));return mod.exports}require.modules={},require.resolve=function(path){var orig=path,reg=path+".js",index=path+"/index.js";return require.modules[reg]&®||require.modules[index]&&index||orig},require.register=function(path,fn){require.modules[path]=fn},require.relative=function(parent){return function(p){if("."!=p.substr(0,1))return require(p);var path=parent.split("/"),segs=p.split("/");path.pop();for(var i=0;i<segs.length;i++){var seg=segs[i];".."==seg?path.pop():"."!=seg&&path.push(seg)}return require(path.join("/"))}},require.register("ejs.js",function(module,exports,require){var utils=require("./utils");exports.version="0.5.0";var filters=exports.filters=require("./filters"),cache={};exports.clearCache=function(){cache={}};function filtered(js){return js.substr(1).split("|").reduce(function(js,filter){var parts=filter.split(":"),name=parts.shift(),args=parts.shift()||"";args&&(args=", "+args);return"filters."+name+"("+js+args+")"})}function rethrow(err,str,filename,lineno){var lines=str.split("\n"),start=Math.max(lineno-3,0),end=Math.min(lines.length,lineno+3),context=lines.slice(start,end).map(function(line,i){var curr=i+start+1;return(curr==lineno?" >> ":" ")+curr+"| "+line}).join("\n");err.path=filename,err.message=(filename||"ejs")+":"+lineno+"\n"+context+"\n\n"+err.message;throw err}var parse=exports.parse=function(str,options){var options=options||{},open=options.open||exports.open||"<%",close=options.close||exports.close||"%>",buf=["var buf = [];","\nwith (locals) {","\n buf.push('"],lineno=1;for(var i=0,len=str.length;i<len;++i)if(str.slice(i,open.length+i)==open){i+=open.length;var prefix,postfix,line="__stack.lineno="+lineno;switch(str.substr(i,1)){case"=":prefix="', escape(("+line+", ",postfix=")), '",++i;break;case"-":prefix="', ("+line+", ",postfix="), '",++i;break;default:prefix="');"+line+";",postfix="; buf.push('"}var end=str.indexOf(close,i),js=str.substring(i,end),start=i,n=0;while(~(n=js.indexOf("\n",n)))n++,lineno++;js.substr(0,1)==":"&&(js=filtered(js)),buf.push(prefix,js,postfix),i+=end-start+close.length-1}else str.substr(i,1)=="\\"?buf.push("\\\\"):str.substr(i,1)=="'"?buf.push("\\'"):str.substr(i,1)=="\r"?buf.push(" "):str.substr(i,1)=="\n"?(buf.push("\\n"),lineno++):buf.push(str.substr(i,1));buf.push("');\n}\nreturn buf.join('');");return buf.join("")},compile=exports.compile=function(str,options){options=options||{};var input=JSON.stringify(str),filename=options.filename?JSON.stringify(options.filename):"undefined";str=["var __stack = { lineno: 1, input: "+input+", filename: "+filename+" };",rethrow.toString(),"try {",exports.parse(str,options),"} catch (err) {"," rethrow(err, __stack.input, __stack.filename, __stack.lineno);","}"].join("\n"),options.debug&&console.log(str);var fn=new Function("locals, filters, escape",str);return function(locals){return fn.call(this,locals,filters,utils.escape)}};exports.render=function(str,options){var fn,options=options||{};if(options.cache)if(options.filename)fn=cache[options.filename]||(cache[options.filename]=compile(str,options));else throw new Error('"cache" option requires "filename".');else fn=compile(str,options);options.__proto__=options.locals;return fn.call(options.scope,options)},exports.renderFile=function(path,options,fn){var key=path+":string";"function"==typeof options&&(fn=options,options={}),options.filename=path;try{var str=options.cache?exports.cache[key]||(exports.cache[key]=fs.readFileSync(path,"utf8")):fs.readFileSync(path,"utf8");fn(null,exports.render(str,options))}catch(err){fn(err)}},exports.__express=exports.renderFile,require.extensions?require.extensions[".ejs"]=function(module,filename){source=require("fs").readFileSync(filename,"utf-8"),module._compile(compile(source,{}),filename)}:require.registerExtension&&require.registerExtension(".ejs",function(src){return compile(src,{})})}),require.register("filters.js",function(module,exports,require){exports.first=function(obj){return obj[0]},exports.last=function(obj){return obj[obj.length-1]},exports.capitalize=function(str){str=String(str);return str[0].toUpperCase()+str.substr(1,str.length)},exports.downcase=function(str){return String(str).toLowerCase()},exports.upcase=function(str){return String(str).toUpperCase()},exports.sort=function(obj){return Object.create(obj).sort()},exports.sort_by=function(obj,prop){return Object.create(obj).sort(function(a,b){a=a[prop],b=b[prop];if(a>b)return 1;if(a<b)return-1;return 0})},exports.size=exports.length=function(obj){return obj.length},exports.plus=function(a,b){return Number(a)+Number(b)},exports.minus=function(a,b){return Number(a)-Number(b)},exports.times=function(a,b){return Number(a)*Number(b)},exports.divided_by=function(a,b){return Number(a)/Number(b)},exports.join=function(obj,str){return obj.join(str||", ")},exports.truncate=function(str,len){str=String(str);return str.substr(0,len)},exports.truncate_words=function(str,n){var str=String(str),words=str.split(/ +/);return words.slice(0,n).join(" ")},exports.replace=function(str,pattern,substitution){return String(str).replace(pattern,substitution||"")},exports.prepend=function(obj,val){return Array.isArray(obj)?[val].concat(obj):val+obj},exports.append=function(obj,val){return Array.isArray(obj)?obj.concat(val):obj+val},exports.map=function(arr,prop){return arr.map(function(obj){return obj[prop]})},exports.reverse=function(obj){return Array.isArray(obj)?obj.reverse():String(obj).split("").reverse().join("")},exports.get=function(obj,prop){return obj[prop]},exports.json=function(obj){return JSON.stringify(obj)}}),require.register("utils.js",function(module,exports,require){exports.escape=function(html){return String(html).replace(/&(?!\w+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}}) |
@@ -11,7 +11,5 @@ | ||
var ret = ejs.render(str, { | ||
locals: { | ||
names: ['foo', 'bar', 'baz'] | ||
} | ||
names: ['foo', 'bar', 'baz'] | ||
}); | ||
console.log(ret); |
0.6.0 / 2011-12-09 | ||
================== | ||
* Changed: you no longer need `{ locals: {} }` | ||
0.5.0 / 2011-11-20 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -18,3 +18,3 @@ | ||
exports.version = '0.5.0'; | ||
exports.version = '0.6.0'; | ||
@@ -227,2 +227,3 @@ /** | ||
, options = options || {}; | ||
if (options.cache) { | ||
@@ -237,3 +238,5 @@ if (options.filename) { | ||
} | ||
return fn.call(options.scope, options.locals || {}); | ||
options.__proto__ = options.locals; | ||
return fn.call(options.scope, options); | ||
}; | ||
@@ -264,3 +267,2 @@ | ||
options.locals = options; | ||
fn(null, exports.render(str, options)); | ||
@@ -267,0 +269,0 @@ } catch (err) { |
{ | ||
"name": "ejs", | ||
"description": "Embedded JavaScript templates", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca>", | ||
"keywords": ["template", "engine", "ejs"], | ||
"devDependencies": { | ||
"expresso": "0.9.2" | ||
"mocha": "*" | ||
}, | ||
"main": "./lib/ejs.js" | ||
} |
@@ -37,3 +37,2 @@ | ||
- `locals` Local variables object | ||
- `cache` Compiled functions are cached, requires `filename` | ||
@@ -45,2 +44,3 @@ - `filename` Used by `cache` to key caches | ||
- `close` Closing tag, defaulting to "%>" | ||
- * All others are template-local variables | ||
@@ -77,9 +77,7 @@ ## Custom tags | ||
ejs.render(str, { | ||
locals: { | ||
users: [ | ||
{ name: 'tj' }, | ||
{ name: 'mape' }, | ||
{ name: 'guillermo' } | ||
] | ||
} | ||
users: [ | ||
{ name: 'tj' }, | ||
{ name: 'mape' }, | ||
{ name: 'guillermo' } | ||
] | ||
}); | ||
@@ -86,0 +84,0 @@ |
@@ -94,2 +94,12 @@ | ||
}, | ||
'test pass options as locals': function(){ | ||
var html = '<p>foo</p>', | ||
str = '<p><%="foo"%></p>'; | ||
assert.equal(html, ejs.render(str)); | ||
var html = '<p>foo</p>', | ||
str = '<p><%=bar%></p>'; | ||
assert.equal(html, ejs.render(str, { bar: 'foo' })); | ||
}, | ||
@@ -240,7 +250,7 @@ 'test whitespace': function(){ | ||
ejs.render(str) | ||
} catch( err ){ | ||
assert.includes(err.message,"name is not defined"); | ||
assert.eql(err.name,"ReferenceError"); | ||
} catch (err) { | ||
assert.ok(~err.message.indexOf("name is not defined")); | ||
assert.deepEqual(err.name, "ReferenceError"); | ||
var lineno = parseInt(err.toString().match(/ejs:(\d+)\n/)[1]); | ||
assert.eql(lineno,3,"Error should been thrown on line 3, was thrown on line "+lineno); | ||
assert.deepEqual(lineno,3, "Error should been thrown on line 3, was thrown on line "+lineno); | ||
} | ||
@@ -264,9 +274,9 @@ }, | ||
ejs.render(str) | ||
} catch( err ){ | ||
assert.includes(err.message,"name is not defined"); | ||
assert.eql(err.name,"ReferenceError"); | ||
} catch (err) { | ||
assert.ok(~err.message.indexOf("name is not defined")); | ||
assert.deepEqual(err.name, "ReferenceError"); | ||
var lineno = parseInt(err.toString().match(/ejs:(\d+)\n/)[1]); | ||
assert.eql(lineno,6,"Error should been thrown on line 3, was thrown on line "+lineno); | ||
assert.deepEqual(lineno, 6, "Error should been thrown on line 3, was thrown on line "+lineno); | ||
} | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
45115
1266
3
151