From 28ca619a24fc2930b0cd4e77d369d57a95abf2bc Mon Sep 17 00:00:00 2001 From: Ashinoko Date: Sat, 28 Jul 2018 07:14:38 +0900 Subject: [PATCH] fix bug: cache actually not working (#664) Resolve issue #663 and add test. --- mustache.js | 5 +++-- test/parse-test.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/mustache.js b/mustache.js index 413b6f25e..64ab2d211 100644 --- a/mustache.js +++ b/mustache.js @@ -444,10 +444,11 @@ */ 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 + ':' + (tags || mustache.tags).join(':')] = parseTemplate(template, tags); + tokens = cache[cacheKey] = parseTemplate(template, tags); return tokens; }; diff --git a/test/parse-test.js b/test/parse-test.js index f7633b4a6..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) { @@ -135,4 +139,15 @@ describe('Mustache.parse', function () { }); }); + 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); + }); + }); + });