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

Commit dfe9983

Browse files
committed
fix($compile): denormalize directive templates
Since developers are allowed to customize start/end interpolation strings, but third-party directive creators don't know about these customizations, we should standardize on {{ }} in templates of reusable (third-party) directives. During the compilation, these templates are then denormalized to use whatever the custom start/end symbol is, effectively translating the template into the syntax of the runtime environment. This addresses an issue raised at http://goo.gl/e8VPV Existing code should not be affected by this change since project that do use custom interpolation markers are not expected to use {{ }} in existing directive templates.
1 parent 0f37194 commit dfe9983

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/ng/compile.js

+12
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ function $CompileProvider($provide) {
297297
}
298298
};
299299

300+
var startSymbol = $interpolate.startSymbol(),
301+
endSymbol = $interpolate.endSymbol(),
302+
denormalizeTemplate = (startSymbol == '{{' || endSymbol == '}}')
303+
? identity
304+
: function denormalizeTemplate(template) {
305+
return template.replace(/\{\{/g, startSymbol).replace(/}}/g, endSymbol);
306+
};
307+
308+
300309
return compile;
301310

302311
//================================
@@ -579,6 +588,7 @@ function $CompileProvider($provide) {
579588
if ((directiveValue = directive.template)) {
580589
assertNoDuplicate('template', templateDirective, directive, $compileNode);
581590
templateDirective = directive;
591+
directiveValue = denormalizeTemplate(directiveValue);
582592

583593
if (directive.replace) {
584594
$template = jqLite('<div>' +
@@ -898,6 +908,8 @@ function $CompileProvider($provide) {
898908
success(function(content) {
899909
var compileNode, tempTemplateAttrs, $template;
900910

911+
content = denormalizeTemplate(content);
912+
901913
if (replace) {
902914
$template = jqLite('<div>' + trim(content) + '</div>').contents();
903915
compileNode = $template[0];

test/ng/compileSpec.js

+41
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,47 @@ describe('$compile', function() {
13721372
'<option>Greet Misko!</option>' +
13731373
'</select>');
13741374
}));
1375+
1376+
1377+
it('should support custom start/end interpolation symbols in template and directive template',
1378+
function() {
1379+
module(function($interpolateProvider, $compileProvider) {
1380+
$interpolateProvider.startSymbol('##').endSymbol(']]');
1381+
$compileProvider.directive('myDirective', function() {
1382+
return {
1383+
template: '<span>{{hello}}|{{hello|uppercase}}</span>'
1384+
};
1385+
});
1386+
});
1387+
1388+
inject(function($compile, $rootScope) {
1389+
element = $compile('<div>##hello|uppercase]]|<div my-directive></div></div>')($rootScope);
1390+
$rootScope.hello = 'ahoj';
1391+
$rootScope.$digest();
1392+
expect(element.text()).toBe('AHOJ|ahoj|AHOJ');
1393+
});
1394+
});
1395+
1396+
1397+
it('should support custom start/end interpolation symbols in async directive template',
1398+
function() {
1399+
module(function($interpolateProvider, $compileProvider) {
1400+
$interpolateProvider.startSymbol('##').endSymbol(']]');
1401+
$compileProvider.directive('myDirective', function() {
1402+
return {
1403+
templateUrl: 'myDirective.html'
1404+
};
1405+
});
1406+
});
1407+
1408+
inject(function($compile, $rootScope, $templateCache) {
1409+
$templateCache.put('myDirective.html', '<span>{{hello}}|{{hello|uppercase}}</span>');
1410+
element = $compile('<div>##hello|uppercase]]|<div my-directive></div></div>')($rootScope);
1411+
$rootScope.hello = 'ahoj';
1412+
$rootScope.$digest();
1413+
expect(element.text()).toBe('AHOJ|ahoj|AHOJ');
1414+
});
1415+
});
13751416
});
13761417

13771418

0 commit comments

Comments
 (0)