Sfoglia il codice sorgente

Throw error when providing .render() with invalid template type.

As we require the template to be a `string` we now throw an error when given
any other data type. This should provide the developers with a meaningful
error rather than a cryptic TypeError from the murky depths of our source code.

Fixes #464
tags/v2.1.3
Phillip Johnsen 11 anni fa
parent
commit
a3f4339c65
3 ha cambiato i file con 28 aggiunte e 0 eliminazioni
  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 Vedi File

@@ -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 Vedi File

@@ -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 Vedi File

@@ -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);



Loading…
Annulla
Salva