Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: cache actually not working #664

Merged
merged 1 commit into from Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions mustache.js
Expand Up @@ -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;
};
Expand Down
15 changes: 15 additions & 0 deletions test/parse-test.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
});

});