dustjs-linkedin
Advanced tools
Comparing version
@@ -221,4 +221,4 @@ // | ||
if (typeof key === "function") { | ||
key = key(chk, ctx).data.join(""); | ||
chk.data = []; //ie7 perf | ||
var tempChk = new Chunk(); | ||
key = key(tempChk, this).data.join(""); | ||
} | ||
@@ -225,0 +225,0 @@ |
196
lib/dust.js
@@ -11,2 +11,56 @@ var dust = {}; | ||
if(!dust) { | ||
return; | ||
} | ||
var ERROR = 'ERROR', | ||
WARN = 'WARN', | ||
INFO = 'INFO', | ||
DEBUG = 'DEBUG', | ||
levels = [DEBUG, INFO, WARN, ERROR], | ||
logger = function() {}; | ||
dust.isDebug = false; | ||
dust.debugLevel = INFO; | ||
// Try to find the console logger in window scope (browsers) or top level scope (node.js) | ||
if (typeof window !== 'undefined' && window && window.console && window.console.log) { | ||
logger = window.console.log; | ||
} else if (typeof console !== 'undefined' && console && console.log) { | ||
logger = console.log; | ||
} | ||
/** | ||
* If dust.isDebug is true, Log dust debug statements, info statements, warning statements, and errors. | ||
* This default implementation will print to the console if it exists. | ||
* @param {String} message the message to print | ||
* @param {String} type the severity of the message(ERROR, WARN, INFO, or DEBUG) | ||
* @public | ||
*/ | ||
dust.log = function(message, type) { | ||
var type = type || INFO; | ||
if(dust.isDebug && levels.indexOf(type) >= levels.indexOf(dust.debugLevel)) { | ||
if(!dust.logQueue) { | ||
dust.logQueue = []; | ||
} | ||
dust.logQueue.push({message: message, type: type}); | ||
logger.call(console || window.console, "[DUST " + type + "]: " + message); | ||
} | ||
}; | ||
/** | ||
* If debugging is turned on(dust.isDebug=true) log the error message and throw it. | ||
* Otherwise try to keep rendering. This is useful to fail hard in dev mode, but keep rendering in production. | ||
* @param {Error} error the error message to throw | ||
* @param {Object} chunk the chunk the error was thrown from | ||
* @public | ||
*/ | ||
dust.onError = function(error, chunk) { | ||
dust.log(error.message || error, ERROR); | ||
if(dust.isDebug) { | ||
throw error; | ||
} else { | ||
return chunk; | ||
} | ||
}; | ||
dust.helpers = {}; | ||
@@ -23,3 +77,7 @@ | ||
var chunk = new Stub(callback).head; | ||
dust.load(name, chunk, Context.wrap(context, name)).end(); | ||
try { | ||
dust.load(name, chunk, Context.wrap(context, name)).end(); | ||
} catch (err) { | ||
dust.onError(err, chunk); | ||
} | ||
}; | ||
@@ -30,3 +88,7 @@ | ||
dust.nextTick(function() { | ||
dust.load(name, stream.head, Context.wrap(context, name)).end(); | ||
try { | ||
dust.load(name, stream.head, Context.wrap(context, name)).end(); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
} | ||
}); | ||
@@ -45,3 +107,8 @@ return stream; | ||
dust.nextTick(function() { | ||
tmpl(master.head, Context.wrap(context, name)).end(); | ||
if(typeof tmpl === 'function') { | ||
tmpl(master.head, Context.wrap(context, name)).end(); | ||
} | ||
else { | ||
dust.onError(new Error('Template [' + name + '] cannot be resolved to a Dust function')); | ||
} | ||
}); | ||
@@ -78,3 +145,3 @@ return master; | ||
dust.isArray = function(arr) { | ||
return Object.prototype.toString.call(arr) == "[object Array]"; | ||
return Object.prototype.toString.call(arr) === "[object Array]"; | ||
}; | ||
@@ -106,7 +173,10 @@ } | ||
auto = null; | ||
dust.log('Using unescape filter on [' + string + ']', DEBUG); | ||
} | ||
// fail silently for invalid filters | ||
else if (typeof dust.filters[name] === 'function') { | ||
string = dust.filters[name](string); | ||
} | ||
else { | ||
dust.onError(new Error('Invalid filter [' + name + ']')); | ||
} | ||
} | ||
@@ -126,4 +196,17 @@ } | ||
uc: encodeURIComponent, | ||
js: function(value) { if (!JSON) { return value; } return JSON.stringify(value); }, | ||
jp: function(value) { if (!JSON) { return value; } return JSON.parse(value); } | ||
js: function(value) { | ||
if (!JSON) { | ||
dust.log('JSON is undefined. JSON stringify has not been used on [' + value + ']', WARN); | ||
return value; | ||
} else { | ||
return JSON.stringify(value); | ||
} | ||
}, | ||
jp: function(value) { | ||
if (!JSON) {dust.log('JSON is undefined. JSON parse has not been used on [' + value + ']', WARN); | ||
return value; | ||
} else { | ||
return JSON.parse(value); | ||
} | ||
} | ||
}; | ||
@@ -150,4 +233,4 @@ | ||
Context.prototype.get = function(key) { | ||
var ctx = this.stack, value; | ||
var ctx = this.stack, value, globalValue; | ||
dust.log('Searching for reference [{' + key + '}] in template [' + this.templateName + ']', DEBUG); | ||
while(ctx) { | ||
@@ -162,3 +245,7 @@ if (ctx.isObject) { | ||
} | ||
return this.global ? this.global[key] : undefined; | ||
globalValue = this.global ? this.global[key] : undefined; | ||
if(typeof globalValue === 'undefined') { | ||
dust.log('Cannot find the value for reference [{' + key + '}] in template [' + this.templateName + ']'); | ||
} | ||
return globalValue; | ||
}; | ||
@@ -169,5 +256,6 @@ | ||
var ctx = this.stack, ctxThis, | ||
len = down.length, | ||
tail = cur ? undefined : this.stack.tail; | ||
len = down.length, | ||
tail = cur ? undefined : this.stack.tail; | ||
dust.log('Searching for reference [{' + down.join('.') + '}] in template [' + this.templateName + ']', DEBUG); | ||
if (cur && len === 0) return ctx.head; | ||
@@ -196,8 +284,8 @@ ctx = ctx.head; | ||
} | ||
} | ||
} | ||
} | ||
if (typeof ctx == 'function'){ | ||
//wrap to preserve context 'this' see #174 | ||
return function(){ | ||
return ctx.apply(ctxThis,arguments); | ||
return function(){ | ||
return ctx.apply(ctxThis,arguments); | ||
}; | ||
@@ -230,3 +318,6 @@ } | ||
if (!blocks) return; | ||
if (!blocks) { | ||
dust.log('No blocks for context[{' + key + '}] in template [' + this.templateName + ']', DEBUG); | ||
return; | ||
} | ||
var len = blocks.length, fn; | ||
@@ -276,2 +367,3 @@ while (len--) { | ||
this.callback(chunk.error); | ||
dust.onError(new Error('Chunk error [' + chunk.error + '] thrown. Ceasing to render this template.')); | ||
this.flush = function() {}; | ||
@@ -300,2 +392,3 @@ return; | ||
this.emit('error', chunk.error); | ||
dust.onError(new Error('Chunk error [' + chunk.error + '] thrown. Ceasing to render this template.')); | ||
this.flush = function() {}; | ||
@@ -313,8 +406,14 @@ return; | ||
Stream.prototype.emit = function(type, data) { | ||
if (!this.events) return false; | ||
if (!this.events) { | ||
dust.log('No events to emit', INFO); | ||
return false; | ||
} | ||
var handler = this.events[type]; | ||
if (!handler) return false; | ||
if (typeof handler == 'function') { | ||
if (!handler) { | ||
dust.log('Event type [' + type + '] does not exist', WARN); | ||
return false; | ||
} | ||
if (typeof handler === 'function') { | ||
handler(data); | ||
} else { | ||
} else if (dust.isArray(handler)) { | ||
var listeners = handler.slice(0); | ||
@@ -324,2 +423,4 @@ for (var i = 0, l = listeners.length; i < l; i++) { | ||
} | ||
} else { | ||
dust.onError(new Error('Event Handler [' + handler + '] is not of a type that is handled by emit')); | ||
} | ||
@@ -333,3 +434,8 @@ }; | ||
if (!this.events[type]) { | ||
this.events[type] = callback; | ||
dust.log('Event type [' + type + '] does not exist. Using just the specified callback.', WARN); | ||
if(callback) { | ||
this.events[type] = callback; | ||
} else { | ||
dust.log('Callback for type [' + type + '] does not exist. Listener not registered.', WARN); | ||
} | ||
} else if(typeof this.events[type] === 'function') { | ||
@@ -345,5 +451,13 @@ this.events[type] = [this.events[type], callback]; | ||
this.on("data", function(data) { | ||
stream.write(data, "utf8"); | ||
try { | ||
stream.write(data, "utf8"); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
} | ||
}).on("end", function() { | ||
stream.end(); | ||
try { | ||
return stream.end(); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
} | ||
}).on("error", function(err) { | ||
@@ -415,3 +529,3 @@ stream.error(err); | ||
elem.isFunction = true; | ||
// Changed the function calling to use apply with the current context to make sure | ||
// Changed the function calling to use apply with the current context to make sure | ||
// that "this" is wat we expect it to be inside the function | ||
@@ -450,3 +564,3 @@ elem = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]); | ||
Dust's default behavior is to enumerate over the array elem, passing each object in the array to the block. | ||
When elem resolves to a value or object instead of an array, Dust sets the current context to the value | ||
When elem resolves to a value or object instead of an array, Dust sets the current context to the value | ||
and renders the block one time. | ||
@@ -459,6 +573,6 @@ */ | ||
if (len > 0) { | ||
// any custom helper can blow up the stack | ||
// any custom helper can blow up the stack | ||
// and store a flattened context, guard defensively | ||
if(context.stack.head) { | ||
context.stack.head['$len'] = len; | ||
context.stack.head['$len'] = len; | ||
} | ||
@@ -476,3 +590,3 @@ for (var i=0; i<len; i++) { | ||
return chunk; | ||
} | ||
} | ||
else if (skip) { | ||
@@ -485,3 +599,3 @@ return skip(this, context); | ||
else if (elem === true) { | ||
if (body) { | ||
if (body) { | ||
return body(this, context); | ||
@@ -500,3 +614,4 @@ } | ||
return skip(this, context); | ||
} | ||
} | ||
dust.log('Not rendering section (#) block in template [' + context.templateName + '], because above key was not found', DEBUG); | ||
return this; | ||
@@ -514,2 +629,3 @@ }; | ||
} | ||
dust.log('Not rendering exists (?) block in template [' + context.templateName + '], because above key was not found', DEBUG); | ||
return this; | ||
@@ -527,2 +643,3 @@ }; | ||
} | ||
dust.log('Not rendering not exists (^) block check in template [' + context.templateName + '], because above key was found', DEBUG); | ||
return this; | ||
@@ -558,4 +675,6 @@ }; | ||
} | ||
if(typeof elem === "string") { | ||
// templateName can be static (string) or dynamic (function) | ||
// e.g. {>"static_template"/} | ||
// {>"{dynamic_template}"/} | ||
if (elem) { | ||
partialContext.templateName = elem; | ||
@@ -580,7 +699,12 @@ } | ||
Chunk.prototype.helper = function(name, context, bodies, params) { | ||
var chunk = this; | ||
// handle invalid helpers, similar to invalid filters | ||
if( dust.helpers[name]){ | ||
return dust.helpers[name](this, context, bodies, params); | ||
} else { | ||
return this; | ||
try { | ||
if(dust.helpers[name]) { | ||
return dust.helpers[name](chunk, context, bodies, params); | ||
} else { | ||
return dust.onError(new Error('Invalid helper [' + name + ']'), chunk); | ||
} | ||
} catch (err) { | ||
return dust.onError(err, chunk); | ||
} | ||
@@ -587,0 +711,0 @@ }; |
{ | ||
"name": "dustjs-linkedin", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"author": "Aleksander Williams", | ||
@@ -5,0 +5,0 @@ "description": "Asynchronous templates for the browser and node.js ( LinkedIn fork )", |
@@ -6,3 +6,3 @@ (function(exports){ | ||
suite.test(test.name, function(){ | ||
testRender(this, test.source, test.context, test.expected, test.options, test.base, test.error || {}); | ||
testRender(this, test.source, test.context, test.expected, test.options, test.base, test.error || {}, test.log); | ||
}); | ||
@@ -109,5 +109,8 @@ }); | ||
function testRender(unit, source, context, expected, options, baseContext, error) { | ||
var name = unit.id; | ||
function testRender(unit, source, context, expected, options, baseContext, error, logMessage) { | ||
var name = unit.id, | ||
messageInLog = ''; | ||
try { | ||
dust.isDebug = !!(error || logMessage); | ||
dust.debugLevel = 'DEBUG'; | ||
dust.loadSource(dust.compile(source, name)); | ||
@@ -118,4 +121,16 @@ if (baseContext){ | ||
dust.render(name, context, function(err, output) { | ||
var log = dust.logQueue; | ||
unit.ifError(err); | ||
unit.equals(output, expected); | ||
if(logMessage) { | ||
for(var i=0; i<log.length; i++) { | ||
if(log[i].message === logMessage) { | ||
messageInLog = true; | ||
break; | ||
} | ||
} | ||
dust.logQueue = []; | ||
unit.equals(messageInLog, true); | ||
} else { | ||
unit.equals(output, expected); | ||
} | ||
}); | ||
@@ -122,0 +137,0 @@ } catch(err) { |
@@ -13,5 +13,8 @@ describe ("Test the basic functionality of dust", function() { | ||
function render(test) { | ||
var messageInLog = false; | ||
return function() { | ||
var context; | ||
try { | ||
dust.isDebug = !!(test.error || test.log); | ||
dust.debugLevel = 'DEBUG'; | ||
dust.loadSource(dust.compile(test.source, test.name, test.strip)); | ||
@@ -23,4 +26,16 @@ context = test.context; | ||
dust.render(test.name, context, function(err, output) { | ||
var log = dust.logQueue; | ||
expect(err).toBeNull(); | ||
expect(test.expected).toEqual(output); | ||
if (test.log) { | ||
for(var i=0; i<log.length; i++) { | ||
if(log[i].message === test.log) { | ||
messageInLog = true; | ||
break; | ||
} | ||
} | ||
dust.logQueue = []; | ||
expect(messageInLog).toEqual(true); | ||
} else { | ||
expect(test.expected).toEqual(output); | ||
} | ||
}); | ||
@@ -36,7 +51,14 @@ } | ||
return function() { | ||
var output ="", flag, context; | ||
var output = "", | ||
messageInLog = false, | ||
log, | ||
flag, | ||
context; | ||
runs(function(){ | ||
flag = false; | ||
output = ""; | ||
log = []; | ||
try { | ||
dust.isDebug = !!(test.error || test.log); | ||
dust.debugLevel = 'DEBUG'; | ||
dust.loadSource(dust.compile(test.source, test.name)); | ||
@@ -47,12 +69,34 @@ context = test.context; | ||
} | ||
// redefine dust.nextTick try catches within async functions to test the error message | ||
dust.nextTick = (function() { | ||
if (typeof process !== "undefined") { | ||
return function(callback) { | ||
process.nextTick(function() { | ||
try { | ||
callback(); | ||
} catch(error) { | ||
output = error.message; | ||
flag = true; | ||
} | ||
}); | ||
}; | ||
} else { | ||
return function(callback) { | ||
setTimeout(callback,0); | ||
}; | ||
} | ||
} )(); | ||
dust.stream(test.name, context) | ||
.on("data", function(data) { | ||
output += data; | ||
log = dust.logQueue; | ||
}) | ||
.on("end", function() { | ||
flag = true; | ||
log = dust.logQueue; | ||
}) | ||
.on("error", function(err) { | ||
output = err.message; | ||
}); | ||
log = dust.logQueue; | ||
}) | ||
} catch(error) { | ||
@@ -71,2 +115,11 @@ output = error.message; | ||
expect(test.error || {} ).toEqual(output); | ||
} else if(test.log) { | ||
for(var i=0; i<log.length; i++) { | ||
if(log[i].message === test.log) { | ||
messageInLog = true; | ||
break; | ||
} | ||
} | ||
dust.logQueue = []; | ||
expect(messageInLog).toEqual(true); | ||
} else { | ||
@@ -81,4 +134,4 @@ expect(test.expected).toEqual(output); | ||
return function() { | ||
var output, outputTwo, flag, flagTwo, context; | ||
runs(function(){ | ||
var output, outputTwo, flag, flagTwo, context, log, logTwo, messageInLog, messageInLogTwo; | ||
runs(function() { | ||
flag = false; | ||
@@ -88,3 +141,9 @@ flagTwo = false; | ||
outputTwo = ""; | ||
log = []; | ||
logTwo = []; | ||
messageInLog = false; | ||
messageInLogTwo = false; | ||
try { | ||
dust.isDebug = !!(test.error || test.log); | ||
dust.debugLevel = 'DEBUG'; | ||
dust.loadSource(dust.compile(test.source, test.name)); | ||
@@ -95,2 +154,23 @@ context = test.context; | ||
} | ||
// redefine dust.nextTick try catches within async functions to test the error message | ||
dust.nextTick = (function() { | ||
if (typeof process !== "undefined") { | ||
return function(callback) { | ||
process.nextTick(function() { | ||
try { | ||
callback(); | ||
} catch(error) { | ||
output = error.message; | ||
outputTwo = error.message; | ||
flag = true; | ||
flagTwo = true; | ||
} | ||
}); | ||
}; | ||
} else { | ||
return function(callback) { | ||
setTimeout(callback,0); | ||
}; | ||
} | ||
} )(); | ||
var tpl = dust.stream(test.name, context); | ||
@@ -100,5 +180,7 @@ tpl.pipe({ | ||
output += data; | ||
log = dust.logQueue; | ||
}, | ||
end: function () { | ||
flag = true; | ||
log = dust.logQueue; | ||
}, | ||
@@ -108,2 +190,3 @@ error: function (err) { | ||
output = err.message; | ||
log = dust.logQueue; | ||
} | ||
@@ -115,5 +198,7 @@ }); | ||
outputTwo += data; | ||
logTwo = logTwo.concat(dust.logQueue); | ||
}, | ||
end: function () { | ||
flagTwo = true; | ||
logTwo = logTwo.concat(dust.logQueue); | ||
}, | ||
@@ -123,2 +208,3 @@ error: function (err) { | ||
outputTwo = err.message; | ||
logTwo = logTwo.concat(dust.logQueue); | ||
} | ||
@@ -142,2 +228,19 @@ }); | ||
expect(test.error || {} ).toEqual(outputTwo); | ||
} else if (test.log) { | ||
for(var i=0; i<log.length; i++) { | ||
if(log[i].message === test.log) { | ||
messageInLog = true; | ||
break; | ||
} | ||
} | ||
dust.logQueue = []; | ||
expect(messageInLog).toEqual(true); | ||
for(var i=0; i<logTwo.length; i++) { | ||
if(logTwo[i].message === test.log) { | ||
messageInLogTwo = true; | ||
break; | ||
} | ||
} | ||
dust.logQueue = []; | ||
expect(messageInLogTwo).toEqual(true); | ||
} else { | ||
@@ -144,0 +247,0 @@ expect(test.expected).toEqual(output); |
@@ -18,2 +18,2 @@ var parser = require('uglify-js').parser, | ||
fs.writeFileSync(path.join(root, process.argv[3]),final_code); | ||
fs.appendFileSync(path.join(root, process.argv[3]),final_code); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
2398663
6.31%94
2.17%64891
7.52%34
9.68%