Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wesleycho authored and petebacondarwin committed Mar 17, 2015
1 parent 2ca34a0 commit 634e467
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 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.
9 changes: 9 additions & 0 deletions src/ng/compile.js
Expand Up @@ -750,6 +750,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
Expand All @@ -768,6 +776,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] = [];
Expand Down
10 changes: 10 additions & 0 deletions test/ng/compileSpec.js
Expand Up @@ -147,6 +147,7 @@ describe('$compile', function() {


describe('configuration', function() {

it('should register a directive', function() {
module(function() {
directive('div', function(log) {
Expand Down Expand Up @@ -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) {});
});
});


Expand Down

0 comments on commit 634e467

Please sign in to comment.