OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library template_binding.src.mustache_tokens; | 5 library template_binding.src.mustache_tokens; |
6 | 6 |
7 import 'package:observe/observe.dart'; | 7 import 'package:observe/observe.dart'; |
8 import 'package:template_binding/template_binding.dart'; | 8 |
| 9 // Dart note: this was added to decouple the parse function below from the rest |
| 10 // of template_binding. This allows using this code in command-line tools as |
| 11 // well. |
| 12 typedef Function DelegateFunctionFactory(String pathString); |
9 | 13 |
10 /** | 14 /** |
11 * Represents a set of parsed tokens from a {{ mustache binding expression }}. | 15 * Represents a set of parsed tokens from a {{ mustache binding expression }}. |
12 * This can be created by calling [parse]. | 16 * This can be created by calling [parse]. |
13 * | 17 * |
14 * For performance reasons the data is stored in one linear array in [_tokens]. | 18 * For performance reasons the data is stored in one linear array in [_tokens]. |
15 * This class wraps that array and provides accessors in an attempt to make the | 19 * This class wraps that array and provides accessors in an attempt to make the |
16 * pattern easier to understand. See [length] and [getText] for example. | 20 * pattern easier to understand. See [length] and [getText] for example. |
17 */ | 21 */ |
18 class MustacheTokens { | 22 class MustacheTokens { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 /** Gets the prepareBinding function for the [i]th token. */ | 71 /** Gets the prepareBinding function for the [i]th token. */ |
68 Function getPrepareBinding(int i) => | 72 Function getPrepareBinding(int i) => |
69 _tokens[i * _TOKEN_SIZE + _TOKEN_PREPAREFN]; | 73 _tokens[i * _TOKEN_SIZE + _TOKEN_PREPAREFN]; |
70 | 74 |
71 | 75 |
72 /** | 76 /** |
73 * Parses {{ mustache }} bindings. | 77 * Parses {{ mustache }} bindings. |
74 * | 78 * |
75 * Returns null if there are no matches. Otherwise returns the parsed tokens. | 79 * Returns null if there are no matches. Otherwise returns the parsed tokens. |
76 */ | 80 */ |
77 static MustacheTokens parse(String s, String name, node, | 81 static MustacheTokens parse(String s, [DelegateFunctionFactory fnFactory]) { |
78 BindingDelegate delegate) { | |
79 if (s == null || s.isEmpty) return null; | 82 if (s == null || s.isEmpty) return null; |
80 | 83 |
81 var tokens = null; | 84 var tokens = null; |
82 var length = s.length; | 85 var length = s.length; |
83 var lastIndex = 0; | 86 var lastIndex = 0; |
84 var onlyOneTime = true; | 87 var onlyOneTime = true; |
85 while (lastIndex < length) { | 88 while (lastIndex < length) { |
86 var startIndex = s.indexOf('{{', lastIndex); | 89 var startIndex = s.indexOf('{{', lastIndex); |
87 var oneTimeStart = s.indexOf('[[', lastIndex); | 90 var oneTimeStart = s.indexOf('[[', lastIndex); |
88 var oneTime = false; | 91 var oneTime = false; |
(...skipping 16 matching lines...) Expand all Loading... |
105 | 108 |
106 tokens.add(s.substring(lastIndex)); // TEXT | 109 tokens.add(s.substring(lastIndex)); // TEXT |
107 break; | 110 break; |
108 } | 111 } |
109 | 112 |
110 if (tokens == null) tokens = []; | 113 if (tokens == null) tokens = []; |
111 tokens.add(s.substring(lastIndex, startIndex)); // TEXT | 114 tokens.add(s.substring(lastIndex, startIndex)); // TEXT |
112 var pathString = s.substring(startIndex + 2, endIndex).trim(); | 115 var pathString = s.substring(startIndex + 2, endIndex).trim(); |
113 tokens.add(oneTime); // ONETIME? | 116 tokens.add(oneTime); // ONETIME? |
114 onlyOneTime = onlyOneTime && oneTime; | 117 onlyOneTime = onlyOneTime && oneTime; |
115 tokens.add(new PropertyPath(pathString)); // PATH | 118 var delegateFn = fnFactory == null ? null : fnFactory(pathString); |
116 var delegateFn = delegate == null ? null : | 119 if (delegateFn == null) { |
117 delegate.prepareBinding(pathString, name, node); | 120 tokens.add(new PropertyPath(pathString)); // PATH |
118 tokens.add(delegateFn); | 121 } else { |
| 122 tokens.add(null); |
| 123 } |
| 124 tokens.add(delegateFn); // DELEGATE_FN |
119 | 125 |
120 lastIndex = endIndex + 2; | 126 lastIndex = endIndex + 2; |
121 } | 127 } |
122 | 128 |
123 if (lastIndex == length) tokens.add(''); | 129 if (lastIndex == length) tokens.add(''); // TEXT |
124 | 130 |
125 return new MustacheTokens._(tokens, onlyOneTime); | 131 return new MustacheTokens._(tokens, onlyOneTime); |
126 } | 132 } |
127 | 133 |
128 | 134 |
129 // Dart note: split "combinator" into the single/list variants, so the | 135 // Dart note: split "combinator" into the single/list variants, so the |
130 // argument can be typed. | 136 // argument can be typed. |
131 String _singleCombinator(Object value) { | 137 String _singleCombinator(Object value) { |
132 if (value == null) value = ''; | 138 if (value == null) value = ''; |
133 return '${getText(0)}$value${getText(length)}'; | 139 return '${getText(0)}$value${getText(length)}'; |
134 } | 140 } |
135 | 141 |
136 String _listCombinator(List<Object> values) { | 142 String _listCombinator(List<Object> values) { |
137 var newValue = new StringBuffer(getText(0)); | 143 var newValue = new StringBuffer(getText(0)); |
138 int len = this.length; | 144 int len = this.length; |
139 for (var i = 0; i < len; i++) { | 145 for (var i = 0; i < len; i++) { |
140 var value = values[i]; | 146 var value = values[i]; |
141 if (value != null) newValue.write(value); | 147 if (value != null) newValue.write(value); |
142 newValue.write(getText(i + 1)); | 148 newValue.write(getText(i + 1)); |
143 } | 149 } |
144 return newValue.toString(); | 150 return newValue.toString(); |
145 } | 151 } |
146 } | 152 } |
OLD | NEW |