From d92a237808ce79a4a32b0b6a5d721ad349008cbb Mon Sep 17 00:00:00 2001 From: Vladimir Lugovsky Date: Thu, 30 Apr 2015 16:02:59 +0300 Subject: [PATCH] fix($compile): validate directive name for whitespaces Throw an exception if directive name contains leading or trailing whitespaces Closes #11397 --- docs/content/error/$compile/baddir.ngdoc | 2 +- src/ng/compile.js | 5 +++++ test/ng/compileSpec.js | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) 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) {}); + }); });