@@ -7918,17 +7918,52 @@ describe('$compile', function() {
7918
7918
} ) ;
7919
7919
7920
7920
7921
- it ( 'should not compile the fallback content if transcluded content is provided' , function ( ) {
7922
- var contentsDidLink = false ;
7921
+ it ( 'should clear the fallback content from the element during compile and before linking' , function ( ) {
7922
+ module ( function ( ) {
7923
+ directive ( 'trans' , function ( ) {
7924
+ return {
7925
+ transclude : true ,
7926
+ template : '<div ng-transclude>fallback content</div>'
7927
+ } ;
7928
+ } ) ;
7929
+ } ) ;
7930
+ inject ( function ( log , $rootScope , $compile ) {
7931
+ element = jqLite ( '<div trans></div>' ) ;
7932
+ var linkfn = $compile ( element ) ;
7933
+ expect ( element . html ( ) ) . toEqual ( '<div ng-transclude=""></div>' ) ;
7934
+ linkfn ( $rootScope ) ;
7935
+ $rootScope . $apply ( ) ;
7936
+ expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude=""><span>fallback content</span></div>' ) ;
7937
+ } ) ;
7938
+ } ) ;
7939
+
7940
+
7941
+ it ( 'should allow cloning of the fallback via ngRepeat' , function ( ) {
7942
+ module ( function ( ) {
7943
+ directive ( 'trans' , function ( ) {
7944
+ return {
7945
+ transclude : true ,
7946
+ template : '<div ng-repeat="i in [0,1,2]"><div ng-transclude>{{i}}</div></div>'
7947
+ } ;
7948
+ } ) ;
7949
+ } ) ;
7950
+ inject ( function ( log , $rootScope , $compile ) {
7951
+ element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
7952
+ $rootScope . $apply ( ) ;
7953
+ expect ( element . text ( ) ) . toEqual ( '012' ) ;
7954
+ } ) ;
7955
+ } ) ;
7956
+
7957
+
7958
+ it ( 'should not link the fallback content if transcluded content is provided' , function ( ) {
7959
+ var linkSpy = jasmine . createSpy ( 'postlink' ) ;
7923
7960
7924
7961
module ( function ( ) {
7925
7962
directive ( 'inner' , function ( ) {
7926
7963
return {
7927
7964
restrict : 'E' ,
7928
7965
template : 'old stuff! ' ,
7929
- link : function ( ) {
7930
- contentsDidLink = true ;
7931
- }
7966
+ link : linkSpy
7932
7967
} ;
7933
7968
} ) ;
7934
7969
@@ -7943,21 +7978,19 @@ describe('$compile', function() {
7943
7978
element = $compile ( '<div trans>unicorn!</div>' ) ( $rootScope ) ;
7944
7979
$rootScope . $apply ( ) ;
7945
7980
expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude=""><span>unicorn!</span></div>' ) ;
7946
- expect ( contentsDidLink ) . toBe ( false ) ;
7981
+ expect ( linkSpy ) . not . toHaveBeenCalled ( ) ;
7947
7982
} ) ;
7948
7983
} ) ;
7949
7984
7950
7985
it ( 'should compile and link the fallback content if no transcluded content is provided' , function ( ) {
7951
- var contentsDidLink = false ;
7986
+ var linkSpy = jasmine . createSpy ( 'postlink' ) ;
7952
7987
7953
7988
module ( function ( ) {
7954
7989
directive ( 'inner' , function ( ) {
7955
7990
return {
7956
7991
restrict : 'E' ,
7957
7992
template : 'old stuff! ' ,
7958
- link : function ( ) {
7959
- contentsDidLink = true ;
7960
- }
7993
+ link : linkSpy
7961
7994
} ;
7962
7995
} ) ;
7963
7996
@@ -7972,7 +8005,50 @@ describe('$compile', function() {
7972
8005
element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
7973
8006
$rootScope . $apply ( ) ;
7974
8007
expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude=""><inner>old stuff! </inner></div>' ) ;
7975
- expect ( contentsDidLink ) . toBe ( true ) ;
8008
+ expect ( linkSpy ) . toHaveBeenCalled ( ) ;
8009
+ } ) ;
8010
+ } ) ;
8011
+
8012
+ it ( 'should compile and link the fallback content if an optional transclusion slot is not provided' , function ( ) {
8013
+ var linkSpy = jasmine . createSpy ( 'postlink' ) ;
8014
+
8015
+ module ( function ( ) {
8016
+ directive ( 'inner' , function ( ) {
8017
+ return {
8018
+ restrict : 'E' ,
8019
+ template : 'old stuff! ' ,
8020
+ link : linkSpy
8021
+ } ;
8022
+ } ) ;
8023
+
8024
+ directive ( 'trans' , function ( ) {
8025
+ return {
8026
+ transclude : { optionalSlot : '?optional' } ,
8027
+ template : '<div ng-transclude="optionalSlot"><inner></inner></div>'
8028
+ } ;
8029
+ } ) ;
8030
+ } ) ;
8031
+ inject ( function ( log , $rootScope , $compile ) {
8032
+ element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
8033
+ $rootScope . $apply ( ) ;
8034
+ expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude="optionalSlot"><inner>old stuff! </inner></div>' ) ;
8035
+ expect ( linkSpy ) . toHaveBeenCalled ( ) ;
8036
+ } ) ;
8037
+ } ) ;
8038
+
8039
+ it ( 'should cope if there is neither transcluded content nor fallback content' , function ( ) {
8040
+ module ( function ( ) {
8041
+ directive ( 'trans' , function ( ) {
8042
+ return {
8043
+ transclude : true ,
8044
+ template : '<div ng-transclude></div>'
8045
+ } ;
8046
+ } ) ;
8047
+ } ) ;
8048
+ inject ( function ( $rootScope , $compile ) {
8049
+ element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
8050
+ $rootScope . $apply ( ) ;
8051
+ expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude=""></div>' ) ;
7976
8052
} ) ;
7977
8053
} ) ;
7978
8054
@@ -9774,37 +9850,6 @@ describe('$compile', function() {
9774
9850
expect ( element . children ( ) . eq ( 2 ) . text ( ) ) . toEqual ( 'dorothy' ) ;
9775
9851
} ) ;
9776
9852
} ) ;
9777
-
9778
- it ( 'should not overwrite the contents of an `ng-transclude` element, if the matching optional slot is not filled' , function ( ) {
9779
- module ( function ( ) {
9780
- directive ( 'minionComponent' , function ( ) {
9781
- return {
9782
- restrict : 'E' ,
9783
- scope : { } ,
9784
- transclude : {
9785
- minionSlot : 'minion' ,
9786
- bossSlot : '?boss'
9787
- } ,
9788
- template :
9789
- '<div class="boss" ng-transclude="bossSlot">default boss content</div>' +
9790
- '<div class="minion" ng-transclude="minionSlot">default minion content</div>' +
9791
- '<div class="other" ng-transclude>default content</div>'
9792
- } ;
9793
- } ) ;
9794
- } ) ;
9795
- inject ( function ( $rootScope , $compile ) {
9796
- element = $compile (
9797
- '<minion-component>' +
9798
- '<minion>stuart</minion>' +
9799
- '<span>dorothy</span>' +
9800
- '<minion>kevin</minion>' +
9801
- '</minion-component>' ) ( $rootScope ) ;
9802
- $rootScope . $apply ( ) ;
9803
- expect ( element . children ( ) . eq ( 0 ) . text ( ) ) . toEqual ( 'default boss content' ) ;
9804
- expect ( element . children ( ) . eq ( 1 ) . text ( ) ) . toEqual ( 'stuartkevin' ) ;
9805
- expect ( element . children ( ) . eq ( 2 ) . text ( ) ) . toEqual ( 'dorothy' ) ;
9806
- } ) ;
9807
- } ) ;
9808
9853
} ) ;
9809
9854
9810
9855
0 commit comments