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

Commit

Permalink
perf($compile): add a flag to compile only EA directives
Browse files Browse the repository at this point in the history
  • Loading branch information
drpicox committed Jun 30, 2016
1 parent 3a40bd4 commit ab35cc9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
21 changes: 20 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,23 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
return TTL;
};

var onlyEA = false;
/**
* @ngdoc method
* @name $compileProvider#compileOnlyEA
* @description
*
* If true compiles only directives at entities and attributes but does
* not compiles comment or class directives.
*
* Default value is false.
*
* @param {boolean} true to compile only entities and attributes
*/
this.compileOnlyEA = function(value) {
onlyEA = value;
};

this.$get = [
'$injector', '$interpolate', '$exceptionHandler', '$templateRequest', '$parse',
'$controller', '$rootScope', '$sce', '$animate', '$$sanitizeUri',
Expand Down Expand Up @@ -2026,6 +2043,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}

// use class as directive
if (onlyEA) break;
className = node.className;
if (isObject(className)) {
// Maybe SVGAnimatedString
Expand All @@ -2052,7 +2070,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
addTextInterpolateDirective(directives, node.nodeValue);
break;
case NODE_TYPE_COMMENT: /* Comment */
collectCommentDirectives();
if (onlyEA) break;
matchCommentDirectives();
break;
}

Expand Down
56 changes: 48 additions & 8 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3128,17 +3128,57 @@ describe('$compile', function() {
});
});

it('should observe interpolated attrs', inject(function($rootScope, $compile) {
$compile('<div some-attr="{{value}}" observer></div>')($rootScope);
describe('decorating with binding info', function() {

var collected;
beforeEach(module(function($compileProvider) {
collected = false;
$compileProvider.directive('testCollect', function() {
return {
restrict: 'ECMA',
link: function(scope, element) {
collected = true;
}
};
});
$compileProvider.compileOnlyEA(true);
}));

// should be async
expect(observeSpy).not.toHaveBeenCalled();
var $compile, $rootScope;
beforeEach(inject(function(_$compile_,_$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));

$rootScope.$apply(function() {
$rootScope.value = 'bound-value';
it('should not compile class directives', function() {
var html = 'Classes collected: <span class="ng-bind: \'Yes\';">No</span>';
element = $compile('<div>' + html + '</div>')($rootScope);
$rootScope.$apply();
expect(element.text()).toBe('Classes collected: No');
});
expect(observeSpy).toHaveBeenCalledOnceWith('bound-value');
}));

it('should not compile comment directives', function() {
var html = '<!-- directive: test-collect -->';
element = $compile('<div>' + html + '</div>')($rootScope);
expect(collected).toBe(false);
});

it('should not prevent to compile entity directives', function() {
element = $compile('<test-collect></test-collect>')($rootScope);
expect(collected).toBe(true);
});

it('should not prevent to compile attribute directives', function() {
element = $compile('<span test-collect></span>')($rootScope);
expect(collected).toBe(true);
});

it('should not prevent to compile interpolated expressions', function() {
element = $compile('<span>{{"text "+"interpolated"}}</span>')($rootScope);
$rootScope.$apply();
expect(element.text()).toBe('text interpolated');
});
});


it('should return a deregistration function while observing an attribute', inject(function($rootScope, $compile) {
Expand Down

0 comments on commit ab35cc9

Please sign in to comment.