You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I haven't run into actual issues with this, but I noticed that Writer.prototype.parse caches the result, where the cache key is just the template text. Therefore, these tests would fail:
describe('Mustache.parse', function () {
...
describe('when parsing a template without tags specified then 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 then a 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);
});
});
});
This issue is easily solved by changing Writer.prototype.parse to this (the change being to include the tags in the cache key):
Writer.prototype.parse = function parse (template, tags) {
var cache = this.cache;
var tokens = cache[template];
if (tokens == null)
tokens = cache[template + ':' + (tags || mustache.tags).join(':')] = parseTemplate(template, tags);
return tokens;
};
It's unlikely that one would actually run into this bug in real life, though parse would be more correct if this simple fix was include. Do we want to fix this bug, even if it is unlikely to manifest?
One could argue that my suggested fix above breaks separation of concerns or some principle of abstraction. Writer.prototype.parse is simply a memoization of parseTemplate -- its only concern should be to cache the results of a parse. The actual details of how parsing is accomplished is in parseTemplate, and the fact that parsing falls back onto mustache.tags is one such detail. Referencing it in Writer.prototype.parse would be introducing a leak into the parseTemplate abstraction. Thoughts?
The text was updated successfully, but these errors were encountered:
Pull requests #643 and #664 together fix the issue reported in issue #617, but the change in behavior is causing semvers concerns. See issue #669. Therefore, roll back #643 and #664 and later reintroduce in next planned major release (v3.0).
Fixes#669
I haven't run into actual issues with this, but I noticed that
Writer.prototype.parse
caches the result, where the cache key is just the template text. Therefore, these tests would fail:This issue is easily solved by changing
Writer.prototype.parse
to this (the change being to include the tags in the cache key):parse
would be more correct if this simple fix was include. Do we want to fix this bug, even if it is unlikely to manifest?Writer.prototype.parse
is simply a memoization ofparseTemplate
-- its only concern should be to cache the results of a parse. The actual details of how parsing is accomplished is inparseTemplate
, and the fact that parsing falls back ontomustache.tags
is one such detail. Referencing it inWriter.prototype.parse
would be introducing a leak into theparseTemplate
abstraction. Thoughts?The text was updated successfully, but these errors were encountered: