Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($interpolate): use the first end symbol that forms a valid expression #8744

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 37 additions & 2 deletions src/ng/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var $interpolateMinErr = minErr('$interpolate');
function $InterpolateProvider() {
var startSymbol = '{{';
var endSymbol = '}}';
var quoteChars = '\'"';
var brackets = {"(": ")", "[": "]", "{": "}"};

/**
* @ngdoc method
Expand Down Expand Up @@ -193,17 +195,42 @@ function $InterpolateProvider() {
textLength = text.length,
exp,
concat = [],
expressionPositions = [];
expressionPositions = [],
quoteChar,
bracketCounts = {},
ch;

forEach(brackets, function(start, end) {
bracketCounts[start] = 0;
bracketCounts[end] = 0;
});
while(index < textLength) {
if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) &&
((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) {
if (index !== startIndex) {
concat.push(unescapeText(text.substring(index, startIndex)));
}
index = startIndex + startSymbolLength;
while (index < endIndex || quoteChar !== undefined || !bracketsMatch(bracketCounts)) {
if (index >= endIndex) {
endIndex = text.indexOf(endSymbol, endIndex + 1);
if (endIndex === -1) {
break;
}
}
ch = text[index];
if (quoteChar) {
if (quoteChar === ch) quoteChar = undefined;
else if (ch === '\\') index++;
} else {
if (ch in bracketCounts) bracketCounts[ch]++;
else if (quoteChars.indexOf(ch) != -1) quoteChar = ch;
}
index++;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was kind of a hard read, I think I would refactor this to something like:

var brackets = [[0, 0, 0], [0, 0, 0]], bracketChars = '({[)}]', ch, quoteCh;
index = startIndex + startSymbolLength;
while (index !== endIndex || quoteCh || !equals(brackets[0], brackets[1])) {
  ch = text[index];
  if (quoteCh) {
    if (ch === '\\') {
      index++;
    } else if (ch === quoteCh) {
      quoteCh = undefined;
    }
  } else if (ch === "'" || ch === '"') {
    quoteCh = ch;
  } else if ((i = bracketChars.indexOf(ch)) != -1) {
    brackets[i/3][i%3]++;
  }
  index++;

  if (index > endIndex) {
    endIndex = text.indexOf(endSymbol, index);
    if (endIndex === -1) {
      break;
    }
  }
}

(haven't tested, I might have some bug there)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggested code will fail with $interpolate('{{"\\}}"}}'), anyhow it is possible to fix it, but it does add some other oddities

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, that's just a suggestion for a direction to get rid of this pretty cryptic switch statement, I didn't really test it. Anyway, I modified it a bit so it will work with the failure you mentioned.

exp = text.substring(startIndex + startSymbolLength, endIndex);
expressions.push(exp);
parseFns.push($parse(exp, parseStringifyInterceptor));
expressions.push(exp);
index = endIndex + endSymbolLength;
expressionPositions.push(concat.length);
concat.push('');
Expand Down Expand Up @@ -312,6 +339,14 @@ function $InterpolateProvider() {
$exceptionHandler(newErr);
}
}

function bracketsMatch(bracketCounts) {
var match = true;
forEach(brackets, function(start, end) {
match = match && bracketCounts[start] === bracketCounts[end];
});
return match;
}
}


Expand Down
10 changes: 9 additions & 1 deletion test/ng/interpolateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('$interpolate', function() {
expect(function() {
$interpolate('{{\\{\\{foo\\}\\}}}')(obj);
}).toThrowMinErr('$parse', 'lexerr',
'Lexer Error: Unexpected next character at columns 0-0 [\\] in expression [\\{\\{foo\\}\\]');
'Lexer Error: Unexpected next character at columns 0-0 [\\] in expression [\\{\\{foo\\}\\}]');
}));


Expand All @@ -116,6 +116,14 @@ describe('$interpolate', function() {
it('should evaluate expressions between escaped start/end symbols', inject(function($interpolate) {
expect($interpolate('\\{\\{Hello, {{bar}}!\\}\\}')(obj)).toBe('{{Hello, World!}}');
}));

it('should pick the first end symbol location that forms a valid expression', inject(function($interpolate) {
expect($interpolate('{{{}}}')(obj)).toBe('{}');
expect($interpolate('{{"{{ }}"}}')(obj)).toBe('{{ }}');
expect($interpolate('{{{foo: "bar"}}}')(obj)).toBe('{"foo":"bar"}');
expect($interpolate('{{{foo: {"bar": "baz"}}}}')(obj)).toBe('{"foo":{"bar":"baz"}}');
expect($interpolate('{{[{foo: {"bar": "baz"}}]}}')(obj)).toBe('[{"foo":{"bar":"baz"}}]');
}));
});


Expand Down