diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-04-20 03:09:25 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-04-24 16:14:29 +0200 |
commit | 82f2b76e25a4a67e01ec67e5ebe39d14ad771ea8 (patch) | |
tree | 965f6eb89b84d65a62b49008fd972c004832ccd1 /node_modules/lodash.template/index.js | |
parent | e6e0cbc387c2a77b48e4065c229daa65bf1aa0fa (diff) |
Reorganize module loading.
We now use webpack instead of SystemJS, effectively bundling modules
into one file (plus commons chunks) for every entry point. This results
in a much smaller extension size (almost half). Furthermore we use
yarn/npm even for extension run-time dependencies. This relieves us
from manually vendoring and building dependencies. It's also easier to
understand for new developers familiar with node.
Diffstat (limited to 'node_modules/lodash.template/index.js')
-rw-r--r-- | node_modules/lodash.template/index.js | 387 |
1 files changed, 280 insertions, 107 deletions
diff --git a/node_modules/lodash.template/index.js b/node_modules/lodash.template/index.js index 230f8ee86..e5a9629b9 100644 --- a/node_modules/lodash.template/index.js +++ b/node_modules/lodash.template/index.js @@ -1,113 +1,252 @@ /** - * Lo-Dash 2.4.1 (Custom Build) <http://lodash.com/> - * Build: `lodash modularize modern exports="npm" -o ./npm/` - * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/> - * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE> - * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors - * Available under MIT license <http://lodash.com/license> + * lodash 3.6.2 (Custom Build) <https://lodash.com/> + * Build: `lodash modern modularize exports="npm" -o ./` + * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/> + * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> + * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors + * Available under MIT license <https://lodash.com/license> */ -var defaults = require('lodash.defaults'), - escape = require('lodash.escape'), - escapeStringChar = require('lodash._escapestringchar'), - keys = require('lodash.keys'), +var baseCopy = require('lodash._basecopy'), + baseToString = require('lodash._basetostring'), + baseValues = require('lodash._basevalues'), + isIterateeCall = require('lodash._isiterateecall'), reInterpolate = require('lodash._reinterpolate'), - templateSettings = require('lodash.templatesettings'), - values = require('lodash.values'); + keys = require('lodash.keys'), + restParam = require('lodash.restparam'), + templateSettings = require('lodash.templatesettings'); + +/** `Object#toString` result references. */ +var errorTag = '[object Error]'; -/** Used to match empty string literals in compiled template source */ +/** Used to match empty string literals in compiled template source. */ var reEmptyStringLeading = /\b__p \+= '';/g, reEmptyStringMiddle = /\b(__p \+=) '' \+/g, reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g; -/** - * Used to match ES6 template delimiters - * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-string-literals - */ +/** Used to match [ES template delimiters](http://ecma-international.org/ecma-262/6.0/#sec-template-literal-lexical-components). */ var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g; -/** Used to ensure capturing order of template delimiters */ +/** Used to ensure capturing order of template delimiters. */ var reNoMatch = /($^)/; -/** Used to match unescaped characters in compiled string literals */ -var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g; +/** Used to match unescaped characters in compiled string literals. */ +var reUnescapedString = /['\n\r\u2028\u2029\\]/g; + +/** Used to escape characters for inclusion in compiled string literals. */ +var stringEscapes = { + '\\': '\\', + "'": "'", + '\n': 'n', + '\r': 'r', + '\u2028': 'u2028', + '\u2029': 'u2029' +}; + +/** + * Used by `_.template` to escape characters for inclusion in compiled string literals. + * + * @private + * @param {string} chr The matched character to escape. + * @returns {string} Returns the escaped character. + */ +function escapeStringChar(chr) { + return '\\' + stringEscapes[chr]; +} + +/** + * Checks if `value` is object-like. + * + * @private + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is object-like, else `false`. + */ +function isObjectLike(value) { + return !!value && typeof value == 'object'; +} + +/** Used for native method references. */ +var objectProto = Object.prototype; + +/** Used to check objects for own properties. */ +var hasOwnProperty = objectProto.hasOwnProperty; + +/** + * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring) + * of values. + */ +var objToString = objectProto.toString; /** - * A micro-templating method that handles arbitrary delimiters, preserves - * whitespace, and correctly escapes quotes within interpolated code. + * Used by `_.template` to customize its `_.assign` use. + * + * **Note:** This function is like `assignDefaults` except that it ignores + * inherited property values when checking if a property is `undefined`. * - * Note: In the development build, `_.template` utilizes sourceURLs for easier - * debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl + * @private + * @param {*} objectValue The destination object property value. + * @param {*} sourceValue The source object property value. + * @param {string} key The key associated with the object and source values. + * @param {Object} object The destination object. + * @returns {*} Returns the value to assign to the destination object. + */ +function assignOwnDefaults(objectValue, sourceValue, key, object) { + return (objectValue === undefined || !hasOwnProperty.call(object, key)) + ? sourceValue + : objectValue; +} + +/** + * A specialized version of `_.assign` for customizing assigned values without + * support for argument juggling, multiple sources, and `this` binding `customizer` + * functions. * - * For more information on precompiling templates see: - * http://lodash.com/custom-builds + * @private + * @param {Object} object The destination object. + * @param {Object} source The source object. + * @param {Function} customizer The function to customize assigned values. + * @returns {Object} Returns `object`. + */ +function assignWith(object, source, customizer) { + var index = -1, + props = keys(source), + length = props.length; + + while (++index < length) { + var key = props[index], + value = object[key], + result = customizer(value, source[key], key, object, source); + + if ((result === result ? (result !== value) : (value === value)) || + (value === undefined && !(key in object))) { + object[key] = result; + } + } + return object; +} + +/** + * The base implementation of `_.assign` without support for argument juggling, + * multiple sources, and `customizer` functions. * - * For more information on Chrome extension sandboxes see: - * http://developer.chrome.com/stable/extensions/sandboxingEval.html + * @private + * @param {Object} object The destination object. + * @param {Object} source The source object. + * @returns {Object} Returns `object`. + */ +function baseAssign(object, source) { + return source == null + ? object + : baseCopy(source, keys(source), object); +} + +/** + * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`, + * `SyntaxError`, `TypeError`, or `URIError` object. * * @static * @memberOf _ - * @category Utilities - * @param {string} text The template text. - * @param {Object} data The data object used to populate the text. + * @category Lang + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is an error object, else `false`. + * @example + * + * _.isError(new Error); + * // => true + * + * _.isError(Error); + * // => false + */ +function isError(value) { + return isObjectLike(value) && typeof value.message == 'string' && objToString.call(value) == errorTag; +} + +/** + * Creates a compiled template function that can interpolate data properties + * in "interpolate" delimiters, HTML-escape interpolated data properties in + * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data + * properties may be accessed as free variables in the template. If a setting + * object is provided it takes precedence over `_.templateSettings` values. + * + * **Note:** In the development build `_.template` utilizes + * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl) + * for easier debugging. + * + * For more information on precompiling templates see + * [lodash's custom builds documentation](https://lodash.com/custom-builds). + * + * For more information on Chrome extension sandboxes see + * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval). + * + * @static + * @memberOf _ + * @category String + * @param {string} [string=''] The template string. * @param {Object} [options] The options object. - * @param {RegExp} [options.escape] The "escape" delimiter. + * @param {RegExp} [options.escape] The HTML "escape" delimiter. * @param {RegExp} [options.evaluate] The "evaluate" delimiter. - * @param {Object} [options.imports] An object to import into the template as local variables. + * @param {Object} [options.imports] An object to import into the template as free variables. * @param {RegExp} [options.interpolate] The "interpolate" delimiter. - * @param {string} [sourceURL] The sourceURL of the template's compiled source. - * @param {string} [variable] The data object variable name. - * @returns {Function|string} Returns a compiled function when no `data` object - * is given, else it returns the interpolated text. + * @param {string} [options.sourceURL] The sourceURL of the template's compiled source. + * @param {string} [options.variable] The data object variable name. + * @param- {Object} [otherOptions] Enables the legacy `options` param signature. + * @returns {Function} Returns the compiled template function. * @example * * // using the "interpolate" delimiter to create a compiled template - * var compiled = _.template('hello <%= name %>'); - * compiled({ 'name': 'fred' }); - * // => 'hello fred' + * var compiled = _.template('hello <%= user %>!'); + * compiled({ 'user': 'fred' }); + * // => 'hello fred!' * - * // using the "escape" delimiter to escape HTML in data property values - * _.template('<b><%- value %></b>', { 'value': '<script>' }); + * // using the HTML "escape" delimiter to escape data property values + * var compiled = _.template('<b><%- value %></b>'); + * compiled({ 'value': '<script>' }); * // => '<b><script></b>' * - * // using the "evaluate" delimiter to generate HTML - * var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>'; - * _.template(list, { 'people': ['fred', 'barney'] }); + * // using the "evaluate" delimiter to execute JavaScript and generate HTML + * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>'); + * compiled({ 'users': ['fred', 'barney'] }); * // => '<li>fred</li><li>barney</li>' * - * // using the ES6 delimiter as an alternative to the default "interpolate" delimiter - * _.template('hello ${ name }', { 'name': 'pebbles' }); - * // => 'hello pebbles' - * * // using the internal `print` function in "evaluate" delimiters - * _.template('<% print("hello " + name); %>!', { 'name': 'barney' }); + * var compiled = _.template('<% print("hello " + user); %>!'); + * compiled({ 'user': 'barney' }); * // => 'hello barney!' * - * // using a custom template delimiters - * _.templateSettings = { - * 'interpolate': /{{([\s\S]+?)}}/g - * }; + * // using the ES delimiter as an alternative to the default "interpolate" delimiter + * var compiled = _.template('hello ${ user }!'); + * compiled({ 'user': 'pebbles' }); + * // => 'hello pebbles!' * - * _.template('hello {{ name }}!', { 'name': 'mustache' }); + * // using custom template delimiters + * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g; + * var compiled = _.template('hello {{ user }}!'); + * compiled({ 'user': 'mustache' }); * // => 'hello mustache!' * - * // using the `imports` option to import jQuery - * var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>'; - * _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } }); + * // using backslashes to treat delimiters as plain text + * var compiled = _.template('<%= "\\<%- value %\\>" %>'); + * compiled({ 'value': 'ignored' }); + * // => '<%- value %>' + * + * // using the `imports` option to import `jQuery` as `jq` + * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>'; + * var compiled = _.template(text, { 'imports': { 'jq': jQuery } }); + * compiled({ 'users': ['fred', 'barney'] }); * // => '<li>fred</li><li>barney</li>' * * // using the `sourceURL` option to specify a custom sourceURL for the template - * var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' }); + * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' }); * compiled(data); * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector * * // using the `variable` option to ensure a with-statement isn't used in the compiled template - * var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' }); + * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' }); * compiled.source; * // => function(data) { - * var __t, __p = '', __e = _.escape; - * __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!'; - * return __p; - * } + * // var __t, __p = ''; + * // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!'; + * // return __p; + * // } * * // using the `source` property to inline compiled templates for meaningful * // line numbers in error messages and a stack trace @@ -117,27 +256,28 @@ var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g; * };\ * '); */ -function template(text, data, options) { - // based on John Resig's `tmpl` implementation - // http://ejohn.org/blog/javascript-micro-templating/ - // and Laura Doktorova's doT.js - // https://github.com/olado/doT +function template(string, options, otherOptions) { + // Based on John Resig's `tmpl` implementation (http://ejohn.org/blog/javascript-micro-templating/) + // and Laura Doktorova's doT.js (https://github.com/olado/doT). var settings = templateSettings.imports._.templateSettings || templateSettings; - text = String(text || ''); - // avoid missing dependencies when `iteratorTemplate` is not defined - options = defaults({}, options, settings); + if (otherOptions && isIterateeCall(string, options, otherOptions)) { + options = otherOptions = undefined; + } + string = baseToString(string); + options = assignWith(baseAssign({}, otherOptions || options), settings, assignOwnDefaults); - var imports = defaults({}, options.imports, settings.imports), + var imports = assignWith(baseAssign({}, options.imports), settings.imports, assignOwnDefaults), importsKeys = keys(imports), - importsValues = values(imports); + importsValues = baseValues(imports, importsKeys); - var isEvaluating, + var isEscaping, + isEvaluating, index = 0, interpolate = options.interpolate || reNoMatch, source = "__p += '"; - // compile the regexp to match each delimiter + // Compile the regexp to match each delimiter. var reDelimiters = RegExp( (options.escape || reNoMatch).source + '|' + interpolate.source + '|' + @@ -145,14 +285,18 @@ function template(text, data, options) { (options.evaluate || reNoMatch).source + '|$' , 'g'); - text.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) { + // Use a sourceURL for easier debugging. + var sourceURL = 'sourceURL' in options ? '//# sourceURL=' + options.sourceURL + '\n' : ''; + + string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) { interpolateValue || (interpolateValue = esTemplateValue); - // escape characters that cannot be included in string literals - source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar); + // Escape characters that can't be included in string literals. + source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar); - // replace delimiters with snippets + // Replace delimiters with snippets. if (escapeValue) { + isEscaping = true; source += "' +\n__e(" + escapeValue + ") +\n'"; } if (evaluateValue) { @@ -164,31 +308,35 @@ function template(text, data, options) { } index = offset + match.length; - // the JS engine embedded in Adobe products requires returning the `match` - // string in order to produce the correct `offset` value + // The JS engine embedded in Adobe products requires returning the `match` + // string in order to produce the correct `offset` value. return match; }); source += "';\n"; - // if `variable` is not specified, wrap a with-statement around the generated - // code to add the data object to the top of the scope chain - var variable = options.variable, - hasVariable = variable; - - if (!hasVariable) { - variable = 'obj'; - source = 'with (' + variable + ') {\n' + source + '\n}\n'; + // If `variable` is not specified wrap a with-statement around the generated + // code to add the data object to the top of the scope chain. + var variable = options.variable; + if (!variable) { + source = 'with (obj) {\n' + source + '\n}\n'; } - // cleanup code by stripping empty strings + // Cleanup code by stripping empty strings. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source) .replace(reEmptyStringMiddle, '$1') .replace(reEmptyStringTrailing, '$1;'); - // frame code as the function body - source = 'function(' + variable + ') {\n' + - (hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') + - "var __t, __p = '', __e = _.escape" + + // Frame code as the function body. + source = 'function(' + (variable || 'obj') + ') {\n' + + (variable + ? '' + : 'obj || (obj = {});\n' + ) + + "var __t, __p = ''" + + (isEscaping + ? ', __e = _.escape' + : '' + ) + (isEvaluating ? ', __j = Array.prototype.join;\n' + "function print() { __p += __j.call(arguments, '') }\n" @@ -197,20 +345,45 @@ function template(text, data, options) { source + 'return __p\n}'; - try { - var result = Function(importsKeys, 'return ' + source ).apply(undefined, importsValues); - } catch(e) { - e.source = source; - throw e; - } - if (data) { - return result(data); - } - // provide the compiled function's source by its `toString` method, in - // supported environments, or the `source` property as a convenience for - // inlining compiled templates during the build process + var result = attempt(function() { + return Function(importsKeys, sourceURL + 'return ' + source).apply(undefined, importsValues); + }); + + // Provide the compiled function's source by its `toString` method or + // the `source` property as a convenience for inlining compiled templates. result.source = source; + if (isError(result)) { + throw result; + } return result; } +/** + * Attempts to invoke `func`, returning either the result or the caught error + * object. Any additional arguments are provided to `func` when it is invoked. + * + * @static + * @memberOf _ + * @category Utility + * @param {Function} func The function to attempt. + * @returns {*} Returns the `func` result or error object. + * @example + * + * // avoid throwing errors for invalid selectors + * var elements = _.attempt(function(selector) { + * return document.querySelectorAll(selector); + * }, '>_>'); + * + * if (_.isError(elements)) { + * elements = []; + * } + */ +var attempt = restParam(function(func, args) { + try { + return func.apply(undefined, args); + } catch(e) { + return isError(e) ? e : new Error(e); + } +}); + module.exports = template; |