This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
function classDirective ( name , selector ) {
4
+ var staticMapClassRegEx = / ^ \s * ( : : ) ? \s * \{ / ;
4
5
name = 'ngClass' + name ;
5
6
return [ '$animate' , function ( $animate ) {
6
7
return {
7
8
restrict : 'AC' ,
8
9
link : function ( scope , element , attr ) {
9
10
var oldVal ;
10
11
11
- scope . $watch ( attr [ name ] , ngClassWatchAction , true ) ;
12
+ // shortcut: if it is clearly a map of classes do not copy values, they are supposed to be boolean (truly/falsy)
13
+ scope [ staticMapClassRegEx . test ( attr [ name ] ) ? '$watchCollection' : '$watch' ] ( attr [ name ] , ngClassWatchAction , true ) ;
12
14
13
15
attr . $observe ( 'class' , function ( value ) {
14
16
ngClassWatchAction ( scope . $eval ( attr [ name ] ) ) ;
Original file line number Diff line number Diff line change @@ -409,6 +409,37 @@ describe('ngClass', function() {
409
409
expect ( e2 . hasClass ( 'even' ) ) . toBeTruthy ( ) ;
410
410
expect ( e2 . hasClass ( 'odd' ) ) . toBeFalsy ( ) ;
411
411
} ) ) ;
412
+
413
+ describe ( 'large objects' , function ( ) {
414
+
415
+ var verylargeobject , getProp ;
416
+ beforeEach ( function ( ) {
417
+ getProp = jasmine . createSpy ( 'getProp' ) ;
418
+ verylargeobject = { } ;
419
+ Object . defineProperty ( verylargeobject , 'prop' , {
420
+ get : getProp ,
421
+ enumerable : true
422
+ } ) ;
423
+ } ) ;
424
+
425
+ it ( 'should not copy large objects via hard map of classes' , inject ( function ( $rootScope , $compile ) {
426
+ element = $compile ( '<div ng-class="{foo: verylargeobject}"></div>' ) ( $rootScope ) ;
427
+ $rootScope . verylargeobject = verylargeobject ;
428
+ $rootScope . $digest ( ) ;
429
+
430
+ expect ( getProp ) . not . toHaveBeenCalled ( ) ;
431
+ } ) ) ;
432
+
433
+ it ( 'should not copy large objects via hard map of classes in one-time binding' , inject ( function ( $rootScope , $compile ) {
434
+ element = $compile ( '<div ng-class="::{foo: verylargeobject}"></div>' ) ( $rootScope ) ;
435
+ $rootScope . verylargeobject = verylargeobject ;
436
+ $rootScope . $digest ( ) ;
437
+
438
+ expect ( getProp ) . not . toHaveBeenCalled ( ) ;
439
+ } ) ) ;
440
+ } ) ;
441
+
442
+
412
443
} ) ;
413
444
414
445
describe ( 'ngClass animations' , function ( ) {
You can’t perform that action at this time.
0 commit comments