| OLD | NEW |
| 1 part of angular.core; | 1 part of angular.core; |
| 2 | 2 |
| 3 String _startSymbol = '{{'; | 3 String _startSymbol = '{{'; |
| 4 String _endSymbol = '}}'; | 4 String _endSymbol = '}}'; |
| 5 num _startSymbolLength = _startSymbol.length; | 5 int _startSymbolLength = _startSymbol.length; |
| 6 num _endSymbolLength = _endSymbol.length; | 6 int _endSymbolLength = _endSymbol.length; |
| 7 | 7 |
| 8 class Interpolation { | 8 class Interpolation { |
| 9 final String template; | 9 final String template; |
| 10 final List<String> seperators; | 10 final List<String> seperators; |
| 11 final List<ParsedGetter> watchExpressions; | 11 final List<Getter> watchExpressions; |
| 12 Function setter = (_) => _; | 12 Function setter = (_) => _; |
| 13 | 13 |
| 14 Interpolation(this.template, this.seperators, this.watchExpressions); | 14 Interpolation(this.template, this.seperators, this.watchExpressions); |
| 15 | 15 |
| 16 String call(List parts, [_, __]) { | 16 String call(List parts, [_, __]) { |
| 17 var str = []; | 17 var str = []; |
| 18 for(var i = 0, ii = parts.length; i < ii; i++) { | 18 for(var i = 0, ii = parts.length; i < ii; i++) { |
| 19 str.add(seperators[i]); | 19 str.add(seperators[i]); |
| 20 var value = parts[i]; | 20 var value = parts[i]; |
| 21 str.add(value == null ? '' : '$value'); | 21 str.add(value == null ? '' : '$value'); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 34 * var exp = $interpolate('Hello {{name}}!'); | 34 * var exp = $interpolate('Hello {{name}}!'); |
| 35 * expect(exp({name:'Angular'}).toEqual('Hello Angular!'); | 35 * expect(exp({name:'Angular'}).toEqual('Hello Angular!'); |
| 36 */ | 36 */ |
| 37 @NgInjectableService() | 37 @NgInjectableService() |
| 38 class Interpolate { | 38 class Interpolate { |
| 39 final Parser _parse; | 39 final Parser _parse; |
| 40 | 40 |
| 41 Interpolate(this._parse); | 41 Interpolate(this._parse); |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * Compile markup text into interpolation function. | 44 * Compiles markup text into interpolation function. |
| 45 * | 45 * |
| 46 * - `text`: The markup text to interpolate in form `foo {{expr}} bar`. | 46 * - `text`: The markup text to interpolate in form `foo {{expr}} bar`. |
| 47 * - `mustHaveExpression`: if set to true then the interpolation string must | 47 * - `mustHaveExpression`: if set to true then the interpolation string must |
| 48 * have embedded expression in order to return an interpolation function. | 48 * have embedded expression in order to return an interpolation function. |
| 49 * Strings with no embedded expression will return null for the | 49 * Strings with no embedded expression will return null for the |
| 50 * interpolation function. | 50 * interpolation function. |
| 51 */ | 51 */ |
| 52 Interpolation call(String template, [bool mustHaveExpression = false]) { | 52 Interpolation call(String template, [bool mustHaveExpression = false]) { |
| 53 int startIndex; | 53 int startIndex; |
| 54 int endIndex; | 54 int endIndex; |
| 55 int index = 0; | 55 int index = 0; |
| 56 int length = template.length; | 56 int length = template.length; |
| 57 bool hasInterpolation = false; | 57 bool hasInterpolation = false; |
| 58 bool shouldAddSeparator = true; |
| 58 String exp; | 59 String exp; |
| 59 List<String> separators = []; | 60 List<String> separators = []; |
| 60 List<ParsedGetter> watchExpressions = []; | 61 List<Getter> watchExpressions = []; |
| 61 | 62 |
| 62 while(index < length) { | 63 while(index < length) { |
| 63 if ( ((startIndex = template.indexOf(_startSymbol, index)) != -1) && | 64 if ( ((startIndex = template.indexOf(_startSymbol, index)) != -1) && |
| 64 ((endIndex = template.indexOf(_endSymbol, startIndex + _startSymbolLe
ngth)) != -1) ) { | 65 ((endIndex = template.indexOf(_endSymbol, startIndex + _startSymbolLe
ngth)) != -1) ) { |
| 65 separators.add(template.substring(index, startIndex)); | 66 separators.add(template.substring(index, startIndex)); |
| 66 exp = template.substring(startIndex + _startSymbolLength, endIndex); | 67 exp = template.substring(startIndex + _startSymbolLength, endIndex); |
| 67 watchExpressions.add((_parse(exp)..exp = exp).eval); | 68 Expression expression = _parse(exp); |
| 69 watchExpressions.add(expression.eval); |
| 68 index = endIndex + _endSymbolLength; | 70 index = endIndex + _endSymbolLength; |
| 69 hasInterpolation = true; | 71 hasInterpolation = true; |
| 70 } else { | 72 } else { |
| 71 // we did not find anything, so we have to add the remainder to the chun
ks array | 73 // we did not find anything, so we have to add the remainder to the chun
ks array |
| 72 separators.add(template.substring(index)); | 74 separators.add(template.substring(index)); |
| 73 index = length; | 75 shouldAddSeparator = false; |
| 76 break; |
| 74 } | 77 } |
| 75 } | 78 } |
| 76 if (separators.length == watchExpressions.length) { | 79 if (shouldAddSeparator) { |
| 77 separators.add(''); | 80 separators.add(''); |
| 78 } | 81 } |
| 79 if (!mustHaveExpression || hasInterpolation) { | 82 if (!mustHaveExpression || hasInterpolation) { |
| 80 return new Interpolation(template, separators, watchExpressions); | 83 return new Interpolation(template, separators, watchExpressions); |
| 81 } | 84 } |
| 82 } | 85 } |
| 83 } | 86 } |
| OLD | NEW |