Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Reverted to not strip internal spaces from tag names. Added additiona…
…l tests.
  • Loading branch information
techhead committed Oct 2, 2012
commit 20eb7906a93dd860507c708037a7738d2ab53781
21 changes: 9 additions & 12 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Mustache;
var exports = {};

exports.name = "mustache.js";
exports.version = "0.7.1";
exports.version = "0.7.0";
exports.tags = ["{{", "}}"];

exports.Scanner = Scanner;
Expand All @@ -29,11 +29,12 @@ var Mustache;

var whiteRe = /\s*/;
var spaceRe = /\s+/;
var spaceReg = /\s+/g;
var nonSpaceRe = /\S/;
var eqRe = /\s*=/;
var curlyRe = /\s*\}/;
var tagRe = /#|\^|\/|>|\{|&|=|!/;
var dotRe = /\s*\.\s*/g;
var pipeRe = /\s*\|\s*/g;

// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
// See https://github.com/janl/mustache.js/issues/189
Expand Down Expand Up @@ -149,8 +150,8 @@ var Mustache;

if (!value) {

if (!view && name.indexOf("|") > 0) {
var names = name.split("|");
if (typeof view === 'undefined' && name.indexOf("|") > 0) {
var names = name.split(pipeRe);
for (var i=0,len=names.length; i<len; i++) {
value = this.lookup(names[i], value);
}
Expand All @@ -164,7 +165,7 @@ var Mustache;

while (context) {
if (name.indexOf(".") > 0) {
var names = name.split("."), i = 0;
var names = name.split(dotRe), i = 0;

value = context.view;

Expand All @@ -187,7 +188,7 @@ var Mustache;
}

if (typeof value === "function") {
value = value.call(view||this.view);
value = value.call(typeof view === 'undefined' ? this.view : view);
}

return value;
Expand Down Expand Up @@ -555,6 +556,8 @@ var Mustache;
throw new Error("Unclosed tag at " + scanner.pos);
}

tokens.push([type, value, start, scanner.pos]);

if (type === "name" || type === "{" || type === "&") {
nonSpace = true;
}
Expand All @@ -563,13 +566,7 @@ var Mustache;
if (type === "=") {
tags = value.split(spaceRe);
tagRes = escapeTags(tags);
} else if (type !== "!") { // If not a comment
// The tag's content MUST be a non-whitespace character sequence NOT containing
// the current closing delimiter.
value = value.replace(spaceReg,'');
}

tokens.push([type, value, start, scanner.pos]);
}

squashTokens(tokens);
Expand Down
4 changes: 2 additions & 2 deletions test/_files/dot_notation.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<p>VAT: {{{price.currency.symbol}}}{{#price}}{{vat}}{{/price}}</p>
<!-- boring part -->
<h2>Test truthy false values:</h2>
<p>Zero: {{truthy.zero}}</p>
<p>False: {{truthy.notTrue}}</p>
<p>Zero: {{truthy . zero}}</p>
<p>False: {{truthy . notTrue}}</p>
2 changes: 1 addition & 1 deletion test/_files/piped_helpers_date.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
({
timestamp: 1,
timestamp: 0,
iso8601Date: function() {
return new Date(this).toISOString();
}
Expand Down
2 changes: 1 addition & 1 deletion test/_files/piped_helpers_date.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1970-01-01T00:00:00.001Z
1970-01-01T00:00:00.000Z
6 changes: 5 additions & 1 deletion test/_files/piped_helpers_sum.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
({
numbers: [1,2,3,4],
numbers: [1,2,3,4,-10],
sum: function() {
var total = 0;
for (var i=0,len=this.length; i<len; i++) {
total += this[i];
}
return total;
},
sign: function() {
var num = +this;
return (num < 0) ? num : '+'+num;
}
})
6 changes: 5 additions & 1 deletion test/_files/piped_helpers_sum.mustache
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{{numbers|sum}}
Numbers:
{{#numbers}}
{{. | sign}}
{{/numbers}}
Sum: {{numbers|sum}}
8 changes: 7 additions & 1 deletion test/_files/piped_helpers_sum.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
10
Numbers:
+1
+2
+3
+4
-10
Sum: 0
10 changes: 0 additions & 10 deletions test/parse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ var Mustache = require('./../mustache');
var expectations = {
'{{hi}}' : [ [ 'name', 'hi', 0, 6 ] ],
'{{hi.world}}' : [ [ 'name', 'hi.world', 0, 12 ] ],
/*
* This is not the assumption that I would have made based on the spec.
* https://github.com/mustache/spec/blob/master/specs/interpolation.yml
*
* The tag's content MUST be a non-whitespace character sequence NOT containing
* the current closing delimiter.
*
* See corrected version below.
'{{hi . world}}' : [ [ 'name', 'hi . world', 0, 14 ] ],
*/
'{{hi . world}}' : [ [ 'name', 'hi.world', 0, 14 ] ],
'{{ hi}}' : [ [ 'name', 'hi', 0, 7 ] ],
'{{hi }}' : [ [ 'name', 'hi', 0, 7 ] ],
'{{ hi }}' : [ [ 'name', 'hi', 0, 8 ] ],
Expand Down