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

Commit 634e467

Browse files
wesleychopetebacondarwin
authored andcommitted
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
1 parent 2ca34a0 commit 634e467

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@ngdoc error
2+
@name $compile:baddir
3+
@fullName Invalid Directive Name
4+
@description
5+
6+
This error occurs when the name of a directive is not valid.
7+
8+
Directives must start with a lowercase character.

src/ng/compile.js

+9
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
750750
return bindings;
751751
}
752752

753+
function assertValidDirectiveName(name) {
754+
var letter = name.charAt(0);
755+
if (!letter || letter !== lowercase(letter)) {
756+
throw $compileMinErr('baddir', "Directive name '{0}' is invalid. The first character must be a lowercase letter", name);
757+
}
758+
return name;
759+
}
760+
753761
/**
754762
* @ngdoc method
755763
* @name $compileProvider#directive
@@ -768,6 +776,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
768776
this.directive = function registerDirective(name, directiveFactory) {
769777
assertNotHasOwnProperty(name, 'directive');
770778
if (isString(name)) {
779+
assertValidDirectiveName(name);
771780
assertArg(directiveFactory, 'directiveFactory');
772781
if (!hasDirectives.hasOwnProperty(name)) {
773782
hasDirectives[name] = [];

test/ng/compileSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe('$compile', function() {
147147

148148

149149
describe('configuration', function() {
150+
150151
it('should register a directive', function() {
151152
module(function() {
152153
directive('div', function(log) {
@@ -201,6 +202,15 @@ describe('$compile', function() {
201202
});
202203
inject(function($compile) {});
203204
});
205+
206+
it('should throw an exception if a directive name starts with a non-lowercase letter', function() {
207+
module(function() {
208+
expect(function() {
209+
directive('BadDirectiveName', function() { });
210+
}).toThrowMinErr('$compile','baddir', "Directive name 'BadDirectiveName' is invalid. The first character must be a lowercase letter");
211+
});
212+
inject(function($compile) {});
213+
});
204214
});
205215

206216

0 commit comments

Comments
 (0)