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

Commit bab474a

Browse files
lugovskypetebacondarwin
authored andcommitted
fix($compile): ensure directive names have no leading or trailing whitespace
Closes #11397 Closes #11772
1 parent f2c94c6 commit bab474a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

docs/content/error/$compile/baddir.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
This error occurs when the name of a directive is not valid.
77

8-
Directives must start with a lowercase character.
8+
Directives must start with a lowercase character and must not contain leading or trailing whitespaces.

src/ng/compile.js

+5
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
802802
if (!letter || letter !== lowercase(letter)) {
803803
throw $compileMinErr('baddir', "Directive name '{0}' is invalid. The first character must be a lowercase letter", name);
804804
}
805+
if (name !== name.trim()) {
806+
throw $compileMinErr('baddir',
807+
"Directive name '{0}' is invalid. The name should not contain leading or trailing whitespaces",
808+
name);
809+
}
805810
}
806811

807812
/**

test/ng/compileSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ describe('$compile', function() {
211211
});
212212
inject(function($compile) {});
213213
});
214+
it('should throw an exception if a directive name has leading or trailing whitespace', function() {
215+
module(function() {
216+
function assertLeadingOrTrailingWhitespaceInDirectiveName(name) {
217+
expect(function() {
218+
directive(name, function() { });
219+
}).toThrowMinErr(
220+
'$compile','baddir', 'Directive name \'' + name + '\' is invalid. ' +
221+
"The name should not contain leading or trailing whitespaces");
222+
}
223+
assertLeadingOrTrailingWhitespaceInDirectiveName(' leadingWhitespaceDirectiveName');
224+
assertLeadingOrTrailingWhitespaceInDirectiveName('trailingWhitespaceDirectiveName ');
225+
assertLeadingOrTrailingWhitespaceInDirectiveName(' leadingAndTrailingWhitespaceDirectiveName ');
226+
});
227+
inject(function($compile) {});
228+
});
214229
});
215230

216231

0 commit comments

Comments
 (0)