Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 806ed69

Browse files
vsavkintravis@travis-ci.org
authored andcommitted
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
1 parent ed40de4 commit 806ed69

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/core/interpolate.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Interpolate implements Function {
4949
if (index < startIdx) {
5050
// Empty strings could be stripped thanks to the stringify
5151
// formatter
52-
expParts.add('"${template.substring(index, startIdx)}"');
52+
expParts.add(_wrapInQuotes(template.substring(index, startIdx)));
5353
}
5454
expParts.add('(' + template.substring(startIdx + startLen, endIdx) +
5555
'|stringify)');
@@ -58,11 +58,16 @@ class Interpolate implements Function {
5858
hasInterpolation = true;
5959
} else {
6060
// we did not find any interpolation, so add the remainder
61-
expParts.add('"${template.substring(index)}"');
61+
expParts.add(_wrapInQuotes(template.substring(index)));
6262
break;
6363
}
6464
}
6565

6666
return !mustHaveExpression || hasInterpolation ? expParts.join('+') : null;
6767
}
68+
69+
String _wrapInQuotes(String s){
70+
final escaped = s.replaceAll(r'\', r'\\').replaceAll(r'"', r'\"');
71+
return '"$escaped"';
72+
}
6873
}

test/core/interpolate_spec.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ main() {
3333
.toEqual('"X\nY"+(A\n+B|stringify)+"C\nD"');
3434
});
3535

36+
it('should escape double quotes', (Interpolate interpolate) {
37+
expect(interpolate(r'"{{a}}')).toEqual(r'"\""+(a|stringify)');
38+
expect(interpolate(r'\"{{a}}')).toEqual(r'"\\\""+(a|stringify)');
39+
});
3640
});
3741
}

0 commit comments

Comments
 (0)