Skip to content
Permalink
Browse files

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 9, 2015
1 parent 2ca34a0 commit 634e467172efa696eb32ef8942ffbedeecbd030e
Showing with 27 additions and 0 deletions.
  1. +8 −0 docs/content/error/$compile/baddir.ngdoc
  2. +9 −0 src/ng/compile.js
  3. +10 −0 test/ng/compileSpec.js
@@ -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.
@@ -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
@@ -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] = [];
@@ -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) {});
});
});


0 comments on commit 634e467

Please sign in to comment.
You can’t perform that action at this time.