From c6930446fedd5fde1f02bff7f5de90bfedd000cc Mon Sep 17 00:00:00 2001 From: Hongbo LU Date: Tue, 17 Mar 2015 17:04:36 +0100 Subject: [PATCH] more template string validation --- js/tmpl.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/js/tmpl.js b/js/tmpl.js index 9380053..a57cd01 100644 --- a/js/tmpl.js +++ b/js/tmpl.js @@ -18,6 +18,9 @@ (function ($) { "use strict"; var tmpl = function (str, data) { + if(typeof(str) !== 'string'){ + throw new Error('str must be a string'); + } var f = !/[^\w\-\.:]/.test(str) ? tmpl.cache[str] = tmpl.cache[str] || tmpl(tmpl.load(str)) : new Function( @@ -32,7 +35,11 @@ }; tmpl.cache = {}; tmpl.load = function (id) { - return document.getElementById(id).innerHTML; + var node = document.getElementById(id); + if(!node){ + throw new Error('DOM Node '+id+' not found'); + } + return node.innerHTML; }; tmpl.regexp = /([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g; tmpl.func = function (s, p1, p2, p3, p4, p5) {