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

fix($compile): validate directive name for whitespaces #11772

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/content/error/$compile/baddir.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

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

Directives must start with a lowercase character.
Directives must start with a lowercase character and must not contain leading or trailing whitespaces.
5 changes: 5 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (!letter || letter !== lowercase(letter)) {
throw $compileMinErr('baddir', "Directive name '{0}' is invalid. The first character must be a lowercase letter", name);
}
if (name !== name.trim()) {
throw $compileMinErr('baddir',
"Directive name '{0}' is invalid. The name should not contain leading or trailing whitespaces",
name);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ describe('$compile', function() {
});
inject(function($compile) {});
});
it('should throw an exception if a directive name has leading or trailing whitespace', function() {
module(function() {
function assertLeadingOrTrailingWhitespaceInDirectiveName(name) {
expect(function() {
directive(name, function() { });
}).toThrowMinErr(
'$compile','baddir', 'Directive name \'' + name + '\' is invalid. ' +
"The name should not contain leading or trailing whitespaces");
}
assertLeadingOrTrailingWhitespaceInDirectiveName(' leadingWhitespaceDirectiveName');
assertLeadingOrTrailingWhitespaceInDirectiveName('trailingWhitespaceDirectiveName ');
assertLeadingOrTrailingWhitespaceInDirectiveName(' leadingAndTrailingWhitespaceDirectiveName ');
});
inject(function($compile) {});
});
});


Expand Down