dustjs-linkedin
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -8,5 +8,7 @@ var dustCompiler = (function(dust) { | ||
} | ||
catch(err) | ||
catch (err) | ||
{ | ||
if(!err.line || !err.column) throw err; | ||
if (!err.line || !err.column) { | ||
throw err; | ||
} | ||
throw new SyntaxError(err.message + " At line : " + err.line + ", column : " + err.column); | ||
@@ -48,4 +50,4 @@ } | ||
comment: nullify, | ||
line: nullify, | ||
col: nullify | ||
line: nullify, | ||
col: nullify | ||
}; | ||
@@ -55,6 +57,9 @@ | ||
esc: function(compiler, context, bodies, params) { | ||
var old = compiler.auto; | ||
if (!context) context = 'h'; | ||
var old = compiler.auto, | ||
out; | ||
if (!context) { | ||
context = 'h'; | ||
} | ||
compiler.auto = (context === 's') ? '' : context; | ||
var out = compileParts(compiler, bodies.block); | ||
out = compileParts(compiler, bodies.block); | ||
compiler.auto = old; | ||
@@ -66,6 +71,9 @@ return out; | ||
function visit(context, node) { | ||
var out = [node[0]]; | ||
for (var i=1, len=node.length; i<len; i++) { | ||
var res = dust.filterNode(context, node[i]); | ||
if (res) out.push(res); | ||
var out = [node[0]], | ||
i, len, res; | ||
for (i=1, len=node.length; i<len; i++) { | ||
res = dust.filterNode(context, node[i]); | ||
if (res) { | ||
out.push(res); | ||
} | ||
} | ||
@@ -77,5 +85,6 @@ return out; | ||
function compactBuffers(context, node) { | ||
var out = [node[0]], memo; | ||
for (var i=1, len=node.length; i<len; i++) { | ||
var res = dust.filterNode(context, node[i]); | ||
var out = [node[0]], | ||
memo, i, len, res; | ||
for (i=1, len=node.length; i<len; i++) { | ||
res = dust.filterNode(context, node[i]); | ||
if (res) { | ||
@@ -106,4 +115,10 @@ if (res[0] === 'buffer') { | ||
function convertSpecial(context, node) { return ['buffer', specialChars[node[1]]] }; | ||
function noop(context, node) { return node }; | ||
function convertSpecial(context, node) { | ||
return ['buffer', specialChars[node[1]]] | ||
}; | ||
function noop(context, node) { | ||
return node | ||
}; | ||
function nullify(){}; | ||
@@ -132,5 +147,6 @@ | ||
var out = [], | ||
blocks = context.blocks; | ||
blocks = context.blocks, | ||
name; | ||
for (var name in blocks) { | ||
for (name in blocks) { | ||
out.push("'" + name + "':" + blocks[name]); | ||
@@ -148,5 +164,6 @@ } | ||
bodies = context.bodies, | ||
blx = context.blocks; | ||
blx = context.blocks, | ||
i, len; | ||
for (var i=0, len=bodies.length; i<len; i++) { | ||
for (i=0, len=bodies.length; i<len; i++) { | ||
out[i] = "function body_" + i + "(chk,ctx){" | ||
@@ -159,4 +176,5 @@ + blx + "return chk" + bodies[i] + ";}"; | ||
function compileParts(context, body) { | ||
var parts = ''; | ||
for (var i=1, len=body.length; i<len; i++) { | ||
var parts = '', | ||
i, len; | ||
for (i=1, len=body.length; i<len; i++) { | ||
parts += dust.compileNode(context, body[i]); | ||
@@ -173,3 +191,4 @@ } | ||
body: function(context, node) { | ||
var id = context.index++, name = "body_" + id; | ||
var id = context.index++, | ||
name = "body_" + id; | ||
context.bodies[id] = compileParts(context, node); | ||
@@ -315,3 +334,3 @@ return name; | ||
key: function(context, node) { | ||
return "ctx.get(\"" + node[1] + "\")"; | ||
return "ctx._get(false, [\"" + node[1] + "\"])"; | ||
}, | ||
@@ -330,3 +349,3 @@ | ||
} | ||
return "ctx.getPath(" + current + ",[" + list.join(',') + "])"; | ||
return "ctx._get(" + current + ",[" + list.join(',') + "])"; | ||
}, | ||
@@ -333,0 +352,0 @@ |
1325
lib/dust.js
@@ -0,1 +1,2 @@ | ||
/*global console */ | ||
var dust = {}; | ||
@@ -11,766 +12,810 @@ | ||
if(!dust) { | ||
return; | ||
} | ||
var ERROR = 'ERROR', | ||
WARN = 'WARN', | ||
INFO = 'INFO', | ||
DEBUG = 'DEBUG', | ||
levels = [DEBUG, INFO, WARN, ERROR], | ||
logger = function() {}; | ||
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; | ||
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; | ||
} | ||
// 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 = []; | ||
/** | ||
* 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); | ||
} | ||
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; | ||
} | ||
}; | ||
/** | ||
* 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 = {}; | ||
dust.helpers = {}; | ||
dust.cache = {}; | ||
dust.cache = {}; | ||
dust.register = function(name, tmpl) { | ||
if (!name) return; | ||
dust.cache[name] = tmpl; | ||
}; | ||
dust.register = function(name, tmpl) { | ||
if (!name) { | ||
return; | ||
} | ||
dust.cache[name] = tmpl; | ||
}; | ||
dust.render = function(name, context, callback) { | ||
var chunk = new Stub(callback).head; | ||
try { | ||
dust.load(name, chunk, Context.wrap(context, name)).end(); | ||
} catch (err) { | ||
dust.onError(err, chunk); | ||
} | ||
}; | ||
dust.stream = function(name, context) { | ||
var stream = new Stream(); | ||
dust.nextTick(function() { | ||
dust.render = function(name, context, callback) { | ||
var chunk = new Stub(callback).head; | ||
try { | ||
dust.load(name, stream.head, Context.wrap(context, name)).end(); | ||
dust.load(name, chunk, Context.wrap(context, name)).end(); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
dust.onError(err, chunk); | ||
} | ||
}); | ||
return stream; | ||
}; | ||
}; | ||
dust.renderSource = function(source, context, callback) { | ||
return dust.compileFn(source)(context, callback); | ||
}; | ||
dust.compileFn = function(source, name) { | ||
var tmpl = dust.loadSource(dust.compile(source, name)); | ||
return function(context, callback) { | ||
var master = callback ? new Stub(callback) : new Stream(); | ||
dust.stream = function(name, context) { | ||
var stream = new Stream(); | ||
dust.nextTick(function() { | ||
if(typeof tmpl === 'function') { | ||
tmpl(master.head, Context.wrap(context, name)).end(); | ||
try { | ||
dust.load(name, stream.head, Context.wrap(context, name)).end(); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
} | ||
else { | ||
dust.onError(new Error('Template [' + name + '] cannot be resolved to a Dust function')); | ||
} | ||
}); | ||
return master; | ||
return stream; | ||
}; | ||
}; | ||
dust.load = function(name, chunk, context) { | ||
var tmpl = dust.cache[name]; | ||
if (tmpl) { | ||
return tmpl(chunk, context); | ||
} else { | ||
if (dust.onLoad) { | ||
return chunk.map(function(chunk) { | ||
dust.onLoad(name, function(err, src) { | ||
if (err) return chunk.setError(err); | ||
if (!dust.cache[name]) dust.loadSource(dust.compile(src, name)); | ||
dust.cache[name](chunk, context).end(); | ||
dust.renderSource = function(source, context, callback) { | ||
return dust.compileFn(source)(context, callback); | ||
}; | ||
dust.compileFn = function(source, name) { | ||
var tmpl = dust.loadSource(dust.compile(source, name)); | ||
return function(context, callback) { | ||
var master = callback ? new Stub(callback) : new Stream(); | ||
dust.nextTick(function() { | ||
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')); | ||
} | ||
}); | ||
return master; | ||
}; | ||
}; | ||
dust.load = function(name, chunk, context) { | ||
var tmpl = dust.cache[name]; | ||
if (tmpl) { | ||
return tmpl(chunk, context); | ||
} else { | ||
if (dust.onLoad) { | ||
return chunk.map(function(chunk) { | ||
dust.onLoad(name, function(err, src) { | ||
if (err) { | ||
return chunk.setError(err); | ||
} | ||
if (!dust.cache[name]) { | ||
dust.loadSource(dust.compile(src, name)); | ||
} | ||
dust.cache[name](chunk, context).end(); | ||
}); | ||
}); | ||
}); | ||
} | ||
return chunk.setError(new Error('Template Not Found: ' + name)); | ||
} | ||
return chunk.setError(new Error("Template Not Found: " + name)); | ||
} | ||
}; | ||
}; | ||
dust.loadSource = function(source, path) { | ||
return eval(source); | ||
}; | ||
if (Array.isArray) { | ||
dust.isArray = Array.isArray; | ||
} else { | ||
dust.isArray = function(arr) { | ||
return Object.prototype.toString.call(arr) === "[object Array]"; | ||
dust.loadSource = function(source, path) { | ||
return eval(source); | ||
}; | ||
} | ||
dust.nextTick = (function() { | ||
if (typeof process !== "undefined") { | ||
return process.nextTick; | ||
if (Array.isArray) { | ||
dust.isArray = Array.isArray; | ||
} else { | ||
return function(callback) { | ||
setTimeout(callback,0); | ||
dust.isArray = function(arr) { | ||
return Object.prototype.toString.call(arr) === '[object Array]'; | ||
}; | ||
} | ||
} )(); | ||
dust.isEmpty = function(value) { | ||
if (dust.isArray(value) && !value.length) return true; | ||
if (value === 0) return false; | ||
return (!value); | ||
}; | ||
dust.nextTick = (function() { | ||
if (typeof process !== 'undefined') { | ||
return process.nextTick; | ||
} else { | ||
return function(callback) { | ||
setTimeout(callback,0); | ||
}; | ||
} | ||
} )(); | ||
// apply the filter chain and return the output string | ||
dust.filter = function(string, auto, filters) { | ||
if (filters) { | ||
for (var i=0, len=filters.length; i<len; i++) { | ||
var name = filters[i]; | ||
if (name === "s") { | ||
auto = null; | ||
dust.log('Using unescape filter on [' + string + ']', DEBUG); | ||
dust.isEmpty = function(value) { | ||
if (dust.isArray(value) && !value.length) { | ||
return true; | ||
} | ||
if (value === 0) { | ||
return false; | ||
} | ||
return (!value); | ||
}; | ||
// apply the filter chain and return the output string | ||
dust.filter = function(string, auto, filters) { | ||
if (filters) { | ||
for (var i=0, len=filters.length; i<len; i++) { | ||
var name = filters[i]; | ||
if (name === 's') { | ||
auto = null; | ||
dust.log('Using unescape filter on [' + string + ']', DEBUG); | ||
} | ||
else if (typeof dust.filters[name] === 'function') { | ||
string = dust.filters[name](string); | ||
} | ||
else { | ||
dust.onError(new Error('Invalid filter [' + name + ']')); | ||
} | ||
} | ||
else if (typeof dust.filters[name] === 'function') { | ||
string = dust.filters[name](string); | ||
} | ||
// by default always apply the h filter, unless asked to unescape with |s | ||
if (auto) { | ||
string = dust.filters[auto](string); | ||
} | ||
return string; | ||
}; | ||
dust.filters = { | ||
h: function(value) { return dust.escapeHtml(value); }, | ||
j: function(value) { return dust.escapeJs(value); }, | ||
u: encodeURI, | ||
uc: encodeURIComponent, | ||
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); | ||
} | ||
else { | ||
dust.onError(new Error('Invalid filter [' + name + ']')); | ||
}, | ||
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); | ||
} | ||
} | ||
}; | ||
function Context(stack, global, blocks, templateName) { | ||
this.stack = stack; | ||
this.global = global; | ||
this.blocks = blocks; | ||
this.templateName = templateName; | ||
} | ||
// by default always apply the h filter, unless asked to unescape with |s | ||
if (auto) { | ||
string = dust.filters[auto](string); | ||
} | ||
return string; | ||
}; | ||
dust.filters = { | ||
h: function(value) { return dust.escapeHtml(value); }, | ||
j: function(value) { return dust.escapeJs(value); }, | ||
u: encodeURI, | ||
uc: encodeURIComponent, | ||
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); | ||
dust.makeBase = function(global) { | ||
return new Context(new Stack(), global); | ||
}; | ||
Context.wrap = function(context, name) { | ||
if (context instanceof Context) { | ||
return context; | ||
} | ||
}, | ||
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); | ||
return new Context(new Stack(context), {}, null, name); | ||
}; | ||
/** | ||
* Public API for getting a value from the context. | ||
* @method get | ||
* @param {string|array} path The path to the value. Supported formats are: | ||
* 'key' | ||
* 'path.to.key' | ||
* '.path.to.key' | ||
* ['path', 'to', 'key'] | ||
* ['key'] | ||
* @param {boolean} [cur=false] Boolean which determines if the search should be limited to the | ||
* current context (true), or if get should search in parent contexts as well (false). | ||
* @public | ||
* @returns {string|object} | ||
*/ | ||
Context.prototype.get = function(path, cur) { | ||
if (typeof path === 'string') { | ||
if (path[0] === '.') { | ||
cur = true; | ||
path = path.substr(1); | ||
} | ||
path = path.split('.'); | ||
} | ||
} | ||
}; | ||
return this._get(cur, path); | ||
}; | ||
function Context(stack, global, blocks, templateName) { | ||
this.stack = stack; | ||
this.global = global; | ||
this.blocks = blocks; | ||
this.templateName = templateName; | ||
} | ||
/** | ||
* Get a value from the context | ||
* @method _get | ||
* @param {boolean} cur Get only from the current context | ||
* @param {array} down An array of each step in the path | ||
* @private | ||
* @return {string | object} | ||
*/ | ||
Context.prototype._get = function(cur, down) { | ||
var ctx = this.stack, | ||
i = 1, | ||
value, first, len, ctxThis; | ||
dust.log('Searching for reference [{' + down.join('.') + '}] in template [' + this.templateName + ']', DEBUG); | ||
first = down[0]; | ||
len = down.length; | ||
dust.makeBase = function(global) { | ||
return new Context(new Stack(), global); | ||
}; | ||
if (cur && len === 0) { | ||
ctxThis = ctx; | ||
ctx = ctx.head; | ||
} else { | ||
if (!cur) { | ||
// Search up the stack for the first value | ||
while (ctx) { | ||
if (ctx.isObject) { | ||
ctxThis = ctx.head; | ||
value = ctx.head[first]; | ||
if (value !== undefined) { | ||
break; | ||
} | ||
} | ||
ctx = ctx.tail; | ||
} | ||
Context.wrap = function(context, name) { | ||
if (context instanceof Context) { | ||
return context; | ||
} | ||
return new Context(new Stack(context), {}, null, name); | ||
}; | ||
if (value !== undefined) { | ||
ctx = value; | ||
} else { | ||
ctx = this.global ? this.global[first] : undefined; | ||
} | ||
} else { | ||
// if scope is limited by a leading dot, don't search up the tree | ||
ctx = ctx.head[first]; | ||
} | ||
Context.prototype.get = function(key) { | ||
var ctx = this.stack, value, globalValue; | ||
dust.log('Searching for reference [{' + key + '}] in template [' + this.templateName + ']', DEBUG); | ||
while(ctx) { | ||
if (ctx.isObject) { | ||
value = ctx.head[key]; | ||
if (!(value === undefined)) { | ||
return value; | ||
while (ctx && i < len) { | ||
ctxThis = ctx; | ||
ctx = ctx[down[i]]; | ||
i++; | ||
} | ||
} | ||
ctx = ctx.tail; | ||
} | ||
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; | ||
}; | ||
//supports dot path resolution, function wrapped apply, and searching global paths | ||
Context.prototype.getPath = function(cur, down) { | ||
var ctx = this.stack, ctxThis, | ||
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; | ||
ctx = ctx.head; | ||
var i = 0; | ||
while(ctx && i < len) { | ||
ctxThis = ctx; | ||
ctx = ctx[down[i]]; | ||
i++; | ||
while (!ctx && !cur){ | ||
// i is the count of number of path elements matched. If > 1 then we have a partial match | ||
// and do not continue to search for the rest of the path. | ||
// Note: a falsey value at the end of a matched path also comes here. | ||
// This returns the value or undefined if we just have a partial match. | ||
if (i > 1) return ctx; | ||
if (tail){ | ||
ctx = tail.head; | ||
tail = tail.tail; | ||
i=0; | ||
} else if (!cur) { | ||
//finally search this.global. we set cur to true to halt after | ||
ctx = this.global; | ||
cur = true; | ||
i=0; | ||
} | ||
// Return the ctx or a function wrapping the application of the context. | ||
if (typeof ctx === 'function') { | ||
var fn = function() { | ||
return ctx.apply(ctxThis, arguments); | ||
}; | ||
fn.isFunction = true; | ||
return fn; | ||
} else { | ||
if (ctx === undefined) { | ||
dust.log('Cannot find the value for reference [{' + down.join('.') + '}] in template [' + this.templateName + ']'); | ||
} | ||
return ctx; | ||
} | ||
} | ||
if (typeof ctx == 'function'){ | ||
//wrap to preserve context 'this' see #174 | ||
return function(){ | ||
return ctx.apply(ctxThis,arguments); | ||
}; | ||
} | ||
else { | ||
return ctx; | ||
} | ||
}; | ||
}; | ||
Context.prototype.push = function(head, idx, len) { | ||
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks, this.templateName); | ||
}; | ||
Context.prototype.getPath = function(cur, down) { | ||
return this._get(cur, down); | ||
}; | ||
Context.prototype.rebase = function(head) { | ||
return new Context(new Stack(head), this.global, this.blocks, this.templateName); | ||
}; | ||
Context.prototype.push = function(head, idx, len) { | ||
return new Context(new Stack(head, this.stack, idx, len), this.global, this.blocks, this.templateName); | ||
}; | ||
Context.prototype.current = function() { | ||
return this.stack.head; | ||
}; | ||
Context.prototype.rebase = function(head) { | ||
return new Context(new Stack(head), this.global, this.blocks, this.templateName); | ||
}; | ||
Context.prototype.getBlock = function(key, chk, ctx) { | ||
if (typeof key === "function") { | ||
var tempChk = new Chunk(); | ||
key = key(tempChk, this).data.join(""); | ||
} | ||
Context.prototype.current = function() { | ||
return this.stack.head; | ||
}; | ||
var blocks = this.blocks; | ||
Context.prototype.getBlock = function(key, chk, ctx) { | ||
if (typeof key === 'function') { | ||
var tempChk = new Chunk(); | ||
key = key(tempChk, this).data.join(''); | ||
} | ||
if (!blocks) { | ||
dust.log('No blocks for context[{' + key + '}] in template [' + this.templateName + ']', DEBUG); | ||
return; | ||
} | ||
var len = blocks.length, fn; | ||
while (len--) { | ||
fn = blocks[len][key]; | ||
if (fn) return fn; | ||
} | ||
}; | ||
var blocks = this.blocks; | ||
Context.prototype.shiftBlocks = function(locals) { | ||
var blocks = this.blocks, | ||
newBlocks; | ||
if (locals) { | ||
if (!blocks) { | ||
newBlocks = [locals]; | ||
} else { | ||
newBlocks = blocks.concat([locals]); | ||
dust.log('No blocks for context[{' + key + '}] in template [' + this.templateName + ']', DEBUG); | ||
return; | ||
} | ||
return new Context(this.stack, this.global, newBlocks, this.templateName); | ||
} | ||
return this; | ||
}; | ||
var len = blocks.length, fn; | ||
while (len--) { | ||
fn = blocks[len][key]; | ||
if (fn) { | ||
return fn; | ||
} | ||
} | ||
}; | ||
function Stack(head, tail, idx, len) { | ||
this.tail = tail; | ||
this.isObject = !dust.isArray(head) && head && typeof head === "object"; | ||
this.head = head; | ||
this.index = idx; | ||
this.of = len; | ||
} | ||
Context.prototype.shiftBlocks = function(locals) { | ||
var blocks = this.blocks, | ||
newBlocks; | ||
function Stub(callback) { | ||
this.head = new Chunk(this); | ||
this.callback = callback; | ||
this.out = ''; | ||
} | ||
if (locals) { | ||
if (!blocks) { | ||
newBlocks = [locals]; | ||
} else { | ||
newBlocks = blocks.concat([locals]); | ||
} | ||
return new Context(this.stack, this.global, newBlocks, this.templateName); | ||
} | ||
return this; | ||
}; | ||
Stub.prototype.flush = function() { | ||
var chunk = this.head; | ||
function Stack(head, tail, idx, len) { | ||
this.tail = tail; | ||
this.isObject = !dust.isArray(head) && head && typeof head === 'object'; | ||
this.head = head; | ||
this.index = idx; | ||
this.of = len; | ||
} | ||
while (chunk) { | ||
if (chunk.flushable) { | ||
this.out += chunk.data.join(""); //ie7 perf | ||
} else if (chunk.error) { | ||
this.callback(chunk.error); | ||
dust.onError(new Error('Chunk error [' + chunk.error + '] thrown. Ceasing to render this template.')); | ||
this.flush = function() {}; | ||
return; | ||
} else { | ||
return; | ||
} | ||
chunk = chunk.next; | ||
this.head = chunk; | ||
function Stub(callback) { | ||
this.head = new Chunk(this); | ||
this.callback = callback; | ||
this.out = ''; | ||
} | ||
this.callback(null, this.out); | ||
}; | ||
function Stream() { | ||
this.head = new Chunk(this); | ||
} | ||
Stub.prototype.flush = function() { | ||
var chunk = this.head; | ||
Stream.prototype.flush = function() { | ||
var chunk = this.head; | ||
while (chunk) { | ||
if (chunk.flushable) { | ||
this.out += chunk.data.join(''); //ie7 perf | ||
} else if (chunk.error) { | ||
this.callback(chunk.error); | ||
dust.onError(new Error('Chunk error [' + chunk.error + '] thrown. Ceasing to render this template.')); | ||
this.flush = function() {}; | ||
return; | ||
} else { | ||
return; | ||
} | ||
chunk = chunk.next; | ||
this.head = chunk; | ||
} | ||
this.callback(null, this.out); | ||
}; | ||
while(chunk) { | ||
if (chunk.flushable) { | ||
this.emit('data', chunk.data.join("")); //ie7 perf | ||
} else if (chunk.error) { | ||
this.emit('error', chunk.error); | ||
dust.onError(new Error('Chunk error [' + chunk.error + '] thrown. Ceasing to render this template.')); | ||
this.flush = function() {}; | ||
return; | ||
} else { | ||
return; | ||
} | ||
chunk = chunk.next; | ||
this.head = chunk; | ||
function Stream() { | ||
this.head = new Chunk(this); | ||
} | ||
this.emit('end'); | ||
}; | ||
Stream.prototype.emit = function(type, data) { | ||
if (!this.events) { | ||
dust.log('No events to emit', INFO); | ||
return false; | ||
} | ||
var handler = this.events[type]; | ||
if (!handler) { | ||
dust.log('Event type [' + type + '] does not exist', WARN); | ||
return false; | ||
} | ||
if (typeof handler === 'function') { | ||
handler(data); | ||
} else if (dust.isArray(handler)) { | ||
var listeners = handler.slice(0); | ||
for (var i = 0, l = listeners.length; i < l; i++) { | ||
listeners[i](data); | ||
Stream.prototype.flush = function() { | ||
var chunk = this.head; | ||
while(chunk) { | ||
if (chunk.flushable) { | ||
this.emit('data', chunk.data.join('')); //ie7 perf | ||
} else if (chunk.error) { | ||
this.emit('error', chunk.error); | ||
dust.onError(new Error('Chunk error [' + chunk.error + '] thrown. Ceasing to render this template.')); | ||
this.flush = function() {}; | ||
return; | ||
} else { | ||
return; | ||
} | ||
chunk = chunk.next; | ||
this.head = chunk; | ||
} | ||
} else { | ||
dust.onError(new Error('Event Handler [' + handler + '] is not of a type that is handled by emit')); | ||
} | ||
}; | ||
this.emit('end'); | ||
}; | ||
Stream.prototype.on = function(type, callback) { | ||
if (!this.events) { | ||
this.events = {}; | ||
} | ||
if (!this.events[type]) { | ||
dust.log('Event type [' + type + '] does not exist. Using just the specified callback.', WARN); | ||
if(callback) { | ||
this.events[type] = callback; | ||
Stream.prototype.emit = function(type, data) { | ||
if (!this.events) { | ||
dust.log('No events to emit', INFO); | ||
return false; | ||
} | ||
var handler = this.events[type]; | ||
if (!handler) { | ||
dust.log('Event type [' + type + '] does not exist', WARN); | ||
return false; | ||
} | ||
if (typeof handler === 'function') { | ||
handler(data); | ||
} else if (dust.isArray(handler)) { | ||
var listeners = handler.slice(0); | ||
for (var i = 0, l = listeners.length; i < l; i++) { | ||
listeners[i](data); | ||
} | ||
} else { | ||
dust.log('Callback for type [' + type + '] does not exist. Listener not registered.', WARN); | ||
dust.onError(new Error('Event Handler [' + handler + '] is not of a type that is handled by emit')); | ||
} | ||
} else if(typeof this.events[type] === 'function') { | ||
this.events[type] = [this.events[type], callback]; | ||
} else { | ||
this.events[type].push(callback); | ||
} | ||
return this; | ||
}; | ||
}; | ||
Stream.prototype.pipe = function(stream) { | ||
this.on("data", function(data) { | ||
try { | ||
stream.write(data, "utf8"); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
Stream.prototype.on = function(type, callback) { | ||
if (!this.events) { | ||
this.events = {}; | ||
} | ||
}).on("end", function() { | ||
try { | ||
return stream.end(); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
if (!this.events[type]) { | ||
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') { | ||
this.events[type] = [this.events[type], callback]; | ||
} else { | ||
this.events[type].push(callback); | ||
} | ||
}).on("error", function(err) { | ||
stream.error(err); | ||
}); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
function Chunk(root, next, taps) { | ||
this.root = root; | ||
this.next = next; | ||
this.data = []; //ie7 perf | ||
this.flushable = false; | ||
this.taps = taps; | ||
} | ||
Stream.prototype.pipe = function(stream) { | ||
this.on('data', function(data) { | ||
try { | ||
stream.write(data, 'utf8'); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
} | ||
}).on('end', function() { | ||
try { | ||
return stream.end(); | ||
} catch (err) { | ||
dust.onError(err, stream.head); | ||
} | ||
}).on('error', function(err) { | ||
stream.error(err); | ||
}); | ||
return this; | ||
}; | ||
Chunk.prototype.write = function(data) { | ||
var taps = this.taps; | ||
if (taps) { | ||
data = taps.go(data); | ||
function Chunk(root, next, taps) { | ||
this.root = root; | ||
this.next = next; | ||
this.data = []; //ie7 perf | ||
this.flushable = false; | ||
this.taps = taps; | ||
} | ||
this.data.push(data); | ||
return this; | ||
}; | ||
Chunk.prototype.end = function(data) { | ||
if (data) { | ||
this.write(data); | ||
} | ||
this.flushable = true; | ||
this.root.flush(); | ||
return this; | ||
}; | ||
Chunk.prototype.write = function(data) { | ||
var taps = this.taps; | ||
Chunk.prototype.map = function(callback) { | ||
var cursor = new Chunk(this.root, this.next, this.taps), | ||
branch = new Chunk(this.root, cursor, this.taps); | ||
if (taps) { | ||
data = taps.go(data); | ||
} | ||
this.data.push(data); | ||
return this; | ||
}; | ||
this.next = branch; | ||
this.flushable = true; | ||
callback(branch); | ||
return cursor; | ||
}; | ||
Chunk.prototype.end = function(data) { | ||
if (data) { | ||
this.write(data); | ||
} | ||
this.flushable = true; | ||
this.root.flush(); | ||
return this; | ||
}; | ||
Chunk.prototype.tap = function(tap) { | ||
var taps = this.taps; | ||
Chunk.prototype.map = function(callback) { | ||
var cursor = new Chunk(this.root, this.next, this.taps), | ||
branch = new Chunk(this.root, cursor, this.taps); | ||
if (taps) { | ||
this.taps = taps.push(tap); | ||
} else { | ||
this.taps = new Tap(tap); | ||
} | ||
return this; | ||
}; | ||
this.next = branch; | ||
this.flushable = true; | ||
callback(branch); | ||
return cursor; | ||
}; | ||
Chunk.prototype.untap = function() { | ||
this.taps = this.taps.tail; | ||
return this; | ||
}; | ||
Chunk.prototype.tap = function(tap) { | ||
var taps = this.taps; | ||
Chunk.prototype.render = function(body, context) { | ||
return body(this, context); | ||
}; | ||
Chunk.prototype.reference = function(elem, context, auto, filters) { | ||
if (typeof elem === "function") { | ||
elem.isFunction = true; | ||
// 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 | ||
elem = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]); | ||
if (elem instanceof Chunk) { | ||
return elem; | ||
if (taps) { | ||
this.taps = taps.push(tap); | ||
} else { | ||
this.taps = new Tap(tap); | ||
} | ||
} | ||
if (!dust.isEmpty(elem)) { | ||
return this.write(dust.filter(elem, auto, filters)); | ||
} else { | ||
return this; | ||
} | ||
}; | ||
}; | ||
Chunk.prototype.section = function(elem, context, bodies, params) { | ||
// anonymous functions | ||
if (typeof elem === "function") { | ||
elem = elem.apply(context.current(), [this, context, bodies, params]); | ||
// functions that return chunks are assumed to have handled the body and/or have modified the chunk | ||
// use that return value as the current chunk and go to the next method in the chain | ||
if (elem instanceof Chunk) { | ||
return elem; | ||
Chunk.prototype.untap = function() { | ||
this.taps = this.taps.tail; | ||
return this; | ||
}; | ||
Chunk.prototype.render = function(body, context) { | ||
return body(this, context); | ||
}; | ||
Chunk.prototype.reference = function(elem, context, auto, filters) { | ||
if (typeof elem === 'function') { | ||
elem.isFunction = true; | ||
// 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 | ||
elem = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]); | ||
if (elem instanceof Chunk) { | ||
return elem; | ||
} | ||
} | ||
} | ||
var body = bodies.block, | ||
skip = bodies['else']; | ||
if (!dust.isEmpty(elem)) { | ||
return this.write(dust.filter(elem, auto, filters)); | ||
} else { | ||
return this; | ||
} | ||
}; | ||
// a.k.a Inline parameters in the Dust documentations | ||
if (params) { | ||
context = context.push(params); | ||
} | ||
Chunk.prototype.section = function(elem, context, bodies, params) { | ||
// anonymous functions | ||
if (typeof elem === 'function') { | ||
elem = elem.apply(context.current(), [this, context, bodies, params]); | ||
// functions that return chunks are assumed to have handled the body and/or have modified the chunk | ||
// use that return value as the current chunk and go to the next method in the chain | ||
if (elem instanceof Chunk) { | ||
return elem; | ||
} | ||
} | ||
var body = bodies.block, | ||
skip = bodies['else']; | ||
/* | ||
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 | ||
and renders the block one time. | ||
*/ | ||
//non empty array is truthy, empty array is falsy | ||
if (dust.isArray(elem)) { | ||
if (body) { | ||
var len = elem.length, chunk = this; | ||
if (len > 0) { | ||
// any custom helper can blow up the stack | ||
// and store a flattened context, guard defensively | ||
if(context.stack.head) { | ||
context.stack.head['$len'] = len; | ||
} | ||
for (var i=0; i<len; i++) { | ||
// a.k.a Inline parameters in the Dust documentations | ||
if (params) { | ||
context = context.push(params); | ||
} | ||
/* | ||
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 | ||
and renders the block one time. | ||
*/ | ||
//non empty array is truthy, empty array is falsy | ||
if (dust.isArray(elem)) { | ||
if (body) { | ||
var len = elem.length, chunk = this; | ||
if (len > 0) { | ||
// any custom helper can blow up the stack | ||
// and store a flattened context, guard defensively | ||
if(context.stack.head) { | ||
context.stack.head['$idx'] = i; | ||
context.stack.head['$len'] = len; | ||
} | ||
chunk = body(chunk, context.push(elem[i], i, len)); | ||
for (var i=0; i<len; i++) { | ||
if(context.stack.head) { | ||
context.stack.head['$idx'] = i; | ||
} | ||
chunk = body(chunk, context.push(elem[i], i, len)); | ||
} | ||
if(context.stack.head) { | ||
context.stack.head['$idx'] = undefined; | ||
context.stack.head['$len'] = undefined; | ||
} | ||
return chunk; | ||
} | ||
if(context.stack.head) { | ||
context.stack.head['$idx'] = undefined; | ||
context.stack.head['$len'] = undefined; | ||
else if (skip) { | ||
return skip(this, context); | ||
} | ||
return chunk; | ||
} | ||
else if (skip) { | ||
return skip(this, context); | ||
} else if (elem === true) { | ||
// true is truthy but does not change context | ||
if (body) { | ||
return body(this, context); | ||
} | ||
} | ||
} | ||
// true is truthy but does not change context | ||
else if (elem === true) { | ||
if (body) { | ||
return body(this, context); | ||
} | ||
} | ||
// everything that evaluates to true are truthy ( e.g. Non-empty strings and Empty objects are truthy. ) | ||
// zero is truthy | ||
// for anonymous functions that did not returns a chunk, truthiness is evaluated based on the return value | ||
// | ||
else if (elem || elem === 0) { | ||
if (body) return body(this, context.push(elem)); | ||
// nonexistent, scalar false value, scalar empty string, null, | ||
// undefined are all falsy | ||
} else if (skip) { | ||
return skip(this, context); | ||
} | ||
dust.log('Not rendering section (#) block in template [' + context.templateName + '], because above key was not found', DEBUG); | ||
return this; | ||
}; | ||
} else if (elem || elem === 0) { | ||
// everything that evaluates to true are truthy ( e.g. Non-empty strings and Empty objects are truthy. ) | ||
// zero is truthy | ||
// for anonymous functions that did not returns a chunk, truthiness is evaluated based on the return value | ||
if (body) { | ||
return body(this, context.push(elem)); | ||
} | ||
// nonexistent, scalar false value, scalar empty string, null, | ||
// undefined are all falsy | ||
} else if (skip) { | ||
return skip(this, context); | ||
} | ||
dust.log('Not rendering section (#) block in template [' + context.templateName + '], because above key was not found', DEBUG); | ||
return this; | ||
}; | ||
Chunk.prototype.exists = function(elem, context, bodies) { | ||
var body = bodies.block, | ||
skip = bodies['else']; | ||
Chunk.prototype.exists = function(elem, context, bodies) { | ||
var body = bodies.block, | ||
skip = bodies['else']; | ||
if (!dust.isEmpty(elem)) { | ||
if (body) return body(this, context); | ||
} else if (skip) { | ||
return skip(this, context); | ||
} | ||
dust.log('Not rendering exists (?) block in template [' + context.templateName + '], because above key was not found', DEBUG); | ||
return this; | ||
}; | ||
if (!dust.isEmpty(elem)) { | ||
if (body) { | ||
return body(this, context); | ||
} | ||
} else if (skip) { | ||
return skip(this, context); | ||
} | ||
dust.log('Not rendering exists (?) block in template [' + context.templateName + '], because above key was not found', DEBUG); | ||
return this; | ||
}; | ||
Chunk.prototype.notexists = function(elem, context, bodies) { | ||
var body = bodies.block, | ||
skip = bodies['else']; | ||
Chunk.prototype.notexists = function(elem, context, bodies) { | ||
var body = bodies.block, | ||
skip = bodies['else']; | ||
if (dust.isEmpty(elem)) { | ||
if (body) return body(this, context); | ||
} else if (skip) { | ||
return skip(this, context); | ||
} | ||
dust.log('Not rendering not exists (^) block check in template [' + context.templateName + '], because above key was found', DEBUG); | ||
return this; | ||
}; | ||
if (dust.isEmpty(elem)) { | ||
if (body) { | ||
return body(this, context); | ||
} | ||
} else if (skip) { | ||
return skip(this, context); | ||
} | ||
dust.log('Not rendering not exists (^) block check in template [' + context.templateName + '], because above key was found', DEBUG); | ||
return this; | ||
}; | ||
Chunk.prototype.block = function(elem, context, bodies) { | ||
var body = bodies.block; | ||
Chunk.prototype.block = function(elem, context, bodies) { | ||
var body = bodies.block; | ||
if (elem) { | ||
body = elem; | ||
} | ||
if (elem) { | ||
body = elem; | ||
} | ||
if (body) { | ||
return body(this, context); | ||
} | ||
return this; | ||
}; | ||
if (body) { | ||
return body(this, context); | ||
} | ||
return this; | ||
}; | ||
Chunk.prototype.partial = function(elem, context, params) { | ||
var partialContext; | ||
//put the params context second to match what section does. {.} matches the current context without parameters | ||
// start with an empty context | ||
partialContext = dust.makeBase(context.global); | ||
partialContext.blocks = context.blocks; | ||
if (context.stack && context.stack.tail){ | ||
// grab the stack(tail) off of the previous context if we have it | ||
partialContext.stack = context.stack.tail; | ||
} | ||
if (params){ | ||
//put params on | ||
partialContext = partialContext.push(params); | ||
} | ||
// templateName can be static (string) or dynamic (function) | ||
// e.g. {>"static_template"/} | ||
// {>"{dynamic_template}"/} | ||
if (elem) { | ||
partialContext.templateName = elem; | ||
} | ||
Chunk.prototype.partial = function(elem, context, params) { | ||
var partialContext; | ||
//put the params context second to match what section does. {.} matches the current context without parameters | ||
// start with an empty context | ||
partialContext = dust.makeBase(context.global); | ||
partialContext.blocks = context.blocks; | ||
if (context.stack && context.stack.tail){ | ||
// grab the stack(tail) off of the previous context if we have it | ||
partialContext.stack = context.stack.tail; | ||
} | ||
if (params){ | ||
//put params on | ||
partialContext = partialContext.push(params); | ||
} | ||
//reattach the head | ||
partialContext = partialContext.push(context.stack.head); | ||
if(typeof elem === 'string') { | ||
partialContext.templateName = elem; | ||
} | ||
var partialChunk; | ||
if (typeof elem === "function") { | ||
partialChunk = this.capture(elem, partialContext, function(name, chunk) { | ||
dust.load(name, chunk, partialContext).end(); | ||
}); | ||
} | ||
else { | ||
partialChunk = dust.load(elem, this, partialContext); | ||
} | ||
return partialChunk; | ||
}; | ||
//reattach the head | ||
partialContext = partialContext.push(context.stack.head); | ||
Chunk.prototype.helper = function(name, context, bodies, params) { | ||
var chunk = this; | ||
// handle invalid helpers, similar to invalid filters | ||
try { | ||
if(dust.helpers[name]) { | ||
return dust.helpers[name](chunk, context, bodies, params); | ||
var partialChunk; | ||
if (typeof elem === 'function') { | ||
partialChunk = this.capture(elem, partialContext, function(name, chunk) { | ||
dust.load(name, chunk, partialContext).end(); | ||
}); | ||
} else { | ||
return dust.onError(new Error('Invalid helper [' + name + ']'), chunk); | ||
partialChunk = dust.load(elem, this, partialContext); | ||
} | ||
} catch (err) { | ||
return dust.onError(err, chunk); | ||
} | ||
}; | ||
return partialChunk; | ||
}; | ||
Chunk.prototype.capture = function(body, context, callback) { | ||
return this.map(function(chunk) { | ||
var stub = new Stub(function(err, out) { | ||
if (err) { | ||
chunk.setError(err); | ||
Chunk.prototype.helper = function(name, context, bodies, params) { | ||
var chunk = this; | ||
// handle invalid helpers, similar to invalid filters | ||
try { | ||
if(dust.helpers[name]) { | ||
return dust.helpers[name](chunk, context, bodies, params); | ||
} else { | ||
callback(out, chunk); | ||
return dust.onError(new Error('Invalid helper [' + name + ']'), chunk); | ||
} | ||
} catch (err) { | ||
return dust.onError(err, chunk); | ||
} | ||
}; | ||
Chunk.prototype.capture = function(body, context, callback) { | ||
return this.map(function(chunk) { | ||
var stub = new Stub(function(err, out) { | ||
if (err) { | ||
chunk.setError(err); | ||
} else { | ||
callback(out, chunk); | ||
} | ||
}); | ||
body(stub.head, context).end(); | ||
}); | ||
body(stub.head, context).end(); | ||
}); | ||
}; | ||
}; | ||
Chunk.prototype.setError = function(err) { | ||
this.error = err; | ||
this.root.flush(); | ||
return this; | ||
}; | ||
Chunk.prototype.setError = function(err) { | ||
this.error = err; | ||
this.root.flush(); | ||
return this; | ||
}; | ||
function Tap(head, tail) { | ||
this.head = head; | ||
this.tail = tail; | ||
} | ||
function Tap(head, tail) { | ||
this.head = head; | ||
this.tail = tail; | ||
} | ||
Tap.prototype.push = function(tap) { | ||
return new Tap(tap, this); | ||
}; | ||
Tap.prototype.push = function(tap) { | ||
return new Tap(tap, this); | ||
}; | ||
Tap.prototype.go = function(value) { | ||
var tap = this; | ||
Tap.prototype.go = function(value) { | ||
var tap = this; | ||
while(tap) { | ||
value = tap.head(value); | ||
tap = tap.tail; | ||
} | ||
return value; | ||
}; | ||
while(tap) { | ||
value = tap.head(value); | ||
tap = tap.tail; | ||
} | ||
return value; | ||
}; | ||
var HCHARS = new RegExp(/[&<>\"\']/), | ||
AMP = /&/g, | ||
LT = /</g, | ||
GT = />/g, | ||
QUOT = /\"/g, | ||
SQUOT = /\'/g; | ||
var HCHARS = new RegExp(/[&<>\"\']/), | ||
AMP = /&/g, | ||
LT = /</g, | ||
GT = />/g, | ||
QUOT = /\"/g, | ||
SQUOT = /\'/g; | ||
dust.escapeHtml = function(s) { | ||
if (typeof s === "string") { | ||
if (!HCHARS.test(s)) { | ||
return s; | ||
dust.escapeHtml = function(s) { | ||
if (typeof s === 'string') { | ||
if (!HCHARS.test(s)) { | ||
return s; | ||
} | ||
return s.replace(AMP,'&').replace(LT,'<').replace(GT,'>').replace(QUOT,'"').replace(SQUOT, '''); | ||
} | ||
return s.replace(AMP,'&').replace(LT,'<').replace(GT,'>').replace(QUOT,'"').replace(SQUOT, '''); | ||
} | ||
return s; | ||
}; | ||
return s; | ||
}; | ||
var BS = /\\/g, | ||
FS = /\//g, | ||
CR = /\r/g, | ||
LS = /\u2028/g, | ||
PS = /\u2029/g, | ||
NL = /\n/g, | ||
LF = /\f/g, | ||
SQ = /'/g, | ||
DQ = /"/g, | ||
TB = /\t/g; | ||
var BS = /\\/g, | ||
FS = /\//g, | ||
CR = /\r/g, | ||
LS = /\u2028/g, | ||
PS = /\u2029/g, | ||
NL = /\n/g, | ||
LF = /\f/g, | ||
SQ = /'/g, | ||
DQ = /"/g, | ||
TB = /\t/g; | ||
dust.escapeJs = function(s) { | ||
if (typeof s === "string") { | ||
return s | ||
.replace(BS, '\\\\') | ||
.replace(FS, '\\/') | ||
.replace(DQ, '\\"') | ||
.replace(SQ, "\\'") | ||
.replace(CR, '\\r') | ||
.replace(LS, '\\u2028') | ||
.replace(PS, '\\u2029') | ||
.replace(NL, '\\n') | ||
.replace(LF, '\\f') | ||
.replace(TB, "\\t"); | ||
} | ||
return s; | ||
}; | ||
dust.escapeJs = function(s) { | ||
if (typeof s === 'string') { | ||
return s | ||
.replace(BS, '\\\\') | ||
.replace(FS, '\\/') | ||
.replace(DQ, '\\"') | ||
.replace(SQ, '\\\'') | ||
.replace(CR, '\\r') | ||
.replace(LS, '\\u2028') | ||
.replace(PS, '\\u2029') | ||
.replace(NL, '\\n') | ||
.replace(LF, '\\f') | ||
.replace(TB, '\\t'); | ||
} | ||
return s; | ||
}; | ||
})(dust); | ||
if (typeof exports !== "undefined") { | ||
if (typeof process !== "undefined") { | ||
require('./server')(dust); | ||
if (typeof exports !== 'undefined') { | ||
if (typeof process !== 'undefined') { | ||
require('./server')(dust); | ||
} | ||
module.exports = dust; | ||
} |
{ | ||
"name": "dustjs-linkedin", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"author": "Aleksander Williams", | ||
@@ -5,0 +5,0 @@ "description": "Asynchronous templates for the browser and node.js ( LinkedIn fork )", |
@@ -1,8 +0,8 @@ | ||
describe ("Test the basic functionality of dust", function() { | ||
describe ('Test the basic functionality of dust', function() { | ||
for (var index = 0; index < coreTests.length; index++) { | ||
for (var i = 0; i < coreTests[index].tests.length; i++) { | ||
var test = coreTests[index].tests[i]; | ||
it ("RENDER: " + test.message, render(test)); | ||
it ("STREAM: " + test.message, stream(test)); | ||
it ("PIPE: " + test.message, pipe(test)); | ||
it ('RENDER: ' + test.message, render(test)); | ||
it ('STREAM: ' + test.message, stream(test)); | ||
it ('PIPE: ' + test.message, pipe(test)); | ||
} | ||
@@ -49,3 +49,3 @@ }; | ||
return function() { | ||
var output = "", | ||
var output = '', | ||
messageInLog = false, | ||
@@ -57,3 +57,3 @@ log, | ||
flag = false; | ||
output = ""; | ||
output = ''; | ||
log = []; | ||
@@ -70,3 +70,3 @@ try { | ||
dust.nextTick = (function() { | ||
if (typeof process !== "undefined") { | ||
if (typeof process !== 'undefined') { | ||
return function(callback) { | ||
@@ -84,3 +84,10 @@ process.nextTick(function() { | ||
return function(callback) { | ||
setTimeout(callback,0); | ||
setTimeout(function() { | ||
try { | ||
callback(); | ||
} catch(error) { | ||
output = error.message; | ||
flag = true; | ||
} | ||
},0); | ||
}; | ||
@@ -90,11 +97,11 @@ } | ||
dust.stream(test.name, context) | ||
.on("data", function(data) { | ||
.on('data', function(data) { | ||
output += data; | ||
log = dust.logQueue; | ||
}) | ||
.on("end", function() { | ||
.on('end', function() { | ||
flag = true; | ||
log = dust.logQueue; | ||
}) | ||
.on("error", function(err) { | ||
.on('error', function(err) { | ||
output = err.message; | ||
@@ -108,7 +115,7 @@ log = dust.logQueue; | ||
}); | ||
waitsFor(function(){ | ||
return flag; | ||
}, "the output", 500); | ||
}, 'the output', 500); | ||
runs(function(){ | ||
@@ -139,4 +146,4 @@ if (test.error) { | ||
flagTwo = false; | ||
output = ""; | ||
outputTwo = ""; | ||
output = ''; | ||
outputTwo = ''; | ||
log = []; | ||
@@ -156,3 +163,3 @@ logTwo = []; | ||
dust.nextTick = (function() { | ||
if (typeof process !== "undefined") { | ||
if (typeof process !== 'undefined') { | ||
return function(callback) { | ||
@@ -172,3 +179,12 @@ process.nextTick(function() { | ||
return function(callback) { | ||
setTimeout(callback,0); | ||
setTimeout(function() { | ||
try { | ||
callback(); | ||
} catch(error) { | ||
output = error.message; | ||
outputTwo = error.message; | ||
flag = true; | ||
flagTwo = true; | ||
} | ||
}, 0); | ||
}; | ||
@@ -216,7 +232,7 @@ } | ||
}); | ||
waitsFor(function(){ | ||
return flag && flagTwo; | ||
}, "the output", 500); | ||
}, 'the output', 500); | ||
runs(function(){ | ||
@@ -226,3 +242,3 @@ if (test.error) { | ||
expect(test.error || {} ).toEqual(outputTwo); | ||
} else if (test.log) { | ||
} else if (test.log) { | ||
for(var i=0; i<log.length; i++) { | ||
@@ -229,0 +245,0 @@ if(log[i].message === test.log) { |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
2537515
96
69310
35