From ab35cc9ab50c0630a04d38f8173a4f9256f354c0 Mon Sep 17 00:00:00 2001 From: David Rodenas Pico Date: Thu, 30 Jun 2016 11:15:42 +0200 Subject: [PATCH] perf($compile): add a flag to compile only EA directives --- src/ng/compile.js | 21 +++++++++++++++- test/ng/compileSpec.js | 56 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index ab68485a09c9..5fdde2de2699 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -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', @@ -2026,6 +2043,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { } // use class as directive + if (onlyEA) break; className = node.className; if (isObject(className)) { // Maybe SVGAnimatedString @@ -2052,7 +2070,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { addTextInterpolateDirective(directives, node.nodeValue); break; case NODE_TYPE_COMMENT: /* Comment */ - collectCommentDirectives(); + if (onlyEA) break; + matchCommentDirectives(); break; } diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 6acc2ec6f7a6..2dd29988e83f 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -3128,17 +3128,57 @@ describe('$compile', function() { }); }); - it('should observe interpolated attrs', inject(function($rootScope, $compile) { - $compile('
')($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: No'; + element = $compile('
' + html + '
')($rootScope); + $rootScope.$apply(); + expect(element.text()).toBe('Classes collected: No'); }); - expect(observeSpy).toHaveBeenCalledOnceWith('bound-value'); - })); + + it('should not compile comment directives', function() { + var html = ''; + element = $compile('
' + html + '
')($rootScope); + expect(collected).toBe(false); + }); + + it('should not prevent to compile entity directives', function() { + element = $compile('')($rootScope); + expect(collected).toBe(true); + }); + + it('should not prevent to compile attribute directives', function() { + element = $compile('')($rootScope); + expect(collected).toBe(true); + }); + + it('should not prevent to compile interpolated expressions', function() { + element = $compile('{{"text "+"interpolated"}}')($rootScope); + $rootScope.$apply(); + expect(element.text()).toBe('text interpolated'); + }); + }); it('should return a deregistration function while observing an attribute', inject(function($rootScope, $compile) {