|
18 | 18 | 'use strict' |
19 | 19 | var tmpl = function (str, data) { |
20 | 20 | var f = !/[^\w\-.:]/.test(str) |
21 | | - ? tmpl.cache[str] = tmpl.cache[str] || tmpl(tmpl.load(str)) |
22 | | - : new Function(// eslint-disable-line no-new-func |
| 21 | + ? (tmpl.cache[str] = tmpl.cache[str] || tmpl(tmpl.load(str))) |
| 22 | + : new Function( // eslint-disable-line no-new-func |
23 | 23 | tmpl.arg + ',tmpl', |
24 | | - 'var _e=tmpl.encode' + tmpl.helper + ",_s='" + |
25 | | - str.replace(tmpl.regexp, tmpl.func) + "';return _s;" |
| 24 | + 'var _e=tmpl.encode' + |
| 25 | + tmpl.helper + |
| 26 | + ",_s='" + |
| 27 | + str.replace(tmpl.regexp, tmpl.func) + |
| 28 | + "';return _s;" |
26 | 29 | ) |
27 | | - return data ? f(data, tmpl) : function (data) { |
28 | | - return f(data, tmpl) |
29 | | - } |
| 30 | + return data |
| 31 | + ? f(data, tmpl) |
| 32 | + : function (data) { |
| 33 | + return f(data, tmpl) |
| 34 | + } |
30 | 35 | } |
31 | 36 | tmpl.cache = {} |
32 | 37 | tmpl.load = function (id) { |
33 | 38 | return document.getElementById(id).innerHTML |
34 | 39 | } |
35 | 40 | tmpl.regexp = /([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g |
36 | 41 | tmpl.func = function (s, p1, p2, p3, p4, p5) { |
37 | | - if (p1) { // whitespace, quote and backspace in HTML context |
38 | | - return { |
39 | | - '\n': '\\n', |
40 | | - '\r': '\\r', |
41 | | - '\t': '\\t', |
42 | | - ' ': ' ' |
43 | | - }[p1] || '\\' + p1 |
| 42 | + if (p1) { |
| 43 | + // whitespace, quote and backspace in HTML context |
| 44 | + return ( |
| 45 | + { |
| 46 | + '\n': '\\n', |
| 47 | + '\r': '\\r', |
| 48 | + '\t': '\\t', |
| 49 | + ' ': ' ' |
| 50 | + }[p1] || '\\' + p1 |
| 51 | + ) |
44 | 52 | } |
45 | | - if (p2) { // interpolation: {%=prop%}, or unescaped: {%#prop%} |
| 53 | + if (p2) { |
| 54 | + // interpolation: {%=prop%}, or unescaped: {%#prop%} |
46 | 55 | if (p2 === '=') { |
47 | 56 | return "'+_e(" + p3 + ")+'" |
48 | 57 | } |
49 | 58 | return "'+(" + p3 + "==null?'':" + p3 + ")+'" |
50 | 59 | } |
51 | | - if (p4) { // evaluation start tag: {% |
| 60 | + if (p4) { |
| 61 | + // evaluation start tag: {% |
52 | 62 | return "';" |
53 | 63 | } |
54 | | - if (p5) { // evaluation end tag: %} |
| 64 | + if (p5) { |
| 65 | + // evaluation end tag: %} |
55 | 66 | return "_s+='" |
56 | 67 | } |
57 | 68 | } |
|
64 | 75 | "'": ''' |
65 | 76 | } |
66 | 77 | tmpl.encode = function (s) { |
67 | | - return (s == null ? '' : '' + s).replace( |
68 | | - tmpl.encReg, |
69 | | - function (c) { |
70 | | - return tmpl.encMap[c] || '' |
71 | | - } |
72 | | - ) |
| 78 | + return (s == null ? '' : '' + s).replace(tmpl.encReg, function (c) { |
| 79 | + return tmpl.encMap[c] || '' |
| 80 | + }) |
73 | 81 | } |
74 | 82 | tmpl.arg = 'o' |
75 | | - tmpl.helper = ",print=function(s,e){_s+=e?(s==null?'':s):_e(s);}" + |
76 | | - ',include=function(s,d){_s+=tmpl(s,d);}' |
| 83 | + tmpl.helper = |
| 84 | + ",print=function(s,e){_s+=e?(s==null?'':s):_e(s);}" + |
| 85 | + ',include=function(s,d){_s+=tmpl(s,d);}' |
77 | 86 | if (typeof define === 'function' && define.amd) { |
78 | 87 | define(function () { |
79 | 88 | return tmpl |
|
83 | 92 | } else { |
84 | 93 | $.tmpl = tmpl |
85 | 94 | } |
86 | | -}(this)) |
| 95 | +})(this) |
0 commit comments