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

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 #937
  • Loading branch information
vsavkin authored and [email protected] committed Apr 23, 2014
1 parent ed40de4 commit 806ed69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 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(_wrapInQuotes(template.substring(index, startIdx)));
}
expParts.add('(' + template.substring(startIdx + startLen, endIdx) +
'|stringify)');
Expand All @@ -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"';
}
}
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 806ed69

Please sign in to comment.