* http://github.com/janl/mustache.js
*/
-/*global define: false*/
+/*global define: false Mustache: true*/
-(function (global, factory) {
- if (typeof exports === "object" && exports) {
+(function defineMustache (global, factory) {
+ if (typeof exports === 'object' && exports && typeof exports.nodeName !== 'string') {
factory(exports); // CommonJS
- } else if (typeof define === "function" && define.amd) {
+ } else if (typeof define === 'function' && define.amd) {
define(['exports'], factory); // AMD
} else {
- factory(global.Mustache = {}); // <script>
+ global.Mustache = {};
+ factory(global.Mustache); // script, wsh, asp
}
-}(this, function (mustache) {
+}(this, function mustacheFactory (mustache) {
- var Object_toString = Object.prototype.toString;
- var isArray = Array.isArray || function (object) {
- return Object_toString.call(object) === '[object Array]';
+ var objectToString = Object.prototype.toString;
+ var isArray = Array.isArray || function isArrayPolyfill (object) {
+ return objectToString.call(object) === '[object Array]';
};
- function isFunction(object) {
+ function isFunction (object) {
return typeof object === 'function';
}
- function escapeRegExp(string) {
- return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
+ /**
+ * More correct typeof string handling array
+ * which normally returns typeof 'object'
+ */
+ function typeStr (obj) {
+ return isArray(obj) ? 'array' : typeof obj;
+ }
+
+ function escapeRegExp (string) {
+ return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
+ }
+
+ /**
+ * Null safe way of checking whether or not an object,
+ * including its prototype, has a given property
+ */
+ function hasProperty (obj, propName) {
+ return obj != null && typeof obj === 'object' && (propName in obj);
+ }
+
+ /**
+ * Safe way of detecting whether or not the given thing is a primitive and
+ * whether it has the given property
+ */
+ function primitiveHasOwnProperty (primitive, propName) {
+ return (
+ primitive != null
+ && typeof primitive !== 'object'
+ && primitive.hasOwnProperty
+ && primitive.hasOwnProperty(propName)
+ );
}
// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
// See https://github.com/janl/mustache.js/issues/189
- var RegExp_test = RegExp.prototype.test;
- function testRegExp(re, string) {
- return RegExp_test.call(re, string);
+ var regExpTest = RegExp.prototype.test;
+ function testRegExp (re, string) {
+ return regExpTest.call(re, string);
}
var nonSpaceRe = /\S/;
- function isWhitespace(string) {
+ function isWhitespace (string) {
return !testRegExp(nonSpaceRe, string);
}
var entityMap = {
- "&": "&",
- "<": "<",
- ">": ">",
+ '&': '&',
+ '<': '<',
+ '>': '>',
'"': '"',
"'": ''',
- "/": '/'
+ '/': '/',
+ '`': '`',
+ '=': '='
};
- function escapeHtml(string) {
- return String(string).replace(/[&<>"'\/]/g, function (s) {
+ function escapeHtml (string) {
+ return String(string).replace(/[&<>"'`=\/]/g, function fromEntityMap (s) {
return entityMap[s];
});
}
* array of tokens in the subtree and 2) the index in the original template at
* which the closing tag for that section begins.
*/
- function parseTemplate(template, tags) {
+ function parseTemplate (template, tags) {
if (!template)
return [];
// Strips all whitespace tokens array for the current line
// if there was a {{#tag}} on it and otherwise only space.
- function stripSpace() {
+ function stripSpace () {
if (hasTag && !nonSpace) {
while (spaces.length)
delete tokens[spaces.pop()];
}
var openingTagRe, closingTagRe, closingCurlyRe;
- function compileTags(tags) {
- if (typeof tags === 'string')
- tags = tags.split(spaceRe, 2);
+ function compileTags (tagsToCompile) {
+ if (typeof tagsToCompile === 'string')
+ tagsToCompile = tagsToCompile.split(spaceRe, 2);
- if (!isArray(tags) || tags.length !== 2)
- throw new Error('Invalid tags: ' + tags);
+ if (!isArray(tagsToCompile) || tagsToCompile.length !== 2)
+ throw new Error('Invalid tags: ' + tagsToCompile);
- openingTagRe = new RegExp(escapeRegExp(tags[0]) + '\\s*');
- closingTagRe = new RegExp('\\s*' + escapeRegExp(tags[1]));
- closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tags[1]));
+ openingTagRe = new RegExp(escapeRegExp(tagsToCompile[0]) + '\\s*');
+ closingTagRe = new RegExp('\\s*' + escapeRegExp(tagsToCompile[1]));
+ closingCurlyRe = new RegExp('\\s*' + escapeRegExp('}' + tagsToCompile[1]));
}
compileTags(tags || mustache.tags);
* Combines the values of consecutive text tokens in the given `tokens` array
* to a single token.
*/
- function squashTokens(tokens) {
+ function squashTokens (tokens) {
var squashedTokens = [];
var token, lastToken;
* all tokens that appear in that section and 2) the index in the original
* template that represents the end of that section.
*/
- function nestTokens(tokens) {
+ function nestTokens (tokens) {
var nestedTokens = [];
var collector = nestedTokens;
var sections = [];
token = tokens[i];
switch (token[0]) {
- case '#':
- case '^':
- collector.push(token);
- sections.push(token);
- collector = token[4] = [];
- break;
- case '/':
- section = sections.pop();
- section[5] = token[2];
- collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
- break;
- default:
- collector.push(token);
+ case '#':
+ case '^':
+ collector.push(token);
+ sections.push(token);
+ collector = token[4] = [];
+ break;
+ case '/':
+ section = sections.pop();
+ section[5] = token[2];
+ collector = sections.length > 0 ? sections[sections.length - 1][4] : nestedTokens;
+ break;
+ default:
+ collector.push(token);
}
}
* A simple string scanner that is used by the template parser to find
* tokens in template strings.
*/
- function Scanner(string) {
+ function Scanner (string) {
this.string = string;
this.tail = string;
this.pos = 0;
/**
* Returns `true` if the tail is empty (end of string).
*/
- Scanner.prototype.eos = function () {
- return this.tail === "";
+ Scanner.prototype.eos = function eos () {
+ return this.tail === '';
};
/**
* Tries to match the given regular expression at the current position.
* Returns the matched text if it can match, the empty string otherwise.
*/
- Scanner.prototype.scan = function (re) {
+ Scanner.prototype.scan = function scan (re) {
var match = this.tail.match(re);
if (!match || match.index !== 0)
* Skips all text until the given regular expression can be matched. Returns
* the skipped string, which is the entire tail if no match can be made.
*/
- Scanner.prototype.scanUntil = function (re) {
+ Scanner.prototype.scanUntil = function scanUntil (re) {
var index = this.tail.search(re), match;
switch (index) {
- case -1:
- match = this.tail;
- this.tail = "";
- break;
- case 0:
- match = "";
- break;
- default:
- match = this.tail.substring(0, index);
- this.tail = this.tail.substring(index);
+ case -1:
+ match = this.tail;
+ this.tail = '';
+ break;
+ case 0:
+ match = '';
+ break;
+ default:
+ match = this.tail.substring(0, index);
+ this.tail = this.tail.substring(index);
}
this.pos += match.length;
* Represents a rendering context by wrapping a view object and
* maintaining a reference to the parent context.
*/
- function Context(view, parentContext) {
- this.view = view == null ? {} : view;
+ function Context (view, parentContext) {
+ this.view = view;
this.cache = { '.': this.view };
this.parent = parentContext;
}
* Creates a new context using the given view with this context
* as the parent.
*/
- Context.prototype.push = function (view) {
+ Context.prototype.push = function push (view) {
return new Context(view, this);
};
* Returns the value of the given name in this context, traversing
* up the context hierarchy if the value is absent in this context's view.
*/
- Context.prototype.lookup = function (name) {
+ Context.prototype.lookup = function lookup (name) {
var cache = this.cache;
var value;
- if (name in cache) {
+ if (cache.hasOwnProperty(name)) {
value = cache[name];
} else {
- var context = this, names, index;
+ var context = this, intermediateValue, names, index, lookupHit = false;
while (context) {
if (name.indexOf('.') > 0) {
- value = context.view;
+ intermediateValue = context.view;
names = name.split('.');
index = 0;
- while (value != null && index < names.length)
- value = value[names[index++]];
- } else if (typeof context.view == 'object') {
- value = context.view[name];
+ /**
+ * Using the dot notion path in `name`, we descend through the
+ * nested objects.
+ *
+ * To be certain that the lookup has been successful, we have to
+ * check if the last object in the path actually has the property
+ * we are looking for. We store the result in `lookupHit`.
+ *
+ * This is specially necessary for when the value has been set to
+ * `undefined` and we want to avoid looking up parent contexts.
+ *
+ * In the case where dot notation is used, we consider the lookup
+ * to be successful even if the last "object" in the path is
+ * not actually an object but a primitive (e.g., a string, or an
+ * integer), because it is sometimes useful to access a property
+ * of an autoboxed primitive, such as the length of a string.
+ **/
+ while (intermediateValue != null && index < names.length) {
+ if (index === names.length - 1)
+ lookupHit = (
+ hasProperty(intermediateValue, names[index])
+ || primitiveHasOwnProperty(intermediateValue, names[index])
+ );
+
+ intermediateValue = intermediateValue[names[index++]];
+ }
+ } else {
+ intermediateValue = context.view[name];
+
+ /**
+ * Only checking against `hasProperty`, which always returns `false` if
+ * `context.view` is not an object. Deliberately omitting the check
+ * against `primitiveHasOwnProperty` if dot notation is not used.
+ *
+ * Consider this example:
+ * ```
+ * Mustache.render("The length of a football field is {{#length}}{{length}}{{/length}}.", {length: "100 yards"})
+ * ```
+ *
+ * If we were to check also against `primitiveHasOwnProperty`, as we do
+ * in the dot notation case, then render call would return:
+ *
+ * "The length of a football field is 9."
+ *
+ * rather than the expected:
+ *
+ * "The length of a football field is 100 yards."
+ **/
+ lookupHit = hasProperty(context.view, name);
}
- if (value != null)
+ if (lookupHit) {
+ value = intermediateValue;
break;
+ }
context = context.parent;
}
* string, given a context. It also maintains a cache of templates to
* avoid the need to parse the same template twice.
*/
- function Writer() {
+ function Writer () {
this.cache = {};
}
/**
* Clears all cached templates in this writer.
*/
- Writer.prototype.clearCache = function () {
+ Writer.prototype.clearCache = function clearCache () {
this.cache = {};
};
/**
- * Parses and caches the given `template` and returns the array of tokens
+ * Parses and caches the given `template` according to the given `tags` or
+ * `mustache.tags` if `tags` is omitted, and returns the array of tokens
* that is generated from the parse.
*/
- Writer.prototype.parse = function (template, tags) {
+ Writer.prototype.parse = function parse (template, tags) {
var cache = this.cache;
- var tokens = cache[template];
+ var cacheKey = template + ':' + (tags || mustache.tags).join(':');
+ var tokens = cache[cacheKey];
if (tokens == null)
- tokens = cache[template] = parseTemplate(template, tags);
+ tokens = cache[cacheKey] = parseTemplate(template, tags);
return tokens;
};
* names and templates of partials that are used in the template. It may
* also be a function that is used to load partial templates on the fly
* that takes a single argument: the name of the partial.
+ *
+ * If the optional `tags` argument is given here it must be an array with two
+ * string values: the opening and closing tags used in the template (e.g.
+ * [ "<%", "%>" ]). The default is to mustache.tags.
*/
- Writer.prototype.render = function (template, view, partials) {
- var tokens = this.parse(template);
+ Writer.prototype.render = function render (template, view, partials, tags) {
+ var tokens = this.parse(template, tags);
var context = (view instanceof Context) ? view : new Context(view);
- return this.renderTokens(tokens, context, partials, template);
+ return this.renderTokens(tokens, context, partials, template, tags);
};
/**
* If the template doesn't use higher-order sections, this argument may
* be omitted.
*/
- Writer.prototype.renderTokens = function (tokens, context, partials, originalTemplate) {
+ Writer.prototype.renderTokens = function renderTokens (tokens, context, partials, originalTemplate, tags) {
var buffer = '';
- // This function is used to render an arbitrary template
- // in the current context by higher-order sections.
- var self = this;
- function subRender(template) {
- return self.render(template, context, partials);
- }
-
- var token, value;
+ var token, symbol, value;
for (var i = 0, numTokens = tokens.length; i < numTokens; ++i) {
+ value = undefined;
token = tokens[i];
+ symbol = token[0];
- switch (token[0]) {
- case '#':
- value = context.lookup(token[1]);
+ if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
+ else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
+ else if (symbol === '>') value = this.renderPartial(token, context, partials, tags);
+ else if (symbol === '&') value = this.unescapedValue(token, context);
+ else if (symbol === 'name') value = this.escapedValue(token, context);
+ else if (symbol === 'text') value = this.rawValue(token);
- if (!value)
- continue;
+ if (value !== undefined)
+ buffer += value;
+ }
- if (isArray(value)) {
- for (var j = 0, valueLength = value.length; j < valueLength; ++j) {
- buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate);
- }
- } else if (typeof value === 'object' || typeof value === 'string') {
- buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate);
- } else if (isFunction(value)) {
- if (typeof originalTemplate !== 'string')
- throw new Error('Cannot use higher-order sections without the original template');
+ return buffer;
+ };
- // Extract the portion of the original template that the section contains.
- value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender);
+ Writer.prototype.renderSection = function renderSection (token, context, partials, originalTemplate) {
+ var self = this;
+ var buffer = '';
+ var value = context.lookup(token[1]);
- if (value != null)
- buffer += value;
- } else {
- buffer += this.renderTokens(token[4], context, partials, originalTemplate);
- }
+ // This function is used to render an arbitrary template
+ // in the current context by higher-order sections.
+ function subRender (template) {
+ return self.render(template, context, partials);
+ }
- break;
- case '^':
- value = context.lookup(token[1]);
+ if (!value) return;
- // Use JavaScript's definition of falsy. Include empty arrays.
- // See https://github.com/janl/mustache.js/issues/186
- if (!value || (isArray(value) && value.length === 0))
- buffer += this.renderTokens(token[4], context, partials, originalTemplate);
+ if (isArray(value)) {
+ for (var j = 0, valueLength = value.length; j < valueLength; ++j) {
+ buffer += this.renderTokens(token[4], context.push(value[j]), partials, originalTemplate);
+ }
+ } else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
+ buffer += this.renderTokens(token[4], context.push(value), partials, originalTemplate);
+ } else if (isFunction(value)) {
+ if (typeof originalTemplate !== 'string')
+ throw new Error('Cannot use higher-order sections without the original template');
- break;
- case '>':
- if (!partials)
- continue;
+ // Extract the portion of the original template that the section contains.
+ value = value.call(context.view, originalTemplate.slice(token[3], token[5]), subRender);
- value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
+ if (value != null)
+ buffer += value;
+ } else {
+ buffer += this.renderTokens(token[4], context, partials, originalTemplate);
+ }
+ return buffer;
+ };
- if (value != null)
- buffer += this.renderTokens(this.parse(value), context, partials, value);
+ Writer.prototype.renderInverted = function renderInverted (token, context, partials, originalTemplate) {
+ var value = context.lookup(token[1]);
- break;
- case '&':
- value = context.lookup(token[1]);
+ // Use JavaScript's definition of falsy. Include empty arrays.
+ // See https://github.com/janl/mustache.js/issues/186
+ if (!value || (isArray(value) && value.length === 0))
+ return this.renderTokens(token[4], context, partials, originalTemplate);
+ };
- if (value != null)
- buffer += value;
+ Writer.prototype.renderPartial = function renderPartial (token, context, partials, tags) {
+ if (!partials) return;
- break;
- case 'name':
- value = context.lookup(token[1]);
+ var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
+ if (value != null)
+ return this.renderTokens(this.parse(value, tags), context, partials, value);
+ };
- if (value != null)
- buffer += mustache.escape(value);
+ Writer.prototype.unescapedValue = function unescapedValue (token, context) {
+ var value = context.lookup(token[1]);
+ if (value != null)
+ return value;
+ };
- break;
- case 'text':
- buffer += token[1];
- break;
- }
- }
+ Writer.prototype.escapedValue = function escapedValue (token, context) {
+ var value = context.lookup(token[1]);
+ if (value != null)
+ return mustache.escape(value);
+ };
- return buffer;
+ Writer.prototype.rawValue = function rawValue (token) {
+ return token[1];
};
- mustache.name = "mustache.js";
- mustache.version = "1.0.0";
- mustache.tags = [ "{{", "}}" ];
+ mustache.name = 'mustache.js';
+ mustache.version = '3.0.1';
+ mustache.tags = [ '{{', '}}' ];
// All high-level mustache.* functions use this writer.
var defaultWriter = new Writer();
/**
* Clears all cached templates in the default writer.
*/
- mustache.clearCache = function () {
+ mustache.clearCache = function clearCache () {
return defaultWriter.clearCache();
};
* array of tokens it contains. Doing this ahead of time avoids the need to
* parse templates on the fly as they are rendered.
*/
- mustache.parse = function (template, tags) {
+ mustache.parse = function parse (template, tags) {
return defaultWriter.parse(template, tags);
};
/**
* Renders the `template` with the given `view` and `partials` using the
- * default writer.
+ * default writer. If the optional `tags` argument is given here it must be an
+ * array with two string values: the opening and closing tags used in the
+ * template (e.g. [ "<%", "%>" ]). The default is to mustache.tags.
*/
- mustache.render = function (template, view, partials) {
- return defaultWriter.render(template, view, partials);
+ mustache.render = function render (template, view, partials, tags) {
+ if (typeof template !== 'string') {
+ throw new TypeError('Invalid template! Template should be a "string" ' +
+ 'but "' + typeStr(template) + '" was given as the first ' +
+ 'argument for mustache#render(template, view, partials)');
+ }
+
+ return defaultWriter.render(template, view, partials, tags);
};
- // This is here for backwards compatibility with 0.4.x.
- mustache.to_html = function (template, view, partials, send) {
+ // This is here for backwards compatibility with 0.4.x.,
+ /*eslint-disable */ // eslint wants camel cased function name
+ mustache.to_html = function to_html (template, view, partials, send) {
+ /*eslint-enable*/
+
var result = mustache.render(template, view, partials);
if (isFunction(send)) {
mustache.Context = Context;
mustache.Writer = Writer;
+ return mustache;
}));