Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. require('./helper');
  2. var renderHelper = require('./render-helper');
  3. var tests = renderHelper.getTests();
  4. describe('Mustache.render', function () {
  5. beforeEach(function () {
  6. Mustache.clearCache();
  7. });
  8. it('requires template to be a string', function () {
  9. assert.throws(function () {
  10. Mustache.render(['dummy template'], ['foo', 'bar']);
  11. }, TypeError, 'Invalid template! Template should be a "string" but ' +
  12. '"array" was given as the first argument ' +
  13. 'for mustache#render(template, view, partials)');
  14. });
  15. describe('custom tags', function () {
  16. it('uses tags argument instead of Mustache.tags when given', function () {
  17. var template = '<<placeholder>>bar{{placeholder}}';
  18. Mustache.tags = ['{{', '}}'];
  19. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']), 'foobar{{placeholder}}');
  20. });
  21. it('uses config.tags argument instead of Mustache.tags when given', function () {
  22. var template = '<<placeholder>>bar{{placeholder}}';
  23. Mustache.tags = ['{{', '}}'];
  24. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['<<', '>>']}), 'foobar{{placeholder}}');
  25. });
  26. it('uses tags argument instead of Mustache.tags when given, even when it previously rendered the template using Mustache.tags', function () {
  27. var template = '((placeholder))bar{{placeholder}}';
  28. Mustache.tags = ['{{', '}}'];
  29. Mustache.render(template, { placeholder: 'foo' });
  30. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['((', '))']), 'foobar{{placeholder}}');
  31. });
  32. it('uses config.tags argument instead of Mustache.tags when given, even when it previously rendered the template using Mustache.tags', function () {
  33. var template = '((placeholder))bar{{placeholder}}';
  34. Mustache.tags = ['{{', '}}'];
  35. Mustache.render(template, { placeholder: 'foo' });
  36. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['((', '))'] }), 'foobar{{placeholder}}');
  37. });
  38. it('uses tags argument instead of Mustache.tags when given, even when it previously rendered the template using different tags', function () {
  39. var template = '[[placeholder]]bar<<placeholder>>';
  40. Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
  41. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['[[', ']]']), 'foobar<<placeholder>>');
  42. });
  43. it('uses config.tags argument instead of Mustache.tags when given, even when it previously rendered the template using different tags', function () {
  44. var template = '[[placeholder]]bar<<placeholder>>';
  45. Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
  46. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['[[', ']]'] }), 'foobar<<placeholder>>');
  47. });
  48. it('does not mutate Mustache.tags when given tags argument', function () {
  49. var correctMustacheTags = ['{{', '}}'];
  50. Mustache.tags = correctMustacheTags;
  51. Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, ['((', '))']);
  52. assert.equal(Mustache.tags, correctMustacheTags);
  53. assert.deepEqual(Mustache.tags, ['{{', '}}']);
  54. });
  55. it('does not mutate Mustache.tags when given config.tags argument', function () {
  56. var correctMustacheTags = ['{{', '}}'];
  57. Mustache.tags = correctMustacheTags;
  58. Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, { tags: ['((', '))'] });
  59. assert.equal(Mustache.tags, correctMustacheTags);
  60. assert.deepEqual(Mustache.tags, ['{{', '}}']);
  61. });
  62. it('uses provided tags when rendering partials', function () {
  63. var output = Mustache.render('<%> partial %>', { name: 'Santa Claus' }, {
  64. partial: '<% name %>'
  65. }, ['<%', '%>']);
  66. assert.equal(output, 'Santa Claus');
  67. });
  68. it('uses provided config.tags when rendering partials', function () {
  69. var output = Mustache.render('<%> partial %>', { name: 'Santa Claus' }, {
  70. partial: '<% name %>'
  71. }, { tags: ['<%', '%>'] });
  72. assert.equal(output, 'Santa Claus');
  73. });
  74. it('uses config.escape argument instead of Mustache.escape when given', function () {
  75. var template = 'Hello, {{placeholder}}';
  76. function escapeBang (text) {
  77. return text + '!';
  78. }
  79. assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, { escape: escapeBang }), 'Hello, world!');
  80. });
  81. it('uses config.escape argument instead of Mustache.escape when given, even when it previously rendered the template using Mustache.escape', function () {
  82. var template = 'Hello, {{placeholder}}';
  83. function escapeQuestion (text) {
  84. return text + '?';
  85. }
  86. Mustache.render(template, { placeholder: 'world' });
  87. assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, { escape: escapeQuestion }), 'Hello, world?');
  88. });
  89. it('uses config.escape argument instead of Mustache.escape when given, even when it previously rendered the template using a different escape function', function () {
  90. var template = 'Hello, {{placeholder}}';
  91. function escapeQuestion (text) {
  92. return text + '?';
  93. }
  94. function escapeBang (text) {
  95. return text + '!';
  96. }
  97. Mustache.render(template, { placeholder: 'foo' }, {}, { escape: escapeQuestion });
  98. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { escape: escapeBang }), 'Hello, foo!');
  99. });
  100. it('does not mutate Mustache.escape when given config.escape argument', function () {
  101. var correctMustacheEscape = Mustache.escape;
  102. function escapeNone (text) {
  103. return text;
  104. }
  105. Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, { escape: escapeNone });
  106. assert.equal(Mustache.escape, correctMustacheEscape);
  107. assert.equal(Mustache.escape('>&'), '&gt;&amp;');
  108. });
  109. it('uses provided config.escape when rendering partials', function () {
  110. function escapeDoubleAmpersand (text) {
  111. return text.replace('&', '&&');
  112. }
  113. var output = Mustache.render('{{> partial }}', { name: 'Ampersand &' }, {
  114. partial: '{{ name }}'
  115. }, { escape: escapeDoubleAmpersand });
  116. assert.equal(output, 'Ampersand &&');
  117. });
  118. it('uses config.tags and config.escape arguments instead of Mustache.tags and Mustache.escape when given', function () {
  119. var template = 'Hello, {{placeholder}} [[placeholder]]';
  120. function escapeTwoBangs (text) {
  121. return text + '!!';
  122. }
  123. var config = {
  124. tags: ['[[', ']]'],
  125. escape: escapeTwoBangs,
  126. };
  127. assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, config), 'Hello, {{placeholder}} world!!');
  128. });
  129. it('uses provided config.tags and config.escape when rendering partials', function () {
  130. function escapeDoubleAmpersand (text) {
  131. return text.replace('&', '&&');
  132. }
  133. var config = {
  134. tags: ['[[', ']]'],
  135. escape: escapeDoubleAmpersand
  136. };
  137. var output = Mustache.render('[[> partial ]]', { name: 'Ampersand &' }, {
  138. partial: '[[ name ]]'
  139. }, config);
  140. assert.equal(output, 'Ampersand &&');
  141. });
  142. it('uses provided config.tags and config.escape when rendering sections', function () {
  143. var template = (
  144. '<[[&value-raw]]: ' +
  145. '[[#test-1]][[value-1]][[/test-1]]' +
  146. '[[^test-2]][[value-2]][[/test-2]], ' +
  147. '[[#test-lambda]][[value-lambda]][[/test-lambda]]' +
  148. '>'
  149. );
  150. function escapeQuotes (text) {
  151. return '"' + text + '"';
  152. }
  153. var config = {
  154. tags: ['[[', ']]'],
  155. escape: escapeQuotes
  156. };
  157. var viewTestTrue = {
  158. 'value-raw': 'foo',
  159. 'test-1': true,
  160. 'value-1': 'abc',
  161. 'test-2': true,
  162. 'value-2': '123',
  163. 'test-lambda': function () {
  164. return function (text, render) { return 'lambda: ' + render(text); };
  165. },
  166. 'value-lambda': 'bar'
  167. };
  168. var viewTestFalse = {
  169. 'value-raw': 'foo',
  170. 'test-1': false,
  171. 'value-1': 'abc',
  172. 'test-2': false,
  173. 'value-2': '123',
  174. 'test-lambda': function () {
  175. return function (text, render) { return 'lambda: ' + render(text); };
  176. },
  177. 'value-lambda': 'bar'
  178. };
  179. var outputTrue = Mustache.render(template, viewTestTrue, {}, config);
  180. var outputFalse = Mustache.render(template, viewTestFalse, {}, config);
  181. assert.equal(outputTrue, '<foo: "abc", lambda: "bar">');
  182. assert.equal(outputFalse, '<foo: "123", lambda: "bar">');
  183. });
  184. });
  185. tests.forEach(function (test) {
  186. var view = eval(test.view);
  187. it('knows how to render ' + test.name, function () {
  188. var output;
  189. if (test.partial) {
  190. output = Mustache.render(test.template, view, { partial: test.partial });
  191. } else {
  192. output = Mustache.render(test.template, view);
  193. }
  194. output.should.equal(test.expect);
  195. });
  196. });
  197. });