mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-15 18:23:09 +00:00
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.
This commit is contained in:
parent
ab044cada6
commit
7d77de2834
2 changed files with 53 additions and 0 deletions
|
|
@ -297,6 +297,15 @@ function $CompileProvider($provide) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var startSymbol = $interpolate.startSymbol(),
|
||||||
|
endSymbol = $interpolate.endSymbol(),
|
||||||
|
denormalizeTemplate = (startSymbol == '{{' || endSymbol == '}}')
|
||||||
|
? identity
|
||||||
|
: function denormalizeTemplate(template) {
|
||||||
|
return template.replace(/\{\{/g, startSymbol).replace(/}}/g, endSymbol);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return compile;
|
return compile;
|
||||||
|
|
||||||
//================================
|
//================================
|
||||||
|
|
@ -579,6 +588,7 @@ function $CompileProvider($provide) {
|
||||||
if ((directiveValue = directive.template)) {
|
if ((directiveValue = directive.template)) {
|
||||||
assertNoDuplicate('template', templateDirective, directive, $compileNode);
|
assertNoDuplicate('template', templateDirective, directive, $compileNode);
|
||||||
templateDirective = directive;
|
templateDirective = directive;
|
||||||
|
directiveValue = denormalizeTemplate(directiveValue);
|
||||||
|
|
||||||
if (directive.replace) {
|
if (directive.replace) {
|
||||||
$template = jqLite('<div>' +
|
$template = jqLite('<div>' +
|
||||||
|
|
@ -898,6 +908,8 @@ function $CompileProvider($provide) {
|
||||||
success(function(content) {
|
success(function(content) {
|
||||||
var compileNode, tempTemplateAttrs, $template;
|
var compileNode, tempTemplateAttrs, $template;
|
||||||
|
|
||||||
|
content = denormalizeTemplate(content);
|
||||||
|
|
||||||
if (replace) {
|
if (replace) {
|
||||||
$template = jqLite('<div>' + trim(content) + '</div>').contents();
|
$template = jqLite('<div>' + trim(content) + '</div>').contents();
|
||||||
compileNode = $template[0];
|
compileNode = $template[0];
|
||||||
|
|
|
||||||
|
|
@ -1372,6 +1372,47 @@ describe('$compile', function() {
|
||||||
'<option>Greet Misko!</option>' +
|
'<option>Greet Misko!</option>' +
|
||||||
'</select>');
|
'</select>');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
it('should support custom start/end interpolation symbols in template and directive template',
|
||||||
|
function() {
|
||||||
|
module(function($interpolateProvider, $compileProvider) {
|
||||||
|
$interpolateProvider.startSymbol('##').endSymbol(']]');
|
||||||
|
$compileProvider.directive('myDirective', function() {
|
||||||
|
return {
|
||||||
|
template: '<span>{{hello}}|{{hello|uppercase}}</span>'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
inject(function($compile, $rootScope) {
|
||||||
|
element = $compile('<div>##hello|uppercase]]|<div my-directive></div></div>')($rootScope);
|
||||||
|
$rootScope.hello = 'ahoj';
|
||||||
|
$rootScope.$digest();
|
||||||
|
expect(element.text()).toBe('AHOJ|ahoj|AHOJ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should support custom start/end interpolation symbols in async directive template',
|
||||||
|
function() {
|
||||||
|
module(function($interpolateProvider, $compileProvider) {
|
||||||
|
$interpolateProvider.startSymbol('##').endSymbol(']]');
|
||||||
|
$compileProvider.directive('myDirective', function() {
|
||||||
|
return {
|
||||||
|
templateUrl: 'myDirective.html'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
inject(function($compile, $rootScope, $templateCache) {
|
||||||
|
$templateCache.put('myDirective.html', '<span>{{hello}}|{{hello|uppercase}}</span>');
|
||||||
|
element = $compile('<div>##hello|uppercase]]|<div my-directive></div></div>')($rootScope);
|
||||||
|
$rootScope.hello = 'ahoj';
|
||||||
|
$rootScope.$digest();
|
||||||
|
expect(element.text()).toBe('AHOJ|ahoj|AHOJ');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue