Przeglądaj źródła

Reintroduce pull requests #643 and #664, and update description for

parse method
tags/v3.0.0
raymond.lam Phillip Johnsen 7 lat temu
rodzic
commit
78458488a4
2 zmienionych plików z 52 dodań i 3 usunięć
  1. +5
    -3
      mustache.js
  2. +47
    -0
      test/parse-test.js

+ 5
- 3
mustache.js Wyświetl plik

@@ -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. * that is generated from the parse.
*/ */
Writer.prototype.parse = function parse (template, tags) { Writer.prototype.parse = function parse (template, tags) {
var cache = this.cache; var cache = this.cache;
var tokens = cache[template];
var cacheKey = template + ':' + (tags || mustache.tags).join(':');
var tokens = cache[cacheKey];


if (tokens == null) if (tokens == null)
tokens = cache[template] = parseTemplate(template, tags);
tokens = cache[cacheKey] = parseTemplate(template, tags);


return tokens; return tokens;
}; };


+ 47
- 0
test/parse-test.js Wyświetl plik

@@ -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 ] ] : [ [ '#', '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 () { describe('Mustache.parse', function () {


for (var template in expectations) { 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);
});
});

}); });

Ładowanie…
Anuluj
Zapisz