fix(compiler): use parentheses around expressions and escape quotes

This commit is contained in:
Tobias Bosch
2014-11-14 14:18:23 -08:00
parent 03882dcccc
commit b2ecdb5da7
4 changed files with 45 additions and 12 deletions

View File

@ -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);
}
}
}