Skip to content

Commit 3105ff1

Browse files
fix($compile): support merging special attribute names in replace directives
When compiling a `replace` directive, the compiler merges the attributes from the replaced element onto the template element. Unfortunately, `setAttribute` and other related DOM methods do not allow certain attribute names - in particular Angular 2 style names such as `(click)` and `[value]`. This is relevant when using ngForward with Angular Material, since the `mgButton` directive uses `replace` and in the former you often use `(click)`. This fixes the problem but for those special attributes the speed is considerably slow. Closes angular#13317
1 parent ccd47ec commit 3105ff1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/ng/compile.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
989989
function($injector, $interpolate, $exceptionHandler, $templateRequest, $parse,
990990
$controller, $rootScope, $document, $sce, $animate, $$sanitizeUri) {
991991

992+
var SIMPLE_ATTR_NAME = /^\w/;
993+
var QUOTE_REGEX = /'/g;
994+
var specialAttrHolder = document.createElement('div');
992995
var Attributes = function(element, attributesToCopy) {
993996
if (attributesToCopy) {
994997
var keys = Object.keys(attributesToCopy);
@@ -1168,7 +1171,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
11681171
if (value === null || isUndefined(value)) {
11691172
this.$$element.removeAttr(attrName);
11701173
} else {
1171-
this.$$element.attr(attrName, value);
1174+
if (SIMPLE_ATTR_NAME.test(attrName)) {
1175+
this.$$element.attr(attrName, value);
1176+
} else {
1177+
setSpecialAttr(this.$$element[0], attrName, value);
1178+
}
11721179
}
11731180
}
11741181

@@ -1221,6 +1228,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
12211228
}
12221229
};
12231230

1231+
function setSpecialAttr(element, attrName, value) {
1232+
specialAttrHolder.innerHTML = "<span " + attrName + "='" + (value || '').replace(QUOTE_REGEX, '&quot;') + "'>";
1233+
var attribute = specialAttrHolder.firstChild.attributes[0].cloneNode();
1234+
element.attributes.setNamedItem(attribute);
1235+
}
12241236

12251237
function safeAddClass($element, className) {
12261238
try {

test/ng/compileSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,16 @@ describe('$compile', function() {
868868
expect(div.attr('id')).toEqual('myid');
869869
}));
870870

871+
872+
it('should correctly merge attributes that contain special characters', inject(function($compile, $rootScope) {
873+
element = $compile(
874+
'<div><div replace (click)="doSomething()" [value]="someExpression"></div><div>')($rootScope);
875+
var div = element.find('div');
876+
expect(div.attr('(click)')).toEqual('doSomething()');
877+
expect(div.attr('[value]')).toEqual('someExpression');
878+
}));
879+
880+
871881
it('should prevent multiple templates per element', inject(function($compile) {
872882
try {
873883
$compile('<div><span replace class="replace"></span></div>');

0 commit comments

Comments
 (0)