Procházet zdrojové kódy

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 před 11 roky
rodič
revize
06b7392200
3 změnil soubory, kde provedl 28 přidání a 0 odebrání
  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 Zobrazit soubor

@@ -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 Zobrazit soubor

@@ -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 Zobrazit soubor

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



Načítá se…
Zrušit
Uložit