Comparing version 0.7.1 to 0.7.2
148
mustache.js
@@ -8,13 +8,11 @@ /*! | ||
var Mustache; | ||
(function (exports) { | ||
if (typeof module !== "undefined" && module.exports) { | ||
module.exports = exports; // CommonJS | ||
} else if (typeof define === "function") { | ||
define(exports); // AMD | ||
(function (root, factory) { | ||
if (typeof exports === "object" && exports) { | ||
module.exports = factory; // CommonJS | ||
} else if (typeof define === "function" && define.amd) { | ||
define(factory); // AMD | ||
} else { | ||
Mustache = exports; // <script> | ||
root.Mustache = factory; // <script> | ||
} | ||
}((function () { | ||
}(this, (function () { | ||
@@ -24,3 +22,3 @@ var exports = {}; | ||
exports.name = "mustache.js"; | ||
exports.version = "0.7.1"; | ||
exports.version = "0.7.2"; | ||
exports.tags = ["{{", "}}"]; | ||
@@ -307,20 +305,2 @@ | ||
/** | ||
* Calculates the bounds of the section represented by the given `token` in | ||
* the original template by drilling down into nested sections to find the | ||
* last token that is part of that section. Returns an array of [start, end]. | ||
*/ | ||
function sectionBounds(token) { | ||
var start = token[3]; | ||
var end = start; | ||
var tokens; | ||
while ((tokens = token[4]) && tokens.length) { | ||
token = tokens[tokens.length - 1]; | ||
end = token[3]; | ||
} | ||
return [start, end]; | ||
} | ||
/** | ||
* Low-level function that compiles the given `tokens` into a function | ||
@@ -352,3 +332,3 @@ * that accepts three arguments: a Writer, a Context, and the template. | ||
case "#": | ||
sectionText = template.slice.apply(template, sectionBounds(token)); | ||
sectionText = template.slice(token[3], token[5]); | ||
buffer += writer._section(token[1], context, sectionText, subRender(i, token[4], template)); | ||
@@ -380,4 +360,5 @@ break; | ||
* Forms the given array of `tokens` into a nested tree structure where | ||
* tokens that represent a section have a fifth item: an array that contains | ||
* all tokens in that section. | ||
* tokens that represent a section have two additional items: 1) an array of | ||
* all tokens that appear in that section and 2) the index in the original | ||
* template that represents the end of that section. | ||
*/ | ||
@@ -388,31 +369,17 @@ function nestTokens(tokens) { | ||
var sections = []; | ||
var token, section; | ||
for (var i = 0; i < tokens.length; ++i) { | ||
var token; | ||
for (var i = 0, len = tokens.length; i < len; ++i) { | ||
token = tokens[i]; | ||
switch (token[0]) { | ||
case "#": | ||
case "^": | ||
token[4] = []; | ||
case '#': | ||
case '^': | ||
sections.push(token); | ||
collector.push(token); | ||
collector = token[4]; | ||
collector = token[4] = []; | ||
break; | ||
case "/": | ||
if (sections.length === 0) { | ||
throw new Error("Unopened section: " + token[1]); | ||
} | ||
section = sections.pop(); | ||
if (section[1] !== token[1]) { | ||
throw new Error("Unclosed section: " + section[1]); | ||
} | ||
if (sections.length > 0) { | ||
collector = sections[sections.length - 1][4]; | ||
} else { | ||
collector = tree; | ||
} | ||
case '/': | ||
var section = sections.pop(); | ||
section[5] = token[2]; | ||
collector = sections.length > 0 ? sections[sections.length - 1][4] : tree; | ||
break; | ||
@@ -424,9 +391,2 @@ default: | ||
// Make sure there were no open sections when we're done. | ||
section = sections.pop(); | ||
if (section) { | ||
throw new Error("Unclosed section: " + section[1]); | ||
} | ||
return tree; | ||
@@ -440,8 +400,8 @@ } | ||
function squashTokens(tokens) { | ||
var token, lastToken, squashedTokens = []; | ||
var squashedTokens = []; | ||
for (var i = 0; i < tokens.length; ++i) { | ||
var token, lastToken; | ||
for (var i = 0, len = tokens.length; i < len; ++i) { | ||
token = tokens[i]; | ||
if (lastToken && lastToken[0] === "text" && token[0] === "text") { | ||
if (token[0] === 'text' && lastToken && lastToken[0] === 'text') { | ||
lastToken[1] += token[1]; | ||
@@ -459,6 +419,2 @@ lastToken[3] = token[3]; | ||
function escapeTags(tags) { | ||
if (tags.length !== 2) { | ||
throw new Error("Invalid tags: " + tags.join(" ")); | ||
} | ||
return [ | ||
@@ -480,9 +436,15 @@ new RegExp(escapeRe(tags[0]) + "\\s*"), | ||
if (typeof tags === 'string') tags = tags.split(spaceRe); | ||
if (tags.length !== 2) { | ||
throw new Error('Invalid tags: ' + tags.join(', ')); | ||
} | ||
var tagRes = escapeTags(tags); | ||
var scanner = new Scanner(template); | ||
var tokens = [], // Buffer to hold the tokens | ||
spaces = [], // Indices of whitespace tokens on the current line | ||
hasTag = false, // Is there a {{tag}} on the current line? | ||
nonSpace = false; // Is there a non-space char on the current line? | ||
var sections = []; // Stack to hold section tokens | ||
var tokens = []; // Buffer to hold the tokens | ||
var spaces = []; // Indices of whitespace tokens on the current line | ||
var hasTag = false; // Is there a {{tag}} on the current line? | ||
var nonSpace = false; // Is there a non-space char on the current line? | ||
@@ -505,3 +467,2 @@ // Strips all whitespace tokens array for the current line | ||
var start, type, value, chr; | ||
while (!scanner.eos()) { | ||
@@ -560,14 +521,33 @@ start = scanner.pos; | ||
if (!scanner.scan(tagRes[1])) { | ||
throw new Error("Unclosed tag at " + scanner.pos); | ||
throw new Error('Unclosed tag at ' + scanner.pos); | ||
} | ||
tokens.push([type, value, start, scanner.pos]); | ||
// Check section nesting. | ||
if (type === '/') { | ||
if (sections.length === 0) { | ||
throw new Error('Unopened section "' + value + '" at ' + start); | ||
} | ||
if (type === "name" || type === "{" || type === "&") { | ||
nonSpace = true; | ||
var section = sections.pop(); | ||
if (section[1] !== value) { | ||
throw new Error('Unclosed section "' + section[1] + '" at ' + start); | ||
} | ||
} | ||
// Set the tags for the next time around. | ||
if (type === "=") { | ||
var token = [type, value, start, scanner.pos]; | ||
tokens.push(token); | ||
if (type === '#' || type === '^') { | ||
sections.push(token); | ||
} else if (type === "name" || type === "{" || type === "&") { | ||
nonSpace = true; | ||
} else if (type === "=") { | ||
// Set the tags for the next time around. | ||
tags = value.split(spaceRe); | ||
if (tags.length !== 2) { | ||
throw new Error('Invalid tags at ' + start + ': ' + tags.join(', ')); | ||
} | ||
tagRes = escapeTags(tags); | ||
@@ -577,5 +557,9 @@ } | ||
tokens = squashTokens(tokens); | ||
// Make sure there are no open sections when we're done. | ||
var section = sections.pop(); | ||
if (section) { | ||
throw new Error('Unclosed section "' + section[1] + '" at ' + scanner.pos); | ||
} | ||
return nestTokens(tokens); | ||
return nestTokens(squashTokens(tokens)); | ||
}; | ||
@@ -582,0 +566,0 @@ |
{ | ||
"name": "mustache", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"description": "Logic-less {{mustache}} templates with JavaScript", | ||
@@ -9,10 +9,10 @@ "author": "mustache.js Authors <http://github.com/janl/mustache.js>", | ||
"devDependencies": { | ||
"vows": "0.6.x" | ||
"mocha": "1.5.0" | ||
}, | ||
"volo": { | ||
"url": "https://raw.github.com/janl/mustache.js/0.7.1/mustache.js" | ||
"url": "https://raw.github.com/janl/mustache.js/0.7.2/mustache.js" | ||
}, | ||
"scripts": { | ||
"test": "vows --spec" | ||
"test": "mocha test" | ||
} | ||
} |
@@ -310,2 +310,28 @@ # mustache.js - Logic-less {{mustache}} templates with JavaScript | ||
### Compiled Templates | ||
Mustache templates can be compiled into JavaScript functions using `Mustache.compile` for improved rendering performance. | ||
If you have template views that are rendered multiple times, compiling your template into a JavaScript function will minimise the amount of work required for each re-render. | ||
Pre-compiled templates can also be generated server-side, for delivery to the browser as ready to use JavaScript functions, further reducing the amount of client side processing required for initialising templates. | ||
**Mustache.compile** | ||
Use `Mustache.compile` to compile standard Mustache string templates into reusable Mustache template functions. | ||
var compiledTemplate = Mustache.compile(stringTemplate); | ||
The function returned from `Mustache.compile` can then be called directly, passing in the template data as an argument (with an object of partials as an optional second parameter), to generate the final output. | ||
var templateOutput = compiledTemplate(templateData); | ||
**Mustache.compilePartial** | ||
Template partials can also be compiled using the `Mustache.compilePartial` function. The first parameter of this function, is the name of the partial as it appears within parent templates. | ||
Mustache.compilePartial('partial-name', stringTemplate); | ||
Compiled partials are then available to both `Mustache.render` and `Mustache.compile`. | ||
## Plugins for JavaScript Libraries | ||
@@ -312,0 +338,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
63566
181
401
1108