From 3aebb387bdfb2dc2b8e8793c836c2b8d80928798 Mon Sep 17 00:00:00 2001 From: Chirayu Krishnappa Date: Wed, 23 Apr 2014 15:55:29 -0700 Subject: [PATCH] fix(interpolate): escape double quotes, etc. in non-mustache text Closes #951 --- lib/core/interpolate.dart | 4 ++-- lib/core/module_internal.dart | 1 + test/core/interpolate_spec.dart | 8 +++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/core/interpolate.dart b/lib/core/interpolate.dart index cd2210ad4..10630edf1 100644 --- a/lib/core/interpolate.dart +++ b/lib/core/interpolate.dart @@ -49,7 +49,7 @@ class Interpolate implements Function { if (index < startIdx) { // Empty strings could be stripped thanks to the stringify // formatter - expParts.add('"${template.substring(index, startIdx)}"'); + expParts.add(JSON.encode(template.substring(index, startIdx))); } expParts.add('(' + template.substring(startIdx + startLen, endIdx) + '|stringify)'); @@ -58,7 +58,7 @@ class Interpolate implements Function { hasInterpolation = true; } else { // we did not find any interpolation, so add the remainder - expParts.add('"${template.substring(index)}"'); + expParts.add(JSON.encode(template.substring(index))); break; } } diff --git a/lib/core/module_internal.dart b/lib/core/module_internal.dart index 13042e70f..dbc8cdc8e 100644 --- a/lib/core/module_internal.dart +++ b/lib/core/module_internal.dart @@ -2,6 +2,7 @@ library angular.core_internal; import 'dart:async' as async; import 'dart:collection'; +import 'dart:convert'; import 'dart:math'; import 'package:intl/intl.dart'; diff --git a/test/core/interpolate_spec.dart b/test/core/interpolate_spec.dart index 7d0bd1758..5894d6a9d 100644 --- a/test/core/interpolate_spec.dart +++ b/test/core/interpolate_spec.dart @@ -33,5 +33,11 @@ main() { .toEqual('"X\nY"+(A\n+B|stringify)+"C\nD"'); }); + it('should not be confused by double quotes, etc. outside mustache', + (Interpolate interpolate) { + expect(interpolate('"{{A}}"')) + .toEqual(r'"\""+(A|stringify)+"\""'); + }); + }); -} \ No newline at end of file +}