소스 검색

Merge pull request #467 from janl/feature/meaningful-type-error

Throw error when providing .render() with invalid template type
tags/v2.1.3
Phillip Johnsen 11 년 전
부모
커밋
06b7392200
3개의 변경된 파일28개의 추가작업 그리고 0개의 파일을 삭제
  1. +14
    -0
      mustache.js
  2. +6
    -0
      test/render-test-browser-tmpl.mustache
  3. +8
    -0
      test/render-test.js

+ 14
- 0
mustache.js 파일 보기

@@ -25,6 +25,14 @@
return typeof object === 'function';
}

/**
* More correct typeof string handling array
* which normally returns typeof 'object'
*/
function typeStr (obj) {
return isArray(obj) ? 'array' : typeof obj;
}

function escapeRegExp (string) {
return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
}
@@ -584,6 +592,12 @@
* default writer.
*/
mustache.render = function render (template, view, partials) {
if (typeof template !== 'string') {
throw new TypeError('Invalid template! Template should be a "string" ' +
'but "' + typeStr(template) + '" was given as the first ' +
'argument for mustache#render(template, view, partials)');
}

return defaultWriter.render(template, view, partials);
};



+ 6
- 0
test/render-test-browser-tmpl.mustache 파일 보기

@@ -5,6 +5,12 @@ describe('Mustache.render', function () {
Mustache.clearCache();
});

it('requires template to be a string', function () {
assert.throws(function () {
Mustache.render(['dummy template'], ['foo', 'bar']);
}, TypeError, 'Invalid template! Template should be a "string" but "array" was given');
});

var i;
var tests = {{{.}}};



+ 8
- 0
test/render-test.js 파일 보기

@@ -9,6 +9,14 @@ describe('Mustache.render', function () {
Mustache.clearCache();
});

it('requires template to be a string', function () {
assert.throws(function () {
Mustache.render(['dummy template'], ['foo', 'bar']);
}, TypeError, 'Invalid template! Template should be a "string" but ' +
'"array" was given as the first argument ' +
'for mustache#render(template, view, partials)');
});

tests.forEach(function (test) {
var view = eval(test.view);



불러오는 중...
취소
저장