From b2481edb3fbc5babf3ff331da2433d977cd860d8 Mon Sep 17 00:00:00 2001 From: "raymond.lam" Date: Thu, 16 Aug 2018 09:16:28 -0400 Subject: [PATCH 1/3] Rollback pull requests #643 and #664 --- mustache.js | 5 ++--- test/parse-test.js | 47 ---------------------------------------------- 2 files changed, 2 insertions(+), 50 deletions(-) diff --git a/mustache.js b/mustache.js index 5840d3b28..d07fd1964 100644 --- a/mustache.js +++ b/mustache.js @@ -444,11 +444,10 @@ */ Writer.prototype.parse = function parse (template, tags) { var cache = this.cache; - var cacheKey = template + ':' + (tags || mustache.tags).join(':'); - var tokens = cache[cacheKey]; + var tokens = cache[template]; if (tokens == null) - tokens = cache[cacheKey] = parseTemplate(template, tags); + tokens = cache[template] = parseTemplate(template, tags); return tokens; }; diff --git a/test/parse-test.js b/test/parse-test.js index 959699bc2..97c26db94 100644 --- a/test/parse-test.js +++ b/test/parse-test.js @@ -53,10 +53,6 @@ var expectations = { : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ] }; -beforeEach(function (){ - Mustache.clearCache(); -}); - describe('Mustache.parse', function () { for (var template in expectations) { @@ -107,47 +103,4 @@ 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); - }); - }); - - describe('when parsing a template with the same tags second time, return the cached tokens', function () { - it('returns the same tokens for the latter parse', function () { - var template = '{{foo}}[bar]'; - var parsedResult1 = Mustache.parse(template); - var parsedResult2 = Mustache.parse(template); - - assert.deepEqual(parsedResult1, parsedResult2); - assert.ok(parsedResult1 === parsedResult2); - }); - }); - }); From 21e65b89d65e91f876ab8bb7b51c96eb3bf12e68 Mon Sep 17 00:00:00 2001 From: "raymond.lam" Date: Thu, 16 Aug 2018 09:30:29 -0400 Subject: [PATCH 2/3] Reintroduce pull requests #643 and #664, and update description for parse method --- mustache.js | 8 +++++--- test/parse-test.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/mustache.js b/mustache.js index d07fd1964..27704b8ee 100644 --- a/mustache.js +++ b/mustache.js @@ -439,15 +439,17 @@ }; /** - * 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 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; }; diff --git a/test/parse-test.js b/test/parse-test.js index 97c26db94..959699bc2 100644 --- a/test/parse-test.js +++ b/test/parse-test.js @@ -53,6 +53,10 @@ var expectations = { : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ] }; +beforeEach(function (){ + Mustache.clearCache(); +}); + describe('Mustache.parse', function () { for (var template in expectations) { @@ -103,4 +107,47 @@ 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); + }); + }); + + describe('when parsing a template with the same tags second time, return the cached tokens', function () { + it('returns the same tokens for the latter parse', function () { + var template = '{{foo}}[bar]'; + var parsedResult1 = Mustache.parse(template); + var parsedResult2 = Mustache.parse(template); + + assert.deepEqual(parsedResult1, parsedResult2); + assert.ok(parsedResult1 === parsedResult2); + }); + }); + }); From 622b9c9b23aa3d65c51993ffc09caec475422b0d Mon Sep 17 00:00:00 2001 From: "raymond.lam" Date: Thu, 16 Aug 2018 22:58:29 -0400 Subject: [PATCH 3/3] Update README.md to reflect correct caching behavior --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index fa33ee535..b7baa5c40 100644 --- a/README.md +++ b/README.md @@ -504,17 +504,12 @@ Custom delimiters can be used in place of `{{` and `}}` by setting the new value #### Setting in JavaScript -The `Mustache.tags` property holds an array consisting of the opening and closing tag values. Set custom values by passing a new array of tags to `parse()`, which gets honored over the default values, or by overriding the `tags` property itself: +The `Mustache.tags` property holds an array consisting of the opening and closing tag values. Set custom values by setting this property. ```js var customTags = [ '<%', '%>' ]; ``` -##### Pass Value into Parse Method -```js -Mustache.parse(template, customTags); -``` - ##### Override Tags Property ```js Mustache.tags = customTags;