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

Commit 20f3c34

Browse files
committed
refactor($compile): detect and log bad directive 'restrict' values on directive factory init
1 parent 0ecf109 commit 20f3c34

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@ngdoc error
2+
@name $compile:badrestrict
3+
@fullName Invalid Directive Restrict
4+
@description
5+
6+
This error occurs when the restrict property of a directive is not valid.
7+
8+
The directive restrict property must be a string with possible character values being:
9+
* E (element)
10+
* A (attribute)
11+
* C (class)
12+
* M (comment)

src/ng/compile.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
969969
return require;
970970
}
971971

972+
function getDirectiveRestrict(directive, name) {
973+
if (directive.restrict != null && !(isString(directive.restrict) && /[EACM]/.test(directive.restrict))) {
974+
throw $compileMinErr('badrestrict',
975+
"Restrict property '{0}' of directive '{1}' is invalid",
976+
directive.restrict,
977+
name);
978+
}
979+
980+
return directive.restrict || 'EA';
981+
}
982+
972983
/**
973984
* @ngdoc method
974985
* @name $compileProvider#directive
@@ -989,6 +1000,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
9891000
if (isString(name)) {
9901001
assertValidDirectiveName(name);
9911002
assertArg(directiveFactory, 'directiveFactory');
1003+
9921004
if (!hasDirectives.hasOwnProperty(name)) {
9931005
hasDirectives[name] = [];
9941006
$provide.factory(name + Suffix, ['$injector', '$exceptionHandler',
@@ -1006,7 +1018,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10061018
directive.index = index;
10071019
directive.name = directive.name || name;
10081020
directive.require = getDirectiveRequire(directive);
1009-
directive.restrict = directive.restrict || 'EA';
1021+
directive.restrict = getDirectiveRestrict(directive, name);
10101022
directive.$$moduleName = directiveFactory.$$moduleName;
10111023
directives.push(directive);
10121024
} catch (e) {

test/ng/compileSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -5558,6 +5558,42 @@ describe('$compile', function() {
55585558
});
55595559

55605560

5561+
it('should throw badrestrict on first compilation when restrict is invalid', function() {
5562+
module(function($compileProvider, $exceptionHandlerProvider) {
5563+
$compileProvider.directive('invalidRestrictBadString', valueFn({restrict: '"'}));
5564+
$compileProvider.directive('invalidRestrictFalse', valueFn({restrict: false}));
5565+
$compileProvider.directive('invalidRestrictTrue', valueFn({restrict: true}));
5566+
$compileProvider.directive('invalidRestrictObject', valueFn({restrict: {}}));
5567+
$compileProvider.directive('invalidRestrictZero', valueFn({restrict: 0}));
5568+
5569+
// We need to test with the exceptionHandler not rethrowing...
5570+
$exceptionHandlerProvider.mode('log');
5571+
});
5572+
5573+
inject(function($exceptionHandler, $compile, $rootScope) {
5574+
$compile('<div invalid-restrict-false>')($rootScope);
5575+
expect($exceptionHandler.errors.length).toBe(1);
5576+
expect($exceptionHandler.errors[0]).toMatch(/\$compile.*badrestrict.*'false'/);
5577+
5578+
$compile('<div invalid-restrict-true>')($rootScope);
5579+
expect($exceptionHandler.errors.length).toBe(2);
5580+
expect($exceptionHandler.errors[1]).toMatch(/\$compile.*badrestrict.*'true'/);
5581+
5582+
$compile('<div invalid-restrict-bad-string>')($rootScope);
5583+
expect($exceptionHandler.errors.length).toBe(3);
5584+
expect($exceptionHandler.errors[2]).toMatch(/\$compile.*badrestrict.*'\"'/);
5585+
5586+
$compile('<div invalid-restrict-object invalid-restrict-zero>')($rootScope);
5587+
expect($exceptionHandler.errors.length).toBe(5);
5588+
expect($exceptionHandler.errors[3]).toMatch(/\$compile.*badrestrict.*'{}'/);
5589+
expect($exceptionHandler.errors[4]).toMatch(/\$compile.*badrestrict.*'0'/);
5590+
5591+
$compile('<div invalid-restrict-true invalid-restrict-zero>')($rootScope);
5592+
expect($exceptionHandler.errors.length).toBe(5);
5593+
});
5594+
});
5595+
5596+
55615597
it('should throw noident when missing controllerAs directive property', function() {
55625598
module(function($compileProvider) {
55635599
$compileProvider.directive('noIdent', valueFn({

0 commit comments

Comments
 (0)