Skip to content

Commit

Permalink
fix(interpolate): changes the interpolate function to escape double q…
Browse files Browse the repository at this point in the history
…uotes

Fix a bug in the interpolate function causing it not to handle expressions containing double quotes properly.

Closes dart-archive#937
  • Loading branch information
vsavkin committed Apr 23, 2014
1 parent 1a2235a commit 6b936ae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/core/interpolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('"${_escapeQuotes(template.substring(index, startIdx))}"');
}
expParts.add('(' + template.substring(startIdx + startLen, endIdx) +
'|stringify)');
Expand All @@ -58,11 +58,13 @@ class Interpolate implements Function {
hasInterpolation = true;
} else {
// we did not find any interpolation, so add the remainder
expParts.add('"${template.substring(index)}"');
expParts.add('"${_escapeQuotes(template.substring(index))}"');
break;
}
}

return !mustHaveExpression || hasInterpolation ? expParts.join('+') : null;
}

String _escapeQuotes(s) => s.replaceAll(r'\', r'\\').replaceAll(r'"', r'\"');
}
4 changes: 4 additions & 0 deletions test/core/interpolate_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
});
});
}

0 comments on commit 6b936ae

Please sign in to comment.