-
Notifications
You must be signed in to change notification settings - Fork 248
fix(interpolate): changes the interpolate function to escape double quotes #944
fix(interpolate): changes the interpolate function to escape double quotes #944
Conversation
break; | ||
} | ||
} | ||
|
||
return !mustHaveExpression || hasInterpolation ? expParts.join('+') : null; | ||
} | ||
|
||
_escapeQuotes(s) => s.replaceAll(r'\', r'\\').replaceAll(r'"', r'\"'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a return type.
It would also miss with already escaped string, right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll the return type. Could you provide an example when an escaped string would cause a problem?
@vsavkin many thanks for that. I've added some inline comments. |
Could https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-convert.HtmlEscape be helpful here ? |
@vicb I looked into HtmlEscape, but I don't think it can be used here. It escapes quotes using html escaping, so "{{true}}" will become "true$quot. Also, I think we want to keep html stuff as it is (so ' |
I'm not sure regexp would be a good fit either... my best guess would be manual parsing when the string contains escape-able chars ? |
Sure, I can use manual parsing. Do you prefer manual parsing just for clarity or do you see any cases where regexp won't handle the input properly? |
May be it's only being too conservative here ?? What if you are in a JS context then |
Sorry, I'm not sure if I understand. Looking at how Interpolator is being used, it looks like it is supposed to return an expression that can be evaluated. For example: console.log(""") prints " // 1 quote character. interpolate(r'console.log(""")') returns "console.log("\"")" It seems that interpolating a piece of js code and then evaluating it produces the same result as just executing it. Is my understand of the responsibilities of Interpolator correct? Could you provide an example showing the issue you are talking about? |
A use case could be
Not sure if this one is really relevant and if more relevant UC can be found... |
@@ -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))}"'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It think adding the quotes ("") in _escapeQuote()
(which should be renamed) would be more efficient (ie not need to parse the string, direct function call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you suggesting something like:
String _wrapInQuotes(String s){
final escaped = s.replaceAll(r'', r'').replaceAll(r'"', r'"');
return '"${escaped}"';
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep '"$escaped"' would be fine.
On 04/23/2014 07:34 PM, Victor Savkin wrote:
In lib/core/interpolate.dart:
@@ -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))}"');
Are you suggesting?
String _wrapInQuotes(String s){
final escaped = s.replaceAll(r'', r'').replaceAll(r'"', r'"');
return '"${escaped}"';
}—
Reply to this email directly or view it on GitHub
https://github.com/angular/angular.dart/pull/944/files#r11912639.
Oops forget what I have said. It should be just fine (latest PR comment). LGTM once the inline comment is adressed |
…uotes Fix a bug in the interpolate function causing it not to handle expressions containing double quotes properly. Closes dart-archive#937
@vicb I've pushed the changes. Also, I've noticed that the injected Parser is not being used. So I'm thinking of creating a PR removing it. But it is an API change, and I'm not sure if it should go thorough some sort of approval process. |
Thanks! The parser is a leftover from #788, you can safely remove it in this PR. |
Removed the parse field. Thanks. |
closed via 9a45829 |
Fix a bug in the interpolate function causing it not to handle expressions containing double quotes properly.
Closes #937