diff options
| author | Misko Hevery | 2012-03-23 14:03:24 -0700 |
|---|---|---|
| committer | Misko Hevery | 2012-03-28 11:16:35 -0700 |
| commit | 2430f52bb97fa9d682e5f028c977c5bf94c5ec38 (patch) | |
| tree | e7529b741d70199f36d52090b430510bad07f233 /src/ng/interpolate.js | |
| parent | 944098a4e0f753f06b40c73ca3e79991cec6c2e2 (diff) | |
| download | angular.js-2430f52bb97fa9d682e5f028c977c5bf94c5ec38.tar.bz2 | |
chore(module): move files around in preparation for more modules
Diffstat (limited to 'src/ng/interpolate.js')
| -rw-r--r-- | src/ng/interpolate.js | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/ng/interpolate.js b/src/ng/interpolate.js new file mode 100644 index 00000000..6d3ae868 --- /dev/null +++ b/src/ng/interpolate.js @@ -0,0 +1,145 @@ +'use strict'; + +/** + * @ngdoc function + * @name angular.module.ng.$interpolateProvider + * @function + * + * @description + * + * Used for configuring the interpolation markup. Deafults to `{{` and `}}`. + */ +function $InterpolateProvider() { + var startSymbol = '{{'; + var endSymbol = '}}'; + + /** + * @ngdoc method + * @name angular.module.ng.$interpolateProvider#startSymbol + * @methodOf angular.module.ng.$interpolateProvider + * @description + * Symbol to denote start of expression in the interpolated string. Defaults to `{{`. + * + * @prop {string=} value new value to set the starting symbol to. + */ + this.startSymbol = function(value){ + if (value) { + startSymbol = value; + return this; + } else { + return startSymbol; + } + }; + + /** + * @ngdoc method + * @name angular.module.ng.$interpolateProvider#endSymbol + * @methodOf angular.module.ng.$interpolateProvider + * @description + * Symbol to denote the end of expression in the interpolated string. Defaults to `}}`. + * + * @prop {string=} value new value to set the ending symbol to. + */ + this.endSymbol = function(value){ + if (value) { + endSymbol = value; + return this; + } else { + return startSymbol; + } + }; + + + this.$get = ['$parse', function($parse) { + var startSymbolLength = startSymbol.length, + endSymbolLength = endSymbol.length; + + /** + * @ngdoc function + * @name angular.module.ng.$interpolate + * @function + * + * @requires $parse + * + * @description + * + * Compiles a string with markup into an interpolation function. This service is used by the + * HTML {@link angular.module.ng.$compile $compile} service for data binding. See + * {@link angular.module.ng.$interpolateProvider $interpolateProvider} for configuring the + * interpolation markup. + * + * + <pre> + var $interpolate = ...; // injected + var exp = $interpolate('Hello {{name}}!'); + expect(exp({name:'Angular'}).toEqual('Hello Angular!'); + </pre> + * + * + * @param {string} text The text with markup to interpolate. + * @param {boolean=} mustHaveExpression if set to true then the interpolation string must have + * embedded expression in order to return an interpolation function. Strings with no + * embedded expression will return null for the interpolation function. + * @returns {function(context)} an interpolation function which is used to compute the interpolated + * string. The function has these parameters: + * + * * `context`: an object against which any expressions embedded in the strings are evaluated + * against. + * + */ + return function(text, mustHaveExpression) { + var startIndex, + endIndex, + index = 0, + parts = [], + length = text.length, + hasInterpolation = false, + fn, + exp, + concat = []; + + while(index < length) { + if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) && + ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) { + (index != startIndex) && parts.push(text.substring(index, startIndex)); + parts.push(fn = $parse(exp = text.substring(startIndex + startSymbolLength, endIndex))); + fn.exp = exp; + index = endIndex + endSymbolLength; + hasInterpolation = true; + } else { + // we did not find anything, so we have to add the remainder to the parts array + (index != length) && parts.push(text.substring(index)); + index = length; + } + } + + if (!(length = parts.length)) { + // we added, nothing, must have been an empty string. + parts.push(''); + length = 1; + } + + if (!mustHaveExpression || hasInterpolation) { + concat.length = length; + fn = function(context) { + for(var i = 0, ii = length, part; i<ii; i++) { + if (typeof (part = parts[i]) == 'function') { + part = part(context); + if (part == null || part == undefined) { + part = ''; + } else if (typeof part != 'string') { + part = toJson(part); + } + } + concat[i] = part; + } + return concat.join(''); + }; + fn.exp = text; + fn.parts = parts; + return fn; + } + }; + }]; +} + |
