Skip to content

Commit 2465398

Browse files
committed
Writer.prototype.parse to cache by tags in addition to template string
1 parent 23beb3a commit 2465398

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

mustache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@
447447
var tokens = cache[template];
448448

449449
if (tokens == null)
450-
tokens = cache[template] = parseTemplate(template, tags);
450+
tokens = cache[template + ':' + (tags || mustache.tags).join(':')] = parseTemplate(template, tags);
451451

452452
return tokens;
453453
};

test/parse-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,36 @@ describe('Mustache.parse', function () {
103103
});
104104
});
105105

106+
describe('when parsing a template without tags specified followed by the same template with tags specified', function() {
107+
it('returns different tokens for the latter parse', function() {
108+
var template = "{{foo}}[bar]";
109+
var parsedWithBraces = Mustache.parse(template);
110+
var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
111+
assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
112+
});
113+
});
114+
115+
describe('when parsing a template with tags specified followed by the same template with different tags specified', function() {
116+
it('returns different tokens for the latter parse', function() {
117+
var template = "(foo)[bar]";
118+
var parsedWithParens = Mustache.parse(template, ['(', ')']);
119+
var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
120+
assert.notDeepEqual(parsedWithBrackets, parsedWithParens);
121+
});
122+
});
123+
124+
describe('when parsing a template after already having parsed that template with a different Mustache.tags', function() {
125+
it('returns different tokens for the latter parse', function() {
126+
var template = "{{foo}}[bar]";
127+
var parsedWithBraces = Mustache.parse(template, ['(', ')']);
128+
129+
var oldTags = Mustache.tags;
130+
Mustache.tags = ['[', ']'];
131+
var parsedWithBrackets = Mustache.parse(template);
132+
Mustache.tags = oldTags;
133+
134+
assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
135+
});
136+
});
137+
106138
});

0 commit comments

Comments
 (0)