This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
SA3/hw7/Claudio_Maggioni/public/dustjs-linkedin/dust-core.js

1158 lines
31 KiB
JavaScript

/*! dustjs-linkedin - v2.7.5
* http://dustjs.com/
* Copyright (c) 2016 Aleksander Williams; Released under the MIT License */
(function (root, factory) {
if (typeof define === 'function' && define.amd && define.amd.dust === true) {
define('dust.core', [], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.dust = factory();
}
}(this, function() {
var dust = {
"version": "2.7.5"
},
NONE = 'NONE', ERROR = 'ERROR', WARN = 'WARN', INFO = 'INFO', DEBUG = 'DEBUG',
EMPTY_FUNC = function() {};
dust.config = {
whitespace: false,
amd: false,
cjs: false,
cache: true
};
// Directive aliases to minify code
dust._aliases = {
"write": "w",
"end": "e",
"map": "m",
"render": "r",
"reference": "f",
"section": "s",
"exists": "x",
"notexists": "nx",
"block": "b",
"partial": "p",
"helper": "h"
};
(function initLogging() {
/*global process, console*/
var loggingLevels = { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, NONE: 4 },
consoleLog,
log;
if (typeof console !== 'undefined' && console.log) {
consoleLog = console.log;
if(typeof consoleLog === 'function') {
log = function() {
consoleLog.apply(console, arguments);
};
} else {
log = function() {
consoleLog(Array.prototype.slice.apply(arguments).join(' '));
};
}
} else {
log = EMPTY_FUNC;
}
/**
* Filters messages based on `dust.debugLevel`.
* This default implementation will print to the console if it exists.
* @param {String|Error} message the message to print/throw
* @param {String} type the severity of the message(ERROR, WARN, INFO, or DEBUG)
* @public
*/
dust.log = function(message, type) {
type = type || INFO;
if (loggingLevels[type] >= loggingLevels[dust.debugLevel]) {
log('[DUST:' + type + ']', message);
}
};
dust.debugLevel = NONE;
if(typeof process !== 'undefined' && process.env && /\bdust\b/.test(process.env.DEBUG)) {
dust.debugLevel = DEBUG;
}
}());
dust.helpers = {};
dust.cache = {};
dust.register = function(name, tmpl) {
if (!name) {
return;
}
tmpl.templateName = name;
if (dust.config.cache !== false) {
dust.cache[name] = tmpl;
}
};
dust.render = function(nameOrTemplate, context, callback) {
var chunk = new Stub(callback).head;
try {
load(nameOrTemplate, chunk, context).end();
} catch (err) {
chunk.setError(err);
}
};
dust.stream = function(nameOrTemplate, context) {
var stream = new Stream(),
chunk = stream.head;
dust.nextTick(function() {
try {
load(nameOrTemplate, chunk, context).end();
} catch (err) {
chunk.setError(err);
}
});
return stream;
};
/**
* Extracts a template function (body_0) from whatever is passed.
* @param nameOrTemplate {*} Could be:
* - the name of a template to load from cache
* - a CommonJS-compiled template (a function with a `template` property)
* - a template function
* @param loadFromCache {Boolean} if false, don't look in the cache
* @return {Function} a template function, if found
*/
function getTemplate(nameOrTemplate, loadFromCache/*=true*/) {
if(!nameOrTemplate) {
return;
}
if(typeof nameOrTemplate === 'function' && nameOrTemplate.template) {
// Sugar away CommonJS module templates
return nameOrTemplate.template;
}
if(dust.isTemplateFn(nameOrTemplate)) {
// Template functions passed directly
return nameOrTemplate;
}
if(loadFromCache !== false) {
// Try loading a template with this name from cache
return dust.cache[nameOrTemplate];
}
}
function load(nameOrTemplate, chunk, context) {
if(!nameOrTemplate) {
return chunk.setError(new Error('No template or template name provided to render'));
}
var template = getTemplate(nameOrTemplate, dust.config.cache);
if (template) {
return template(chunk, Context.wrap(context, template.templateName));
} else {
if (dust.onLoad) {
return chunk.map(function(chunk) {
// Alias just so it's easier to read that this would always be a name
var name = nameOrTemplate;
// Three possible scenarios for a successful callback:
// - `require(nameOrTemplate)(dust); cb()`
// - `src = readFile('src.dust'); cb(null, src)`
// - `compiledTemplate = require(nameOrTemplate)(dust); cb(null, compiledTemplate)`
function done(err, srcOrTemplate) {
var template;
if (err) {
return chunk.setError(err);
}
// Prefer a template that is passed via callback over the cached version.
template = getTemplate(srcOrTemplate, false) || getTemplate(name, dust.config.cache);
if (!template) {
// It's a template string, compile it and register under `name`
if(dust.compile) {
template = dust.loadSource(dust.compile(srcOrTemplate, name));
} else {
return chunk.setError(new Error('Dust compiler not available'));
}
}
template(chunk, Context.wrap(context, template.templateName)).end();
}
if(dust.onLoad.length === 3) {
dust.onLoad(name, context.options, done);
} else {
dust.onLoad(name, done);
}
});
}
return chunk.setError(new Error('Template Not Found: ' + nameOrTemplate));
}
}
dust.loadSource = function(source) {
/*jshint evil:true*/
return eval(source);
};
if (Array.isArray) {
dust.isArray = Array.isArray;
} else {
dust.isArray = function(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
};
}
dust.nextTick = (function() {
return function(callback) {
setTimeout(callback, 0);
};
})();
/**
* Dust has its own rules for what is "empty"-- which is not the same as falsy.
* Empty arrays, null, and undefined are empty
*/
dust.isEmpty = function(value) {
if (value === 0) {
return false;
}
if (dust.isArray(value) && !value.length) {
return true;
}
return !value;
};
dust.isEmptyObject = function(obj) {
var key;
if (obj === null) {
return false;
}
if (obj === undefined) {
return false;
}
if (obj.length > 0) {
return false;
}
for (key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
};
dust.isTemplateFn = function(elem) {
return typeof elem === 'function' &&
elem.__dustBody;
};
/**
* Decide somewhat-naively if something is a Thenable.
* @param elem {*} object to inspect
* @return {Boolean} is `elem` a Thenable?
*/
dust.isThenable = function(elem) {
return elem &&
typeof elem === 'object' &&
typeof elem.then === 'function';
};
/**
* Decide very naively if something is a Stream.
* @param elem {*} object to inspect
* @return {Boolean} is `elem` a Stream?
*/
dust.isStreamable = function(elem) {
return elem &&
typeof elem.on === 'function' &&
typeof elem.pipe === 'function';
};
// apply the filter chain and return the output string
dust.filter = function(string, auto, filters, context) {
var i, len, name, filter;
if (filters) {
for (i = 0, len = filters.length; i < len; i++) {
name = filters[i];
if (!name.length) {
continue;
}
filter = dust.filters[name];
if (name === 's') {
auto = null;
} else if (typeof filter === 'function') {
string = filter(string, context);
} else {
dust.log('Invalid filter `' + name + '`', WARN);
}
}
}
// by default always apply the h filter, unless asked to unescape with |s
if (auto) {
string = dust.filters[auto](string, context);
}
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) { return dust.escapeJSON(value); },
jp: function(value) {
if (!JSON) {dust.log('JSON is undefined; could not parse `' + value + '`', WARN);
return value;
} else {
return JSON.parse(value);
}
}
};
function Context(stack, global, options, blocks, templateName) {
if(stack !== undefined && !(stack instanceof Stack)) {
stack = new Stack(stack);
}
this.stack = stack;
this.global = global;
this.options = options;
this.blocks = blocks;
this.templateName = templateName;
this._isContext = true;
}
dust.makeBase = dust.context = function(global, options) {
return new Context(undefined, global, options);
};
dust.isContext = function(obj) {
return typeof obj === "object" && obj._isContext === true;
};
/**
* Factory function that creates a closure scope around a Thenable-callback.
* Returns a function that can be passed to a Thenable that will resume a
* Context lookup once the Thenable resolves with new data, adding that new
* data to the lookup stack.
*/
function getWithResolvedData(ctx, cur, down) {
return function(data) {
return ctx.push(data)._get(cur, down);
};
}
Context.wrap = function(context, name) {
if (dust.isContext(context)) {
return context;
}
return new Context(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);
};
/**
* 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, fn;
first = down[0];
len = down.length;
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;
}
// Try looking in the global context if we haven't found anything yet
if (value !== undefined) {
ctx = value;
} else {
ctx = this.global && this.global[first];
}
} else if (ctx) {
// if scope is limited by a leading dot, don't search up the tree
if(ctx.head) {
ctx = ctx.head[first];
} else {
// context's head is empty, value we are searching for is not defined
ctx = undefined;
}
}
while (ctx && i < len) {
if (dust.isThenable(ctx)) {
// Bail early by returning a Thenable for the remainder of the search tree
return ctx.then(getWithResolvedData(this, cur, down.slice(i)));
}
ctxThis = ctx;
ctx = ctx[down[i]];
i++;
}
}
if (typeof ctx === 'function') {
fn = function() {
try {
return ctx.apply(ctxThis, arguments);
} catch (err) {
dust.log(err, ERROR);
throw err;
}
};
fn.__dustBody = !!ctx.__dustBody;
return fn;
} else {
if (ctx === undefined) {
dust.log('Cannot find reference `{' + down.join('.') + '}` in template `' + this.getTemplateName() + '`', INFO);
}
return ctx;
}
};
Context.prototype.getPath = function(cur, down) {
return this._get(cur, down);
};
Context.prototype.push = function(head, idx, len) {
if(head === undefined) {
dust.log("Not pushing an undefined variable onto the context", INFO);
return this;
}
return this.rebase(new Stack(head, this.stack, idx, len));
};
Context.prototype.pop = function() {
var head = this.current();
this.stack = this.stack && this.stack.tail;
return head;
};
Context.prototype.rebase = function(head) {
return new Context(head, this.global, this.options, this.blocks, this.getTemplateName());
};
Context.prototype.clone = function() {
var context = this.rebase();
context.stack = this.stack;
return context;
};
Context.prototype.current = function() {
return this.stack && this.stack.head;
};
Context.prototype.getBlock = function(key) {
var blocks, len, fn;
if (typeof key === 'function') {
key = key(new Chunk(), this).data.join('');
}
blocks = this.blocks;
if (!blocks) {
dust.log('No blocks for context `' + key + '` in template `' + this.getTemplateName() + '`', DEBUG);
return false;
}
len = blocks.length;
while (len--) {
fn = blocks[len][key];
if (fn) {
return fn;
}
}
dust.log('Malformed template `' + this.getTemplateName() + '` was missing one or more blocks.');
return false;
};
Context.prototype.shiftBlocks = function(locals) {
var blocks = this.blocks,
newBlocks;
if (locals) {
if (!blocks) {
newBlocks = [locals];
} else {
newBlocks = blocks.concat([locals]);
}
return new Context(this.stack, this.global, this.options, newBlocks, this.getTemplateName());
}
return this;
};
Context.prototype.resolve = function(body) {
var chunk;
if(typeof body !== 'function') {
return body;
}
chunk = new Chunk().render(body, this);
if(chunk instanceof Chunk) {
return chunk.data.join(''); // ie7 perf
}
return chunk;
};
Context.prototype.getTemplateName = function() {
return this.templateName;
};
function Stack(head, tail, idx, len) {
this.tail = tail;
this.isObject = head && typeof head === 'object';
this.head = head;
this.index = idx;
this.of = len;
}
function Stub(callback) {
this.head = new Chunk(this);
this.callback = callback;
this.out = '';
}
Stub.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.log('Rendering failed with error `' + chunk.error + '`', ERROR);
this.flush = EMPTY_FUNC;
return;
} else {
return;
}
chunk = chunk.next;
this.head = chunk;
}
this.callback(null, this.out);
};
/**
* Creates an interface sort of like a Streams2 ReadableStream.
*/
function Stream() {
this.head = new Chunk(this);
}
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);
this.emit('end');
dust.log('Streaming failed with error `' + chunk.error + '`', ERROR);
this.flush = EMPTY_FUNC;
return;
} else {
return;
}
chunk = chunk.next;
this.head = chunk;
}
this.emit('end');
};
/**
* Executes listeners for `type` by passing data. Note that this is different from a
* Node stream, which can pass an arbitrary number of arguments
* @return `true` if event had listeners, `false` otherwise
*/
Stream.prototype.emit = function(type, data) {
var events = this.events || {},
handlers = events[type] || [],
i, l;
if (!handlers.length) {
dust.log('Stream broadcasting, but no listeners for `' + type + '`', DEBUG);
return false;
}
handlers = handlers.slice(0);
for (i = 0, l = handlers.length; i < l; i++) {
handlers[i](data);
}
return true;
};
Stream.prototype.on = function(type, callback) {
var events = this.events = this.events || {},
handlers = events[type] = events[type] || [];
if(typeof callback !== 'function') {
dust.log('No callback function provided for `' + type + '` event listener', WARN);
} else {
handlers.push(callback);
}
return this;
};
/**
* Pipes to a WritableStream. Note that backpressure isn't implemented,
* so we just write as fast as we can.
* @param stream {WritableStream}
* @return self
*/
Stream.prototype.pipe = function(stream) {
if(typeof stream.write !== 'function' ||
typeof stream.end !== 'function') {
dust.log('Incompatible stream passed to `pipe`', WARN);
return this;
}
var destEnded = false;
if(typeof stream.emit === 'function') {
stream.emit('pipe', this);
}
if(typeof stream.on === 'function') {
stream.on('error', function() {
destEnded = true;
});
}
return this
.on('data', function(data) {
if(destEnded) {
return;
}
try {
stream.write(data, 'utf8');
} catch (err) {
dust.log(err, ERROR);
}
})
.on('end', function() {
if(destEnded) {
return;
}
try {
stream.end();
destEnded = true;
} catch (err) {
dust.log(err, ERROR);
}
});
};
function Chunk(root, next, taps) {
this.root = root;
this.next = next;
this.data = []; //ie7 perf
this.flushable = false;
this.taps = taps;
}
Chunk.prototype.write = function(data) {
var taps = this.taps;
if (taps) {
data = taps.go(data);
}
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.map = function(callback) {
var cursor = new Chunk(this.root, this.next, this.taps),
branch = new Chunk(this.root, cursor, this.taps);
this.next = branch;
this.flushable = true;
try {
callback(branch);
} catch(err) {
dust.log(err, ERROR);
branch.setError(err);
}
return cursor;
};
Chunk.prototype.tap = function(tap) {
var taps = this.taps;
if (taps) {
this.taps = taps.push(tap);
} else {
this.taps = new Tap(tap);
}
return this;
};
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 = elem.apply(context.current(), [this, context, null, {auto: auto, filters: filters}]);
if (elem instanceof Chunk) {
return elem;
} else {
return this.reference(elem, context, auto, filters);
}
}
if (dust.isThenable(elem)) {
return this.await(elem, context, null, auto, filters);
} else if (dust.isStreamable(elem)) {
return this.stream(elem, context, null, auto, filters);
} else if (!dust.isEmpty(elem)) {
return this.write(dust.filter(elem, auto, filters, context));
} else {
return this;
}
};
Chunk.prototype.section = function(elem, context, bodies, params) {
var body = bodies.block,
skip = bodies['else'],
chunk = this,
i, len, head;
if (typeof elem === 'function' && !dust.isTemplateFn(elem)) {
try {
elem = elem.apply(context.current(), [this, context, bodies, params]);
} catch(err) {
dust.log(err, ERROR);
return this.setError(err);
}
// Functions that return chunks are assumed to have handled the chunk manually.
// Make that chunk the current one and go to the next method in the chain.
if (elem instanceof Chunk) {
return elem;
}
}
if (dust.isEmptyObject(bodies)) {
// No bodies to render, and we've already invoked any function that was available in
// hopes of returning a Chunk.
return chunk;
}
if (!dust.isEmptyObject(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.
*/
if (dust.isArray(elem)) {
if (body) {
len = elem.length;
if (len > 0) {
head = context.stack && context.stack.head || {};
head.$len = len;
for (i = 0; i < len; i++) {
head.$idx = i;
chunk = body(chunk, context.push(elem[i], i, len));
}
head.$idx = undefined;
head.$len = undefined;
return chunk;
} else if (skip) {
return skip(this, context);
}
}
} else if (dust.isThenable(elem)) {
return this.await(elem, context, bodies);
} else if (dust.isStreamable(elem)) {
return this.stream(elem, context, bodies);
} else if (elem === true) {
// true is truthy but does not change context
if (body) {
return body(this, context);
}
} 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('Section without corresponding key in template `' + context.getTemplateName() + '`', DEBUG);
return this;
};
Chunk.prototype.exists = function(elem, context, bodies) {
var body = bodies.block,
skip = bodies['else'];
if (!dust.isEmpty(elem)) {
if (body) {
return body(this, context);
}
dust.log('No block for exists check in template `' + context.getTemplateName() + '`', DEBUG);
} else if (skip) {
return skip(this, context);
}
return this;
};
Chunk.prototype.notexists = function(elem, context, bodies) {
var body = bodies.block,
skip = bodies['else'];
if (dust.isEmpty(elem)) {
if (body) {
return body(this, context);
}
dust.log('No block for not-exists check in template `' + context.getTemplateName() + '`', DEBUG);
} else if (skip) {
return skip(this, context);
}
return this;
};
Chunk.prototype.block = function(elem, context, bodies) {
var body = elem || bodies.block;
if (body) {
return body(this, context);
}
return this;
};
Chunk.prototype.partial = function(elem, context, partialContext, params) {
var head;
if(params === undefined) {
// Compatibility for < 2.7.0 where `partialContext` did not exist
params = partialContext;
partialContext = context;
}
if (!dust.isEmptyObject(params)) {
partialContext = partialContext.clone();
head = partialContext.pop();
partialContext = partialContext.push(params)
.push(head);
}
if (dust.isTemplateFn(elem)) {
// The eventual result of evaluating `elem` is a partial name
// Load the partial after getting its name and end the async chunk
return this.capture(elem, context, function(name, chunk) {
partialContext.templateName = name;
load(name, chunk, partialContext).end();
});
} else {
partialContext.templateName = elem;
return load(elem, this, partialContext);
}
};
Chunk.prototype.helper = function(name, context, bodies, params, auto) {
var chunk = this,
filters = params.filters,
ret;
// Pre-2.7.1 compat: if auto is undefined, it's an old template. Automatically escape
if (auto === undefined) {
auto = 'h';
}
// handle invalid helpers, similar to invalid filters
if(dust.helpers[name]) {
try {
ret = dust.helpers[name](chunk, context, bodies, params);
if (ret instanceof Chunk) {
return ret;
}
if(typeof filters === 'string') {
filters = filters.split('|');
}
if (!dust.isEmptyObject(bodies)) {
return chunk.section(ret, context, bodies, params);
}
// Helpers act slightly differently from functions in context in that they will act as
// a reference if they are self-closing (due to grammar limitations)
// In the Chunk.await function we check to make sure bodies is null before acting as a reference
return chunk.reference(ret, context, auto, filters);
} catch(err) {
dust.log('Error in helper `' + name + '`: ' + err.message, ERROR);
return chunk.setError(err);
}
} else {
dust.log('Helper `' + name + '` does not exist', WARN);
return chunk;
}
};
/**
* Reserve a chunk to be evaluated once a thenable is resolved or rejected
* @param thenable {Thenable} the target thenable to await
* @param context {Context} context to use to render the deferred chunk
* @param bodies {Object} must contain a "body", may contain an "error"
* @param auto {String} automatically apply this filter if the Thenable is a reference
* @param filters {Array} apply these filters if the Thenable is a reference
* @return {Chunk}
*/
Chunk.prototype.await = function(thenable, context, bodies, auto, filters) {
return this.map(function(chunk) {
thenable.then(function(data) {
if (bodies) {
chunk = chunk.section(data, context, bodies);
} else {
// Actually a reference. Self-closing sections don't render
chunk = chunk.reference(data, context, auto, filters);
}
chunk.end();
}, function(err) {
var errorBody = bodies && bodies.error;
if(errorBody) {
chunk.render(errorBody, context.push(err)).end();
} else {
dust.log('Unhandled promise rejection in `' + context.getTemplateName() + '`', INFO);
chunk.end();
}
});
});
};
/**
* Reserve a chunk to be evaluated with the contents of a streamable.
* Currently an error event will bomb out the stream. Once an error
* is received, we push it to an {:error} block if one exists, and log otherwise,
* then stop listening to the stream.
* @param streamable {Streamable} the target streamable that will emit events
* @param context {Context} context to use to render each thunk
* @param bodies {Object} must contain a "body", may contain an "error"
* @return {Chunk}
*/
Chunk.prototype.stream = function(stream, context, bodies, auto, filters) {
var body = bodies && bodies.block,
errorBody = bodies && bodies.error;
return this.map(function(chunk) {
var ended = false;
stream
.on('data', function data(thunk) {
if(ended) {
return;
}
if(body) {
// Fork a new chunk out of the blockstream so that we can flush it independently
chunk = chunk.map(function(chunk) {
chunk.render(body, context.push(thunk)).end();
});
} else if(!bodies) {
// When actually a reference, don't fork, just write into the master async chunk
chunk = chunk.reference(thunk, context, auto, filters);
}
})
.on('error', function error(err) {
if(ended) {
return;
}
if(errorBody) {
chunk.render(errorBody, context.push(err));
} else {
dust.log('Unhandled stream error in `' + context.getTemplateName() + '`', INFO);
}
if(!ended) {
ended = true;
chunk.end();
}
})
.on('end', function end() {
if(!ended) {
ended = true;
chunk.end();
}
});
});
};
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();
});
};
Chunk.prototype.setError = function(err) {
this.error = err;
this.root.flush();
return this;
};
// Chunk aliases
for(var f in Chunk.prototype) {
if(dust._aliases[f]) {
Chunk.prototype[dust._aliases[f]] = Chunk.prototype[f];
}
}
function Tap(head, tail) {
this.head = head;
this.tail = tail;
}
Tap.prototype.push = function(tap) {
return new Tap(tap, this);
};
Tap.prototype.go = function(value) {
var tap = this;
while(tap) {
value = tap.head(value);
tap = tap.tail;
}
return value;
};
var HCHARS = /[&<>"']/,
AMP = /&/g,
LT = /</g,
GT = />/g,
QUOT = /\"/g,
SQUOT = /\'/g;
dust.escapeHtml = function(s) {
if (typeof s === "string" || (s && typeof s.toString === "function")) {
if (typeof s !== "string") {
s = s.toString();
}
if (!HCHARS.test(s)) {
return s;
}
return s.replace(AMP,'&amp;').replace(LT,'&lt;').replace(GT,'&gt;').replace(QUOT,'&quot;').replace(SQUOT, '&#39;');
}
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;
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.escapeJSON = function(o) {
if (!JSON) {
dust.log('JSON is undefined; could not escape `' + o + '`', WARN);
return o;
} else {
return JSON.stringify(o)
.replace(LS, '\\u2028')
.replace(PS, '\\u2029')
.replace(LT, '\\u003c');
}
};
return dust;
}));
if (typeof define === "function" && define.amd && define.amd.dust === true) {
define(["require", "dust.core"], function(require, dust) {
dust.onLoad = function(name, cb) {
require([name], function() {
cb();
});
};
return dust;
});
}