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

Commit 18226ee

Browse files
committed
fix($compile): special chars are sanitized from directive name in markup
this fixes bug introduced in 1.6.x when directive name with preceeding special char in HTML markup does not match the normalized registered name
1 parent 817ac56 commit 18226ee

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3637,7 +3637,7 @@ function SimpleChange(previous, current) {
36373637
SimpleChange.prototype.isFirstChange = function() { return this.previousValue === _UNINITIALIZED_VALUE; };
36383638

36393639

3640-
var PREFIX_REGEXP = /^((?:x|data)[:\-_])/i;
3640+
var PREFIX_REGEXP = /^((?:x|data)*[:\-_])/i;
36413641
var SPECIAL_CHARS_REGEXP = /[:\-_]+(.)/g;
36423642

36433643
/**

test/ng/compileSpec.js

+88-1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,26 @@ describe('$compile', function() {
294294
inject(function($compile) {});
295295
});
296296

297+
it('should omit special chars before processing directive name', function() {
298+
module(function() {
299+
directive('t', function(log) {
300+
return {
301+
restrict: 'A',
302+
link: {
303+
pre: log.fn('pre'),
304+
post: log.fn('post')
305+
}
306+
};
307+
});
308+
});
309+
inject(function($compile, $rootScope, log) {
310+
element = $compile('<div _t></div>')($rootScope);
311+
element = $compile('<div -t></div>')($rootScope);
312+
element = $compile('<div :t></div>')($rootScope);
313+
expect(log).toEqual('pre; post; pre; post; pre; post');
314+
});
315+
});
316+
297317
it('should throw an exception if the directive factory is not defined', function() {
298318
module(function() {
299319
expect(function() {
@@ -10638,7 +10658,10 @@ describe('$compile', function() {
1063810658
template:
1063910659
'<div class="a" ng-transclude="ng-transclude"></div>' +
1064010660
'<div class="b" ng:transclude="ng:transclude"></div>' +
10641-
'<div class="c" data-ng-transclude="data-ng-transclude"></div>'
10661+
'<div class="c" data-ng-transclude="data-ng-transclude"></div>' +
10662+
'<div class="d" -ng-transclude="-ng-transclude"></div>' +
10663+
'<div class="e" _ng_transclude="_ng_transclude"></div>' +
10664+
'<div class="f" :ng:transclude=":ng:transclude"></div>'
1064210665
};
1064310666
});
1064410667
});
@@ -10653,12 +10676,21 @@ describe('$compile', function() {
1065310676
var a = element.children().eq(0);
1065410677
var b = element.children().eq(1);
1065510678
var c = element.children().eq(2);
10679+
var d = element.children().eq(3);
10680+
var e = element.children().eq(4);
10681+
var f = element.children().eq(5);
1065610682
expect(a).toHaveClass('a');
1065710683
expect(b).toHaveClass('b');
1065810684
expect(c).toHaveClass('c');
10685+
expect(d).toHaveClass('d');
10686+
expect(e).toHaveClass('e');
10687+
expect(f).toHaveClass('f');
1065910688
expect(a.text()).toEqual('stuartbobkevin');
1066010689
expect(b.text()).toEqual('stuartbobkevin');
1066110690
expect(c.text()).toEqual('stuartbobkevin');
10691+
expect(d.text()).toEqual('stuartbobkevin');
10692+
expect(e.text()).toEqual('stuartbobkevin');
10693+
expect(f.text()).toEqual('stuartbobkevin');
1066210694
});
1066310695
});
1066410696

@@ -11710,6 +11742,18 @@ describe('$compile', function() {
1171011742
expect(element.attr('dash-test4')).toBe('JamieMason');
1171111743
}));
1171211744

11745+
it('should work if they are prefixed with special chars', inject(function() {
11746+
$rootScope.name = 'JamieMason';
11747+
element = $compile('<span -ng-attr-dash-test2="{{name}}" _ng-attr-dash-test3="{{name}}" :ng:attr-dash-test4="{{name}}"></span>')($rootScope);
11748+
expect(element.attr('dash-test2')).toBeUndefined();
11749+
expect(element.attr('dash-test3')).toBeUndefined();
11750+
expect(element.attr('dash-test4')).toBeUndefined();
11751+
$rootScope.$digest();
11752+
expect(element.attr('dash-test2')).toBe('JamieMason');
11753+
expect(element.attr('dash-test3')).toBe('JamieMason');
11754+
expect(element.attr('dash-test4')).toBe('JamieMason');
11755+
}));
11756+
1171311757
it('should keep attributes ending with -start single-element directives', function() {
1171411758
module(function($compileProvider) {
1171511759
$compileProvider.directive('dashStarter', function(log) {
@@ -11774,6 +11818,27 @@ describe('$compile', function() {
1177411818
expect(element.find('feDiffuseLighting').attr('surfaceScale')).toBe('1');
1177511819
expect(element.find('feSpecularLighting').attr('surfaceScale')).toBe('1');
1177611820
}));
11821+
11822+
it('should work if they are prefixed with special chars', inject(function($compile, $rootScope) {
11823+
$rootScope.dimensions = '0 0 0 0';
11824+
$rootScope.number = 0.42;
11825+
$rootScope.scale = 1;
11826+
11827+
element = $compile('<svg -ng-attr-view_box="{{dimensions}}">' +
11828+
'<filter _ng-attr-filter_units="{{number}}">' +
11829+
'<feDiffuseLighting -ng:attr_surface_scale="{{scale}}"' +
11830+
':ng:attr_diffuse_constant="{{number}}"></feDiffuseLighting>' +
11831+
'<feSpecularLighting _ng:attr_surface_scale="{{scale}}"' +
11832+
':ng-attr_diffuse_constant="{{number}}"></feSpecularLighting>')($rootScope);
11833+
expect(element.attr('viewBox')).toBeUndefined();
11834+
$rootScope.$digest();
11835+
expect(element.attr('viewBox')).toBe('0 0 0 0');
11836+
expect(element.find('filter').attr('filterUnits')).toBe('0.42');
11837+
expect(element.find('feDiffuseLighting').attr('surfaceScale')).toBe('1');
11838+
expect(element.find('feDiffuseLighting').attr('diffuseConstant')).toBe('0.42');
11839+
expect(element.find('feSpecularLighting').attr('surfaceScale')).toBe('1');
11840+
expect(element.find('feSpecularLighting').attr('diffuseConstant')).toBe('0.42');
11841+
}));
1177711842
});
1177811843

1177911844
describe('multi-element directive', function() {
@@ -12129,6 +12194,28 @@ describe('$compile', function() {
1212912194
expect(spans.eq(2)).toBeHidden();
1213012195
expect(spans.eq(3)).toBeHidden();
1213112196
}));
12197+
12198+
12199+
it('should support special char prefix', inject(function($compile, $rootScope) {
12200+
$rootScope.show = false;
12201+
element = $compile(
12202+
'<div>' +
12203+
'<span -ng-show-start="show"></span>' +
12204+
'<span -ng-show-end></span>' +
12205+
'<span :ng-show-start="show"></span>' +
12206+
'<span :ng-show-end></span>' +
12207+
'<span _ng-show-start="show"></span>' +
12208+
'<span _ng-show-end></span>' +
12209+
'</div>')($rootScope);
12210+
$rootScope.$digest();
12211+
var spans = element.find('span');
12212+
expect(spans.eq(0)).toBeHidden();
12213+
expect(spans.eq(1)).toBeHidden();
12214+
expect(spans.eq(2)).toBeHidden();
12215+
expect(spans.eq(3)).toBeHidden();
12216+
expect(spans.eq(4)).toBeHidden();
12217+
expect(spans.eq(5)).toBeHidden();
12218+
}));
1213212219
});
1213312220

1213412221
describe('$animate animation hooks', function() {

0 commit comments

Comments
 (0)