OLD | NEW |
| (Empty) |
1 part of angular.core; | |
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 | |
24 /** | |
25 * Compiles a string with markup into an interpolation function. This service | |
26 * is used by the HTML [Compiler] service for data binding. | |
27 * | |
28 * | |
29 * var $interpolate = ...; // injected | |
30 * var exp = $interpolate('Hello {{name}}!'); | |
31 * expect(exp({name:'Angular'}).toEqual('Hello Angular!'); | |
32 */ | |
33 @NgInjectableService() | |
34 class Interpolate { | |
35 final Parser _parse; | |
36 | |
37 Interpolate(this._parse); | |
38 | |
39 /** | |
40 * Compiles markup text into interpolation function. | |
41 * | |
42 * - `text`: The markup text to interpolate in form `foo {{expr}} bar`. | |
43 * - `mustHaveExpression`: if set to true then the interpolation string must | |
44 * have embedded expression in order to return an interpolation function. | |
45 * Strings with no embedded expression will return null for the | |
46 * interpolation function. | |
47 * - `startSymbol`: The symbol to start interpolation. '{{' by default. | |
48 * - `endSymbol`: The symbol to end interpolation. '}}' by default. | |
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 = []; | |
63 | |
64 while(index < length) { | |
65 if ( ((startIndex = template.indexOf(startSymbol, index)) != -1) && | |
66 ((endIndex = template.indexOf(endSymbol, startIndex + startSymbolLeng
th)) != -1) ) { | |
67 separators.add(template.substring(index, startIndex)); | |
68 exp = template.substring(startIndex + startSymbolLength, endIndex); | |
69 expressions.add(exp); | |
70 index = endIndex + endSymbolLength; | |
71 hasInterpolation = true; | |
72 } else { | |
73 // we did not find anything, so we have to add the remainder to the chun
ks array | |
74 separators.add(template.substring(index)); | |
75 shouldAddSeparator = false; | |
76 break; | |
77 } | |
78 } | |
79 if (shouldAddSeparator) { | |
80 separators.add(''); | |
81 } | |
82 return (!mustHaveExpression || hasInterpolation) | |
83 ? new Interpolation(template, separators, expressions) | |
84 : null; | |
85 } | |
86 } | |
OLD | NEW |