Skip to content

Commit

Permalink
fix($compile): ensure directive names have no leading or trailing whi…
Browse files Browse the repository at this point in the history
…tespace

Closes angular#11397
Closes angular#11772
  • Loading branch information
lugovsky authored and netman92 committed Aug 8, 2015
1 parent dda6962 commit 45af1e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/content/error/$compile/baddir.ngdoc
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
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
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

0 comments on commit 45af1e0

Please sign in to comment.