@@ -7815,4 +7815,125 @@ describe('$compile', function() {
7815
7815
expect ( element . hasClass ( 'fire' ) ) . toBe ( true ) ;
7816
7816
} ) ) ;
7817
7817
} ) ;
7818
+
7819
+ describe ( 'element replacement' , function ( ) {
7820
+ it ( 'should broadcast $destroy only on removed elements, not replaced' , function ( ) {
7821
+ var linkCalls = [ ] ;
7822
+ var destroyCalls = [ ] ;
7823
+
7824
+ module ( function ( $compileProvider ) {
7825
+ $compileProvider . directive ( 'replace' , function ( ) {
7826
+ return {
7827
+ multiElement : true ,
7828
+ replace : true ,
7829
+ templateUrl : 'template123'
7830
+ } ;
7831
+ } ) ;
7832
+
7833
+ $compileProvider . directive ( 'foo' , function ( ) {
7834
+ return {
7835
+ priority : 1 , //before the replace directive
7836
+ link : function ( $scope , $element , $attrs ) {
7837
+ linkCalls . push ( $attrs . foo ) ;
7838
+ $element . on ( '$destroy' , function ( ) {
7839
+ destroyCalls . push ( $attrs . foo ) ;
7840
+ } ) ;
7841
+ }
7842
+ } ;
7843
+ } ) ;
7844
+ } ) ;
7845
+
7846
+ inject ( function ( $compile , $templateCache , $rootScope ) {
7847
+ $templateCache . put ( 'template123' , '<div></div>' ) ;
7848
+
7849
+ $compile (
7850
+ '<div replace-start foo="1"><span foo="1.1"></span></div>' +
7851
+ '<div foo="2"><span foo="2.1"></span></div>' +
7852
+ '<div replace-end foo="3"><span foo="3.1"></span></div>'
7853
+ ) ( $rootScope ) ;
7854
+
7855
+ expect ( linkCalls ) . toEqual ( [ '2' , '3' ] ) ;
7856
+ expect ( destroyCalls ) . toEqual ( [ ] ) ;
7857
+ $rootScope . $apply ( ) ;
7858
+ expect ( linkCalls ) . toEqual ( [ '2' , '3' , '1' ] ) ;
7859
+ expect ( destroyCalls ) . toEqual ( [ '2' , '3' ] ) ;
7860
+ } ) ;
7861
+ } ) ;
7862
+
7863
+ function getAll ( $root ) {
7864
+ return [ $root [ 0 ] ] . concat ( $root [ 0 ] . querySelectorAll ? sliceArgs ( $root [ 0 ] . querySelectorAll ( '*' ) ) : [ ] ) ;
7865
+ }
7866
+
7867
+ function testCompileLinkDataCleanup ( template ) {
7868
+ inject ( function ( $compile , $rootScope ) {
7869
+ var toCompile = jqLite ( template ) ;
7870
+
7871
+ var preCompiledChildren = getAll ( toCompile )
7872
+ forEach ( preCompiledChildren , function ( element , i ) {
7873
+ jqLite . data ( element , 'foo' , 'template#' + i ) ;
7874
+ } ) ;
7875
+
7876
+ var linkedElements = $compile ( toCompile ) ( $rootScope ) ;
7877
+ $rootScope . $apply ( ) ;
7878
+ linkedElements . remove ( ) ;
7879
+
7880
+ forEach ( preCompiledChildren , function ( element , i ) {
7881
+ expect ( jqLite . hasData ( element ) ) . toBe ( false , "template#" + i ) ;
7882
+ } ) ;
7883
+ forEach ( getAll ( linkedElements ) , function ( element , i ) {
7884
+ expect ( jqLite . hasData ( element ) ) . toBe ( false , "linked#" + i ) ;
7885
+ } ) ;
7886
+ } ) ;
7887
+ }
7888
+ it ( 'should clean data of element-transcluded link-cloned elements' , function ( ) {
7889
+ testCompileLinkDataCleanup ( '<div><div ng-repeat-start="i in [1,2]"><span></span></div><div ng-repeat-end></div></div>' ) ;
7890
+ } ) ;
7891
+ it ( 'should clean data of element-transcluded elements' , function ( ) {
7892
+ testCompileLinkDataCleanup ( '<div ng-if-start="false"><span><span/></div><span></span><div ng-if-end><span></span></div>' ) ;
7893
+ } ) ;
7894
+
7895
+ function testReplaceElementCleanup ( replace , transclude , asyncTemplate ) {
7896
+ var template = '<div></div>' ;
7897
+ module ( function ( $compileProvider ) {
7898
+ $compileProvider . directive ( 'theDir' , function ( ) {
7899
+ return {
7900
+ multiElement : true ,
7901
+ replace : replace ,
7902
+ transclude : transclude ,
7903
+ template : asyncTemplate ? undefined : template ,
7904
+ templateUrl : asyncTemplate ? 'the-dir-template-url' : undefined
7905
+ } ;
7906
+ } ) ;
7907
+ } ) ;
7908
+ inject ( function ( $templateCache , $compile , $rootScope ) {
7909
+ $templateCache . put ( 'the-dir-template-url' , template ) ;
7910
+
7911
+ testCompileLinkDataCleanup (
7912
+ '<div>' +
7913
+ '<div the-dir-start><span></span></div>' +
7914
+ '<div><span></span><span></span></div>' +
7915
+ '<div the-dir-end><span></span></div>' +
7916
+ '</div>'
7917
+ ) ;
7918
+ } ) ;
7919
+ }
7920
+ it ( 'should clean data of elements removed for directive template' , function ( ) {
7921
+ testReplaceElementCleanup ( false , false , false ) ;
7922
+ } ) ;
7923
+ it ( 'should clean data of elements removed for directive templateUrl' , function ( ) {
7924
+ testReplaceElementCleanup ( false , false , true ) ;
7925
+ } ) ;
7926
+ it ( 'should clean data of elements transcluded into directive template' , function ( ) {
7927
+ testReplaceElementCleanup ( false , true , false ) ;
7928
+ } ) ;
7929
+ it ( 'should clean data of elements transcluded into directive templateUrl' , function ( ) {
7930
+ testReplaceElementCleanup ( false , true , true ) ;
7931
+ } ) ;
7932
+ it ( 'should clean data of elements replaced with directive template' , function ( ) {
7933
+ testReplaceElementCleanup ( true , false , false ) ;
7934
+ } ) ;
7935
+ it ( 'should clean data of elements replaced with directive templateUrl' , function ( ) {
7936
+ testReplaceElementCleanup ( true , false , true ) ;
7937
+ } ) ;
7938
+ } ) ;
7818
7939
} ) ;
0 commit comments