No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

208 líneas
10KB

  1. require('./helper');
  2. // A map of templates to their expected token output. Tokens are in the format:
  3. // [type, value, startIndex, endIndex, subTokens].
  4. var expectations = {
  5. '' : [],
  6. '{{hi}}' : [ [ 'name', 'hi', 0, 6 ] ],
  7. '{{hi.world}}' : [ [ 'name', 'hi.world', 0, 12 ] ],
  8. '{{hi . world}}' : [ [ 'name', 'hi . world', 0, 14 ] ],
  9. '{{ hi}}' : [ [ 'name', 'hi', 0, 7 ] ],
  10. '{{hi }}' : [ [ 'name', 'hi', 0, 7 ] ],
  11. '{{ hi }}' : [ [ 'name', 'hi', 0, 8 ] ],
  12. '{{{hi}}}' : [ [ '&', 'hi', 0, 8 ] ],
  13. '{{!hi}}' : [ [ '!', 'hi', 0, 7 ] ],
  14. '{{! hi}}' : [ [ '!', 'hi', 0, 8 ] ],
  15. '{{! hi }}' : [ [ '!', 'hi', 0, 9 ] ],
  16. '{{ !hi}}' : [ [ '!', 'hi', 0, 8 ] ],
  17. '{{ ! hi}}' : [ [ '!', 'hi', 0, 9 ] ],
  18. '{{ ! hi }}' : [ [ '!', 'hi', 0, 10 ] ],
  19. 'a\n b' : [ [ 'text', 'a\n b', 0, 4 ] ],
  20. 'a{{hi}}' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ] ],
  21. 'a {{hi}}' : [ [ 'text', 'a ', 0, 2 ], [ 'name', 'hi', 2, 8 ] ],
  22. ' a{{hi}}' : [ [ 'text', ' a', 0, 2 ], [ 'name', 'hi', 2, 8 ] ],
  23. ' a {{hi}}' : [ [ 'text', ' a ', 0, 3 ], [ 'name', 'hi', 3, 9 ] ],
  24. 'a{{hi}}b' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b', 7, 8 ] ],
  25. 'a{{hi}} b' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', ' b', 7, 9 ] ],
  26. 'a{{hi}}b ' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b ', 7, 9 ] ],
  27. 'a\n{{hi}} b \n' : [ [ 'text', 'a\n', 0, 2 ], [ 'name', 'hi', 2, 8 ], [ 'text', ' b \n', 8, 12 ] ],
  28. 'a\n {{hi}} \nb' : [ [ 'text', 'a\n ', 0, 3 ], [ 'name', 'hi', 3, 9 ], [ 'text', ' \nb', 9, 12 ] ],
  29. 'a\n {{!hi}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '!', 'hi', 3, 10 ], [ 'text', 'b', 12, 13 ] ],
  30. 'a\n{{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 8 ], [ 'text', 'b', 15, 16 ] ],
  31. 'a\n {{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 16, 17 ] ],
  32. 'a\n {{#a}}{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 17, 18 ] ],
  33. 'a\n{{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ 'text', 'b', 16, 17 ] ],
  34. 'a\n {{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 17, 18 ] ],
  35. 'a\n {{#a}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 18, 19 ] ],
  36. 'a\n{{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ '#', 'b', 16, 22, [], 23 ], [ 'text', 'b', 30, 31 ] ],
  37. 'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 31, 32 ] ],
  38. 'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 32, 33 ] ],
  39. 'a\n{{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [ [ '#', 'b', 9, 15, [], 16 ] ], 23 ], [ 'text', 'b', 30, 31 ] ],
  40. 'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 31, 32 ] ],
  41. 'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 32, 33 ] ],
  42. '{{>abc}}' : [ [ '>', 'abc', 0, 8, '', 0, false ] ],
  43. '{{> abc }}' : [ [ '>', 'abc', 0, 10, '', 0, false ] ],
  44. '{{ > abc }}' : [ [ '>', 'abc', 0, 11, '', 0, false ] ],
  45. ' {{> abc }}\n' : [ [ '>', 'abc', 2, 12, ' ', 0, false ] ],
  46. ' {{> abc }} {{> abc }}\n' : [ [ '>', 'abc', 2, 12, ' ', 0, false ], [ '>', 'abc', 13, 23, ' ', 1, false ] ],
  47. '{{ > abc }}' : [ [ '>', 'abc', 0, 11, '', 0, false ] ],
  48. '{{=<% %>=}}' : [ [ '=', '<% %>', 0, 11 ] ],
  49. '{{= <% %> =}}' : [ [ '=', '<% %>', 0, 13 ] ],
  50. '{{=<% %>=}}<%={{ }}=%>' : [ [ '=', '<% %>', 0, 11 ], [ '=', '{{ }}', 11, 22 ] ],
  51. '{{=<% %>=}}<%hi%>' : [ [ '=', '<% %>', 0, 11 ], [ 'name', 'hi', 11, 17 ] ],
  52. '{{#a}}{{/a}}hi{{#b}}{{/b}}\n' : [ [ '#', 'a', 0, 6, [], 6 ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 20, [], 20 ], [ 'text', '\n', 26, 27 ] ],
  53. '{{a}}\n{{b}}\n\n{{#c}}\n{{/c}}\n' : [ [ 'name', 'a', 0, 5 ], [ 'text', '\n', 5, 6 ], [ 'name', 'b', 6, 11 ], [ 'text', '\n\n', 11, 13 ], [ '#', 'c', 13, 19, [], 20 ] ],
  54. '{{#foo}}\n {{#a}}\n {{b}}\n {{/a}}\n{{/foo}}\n'
  55. : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ]
  56. };
  57. beforeEach(function (){
  58. Mustache.clearCache();
  59. Mustache.templateCache = {
  60. _cache: {},
  61. set: function set (key, value) {
  62. this._cache[key] = value;
  63. },
  64. get: function get (key) {
  65. return this._cache[key];
  66. },
  67. clear: function clear () {
  68. this._cache = {};
  69. }
  70. };
  71. });
  72. describe('Mustache.parse', function () {
  73. for (var template in expectations) {
  74. (function (template, tokens) {
  75. it('knows how to parse ' + JSON.stringify(template), function () {
  76. assert.deepEqual(Mustache.parse(template), tokens);
  77. });
  78. })(template, expectations[template]);
  79. }
  80. describe('when there is an unclosed tag', function () {
  81. it('throws an error', function () {
  82. assert.throws(function () {
  83. Mustache.parse('My name is {{name');
  84. }, /unclosed tag at 17/i);
  85. });
  86. });
  87. describe('when there is an unclosed section', function () {
  88. it('throws an error', function () {
  89. assert.throws(function () {
  90. Mustache.parse('A list: {{#people}}{{name}}');
  91. }, /unclosed section "people" at 27/i);
  92. });
  93. });
  94. describe('when there is an unopened section', function () {
  95. it('throws an error', function () {
  96. assert.throws(function () {
  97. Mustache.parse('The end of the list! {{/people}}');
  98. }, /unopened section "people" at 21/i);
  99. });
  100. });
  101. describe('when invalid tags are given as an argument', function () {
  102. it('throws an error', function () {
  103. assert.throws(function () {
  104. Mustache.parse('A template <% name %>', [ '<%' ]);
  105. }, /invalid tags/i);
  106. });
  107. });
  108. describe('when the template contains invalid tags', function () {
  109. it('throws an error', function () {
  110. assert.throws(function () {
  111. Mustache.parse('A template {{=<%=}}');
  112. }, /invalid tags/i);
  113. });
  114. });
  115. describe('when parsing a template without tags specified followed by the same template with tags specified', function () {
  116. it('returns different tokens for the latter parse', function () {
  117. var template = '{{foo}}[bar]';
  118. var parsedWithBraces = Mustache.parse(template);
  119. var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
  120. assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
  121. });
  122. });
  123. describe('when parsing a template with tags specified followed by the same template with different tags specified', function () {
  124. it('returns different tokens for the latter parse', function () {
  125. var template = '(foo)[bar]';
  126. var parsedWithParens = Mustache.parse(template, ['(', ')']);
  127. var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
  128. assert.notDeepEqual(parsedWithBrackets, parsedWithParens);
  129. });
  130. });
  131. describe('when parsing a template after already having parsed that template with a different Mustache.tags', function () {
  132. it('returns different tokens for the latter parse', function () {
  133. var template = '{{foo}}[bar]';
  134. var parsedWithBraces = Mustache.parse(template);
  135. var oldTags = Mustache.tags;
  136. Mustache.tags = ['[', ']'];
  137. var parsedWithBrackets = Mustache.parse(template);
  138. Mustache.tags = oldTags;
  139. assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
  140. });
  141. });
  142. describe('when parsing a template with the same tags second time, return the cached tokens', function () {
  143. it('returns the same tokens for the latter parse', function () {
  144. var template = '{{foo}}[bar]';
  145. var parsedResult1 = Mustache.parse(template);
  146. var parsedResult2 = Mustache.parse(template);
  147. assert.deepEqual(parsedResult1, parsedResult2);
  148. assert.ok(parsedResult1 === parsedResult2);
  149. });
  150. });
  151. describe('when parsing a template with caching disabled and the same tags second time, do not return the cached tokens', function () {
  152. it('returns different tokens for the latter parse', function () {
  153. Mustache.templateCache = undefined;
  154. var template = '{{foo}}[bar]';
  155. var parsedResult1 = Mustache.parse(template);
  156. var parsedResult2 = Mustache.parse(template);
  157. assert.deepEqual(parsedResult1, parsedResult2);
  158. assert.ok(parsedResult1 !== parsedResult2);
  159. });
  160. });
  161. describe('when parsing a template with custom caching and the same tags second time, do not return the cached tokens', function () {
  162. it('returns the same tokens for the latter parse', function () {
  163. Mustache.templateCache = {
  164. _cache: [],
  165. set: function set (key, value) {
  166. this._cache.push([key, value]);
  167. },
  168. get: function get (key) {
  169. var entry = this._cache.find(function (item) {
  170. return item[0] === key;
  171. });
  172. return entry && entry[1];
  173. },
  174. clear: function clear () {
  175. this._cache = [];
  176. }
  177. };
  178. var template = '{{foo}}[bar]';
  179. var parsedResult1 = Mustache.parse(template);
  180. var parsedResult2 = Mustache.parse(template);
  181. assert.deepEqual(parsedResult1, parsedResult2);
  182. assert.ok(parsedResult1 === parsedResult2);
  183. });
  184. });
  185. });