Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 34 additions & 6 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,29 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
return TTL;
};


function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function createGlobalRegexMatcher(str) {
return new RegExp(escapeRegExp(str), 'g');
}
var moduleSymbolMap = createMap();
var defaultSymbols = {
startSymbol: '{{',
startRegex: /\{\{/g,
endSymbol: '}}',
endRegex: /\}\}/g
};
this.moduleSymbols = function(moduleName, startSymbol, endSymbol) {
moduleSymbolMap[moduleName] = {
startSymbol: startSymbol,
startRegex: createGlobalRegexMatcher(startSymbol),
endSymbol: endSymbol,
endRegex: createGlobalRegexMatcher(endSymbol)
};
};

this.$get = [
'$injector', '$interpolate', '$exceptionHandler', '$templateRequest', '$parse',
'$controller', '$rootScope', '$sce', '$animate', '$$sanitizeUri',
Expand Down Expand Up @@ -1585,10 +1608,15 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

var startSymbol = $interpolate.startSymbol(),
endSymbol = $interpolate.endSymbol(),
denormalizeTemplate = (startSymbol === '{{' && endSymbol === '}}')
? identity
: function denormalizeTemplate(template) {
return template.replace(/\{\{/g, startSymbol).replace(/}}/g, endSymbol);
denormalizeTemplate = function(moduleName, template) {
var moduleSymbols = moduleSymbolMap[moduleName] || defaultSymbols;
if (moduleSymbols.startSymbol !== startSymbol) {
template = template.replace(moduleSymbols.startRegex, startSymbol);
}
if (moduleSymbols.endSymbol !== endSymbol) {
template = template.replace(moduleSymbols.endRegex, endSymbol);
}
return template;
},
NG_ATTR_BINDING = /^ngAttr[A-Z]/;
var MULTI_ELEMENT_DIR_RE = /^(.+)Start$/;
Expand Down Expand Up @@ -2274,7 +2302,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
? directive.template($compileNode, templateAttrs)
: directive.template;

directiveValue = denormalizeTemplate(directiveValue);
directiveValue = denormalizeTemplate(directive.$$moduleName, directiveValue);

if (directive.replace) {
replaceDirective = directive;
Expand Down Expand Up @@ -2782,7 +2810,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
.then(function(content) {
var compileNode, tempTemplateAttrs, $template, childBoundTranscludeFn;

content = denormalizeTemplate(content);
content = denormalizeTemplate(origAsyncDirective.$$moduleName, content);

if (origAsyncDirective.replace) {
if (jqLiteIsTextNode(content)) {
Expand Down
20 changes: 20 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3155,6 +3155,26 @@ describe('$compile', function() {
});
});

it('should allow modules to specify what interpolation symbol is used in templates', function() {
angular.module('symbol-test', [])
.directive('myDirective', function() {
return {
template: '<span foo=\'{"ctx":{"id":3}}\'></span>'
};
});

module('symbol-test', function($interpolateProvider, $compileProvider) {
$interpolateProvider.startSymbol('##');
Copy link
Contributor

Choose a reason for hiding this comment

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

these two lines should not be there, as with them, the test makes no sense.

$interpolateProvider.endSymbol(']]');
$compileProvider.moduleSymbols('symbol-test', '##', ']]');
});

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


it('should support custom start interpolation symbol, even when `endSymbol` doesn\'t change',
function() {
Expand Down