Skip to content

Commit 3e5f67a

Browse files
committed
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 dart-archive#937
1 parent 1a2235a commit 3e5f67a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/core/interpolate.dart

Lines changed: 4 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('"${_escapeQuotes(template.substring(index, startIdx))}"');
5353
}
5454
expParts.add('(' + template.substring(startIdx + startLen, endIdx) +
5555
'|stringify)');
@@ -58,11 +58,13 @@ 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('"${_escapeQuotes(template.substring(index))}"');
6262
break;
6363
}
6464
}
6565

6666
return !mustHaveExpression || hasInterpolation ? expParts.join('+') : null;
6767
}
68+
69+
_escapeQuotes(s) => s.replaceAll(r'\', r'\\').replaceAll(r'"', r'\"');
6870
}

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)