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

fix(interpolate): changes the interpolate function to escape double quotes #944

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/core/interpolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ part of angular.core_internal;
*/
@Injectable()
class Interpolate implements Function {
final Parser _parse;

Interpolate(this._parse);

/**
* Compiles markup text into expression.
*
Expand Down Expand Up @@ -49,7 +45,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 +54,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)');
});
});
}