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

Commit 2c64dc4

Browse files
committed
fix($interpolate): use the first end symbol that forms a valid expression
When an interpolation string has multiple end symbol choices, pick the occurrence that forms a valid expression Closes #8642
1 parent 8ac9035 commit 2c64dc4

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/ng/interpolate.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,20 @@ function $InterpolateProvider() {
201201
if (index !== startIndex) {
202202
concat.push(unescapeText(text.substring(index, startIndex)));
203203
}
204-
exp = text.substring(startIndex + startSymbolLength, endIndex);
205-
expressions.push(exp);
206-
parseFns.push($parse(exp, parseStringifyInterceptor));
204+
do {
205+
try {
206+
exp = text.substring(startIndex + startSymbolLength, endIndex);
207+
parseFns.push($parse(exp, parseStringifyInterceptor));
208+
expressions.push(exp);
209+
} catch (e) {
210+
endIndex = text.indexOf(endSymbol, endIndex + 1);
211+
if (endIndex === -1) {
212+
throw e;
213+
} else {
214+
exp = undefined;
215+
}
216+
}
217+
} while (isUndefined(exp));
207218
index = endIndex + endSymbolLength;
208219
expressionPositions.push(concat.length);
209220
concat.push('');

test/ng/interpolateSpec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('$interpolate', function() {
105105
expect(function() {
106106
$interpolate('{{\\{\\{foo\\}\\}}}')(obj);
107107
}).toThrowMinErr('$parse', 'lexerr',
108-
'Lexer Error: Unexpected next character at columns 0-0 [\\] in expression [\\{\\{foo\\}\\]');
108+
'Lexer Error: Unexpected next character at columns 0-0 [\\] in expression [\\{\\{foo\\}\\}]');
109109
}));
110110

111111

@@ -116,6 +116,14 @@ describe('$interpolate', function() {
116116
it('should evaluate expressions between escaped start/end symbols', inject(function($interpolate) {
117117
expect($interpolate('\\{\\{Hello, {{bar}}!\\}\\}')(obj)).toBe('{{Hello, World!}}');
118118
}));
119+
120+
it('should pick the first end symbol location that forms a valid expression', inject(function($interpolate) {
121+
expect($interpolate('{{{}}}')(obj)).toBe('{}');
122+
expect($interpolate('{{"{{ }}"}}')(obj)).toBe('{{ }}');
123+
expect($interpolate('{{{foo: "bar"}}}')(obj)).toBe('{"foo":"bar"}');
124+
expect($interpolate('{{{foo: {"bar": "baz"}}}}')(obj)).toBe('{"foo":{"bar":"baz"}}');
125+
expect($interpolate('{{[{foo: {"bar": "baz"}}]}}')(obj)).toBe('[{"foo":{"bar":"baz"}}]');
126+
}));
119127
});
120128

121129

0 commit comments

Comments
 (0)