| OLD | NEW |
| 1 part of angular.core_internal; | 1 part of angular.core; |
| 2 | 2 |
| 3 class Interpolation { |
| 4 final String template; |
| 5 final List<String> seperators; |
| 6 final List<String> expressions; |
| 7 Function setter = (_) => _; |
| 8 |
| 9 Interpolation(this.template, this.seperators, this.expressions); |
| 10 |
| 11 String call(List parts, [_]) { |
| 12 if (parts == null) return seperators.join(''); |
| 13 var str = []; |
| 14 for (var i = 0; i < parts.length; i++) { |
| 15 str.add(seperators[i]); |
| 16 var value = parts[i]; |
| 17 str.add(value == null ? '' : '$value'); |
| 18 } |
| 19 str.add(seperators.last); |
| 20 return setter(str.join('')); |
| 21 } |
| 22 } |
| 23 |
| 3 /** | 24 /** |
| 4 * Compiles a string with markup into an expression. This service is used by the | 25 * Compiles a string with markup into an interpolation function. This service |
| 5 * HTML [Compiler] service for data binding. | 26 * is used by the HTML [Compiler] service for data binding. |
| 6 * | 27 * |
| 7 * var interpolate = ...; // injected | 28 * |
| 8 * var exp = interpolate('Hello {{name}}!'); | 29 * var $interpolate = ...; // injected |
| 9 * expect(exp).toEqual('"Hello "+(name|stringify)+"!"'); | 30 * var exp = $interpolate('Hello {{name}}!'); |
| 31 * expect(exp({name:'Angular'}).toEqual('Hello Angular!'); |
| 10 */ | 32 */ |
| 11 @Injectable() | 33 @NgInjectableService() |
| 12 class Interpolate implements Function { | 34 class Interpolate { |
| 35 final Parser _parse; |
| 36 |
| 37 Interpolate(this._parse); |
| 38 |
| 13 /** | 39 /** |
| 14 * Compiles markup text into expression. | 40 * Compiles markup text into interpolation function. |
| 15 * | 41 * |
| 16 * - [template]: The markup text to interpolate in form `foo {{expr}} bar`. | 42 * - `text`: The markup text to interpolate in form `foo {{expr}} bar`. |
| 17 * - [mustHaveExpression]: if set to true then the interpolation string must | 43 * - `mustHaveExpression`: if set to true then the interpolation string must |
| 18 * have embedded expression in order to return an expression. Strings with | 44 * have embedded expression in order to return an interpolation function. |
| 19 * no embedded expression will return null. | 45 * Strings with no embedded expression will return null for the |
| 20 * - [startSymbol]: The symbol to start interpolation. '{{' by default. | 46 * interpolation function. |
| 21 * - [endSymbol]: The symbol to end interpolation. '}}' by default. | 47 * - `startSymbol`: The symbol to start interpolation. '{{' by default. |
| 48 * - `endSymbol`: The symbol to end interpolation. '}}' by default. |
| 22 */ | 49 */ |
| 50 Interpolation call(String template, [bool mustHaveExpression = false, |
| 51 String startSymbol = '{{', String endSymbol = '}}']) { |
| 52 int startSymbolLength = startSymbol.length; |
| 53 int endSymbolLength = endSymbol.length; |
| 54 int startIndex; |
| 55 int endIndex; |
| 56 int index = 0; |
| 57 int length = template.length; |
| 58 bool hasInterpolation = false; |
| 59 bool shouldAddSeparator = true; |
| 60 String exp; |
| 61 List<String> separators = []; |
| 62 List<String> expressions = []; |
| 23 | 63 |
| 24 String call(String template, [bool mustHaveExpression = false, | 64 while(index < length) { |
| 25 String startSymbol = '{{', String endSymbol = '}}']) { | 65 if ( ((startIndex = template.indexOf(startSymbol, index)) != -1) && |
| 26 if (template == null || template.isEmpty) return ""; | 66 ((endIndex = template.indexOf(endSymbol, startIndex + startSymbolLeng
th)) != -1) ) { |
| 27 | 67 separators.add(template.substring(index, startIndex)); |
| 28 final startLen = startSymbol.length; | 68 exp = template.substring(startIndex + startSymbolLength, endIndex); |
| 29 final endLen = endSymbol.length; | 69 expressions.add(exp); |
| 30 final length = template.length; | 70 index = endIndex + endSymbolLength; |
| 31 | |
| 32 int startIdx; | |
| 33 int endIdx; | |
| 34 int index = 0; | |
| 35 | |
| 36 bool hasInterpolation = false; | |
| 37 | |
| 38 String exp; | |
| 39 final expParts = <String>[]; | |
| 40 | |
| 41 while (index < length) { | |
| 42 startIdx = template.indexOf(startSymbol, index); | |
| 43 endIdx = template.indexOf(endSymbol, startIdx + startLen); | |
| 44 if (startIdx != -1 && endIdx != -1) { | |
| 45 if (index < startIdx) { | |
| 46 // Empty strings could be stripped thanks to the stringify | |
| 47 // formatter | |
| 48 expParts.add(_wrapInQuotes(template.substring(index, startIdx))); | |
| 49 } | |
| 50 expParts.add('(' + template.substring(startIdx + startLen, endIdx) + | |
| 51 '|stringify)'); | |
| 52 | |
| 53 index = endIdx + endLen; | |
| 54 hasInterpolation = true; | 71 hasInterpolation = true; |
| 55 } else { | 72 } else { |
| 56 // we did not find any interpolation, so add the remainder | 73 // we did not find anything, so we have to add the remainder to the chun
ks array |
| 57 expParts.add(_wrapInQuotes(template.substring(index))); | 74 separators.add(template.substring(index)); |
| 75 shouldAddSeparator = false; |
| 58 break; | 76 break; |
| 59 } | 77 } |
| 60 } | 78 } |
| 61 | 79 if (shouldAddSeparator) { |
| 62 return !mustHaveExpression || hasInterpolation ? expParts.join('+') : null; | 80 separators.add(''); |
| 63 } | 81 } |
| 64 | 82 return (!mustHaveExpression || hasInterpolation) |
| 65 String _wrapInQuotes(String s){ | 83 ? new Interpolation(template, separators, expressions) |
| 66 final escaped = s.replaceAll(r'\', r'\\').replaceAll(r'"', r'\"'); | 84 : null; |
| 67 return '"$escaped"'; | |
| 68 } | 85 } |
| 69 } | 86 } |
| OLD | NEW |