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

fix($compile): sanitize special chars in directive name #16314

Merged
Show file tree
Hide file tree
Changes from 5 commits
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
4 changes: 3 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3647,7 +3647,9 @@ var SPECIAL_CHARS_REGEXP = /[:\-_]+(.)/g;
function directiveNormalize(name) {
return name
.replace(PREFIX_REGEXP, '')
.replace(SPECIAL_CHARS_REGEXP, fnCamelCaseReplace);
.replace(SPECIAL_CHARS_REGEXP, function(_, letter, offset) {
return offset ? letter.toUpperCase() : letter;
});
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,27 @@ describe('$compile', function() {
inject(function($compile) {});
});

it('should omit special chars before processing attribute directive name', function() {
Copy link
Member

Choose a reason for hiding this comment

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

I would change "omit" to "ignore", because I think it bettr describes the behavior.

// a regression https://github.com/angular/angular.js/issues/16278
module(function() {
directive('t', function(log) {
return {
restrict: 'A',
link: {
pre: log.fn('pre'),
post: log.fn('post')
}
};
});
});
inject(function($compile, $rootScope, log) {
element = $compile('<div _t></div>')($rootScope);
Copy link
Member

Choose a reason for hiding this comment

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

The element = parts are redundant.

element = $compile('<div -t></div>')($rootScope);
element = $compile('<div :t></div>')($rootScope);
expect(log).toEqual('pre; post; pre; post; pre; post');
});
});

it('should throw an exception if the directive factory is not defined', function() {
module(function() {
expect(function() {
Expand Down