1 var assignInWith = require('./assignInWith'),
2 attempt = require('./attempt'),
3 baseValues = require('./_baseValues'),
4 customDefaultsAssignIn = require('./_customDefaultsAssignIn'),
5 escapeStringChar = require('./_escapeStringChar'),
6 isError = require('./isError'),
7 isIterateeCall = require('./_isIterateeCall'),
8 keys = require('./keys'),
9 reInterpolate = require('./_reInterpolate'),
10 templateSettings = require('./templateSettings'),
11 toString = require('./toString');
13 /** Error message constants. */
14 var INVALID_TEMPL_VAR_ERROR_TEXT = 'Invalid `variable` option passed into `_.template`';
16 /** Used to match empty string literals in compiled template source. */
17 var reEmptyStringLeading = /\b__p \+= '';/g,
18 reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
19 reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
22 * Used to validate the `validate` option in `_.template` variable.
24 * Forbids characters which could potentially change the meaning of the function argument definition:
25 * - "()," (modification of function parameters)
26 * - "=" (default value)
27 * - "[]{}" (destructuring of function parameters)
28 * - "/" (beginning of a comment)
31 var reForbiddenIdentifierChars = /[()=,{}\[\]\/\s]/;
35 * [ES template delimiters](http://ecma-international.org/ecma-262/7.0/#sec-template-literal-lexical-components).
37 var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
39 /** Used to ensure capturing order of template delimiters. */
40 var reNoMatch = /($^)/;
42 /** Used to match unescaped characters in compiled string literals. */
43 var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
45 /** Used for built-in method references. */
46 var objectProto = Object.prototype;
48 /** Used to check objects for own properties. */
49 var hasOwnProperty = objectProto.hasOwnProperty;
52 * Creates a compiled template function that can interpolate data properties
53 * in "interpolate" delimiters, HTML-escape interpolated data properties in
54 * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
55 * properties may be accessed as free variables in the template. If a setting
56 * object is given, it takes precedence over `_.templateSettings` values.
58 * **Note:** In the development build `_.template` utilizes
59 * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
60 * for easier debugging.
62 * For more information on precompiling templates see
63 * [lodash's custom builds documentation](https://lodash.com/custom-builds).
65 * For more information on Chrome extension sandboxes see
66 * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).
72 * @param {string} [string=''] The template string.
73 * @param {Object} [options={}] The options object.
74 * @param {RegExp} [options.escape=_.templateSettings.escape]
75 * The HTML "escape" delimiter.
76 * @param {RegExp} [options.evaluate=_.templateSettings.evaluate]
77 * The "evaluate" delimiter.
78 * @param {Object} [options.imports=_.templateSettings.imports]
79 * An object to import into the template as free variables.
80 * @param {RegExp} [options.interpolate=_.templateSettings.interpolate]
81 * The "interpolate" delimiter.
82 * @param {string} [options.sourceURL='templateSources[n]']
83 * The sourceURL of the compiled template.
84 * @param {string} [options.variable='obj']
85 * The data object variable name.
86 * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
87 * @returns {Function} Returns the compiled template function.
90 * // Use the "interpolate" delimiter to create a compiled template.
91 * var compiled = _.template('hello <%= user %>!');
92 * compiled({ 'user': 'fred' });
95 * // Use the HTML "escape" delimiter to escape data property values.
96 * var compiled = _.template('<b><%- value %></b>');
97 * compiled({ 'value': '<script>' });
98 * // => '<b><script></b>'
100 * // Use the "evaluate" delimiter to execute JavaScript and generate HTML.
101 * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
102 * compiled({ 'users': ['fred', 'barney'] });
103 * // => '<li>fred</li><li>barney</li>'
105 * // Use the internal `print` function in "evaluate" delimiters.
106 * var compiled = _.template('<% print("hello " + user); %>!');
107 * compiled({ 'user': 'barney' });
108 * // => 'hello barney!'
110 * // Use the ES template literal delimiter as an "interpolate" delimiter.
111 * // Disable support by replacing the "interpolate" delimiter.
112 * var compiled = _.template('hello ${ user }!');
113 * compiled({ 'user': 'pebbles' });
114 * // => 'hello pebbles!'
116 * // Use backslashes to treat delimiters as plain text.
117 * var compiled = _.template('<%= "\\<%- value %\\>" %>');
118 * compiled({ 'value': 'ignored' });
119 * // => '<%- value %>'
121 * // Use the `imports` option to import `jQuery` as `jq`.
122 * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
123 * var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
124 * compiled({ 'users': ['fred', 'barney'] });
125 * // => '<li>fred</li><li>barney</li>'
127 * // Use the `sourceURL` option to specify a custom sourceURL for the template.
128 * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
130 * // => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.
132 * // Use the `variable` option to ensure a with-statement isn't used in the compiled template.
133 * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
135 * // => function(data) {
136 * // var __t, __p = '';
137 * // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
141 * // Use custom template delimiters.
142 * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
143 * var compiled = _.template('hello {{ user }}!');
144 * compiled({ 'user': 'mustache' });
145 * // => 'hello mustache!'
147 * // Use the `source` property to inline compiled templates for meaningful
148 * // line numbers in error messages and stack traces.
149 * fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\
151 * "main": ' + _.template(mainText).source + '\
155 function template(string, options, guard) {
156 // Based on John Resig's `tmpl` implementation
157 // (http://ejohn.org/blog/javascript-micro-templating/)
158 // and Laura Doktorova's doT.js (https://github.com/olado/doT).
159 var settings = templateSettings.imports._.templateSettings || templateSettings;
161 if (guard && isIterateeCall(string, options, guard)) {
164 string = toString(string);
165 options = assignInWith({}, options, settings, customDefaultsAssignIn);
167 var imports = assignInWith({}, options.imports, settings.imports, customDefaultsAssignIn),
168 importsKeys = keys(imports),
169 importsValues = baseValues(imports, importsKeys);
174 interpolate = options.interpolate || reNoMatch,
177 // Compile the regexp to match each delimiter.
178 var reDelimiters = RegExp(
179 (options.escape || reNoMatch).source + '|' +
180 interpolate.source + '|' +
181 (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
182 (options.evaluate || reNoMatch).source + '|$'
185 // Use a sourceURL for easier debugging.
186 // The sourceURL gets injected into the source that's eval-ed, so be careful
187 // to normalize all kinds of whitespace, so e.g. newlines (and unicode versions of it) can't sneak in
188 // and escape the comment, thus injecting code that gets evaled.
189 var sourceURL = hasOwnProperty.call(options, 'sourceURL')
190 ? ('//# sourceURL=' +
191 (options.sourceURL + '').replace(/\s/g, ' ') +
195 string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
196 interpolateValue || (interpolateValue = esTemplateValue);
198 // Escape characters that can't be included in string literals.
199 source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
201 // Replace delimiters with snippets.
204 source += "' +\n__e(" + escapeValue + ") +\n'";
208 source += "';\n" + evaluateValue + ";\n__p += '";
210 if (interpolateValue) {
211 source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
213 index = offset + match.length;
215 // The JS engine embedded in Adobe products needs `match` returned in
216 // order to produce the correct `offset` value.
222 // If `variable` is not specified wrap a with-statement around the generated
223 // code to add the data object to the top of the scope chain.
224 var variable = hasOwnProperty.call(options, 'variable') && options.variable;
226 source = 'with (obj) {\n' + source + '\n}\n';
228 // Throw an error if a forbidden character was found in `variable`, to prevent
229 // potential command injection attacks.
230 else if (reForbiddenIdentifierChars.test(variable)) {
231 throw new Error(INVALID_TEMPL_VAR_ERROR_TEXT);
234 // Cleanup code by stripping empty strings.
235 source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
236 .replace(reEmptyStringMiddle, '$1')
237 .replace(reEmptyStringTrailing, '$1;');
239 // Frame code as the function body.
240 source = 'function(' + (variable || 'obj') + ') {\n' +
243 : 'obj || (obj = {});\n'
245 "var __t, __p = ''" +
251 ? ', __j = Array.prototype.join;\n' +
252 "function print() { __p += __j.call(arguments, '') }\n"
258 var result = attempt(function() {
259 return Function(importsKeys, sourceURL + 'return ' + source)
260 .apply(undefined, importsValues);
263 // Provide the compiled function's source by its `toString` method or
264 // the `source` property as a convenience for inlining compiled templates.
265 result.source = source;
266 if (isError(result)) {
272 module.exports = template;