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

Commit 6c4490f

Browse files
committed
add ability to escape interpolation symbols
1 parent 0559652 commit 6c4490f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/ng/interpolate.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ var $interpolateMinErr = minErr('$interpolate');
4141
function $InterpolateProvider() {
4242
var startSymbol = '{{';
4343
var endSymbol = '}}';
44+
var escapedStartSymbol = '\\{\\{';
45+
var escapedEndSymbol = '\\}\\}';
4446

4547
/**
4648
* @ngdoc method
@@ -134,17 +136,26 @@ function $InterpolateProvider() {
134136
exp,
135137
concat = [];
136138

139+
function quoteForRegexp(pattern) {
140+
return pattern.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
141+
}
142+
function unescapeSymbols(text) {
143+
var startSymbolRegexp = new RegExp(quoteForRegexp(escapedStartSymbol), 'g');
144+
var endSymbolRegexp = new RegExp(quoteForRegexp(escapedEndSymbol), 'g');
145+
return text.replace(startSymbolRegexp, startSymbol).replace(endSymbolRegexp, endSymbol);
146+
}
147+
137148
while(index < length) {
138149
if ( ((startIndex = text.indexOf(startSymbol, index)) != -1) &&
139150
((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1) ) {
140-
(index != startIndex) && parts.push(text.substring(index, startIndex));
151+
(index != startIndex) && parts.push(unescapeSymbols(text.substring(index, startIndex)));
141152
parts.push(fn = $parse(exp = text.substring(startIndex + startSymbolLength, endIndex)));
142153
fn.exp = exp;
143154
index = endIndex + endSymbolLength;
144155
hasInterpolation = true;
145156
} else {
146157
// we did not find anything, so we have to add the remainder to the parts array
147-
(index != length) && parts.push(text.substring(index));
158+
(index != length) && parts.push(unescapeSymbols(text.substring(index)));
148159
index = length;
149160
}
150161
}

test/ng/interpolateSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ describe('$interpolate', function() {
6666
expect($interpolate("Hello {{'World' + foo()}}")($rootScope)).toEqual('Hello World');
6767
}));
6868

69+
it('should unescape escaped interpolation brackets', inject(function($interpolate, $rootScope) {
70+
expect($interpolate("Hello \\{\\{World\\}\\}!")($rootScope)).toEqual('Hello {{World}}!');
71+
expect($interpolate("{{ 'Hello' }} \\{\\{World\\}\\}!")($rootScope)).toEqual('Hello {{World}}!');
72+
expect($interpolate("Hello \\{\\{World\\}\\}{{ '!' }}")($rootScope)).toEqual('Hello {{World}}!');
73+
}));
74+
6975

7076
describe('interpolating in a trusted context', function() {
7177
var sce;

0 commit comments

Comments
 (0)