Skip to content

Commit 974ccdd

Browse files
committed
fix($compile): validate directive name for whitespaces
Throw an exception if directive name contains leading or trailing whitespaces Closes angular#11397
1 parent d83bddc commit 974ccdd

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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('baddirwspc',
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','baddirwspc', '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)