Skip to content

Commit f1e8176

Browse files
author
Tim Blasi
committed
fix(change_detect): Handle '$' in change detector strings
In Dart, '$' indicates the beginning of an interpolation. - Escapes '$' in strings when generating change detector classes. - Adds a unit test to cover this case.
1 parent 621604d commit f1e8176

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

modules/angular2/src/transform/template_compiler/change_detector_codegen.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class _CodegenState {
142142
$_changeDetectorTypeName(
143143
this.$_DISPATCHER_ACCESSOR,
144144
this.$_PROTOS_ACCESSOR,
145-
this.$_DIRECTIVES_ACCESSOR) : super(${JSON.encode(_changeDetectorDefId)});
145+
this.$_DIRECTIVES_ACCESSOR) : super(${_encodeValue(_changeDetectorDefId)});
146146
147147
void detectChangesInRecords(throwOnChange) {
148148
if (!hydrated()) {
@@ -383,7 +383,7 @@ class _CodegenState {
383383
break;
384384

385385
case RecordType.CONST:
386-
rhs = JSON.encode(r.funcOrValue);
386+
rhs = _encodeValue(r.funcOrValue);
387387
break;
388388

389389
case RecordType.PROPERTY:
@@ -435,9 +435,9 @@ class _CodegenState {
435435
for (var i = 0; i < r.args.length; ++i) {
436436
var name = _localNames[r.args[i]];
437437
res.write(
438-
'${JSON.encode(r.fixedArgs[i])} "\$\{$name == null ? "" : $name\}" ');
438+
'${_encodeValue(r.fixedArgs[i])} "\$\{$name == null ? "" : $name\}" ');
439439
}
440-
res.write(JSON.encode(r.fixedArgs[r.args.length]));
440+
res.write(_encodeValue(r.fixedArgs[r.args.length]));
441441
return '$res';
442442
}
443443

@@ -523,6 +523,9 @@ class _CodegenState {
523523
}
524524
''';
525525
}
526+
527+
String _encodeValue(funcOrValue) =>
528+
JSON.encode(funcOrValue).replaceAll(r'$', r'\$');
526529
}
527530

528531
const PROTO_CHANGE_DETECTOR_FACTORY_METHOD = 'newProtoChangeDetector';

modules/angular2/test/change_detection/change_detector_config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class _DirectiveUpdating {
263263
* equivalent to their ids.
264264
*/
265265
var _availableDefinitions = [
266+
'"$"',
266267
'10',
267268
'"str"',
268269
'"a\n\nb"',

modules/angular2/test/change_detection/change_detector_spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ export function main() {
302302
expect(val.dispatcher.log).toEqual(['propName=BvalueA']);
303303
});
304304

305+
it('should escape values in literals that indicate interpolation',
306+
() => { expect(_bindSimpleValue('"$"')).toEqual(['propName=$']); });
307+
305308
describe('pure functions', () => {
306309
it('should preserve memoized result', () => {
307310
var person = new Person('bob');

modules/angular2/test/change_detection/generator/gen_change_detectors.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main(List<String> args) {
2121
if (i > 0) {
2222
buf.write(',');
2323
}
24-
buf.write(" '''${allDefs[i].cdDef.id}''': "
24+
buf.write(" '''${_escape(allDefs[i].cdDef.id)}''': "
2525
"$className.$PROTO_CHANGE_DETECTOR_FACTORY_METHOD");
2626
}
2727
buf.write('};');
@@ -37,4 +37,6 @@ void main(List<String> args) {
3737
'''));
3838
}
3939

40+
String _escape(String id) => id.replaceAll(r'$', r'\$');
41+
4042
const _MAP_NAME = '_idToProtoMap';

0 commit comments

Comments
 (0)