@@ -7872,4 +7872,125 @@ describe('$compile', function() {
7872
7872
expect ( element . hasClass ( 'fire' ) ) . toBe ( true ) ;
7873
7873
} ) ) ;
7874
7874
} ) ;
7875
+
7876
+ describe ( 'element replacement' , function ( ) {
7877
+ it ( 'should broadcast $destroy only on removed elements, not replaced' , function ( ) {
7878
+ var linkCalls = [ ] ;
7879
+ var destroyCalls = [ ] ;
7880
+
7881
+ module ( function ( $compileProvider ) {
7882
+ $compileProvider . directive ( 'replace' , function ( ) {
7883
+ return {
7884
+ multiElement : true ,
7885
+ replace : true ,
7886
+ templateUrl : 'template123'
7887
+ } ;
7888
+ } ) ;
7889
+
7890
+ $compileProvider . directive ( 'foo' , function ( ) {
7891
+ return {
7892
+ priority : 1 , //before the replace directive
7893
+ link : function ( $scope , $element , $attrs ) {
7894
+ linkCalls . push ( $attrs . foo ) ;
7895
+ $element . on ( '$destroy' , function ( ) {
7896
+ destroyCalls . push ( $attrs . foo ) ;
7897
+ } ) ;
7898
+ }
7899
+ } ;
7900
+ } ) ;
7901
+ } ) ;
7902
+
7903
+ inject ( function ( $compile , $templateCache , $rootScope ) {
7904
+ $templateCache . put ( 'template123' , '<div></div>' ) ;
7905
+
7906
+ $compile (
7907
+ '<div replace-start foo="1"><span foo="1.1"></span></div>' +
7908
+ '<div foo="2"><span foo="2.1"></span></div>' +
7909
+ '<div replace-end foo="3"><span foo="3.1"></span></div>'
7910
+ ) ( $rootScope ) ;
7911
+
7912
+ expect ( linkCalls ) . toEqual ( [ '2' , '3' ] ) ;
7913
+ expect ( destroyCalls ) . toEqual ( [ ] ) ;
7914
+ $rootScope . $apply ( ) ;
7915
+ expect ( linkCalls ) . toEqual ( [ '2' , '3' , '1' ] ) ;
7916
+ expect ( destroyCalls ) . toEqual ( [ '2' , '3' ] ) ;
7917
+ } ) ;
7918
+ } ) ;
7919
+
7920
+ function getAll ( $root ) {
7921
+ return [ $root [ 0 ] ] . concat ( $root [ 0 ] . querySelectorAll ? sliceArgs ( $root [ 0 ] . querySelectorAll ( '*' ) ) : [ ] ) ;
7922
+ }
7923
+
7924
+ function testCompileLinkDataCleanup ( template ) {
7925
+ inject ( function ( $compile , $rootScope ) {
7926
+ var toCompile = jqLite ( template ) ;
7927
+
7928
+ var preCompiledChildren = getAll ( toCompile ) ;
7929
+ forEach ( preCompiledChildren , function ( element , i ) {
7930
+ jqLite . data ( element , 'foo' , 'template#' + i ) ;
7931
+ } ) ;
7932
+
7933
+ var linkedElements = $compile ( toCompile ) ( $rootScope ) ;
7934
+ $rootScope . $apply ( ) ;
7935
+ linkedElements . remove ( ) ;
7936
+
7937
+ forEach ( preCompiledChildren , function ( element , i ) {
7938
+ expect ( jqLite . hasData ( element ) ) . toBe ( false , "template#" + i ) ;
7939
+ } ) ;
7940
+ forEach ( getAll ( linkedElements ) , function ( element , i ) {
7941
+ expect ( jqLite . hasData ( element ) ) . toBe ( false , "linked#" + i ) ;
7942
+ } ) ;
7943
+ } ) ;
7944
+ }
7945
+ it ( 'should clean data of element-transcluded link-cloned elements' , function ( ) {
7946
+ testCompileLinkDataCleanup ( '<div><div ng-repeat-start="i in [1,2]"><span></span></div><div ng-repeat-end></div></div>' ) ;
7947
+ } ) ;
7948
+ it ( 'should clean data of element-transcluded elements' , function ( ) {
7949
+ testCompileLinkDataCleanup ( '<div ng-if-start="false"><span><span/></div><span></span><div ng-if-end><span></span></div>' ) ;
7950
+ } ) ;
7951
+
7952
+ function testReplaceElementCleanup ( replace , transclude , asyncTemplate ) {
7953
+ var template = '<div></div>' ;
7954
+ module ( function ( $compileProvider ) {
7955
+ $compileProvider . directive ( 'theDir' , function ( ) {
7956
+ return {
7957
+ multiElement : true ,
7958
+ replace : replace ,
7959
+ transclude : transclude ,
7960
+ template : asyncTemplate ? undefined : template ,
7961
+ templateUrl : asyncTemplate ? 'the-dir-template-url' : undefined
7962
+ } ;
7963
+ } ) ;
7964
+ } ) ;
7965
+ inject ( function ( $templateCache , $compile , $rootScope ) {
7966
+ $templateCache . put ( 'the-dir-template-url' , template ) ;
7967
+
7968
+ testCompileLinkDataCleanup (
7969
+ '<div>' +
7970
+ '<div the-dir-start><span></span></div>' +
7971
+ '<div><span></span><span></span></div>' +
7972
+ '<div the-dir-end><span></span></div>' +
7973
+ '</div>'
7974
+ ) ;
7975
+ } ) ;
7976
+ }
7977
+ it ( 'should clean data of elements removed for directive template' , function ( ) {
7978
+ testReplaceElementCleanup ( false , false , false ) ;
7979
+ } ) ;
7980
+ it ( 'should clean data of elements removed for directive templateUrl' , function ( ) {
7981
+ testReplaceElementCleanup ( false , false , true ) ;
7982
+ } ) ;
7983
+ it ( 'should clean data of elements transcluded into directive template' , function ( ) {
7984
+ testReplaceElementCleanup ( false , true , false ) ;
7985
+ } ) ;
7986
+ it ( 'should clean data of elements transcluded into directive templateUrl' , function ( ) {
7987
+ testReplaceElementCleanup ( false , true , true ) ;
7988
+ } ) ;
7989
+ it ( 'should clean data of elements replaced with directive template' , function ( ) {
7990
+ testReplaceElementCleanup ( true , false , false ) ;
7991
+ } ) ;
7992
+ it ( 'should clean data of elements replaced with directive templateUrl' , function ( ) {
7993
+ testReplaceElementCleanup ( true , false , true ) ;
7994
+ } ) ;
7995
+ } ) ;
7875
7996
} ) ;
0 commit comments