fix(compiler): use parentheses around expressions and escape quotes
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import {RegExpWrapper, StringWrapper} from 'facade/lang';
|
||||
import {RegExpWrapper, StringWrapper, isPresent} from 'facade/lang';
|
||||
import {Node, DOM} from 'facade/dom';
|
||||
|
||||
import {CompileStep} from './compile_step';
|
||||
@ -7,6 +7,7 @@ import {CompileControl} from './compile_control';
|
||||
|
||||
// TODO(tbosch): Cannot make this const/final right now because of the transpiler...
|
||||
var INTERPOLATION_REGEXP = RegExpWrapper.create('\\{\\{(.*?)\\}\\}');
|
||||
var QUOTE_REGEXP = RegExpWrapper.create("'");
|
||||
|
||||
/**
|
||||
* Parses interpolations in direct text child nodes of the current element.
|
||||
@ -27,23 +28,30 @@ export class TextInterpolationParser extends CompileStep {
|
||||
}
|
||||
|
||||
_parseTextNode(pipelineElement, node, nodeIndex) {
|
||||
// TODO: escape fixed string quotes
|
||||
// TODO: add braces around the expression
|
||||
// TODO: suppress empty strings
|
||||
// TODO: add stringify formatter
|
||||
// TODO: add stringify formatter when we support formatters
|
||||
var parts = StringWrapper.split(node.nodeValue, INTERPOLATION_REGEXP);
|
||||
if (parts.length > 1) {
|
||||
var expression = '';
|
||||
for (var i=0; i<parts.length; i++) {
|
||||
var expressionPart = null;
|
||||
if (i%2 === 0) {
|
||||
// fixed string
|
||||
parts[i] = "'" + parts[i] + "'";
|
||||
if (parts[i].length > 0) {
|
||||
expressionPart = "'" + StringWrapper.replaceAll(parts[i], QUOTE_REGEXP, "\\'") + "'";
|
||||
}
|
||||
} else {
|
||||
// expression
|
||||
parts[i] = "" + parts[i] + "";
|
||||
expressionPart = "(" + parts[i] + ")";
|
||||
}
|
||||
if (isPresent(expressionPart)) {
|
||||
if (expression.length > 0) {
|
||||
expression += '+';
|
||||
}
|
||||
expression += expressionPart;
|
||||
}
|
||||
}
|
||||
DOM.setText(node, ' ');
|
||||
pipelineElement.addTextNodeBinding(nodeIndex, parts.join('+'));
|
||||
pipelineElement.addTextNodeBinding(nodeIndex, expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user