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

fix($compile): prevent inlined JSON values in directive templates from being broken by a wrong replacement of '}}' with the user-defined endSymbol. #6493

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 28 additions & 3 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,13 +793,38 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}
};

// Search for '{{' and '}}' and replace them with the start and end symbols defined
// by the developer in $interpolateProvider.
function denormalizeTemplateFn(template) {
var length = template.length,
index = 0,
startIndex = template.indexOf('{{'),
endIndex;
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we did it like this instead:

template.replace(/\{\{(.*?)\}\}/g, function(_, expr) {
  return startSymbol + expr + endSymbol;
});

I could be wrong (and probably am slightly off on the syntax here), but I would expect this to work a lot better, and be significantly less code.


while (index < length) {
if ( ((startIndex = template.indexOf('{{', index)) != -1) && ((endIndex = template.indexOf('}}', startIndex + 2)) != -1) ) {
template = template.substring(0, startIndex)
+ startSymbol
+ template.substring(startIndex + 2, endIndex)
+ endSymbol
+ template.substring(endIndex + 2);
index = endIndex + (startSymbol.length - 2) + endSymbol.length;
// length of template may have changed if startSymbol and/or endSymbol
// have different length than default symbols ('{{' and '}}')
length = template.length;
} else {
index = length;
}
}

return template;
}

var startSymbol = $interpolate.startSymbol(),
endSymbol = $interpolate.endSymbol(),
denormalizeTemplate = (startSymbol == '{{' || endSymbol == '}}')
? identity
: function denormalizeTemplate(template) {
return template.replace(/\{\{/g, startSymbol).replace(/}}/g, endSymbol);
},
: denormalizeTemplateFn,
NG_ATTR_BINDING = /^ngAttr[A-Z]/;


Expand Down
18 changes: 18 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,24 @@ describe('$compile', function() {
});


it('should NOT break inlined JSON values in directive template',
function() {
module(function($interpolateProvider, $compileProvider) {
$interpolateProvider.startSymbol('##').endSymbol(']]');
$compileProvider.directive('myDirective', function() {
return {
template: '<span foo=\'{"ctx":{"id":3}}\'></span>'
};
});
});

inject(function($compile) {
element = $compile('<div>##hello|uppercase]]|<div my-directive></div></div>')($rootScope);
expect(element.children('div').children('span').attr('foo')).toBe('{"ctx":{"id":3}}');
});
});


it('should support custom start/end interpolation symbols in async directive template',
function() {
module(function($interpolateProvider, $compileProvider) {
Expand Down