diff --git a/docs/content/error/$compile/baddir.ngdoc b/docs/content/error/$compile/baddir.ngdoc index 56ff07a73e55..3aef03d1e91b 100644 --- a/docs/content/error/$compile/baddir.ngdoc +++ b/docs/content/error/$compile/baddir.ngdoc @@ -5,4 +5,4 @@ This error occurs when the name of a directive is not valid. -Directives must start with a lowercase character. \ No newline at end of file +Directives must start with a lowercase character and must not contain leading or trailing whitespaces. diff --git a/src/ng/compile.js b/src/ng/compile.js index 0aba65aec107..140e5b61559c 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -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); + } } /** diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index b89f1d08312c..27e5dcb3c8a0 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -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) {}); + }); });