basant307/AI_Governance_Project
045
1import arrayEach from './_arrayEach.js';2import assignWith from './assignWith.js';3import attempt from './attempt.js';4import baseValues from './_baseValues.js';5import customDefaultsAssignIn from './_customDefaultsAssignIn.js';6import escapeStringChar from './_escapeStringChar.js';7import isError from './isError.js';8import isIterateeCall from './_isIterateeCall.js';9import keys from './keys.js';10import reInterpolate from './_reInterpolate.js';11import templateSettings from './templateSettings.js';12import toString from './toString.js';13 14/** Error message constants. */15var INVALID_TEMPL_VAR_ERROR_TEXT = 'Invalid `variable` option passed into `_.template`',16 INVALID_TEMPL_IMPORTS_ERROR_TEXT = 'Invalid `imports` option passed into `_.template`';17 18/** Used to match empty string literals in compiled template source. */19var reEmptyStringLeading = /\b__p \+= '';/g,20 reEmptyStringMiddle = /\b(__p \+=) '' \+/g,21 reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;22 23/**24 * Used to validate the `validate` option in `_.template` variable.25 *26 * Forbids characters which could potentially change the meaning of the function argument definition:27 * - "()," (modification of function parameters)28 * - "=" (default value)29 * - "[]{}" (destructuring of function parameters)30 * - "/" (beginning of a comment)31 * - whitespace32 */33var reForbiddenIdentifierChars = /[()=,{}\[\]\/\s]/;34 35/**36 * Used to match37 * [ES template delimiters](http://ecma-international.org/ecma-262/7.0/#sec-template-literal-lexical-components).38 */39var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;40 41/** Used to ensure capturing order of template delimiters. */42var reNoMatch = /($^)/;43 44/** Used to match unescaped characters in compiled string literals. */45var reUnescapedString = /['\n\r\u2028\u2029\\]/g;46 47/** Used for built-in method references. */48var objectProto = Object.prototype;49 50/** Used to check objects for own properties. */51var hasOwnProperty = objectProto.hasOwnProperty;52 53/**54 * Creates a compiled template function that can interpolate data properties55 * in "interpolate" delimiters, HTML-escape interpolated data properties in56 * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data57 * properties may be accessed as free variables in the template. If a setting58 * object is given, it takes precedence over `_.templateSettings` values.59 *60 * **Security:** `_.template` is insecure and should not be used. It will be61 * removed in Lodash v5. Avoid untrusted input. See62 * [threat model](https://github.com/lodash/lodash/blob/main/threat-model.md).63 *64 * **Note:** In the development build `_.template` utilizes65 * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)66 * for easier debugging.67 *68 * For more information on precompiling templates see69 * [lodash's custom builds documentation](https://lodash.com/custom-builds).70 *71 * For more information on Chrome extension sandboxes see72 * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).73 *74 * @static75 * @since 0.1.076 * @memberOf _77 * @category String78 * @param {string} [string=''] The template string.79 * @param {Object} [options={}] The options object.80 * @param {RegExp} [options.escape=_.templateSettings.escape]81 * The HTML "escape" delimiter.82 * @param {RegExp} [options.evaluate=_.templateSettings.evaluate]83 * The "evaluate" delimiter.84 * @param {Object} [options.imports=_.templateSettings.imports]85 * An object to import into the template as free variables.86 * @param {RegExp} [options.interpolate=_.templateSettings.interpolate]87 * The "interpolate" delimiter.88 * @param {string} [options.sourceURL='templateSources[n]']89 * The sourceURL of the compiled template.90 * @param {string} [options.variable='obj']91 * The data object variable name.92 * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.93 * @returns {Function} Returns the compiled template function.94 * @example95 *96 * // Use the "interpolate" delimiter to create a compiled template.97 * var compiled = _.template('hello <%= user %>!');98 * compiled({ 'user': 'fred' });99 * // => 'hello fred!'100 *101 * // Use the HTML "escape" delimiter to escape data property values.102 * var compiled = _.template('<b><%- value %></b>');103 * compiled({ 'value': '<script>' });104 * // => '<b><script></b>'105 *106 * // Use the "evaluate" delimiter to execute JavaScript and generate HTML.107 * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');108 * compiled({ 'users': ['fred', 'barney'] });109 * // => '<li>fred</li><li>barney</li>'110 *111 * // Use the internal `print` function in "evaluate" delimiters.112 * var compiled = _.template('<% print("hello " + user); %>!');113 * compiled({ 'user': 'barney' });114 * // => 'hello barney!'115 *116 * // Use the ES template literal delimiter as an "interpolate" delimiter.117 * // Disable support by replacing the "interpolate" delimiter.118 * var compiled = _.template('hello ${ user }!');119 * compiled({ 'user': 'pebbles' });120 * // => 'hello pebbles!'121 *122 * // Use backslashes to treat delimiters as plain text.123 * var compiled = _.template('<%= "\\<%- value %\\>" %>');124 * compiled({ 'value': 'ignored' });125 * // => '<%- value %>'126 *127 * // Use the `imports` option to import `jQuery` as `jq`.128 * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';129 * var compiled = _.template(text, { 'imports': { 'jq': jQuery } });130 * compiled({ 'users': ['fred', 'barney'] });131 * // => '<li>fred</li><li>barney</li>'132 *133 * // Use the `sourceURL` option to specify a custom sourceURL for the template.134 * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });135 * compiled(data);136 * // => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.137 *138 * // Use the `variable` option to ensure a with-statement isn't used in the compiled template.139 * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });140 * compiled.source;141 * // => function(data) {142 * // var __t, __p = '';143 * // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';144 * // return __p;145 * // }146 *147 * // Use custom template delimiters.148 * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;149 * var compiled = _.template('hello {{ user }}!');150 * compiled({ 'user': 'mustache' });151 * // => 'hello mustache!'152 *153 * // Use the `source` property to inline compiled templates for meaningful154 * // line numbers in error messages and stack traces.155 * fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\156 * var JST = {\157 * "main": ' + _.template(mainText).source + '\158 * };\159 * ');160 */161function template(string, options, guard) {162 // Based on John Resig's `tmpl` implementation163 // (http://ejohn.org/blog/javascript-micro-templating/)164 // and Laura Doktorova's doT.js (https://github.com/olado/doT).165 var settings = templateSettings.imports._.templateSettings || templateSettings;166 167 if (guard && isIterateeCall(string, options, guard)) {168 options = undefined;169 }170 string = toString(string);171 options = assignWith({}, options, settings, customDefaultsAssignIn);172 173 var imports = assignWith({}, options.imports, settings.imports, customDefaultsAssignIn),174 importsKeys = keys(imports),175 importsValues = baseValues(imports, importsKeys);176 177 arrayEach(importsKeys, function(key) {178 if (reForbiddenIdentifierChars.test(key)) {179 throw new Error(INVALID_TEMPL_IMPORTS_ERROR_TEXT);180 }181 });182 183 var isEscaping,184 isEvaluating,185 index = 0,186 interpolate = options.interpolate || reNoMatch,187 source = "__p += '";188 189 // Compile the regexp to match each delimiter.190 var reDelimiters = RegExp(191 (options.escape || reNoMatch).source + '|' +192 interpolate.source + '|' +193 (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +194 (options.evaluate || reNoMatch).source + '|$'195 , 'g');196 197 // Use a sourceURL for easier debugging.198 // The sourceURL gets injected into the source that's eval-ed, so be careful199 // to normalize all kinds of whitespace, so e.g. newlines (and unicode versions of it) can't sneak in200 // and escape the comment, thus injecting code that gets evaled.201 var sourceURL = hasOwnProperty.call(options, 'sourceURL')202 ? ('//# sourceURL=' +203 (options.sourceURL + '').replace(/\s/g, ' ') +204 '\n')205 : '';206 207 string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {208 interpolateValue || (interpolateValue = esTemplateValue);209 210 // Escape characters that can't be included in string literals.211 source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);212 213 // Replace delimiters with snippets.214 if (escapeValue) {215 isEscaping = true;216 source += "' +\n__e(" + escapeValue + ") +\n'";217 }218 if (evaluateValue) {219 isEvaluating = true;220 source += "';\n" + evaluateValue + ";\n__p += '";221 }222 if (interpolateValue) {223 source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";224 }225 index = offset + match.length;226 227 // The JS engine embedded in Adobe products needs `match` returned in228 // order to produce the correct `offset` value.229 return match;230 });231 232 source += "';\n";233 234 // If `variable` is not specified wrap a with-statement around the generated235 // code to add the data object to the top of the scope chain.236 var variable = hasOwnProperty.call(options, 'variable') && options.variable;237 if (!variable) {238 source = 'with (obj) {\n' + source + '\n}\n';239 }240 // Throw an error if a forbidden character was found in `variable`, to prevent241 // potential command injection attacks.242 else if (reForbiddenIdentifierChars.test(variable)) {243 throw new Error(INVALID_TEMPL_VAR_ERROR_TEXT);244 }245 246 // Cleanup code by stripping empty strings.247 source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)248 .replace(reEmptyStringMiddle, '$1')249 .replace(reEmptyStringTrailing, '$1;');250 251 // Frame code as the function body.252 source = 'function(' + (variable || 'obj') + ') {\n' +253 (variable254 ? ''255 : 'obj || (obj = {});\n'256 ) +257 "var __t, __p = ''" +258 (isEscaping259 ? ', __e = _.escape'260 : ''261 ) +262 (isEvaluating263 ? ', __j = Array.prototype.join;\n' +264 "function print() { __p += __j.call(arguments, '') }\n"265 : ';\n'266 ) +267 source +268 'return __p\n}';269 270 var result = attempt(function() {271 return Function(importsKeys, sourceURL + 'return ' + source)272 .apply(undefined, importsValues);273 });274 275 // Provide the compiled function's source by its `toString` method or276 // the `source` property as a convenience for inlining compiled templates.277 result.source = source;278 if (isError(result)) {279 throw result;280 }281 return result;282}283 284export default template;285 