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

feat($compile): Throw error on incorrect casing #11281

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
return bindings;
}

function assertValidDirectiveName(name) {
var letter = name.charAt(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the lowercase function as it handles some locale issues

if (!letter || letter !== lowercase(letter)) {
throw ngMinErr('badname', "Directive name '{0}' is invalid. The first letter of a directive must be a lowercase letter");
}
return name;
}

/**
* @ngdoc method
* @name $compileProvider#directive
Expand All @@ -797,6 +805,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
11 changes: 10 additions & 1 deletion test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ describe('$compile', function() {


describe('configuration', function() {

it('should fail to register a directive that does not start with a lowercase letter', function() {
module(function($compileProvider) {
expect($compileProvider.directive('BadDirectiveName', function() {
return {};
})).toThrowMinErr('badname', "Directive name 'BadDirectiveName' is invalid. The first letter of a directive must be a lowercase letter");
});
});

it('should register a directive', function() {
module(function() {
directive('div', function(log) {
Expand Down Expand Up @@ -7429,4 +7438,4 @@ describe('$compile', function() {
expect(element.hasClass('fire')).toBe(true);
}));
});
});
});