Skip to content

Commit

Permalink
Add an escape sequence to avoid polymer to bind the content of curly/…
Browse files Browse the repository at this point in the history
…square brackets.

Usage:

{{test}}: normal behaviour
{{\test}}: don't bind to test and print {{test}}
{{\\test}}: don't bind to test and print {{\test}}

[[test]]: normal behaviour
[[\test]]: don't bind to test and print [[test]]
[[\\test]]: don't bind to test and print [[\test]]
  • Loading branch information
theboolean committed Mar 23, 2016
1 parent e7254d0 commit 533ed7b
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions src/lib/annotations/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
'(?:' + ARGUMENTS + '?' + ')' +
'\\)\\s*' + ')';
var BINDING = '(' + IDENT + '\\s*' + ARGUMENT_LIST + '?' + ')'; // Group 3
var OPEN_BRACKET = '(\\[\\[|{{)' + '\\s*';
var OPEN_BRACKET = '(\\[\\[\\\\*|{{\\\\*)' + '\\s*';
var CLOSE_BRACKET = '(?:]]|}})';
var NEGATE = '(?:(!)\\s*)?'; // Group 2
var EXPRESSION = OPEN_BRACKET + NEGATE + BINDING + CLOSE_BRACKET;
Expand All @@ -123,31 +123,48 @@
// m[1]: '{{' '[['
// m[2]: '' '!'
// m[3]: 'prop' 'compute(foo,bar)'
// Example: "literal1{{\prop}}literal2[[\!compute(foo,bar)]]final"
// Regex matches:
// Iteration 1: Iteration 2:
// m[1]: '{{\' '[[\'
// m[2]: '' '!'
// m[3]: 'prop' 'compute(foo,bar)'
while ((m = re.exec(text)) !== null) {
// Add literal part
if (m.index > lastIndex) {
parts.push({literal: text.slice(lastIndex, m.index)});
}
// Add binding part
// Mode (one-way or two)
var mode = m[1][0];
var negate = Boolean(m[2]);
var value = m[3].trim();
var customEvent, notifyEvent, colon;
if (mode == '{' && (colon = value.indexOf('::')) > 0) {
notifyEvent = value.substring(colon + 2);
value = value.substring(0, colon);
customEvent = true;
// Check if the expression inside brackets must be treated as literal
var treatAsLiteral = m[1][2] === '\\';
if (!treatAsLiteral) {
// Add binding part
// Mode (one-way or two)
var mode = m[1][0];
var negate = Boolean(m[2]);
var value = m[3].trim();
var customEvent, notifyEvent, colon;
if (mode == '{' && (colon = value.indexOf('::')) > 0) {
notifyEvent = value.substring(colon + 2);
value = value.substring(0, colon);
customEvent = true;
}
parts.push({
compoundIndex: parts.length,
value: value,
mode: mode,
negate: negate,
event: notifyEvent,
customEvent: customEvent
});
lastIndex = re.lastIndex;
} else {
// Add expression as literal
var literalExpression = text.slice(m.index, re.lastIndex);
// Remove a slash
literalExpression = literalExpression.slice(0, 2) + literalExpression.slice(3);
parts.push({literal: literalExpression});
lastIndex = re.lastIndex;
}
parts.push({
compoundIndex: parts.length,
value: value,
mode: mode,
negate: negate,
event: notifyEvent,
customEvent: customEvent
});
lastIndex = re.lastIndex;
}
// Add a final literal part
if (lastIndex && lastIndex < text.length) {
Expand Down

0 comments on commit 533ed7b

Please sign in to comment.