From 170ff9a37dea8772dda7c89e84176ac1a8992878 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Mon, 9 Mar 2015 11:12:25 -0700 Subject: [PATCH] fix($compile): throw error on invalid directive name Directive names must start with a lower case letter. Previously the compiler would quietly fail. This change adds an assertion that fails if this is not the case. Closes #11281 Closes #11109 --- docs/content/error/$compile/baddir.ngdoc | 8 ++++++++ src/ng/compile.js | 9 +++++++++ test/ng/compileSpec.js | 10 ++++++++++ 3 files changed, 27 insertions(+) create mode 100644 docs/content/error/$compile/baddir.ngdoc diff --git a/docs/content/error/$compile/baddir.ngdoc b/docs/content/error/$compile/baddir.ngdoc new file mode 100644 index 000000000000..56ff07a73e55 --- /dev/null +++ b/docs/content/error/$compile/baddir.ngdoc @@ -0,0 +1,8 @@ +@ngdoc error +@name $compile:baddir +@fullName Invalid Directive Name +@description + +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 diff --git a/src/ng/compile.js b/src/ng/compile.js index 9301582e904c..a071fcae56cc 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -790,6 +790,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { return bindings; } + function assertValidDirectiveName(name) { + var letter = name.charAt(0); + if (!letter || letter !== lowercase(letter)) { + throw $compileMinErr('baddir', "Directive name '{0}' is invalid. The first character must be a lowercase letter", name); + } + return name; + } + /** * @ngdoc method * @name $compileProvider#directive @@ -808,6 +816,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { this.directive = function registerDirective(name, directiveFactory) { assertNotHasOwnProperty(name, 'directive'); if (isString(name)) { + assertValidDirectiveName(name); assertArg(directiveFactory, 'directiveFactory'); if (!hasDirectives.hasOwnProperty(name)) { hasDirectives[name] = []; diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 613902bd120f..b89f1d08312c 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -147,6 +147,7 @@ describe('$compile', function() { describe('configuration', function() { + it('should register a directive', function() { module(function() { directive('div', function(log) { @@ -201,6 +202,15 @@ describe('$compile', function() { }); inject(function($compile) {}); }); + + it('should throw an exception if a directive name starts with a non-lowercase letter', function() { + module(function() { + expect(function() { + directive('BadDirectiveName', function() { }); + }).toThrowMinErr('$compile','baddir', "Directive name 'BadDirectiveName' is invalid. The first character must be a lowercase letter"); + }); + inject(function($compile) {}); + }); });