From 806ed69576b91e3612a4acd5477042566add744a Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Wed, 23 Apr 2014 07:58:06 -0400 Subject: [PATCH] fix(interpolate): changes the interpolate function to escape double quotes Fix a bug in the interpolate function causing it not to handle expressions containing double quotes properly. Closes #937 --- lib/core/interpolate.dart | 9 +++++++-- test/core/interpolate_spec.dart | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/core/interpolate.dart b/lib/core/interpolate.dart index cd2210ad4..ccb3d75f4 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(_wrapInQuotes(template.substring(index, startIdx))); } expParts.add('(' + template.substring(startIdx + startLen, endIdx) + '|stringify)'); @@ -58,11 +58,16 @@ class Interpolate implements Function { hasInterpolation = true; } else { // we did not find any interpolation, so add the remainder - expParts.add('"${template.substring(index)}"'); + expParts.add(_wrapInQuotes(template.substring(index))); break; } } return !mustHaveExpression || hasInterpolation ? expParts.join('+') : null; } + + String _wrapInQuotes(String s){ + final escaped = s.replaceAll(r'\', r'\\').replaceAll(r'"', r'\"'); + return '"$escaped"'; + } } diff --git a/test/core/interpolate_spec.dart b/test/core/interpolate_spec.dart index 7d0bd1758..348bef28c 100644 --- a/test/core/interpolate_spec.dart +++ b/test/core/interpolate_spec.dart @@ -33,5 +33,9 @@ main() { .toEqual('"X\nY"+(A\n+B|stringify)+"C\nD"'); }); + it('should escape double quotes', (Interpolate interpolate) { + expect(interpolate(r'"{{a}}')).toEqual(r'"\""+(a|stringify)'); + expect(interpolate(r'\"{{a}}')).toEqual(r'"\\\""+(a|stringify)'); + }); }); } \ No newline at end of file