From 24653984d08c7863d30451850c6e05fdafa31e37 Mon Sep 17 00:00:00 2001 From: "raymond.lam" Date: Sun, 11 Jun 2017 09:09:25 -0400 Subject: [PATCH 1/2] Writer.prototype.parse to cache by tags in addition to template string --- mustache.js | 2 +- test/parse-test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/mustache.js b/mustache.js index 5ffdfe6..413b6f2 100644 --- a/mustache.js +++ b/mustache.js @@ -447,7 +447,7 @@ var tokens = cache[template]; if (tokens == null) - tokens = cache[template] = parseTemplate(template, tags); + tokens = cache[template + ':' + (tags || mustache.tags).join(':')] = parseTemplate(template, tags); return tokens; }; diff --git a/test/parse-test.js b/test/parse-test.js index 97c26db..e291bc4 100644 --- a/test/parse-test.js +++ b/test/parse-test.js @@ -103,4 +103,36 @@ describe('Mustache.parse', function () { }); }); + describe('when parsing a template without tags specified followed by the same template with tags specified', function() { + it('returns different tokens for the latter parse', function() { + var template = "{{foo}}[bar]"; + var parsedWithBraces = Mustache.parse(template); + var parsedWithBrackets = Mustache.parse(template, ['[', ']']); + assert.notDeepEqual(parsedWithBrackets, parsedWithBraces); + }); + }); + + describe('when parsing a template with tags specified followed by the same template with different tags specified', function() { + it('returns different tokens for the latter parse', function() { + var template = "(foo)[bar]"; + var parsedWithParens = Mustache.parse(template, ['(', ')']); + var parsedWithBrackets = Mustache.parse(template, ['[', ']']); + assert.notDeepEqual(parsedWithBrackets, parsedWithParens); + }); + }); + + describe('when parsing a template after already having parsed that template with a different Mustache.tags', function() { + it('returns different tokens for the latter parse', function() { + var template = "{{foo}}[bar]"; + var parsedWithBraces = Mustache.parse(template, ['(', ')']); + + var oldTags = Mustache.tags; + Mustache.tags = ['[', ']']; + var parsedWithBrackets = Mustache.parse(template); + Mustache.tags = oldTags; + + assert.notDeepEqual(parsedWithBrackets, parsedWithBraces); + }); + }); + }); From 198a5657be5aae1c5042d20a36cfbab20f2f8a56 Mon Sep 17 00:00:00 2001 From: "raymond.lam" Date: Sat, 26 Aug 2017 12:05:36 -0400 Subject: [PATCH 2/2] fix typo in parse-caching/Mustache.tags test --- test/parse-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parse-test.js b/test/parse-test.js index e291bc4..f7633b4 100644 --- a/test/parse-test.js +++ b/test/parse-test.js @@ -124,7 +124,7 @@ describe('Mustache.parse', function () { describe('when parsing a template after already having parsed that template with a different Mustache.tags', function() { it('returns different tokens for the latter parse', function() { var template = "{{foo}}[bar]"; - var parsedWithBraces = Mustache.parse(template, ['(', ')']); + var parsedWithBraces = Mustache.parse(template); var oldTags = Mustache.tags; Mustache.tags = ['[', ']'];