Index: third_party/pkg/angular/lib/core/interpolate.dart |
diff --git a/third_party/pkg/angular/lib/core/interpolate.dart b/third_party/pkg/angular/lib/core/interpolate.dart |
index 28d09bee75654eba941bd4a4d44b4ebab811408a..aed34f53fff1ae041f7e59d640d14d588a23b6b9 100644 |
--- a/third_party/pkg/angular/lib/core/interpolate.dart |
+++ b/third_party/pkg/angular/lib/core/interpolate.dart |
@@ -1,86 +1,69 @@ |
-part of angular.core; |
- |
-class Interpolation { |
- final String template; |
- final List<String> seperators; |
- final List<String> expressions; |
- Function setter = (_) => _; |
- |
- Interpolation(this.template, this.seperators, this.expressions); |
- |
- String call(List parts, [_]) { |
- if (parts == null) return seperators.join(''); |
- var str = []; |
- for (var i = 0; i < parts.length; i++) { |
- str.add(seperators[i]); |
- var value = parts[i]; |
- str.add(value == null ? '' : '$value'); |
- } |
- str.add(seperators.last); |
- return setter(str.join('')); |
- } |
-} |
+part of angular.core_internal; |
/** |
- * Compiles a string with markup into an interpolation function. This service |
- * is used by the HTML [Compiler] service for data binding. |
- * |
+ * Compiles a string with markup into an expression. This service is used by the |
+ * HTML [Compiler] service for data binding. |
* |
- * var $interpolate = ...; // injected |
- * var exp = $interpolate('Hello {{name}}!'); |
- * expect(exp({name:'Angular'}).toEqual('Hello Angular!'); |
+ * var interpolate = ...; // injected |
+ * var exp = interpolate('Hello {{name}}!'); |
+ * expect(exp).toEqual('"Hello "+(name|stringify)+"!"'); |
*/ |
-@NgInjectableService() |
-class Interpolate { |
- final Parser _parse; |
- |
- Interpolate(this._parse); |
- |
+@Injectable() |
+class Interpolate implements Function { |
/** |
- * Compiles markup text into interpolation function. |
+ * Compiles markup text into expression. |
* |
- * - `text`: The markup text to interpolate in form `foo {{expr}} bar`. |
- * - `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. |
- * - `startSymbol`: The symbol to start interpolation. '{{' by default. |
- * - `endSymbol`: The symbol to end interpolation. '}}' by default. |
+ * - [template]: The markup text to interpolate in form `foo {{expr}} bar`. |
+ * - [mustHaveExpression]: if set to true then the interpolation string must |
+ * have embedded expression in order to return an expression. Strings with |
+ * no embedded expression will return null. |
+ * - [startSymbol]: The symbol to start interpolation. '{{' by default. |
+ * - [endSymbol]: The symbol to end interpolation. '}}' by default. |
*/ |
- Interpolation call(String template, [bool mustHaveExpression = false, |
- String startSymbol = '{{', String endSymbol = '}}']) { |
- int startSymbolLength = startSymbol.length; |
- int endSymbolLength = endSymbol.length; |
- int startIndex; |
- int endIndex; |
+ |
+ String call(String template, [bool mustHaveExpression = false, |
+ String startSymbol = '{{', String endSymbol = '}}']) { |
+ if (template == null || template.isEmpty) return ""; |
+ |
+ final startLen = startSymbol.length; |
+ final endLen = endSymbol.length; |
+ final length = template.length; |
+ |
+ int startIdx; |
+ int endIdx; |
int index = 0; |
- int length = template.length; |
+ |
bool hasInterpolation = false; |
- bool shouldAddSeparator = true; |
+ |
String exp; |
- List<String> separators = []; |
- List<String> expressions = []; |
+ final expParts = <String>[]; |
+ |
+ while (index < length) { |
+ startIdx = template.indexOf(startSymbol, index); |
+ endIdx = template.indexOf(endSymbol, startIdx + startLen); |
+ if (startIdx != -1 && endIdx != -1) { |
+ if (index < startIdx) { |
+ // Empty strings could be stripped thanks to the stringify |
+ // formatter |
+ expParts.add(_wrapInQuotes(template.substring(index, startIdx))); |
+ } |
+ expParts.add('(' + template.substring(startIdx + startLen, endIdx) + |
+ '|stringify)'); |
- while(index < length) { |
- if ( ((startIndex = template.indexOf(startSymbol, index)) != -1) && |
- ((endIndex = template.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) { |
- separators.add(template.substring(index, startIndex)); |
- exp = template.substring(startIndex + startSymbolLength, endIndex); |
- expressions.add(exp); |
- index = endIndex + endSymbolLength; |
+ index = endIdx + endLen; |
hasInterpolation = true; |
} else { |
- // we did not find anything, so we have to add the remainder to the chunks array |
- separators.add(template.substring(index)); |
- shouldAddSeparator = false; |
+ // we did not find any interpolation, so add the remainder |
+ expParts.add(_wrapInQuotes(template.substring(index))); |
break; |
} |
} |
- if (shouldAddSeparator) { |
- separators.add(''); |
- } |
- return (!mustHaveExpression || hasInterpolation) |
- ? new Interpolation(template, separators, expressions) |
- : null; |
+ |
+ return !mustHaveExpression || hasInterpolation ? expParts.join('+') : null; |
+ } |
+ |
+ String _wrapInQuotes(String s){ |
+ final escaped = s.replaceAll(r'\', r'\\').replaceAll(r'"', r'\"'); |
+ return '"$escaped"'; |
} |
} |