@@ -7970,17 +7970,52 @@ describe('$compile', function() {
7970
7970
} ) ;
7971
7971
7972
7972
7973
- it ( 'should not compile the fallback content if transcluded content is provided' , function ( ) {
7974
- var contentsDidLink = false ;
7973
+ it ( 'should clear the fallback content from the element during compile and before linking' , function ( ) {
7974
+ module ( function ( ) {
7975
+ directive ( 'trans' , function ( ) {
7976
+ return {
7977
+ transclude : true ,
7978
+ template : '<div ng-transclude>fallback content</div>'
7979
+ } ;
7980
+ } ) ;
7981
+ } ) ;
7982
+ inject ( function ( log , $rootScope , $compile ) {
7983
+ element = jqLite ( '<div trans></div>' ) ;
7984
+ var linkfn = $compile ( element ) ;
7985
+ expect ( element . html ( ) ) . toEqual ( '<div ng-transclude=""></div>' ) ;
7986
+ linkfn ( $rootScope ) ;
7987
+ $rootScope . $apply ( ) ;
7988
+ expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude="">fallback content</div>' ) ;
7989
+ } ) ;
7990
+ } ) ;
7991
+
7992
+
7993
+ it ( 'should allow cloning of the fallback via ngRepeat' , function ( ) {
7994
+ module ( function ( ) {
7995
+ directive ( 'trans' , function ( ) {
7996
+ return {
7997
+ transclude : true ,
7998
+ template : '<div ng-repeat="i in [0,1,2]"><div ng-transclude>{{i}}</div></div>'
7999
+ } ;
8000
+ } ) ;
8001
+ } ) ;
8002
+ inject ( function ( log , $rootScope , $compile ) {
8003
+ element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
8004
+ $rootScope . $apply ( ) ;
8005
+ expect ( element . text ( ) ) . toEqual ( '012' ) ;
8006
+ } ) ;
8007
+ } ) ;
8008
+
8009
+
8010
+ it ( 'should not link the fallback content if transcluded content is provided' , function ( ) {
8011
+ var linkSpy = jasmine . createSpy ( 'postlink' ) ;
7975
8012
7976
8013
module ( function ( ) {
7977
8014
directive ( 'inner' , function ( ) {
7978
8015
return {
7979
8016
restrict : 'E' ,
7980
8017
template : 'old stuff! ' ,
7981
- link : function ( ) {
7982
- contentsDidLink = true ;
7983
- }
8018
+ link : linkSpy
7984
8019
} ;
7985
8020
} ) ;
7986
8021
@@ -7995,21 +8030,19 @@ describe('$compile', function() {
7995
8030
element = $compile ( '<div trans>unicorn!</div>' ) ( $rootScope ) ;
7996
8031
$rootScope . $apply ( ) ;
7997
8032
expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude="">unicorn!</div>' ) ;
7998
- expect ( contentsDidLink ) . toBe ( false ) ;
8033
+ expect ( linkSpy ) . not . toHaveBeenCalled ( ) ;
7999
8034
} ) ;
8000
8035
} ) ;
8001
8036
8002
8037
it ( 'should compile and link the fallback content if no transcluded content is provided' , function ( ) {
8003
- var contentsDidLink = false ;
8038
+ var linkSpy = jasmine . createSpy ( 'postlink' ) ;
8004
8039
8005
8040
module ( function ( ) {
8006
8041
directive ( 'inner' , function ( ) {
8007
8042
return {
8008
8043
restrict : 'E' ,
8009
8044
template : 'old stuff! ' ,
8010
- link : function ( ) {
8011
- contentsDidLink = true ;
8012
- }
8045
+ link : linkSpy
8013
8046
} ;
8014
8047
} ) ;
8015
8048
@@ -8024,7 +8057,50 @@ describe('$compile', function() {
8024
8057
element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
8025
8058
$rootScope . $apply ( ) ;
8026
8059
expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude=""><inner>old stuff! </inner></div>' ) ;
8027
- expect ( contentsDidLink ) . toBe ( true ) ;
8060
+ expect ( linkSpy ) . toHaveBeenCalled ( ) ;
8061
+ } ) ;
8062
+ } ) ;
8063
+
8064
+ it ( 'should compile and link the fallback content if an optional transclusion slot is not provided' , function ( ) {
8065
+ var linkSpy = jasmine . createSpy ( 'postlink' ) ;
8066
+
8067
+ module ( function ( ) {
8068
+ directive ( 'inner' , function ( ) {
8069
+ return {
8070
+ restrict : 'E' ,
8071
+ template : 'old stuff! ' ,
8072
+ link : linkSpy
8073
+ } ;
8074
+ } ) ;
8075
+
8076
+ directive ( 'trans' , function ( ) {
8077
+ return {
8078
+ transclude : { optionalSlot : '?optional' } ,
8079
+ template : '<div ng-transclude="optionalSlot"><inner></inner></div>'
8080
+ } ;
8081
+ } ) ;
8082
+ } ) ;
8083
+ inject ( function ( log , $rootScope , $compile ) {
8084
+ element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
8085
+ $rootScope . $apply ( ) ;
8086
+ expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude="optionalSlot"><inner>old stuff! </inner></div>' ) ;
8087
+ expect ( linkSpy ) . toHaveBeenCalled ( ) ;
8088
+ } ) ;
8089
+ } ) ;
8090
+
8091
+ it ( 'should cope if there is neither transcluded content nor fallback content' , function ( ) {
8092
+ module ( function ( ) {
8093
+ directive ( 'trans' , function ( ) {
8094
+ return {
8095
+ transclude : true ,
8096
+ template : '<div ng-transclude></div>'
8097
+ } ;
8098
+ } ) ;
8099
+ } ) ;
8100
+ inject ( function ( $rootScope , $compile ) {
8101
+ element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
8102
+ $rootScope . $apply ( ) ;
8103
+ expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude=""></div>' ) ;
8028
8104
} ) ;
8029
8105
} ) ;
8030
8106
@@ -9824,37 +9900,6 @@ describe('$compile', function() {
9824
9900
expect ( element . children ( ) . eq ( 2 ) . text ( ) ) . toEqual ( 'dorothy' ) ;
9825
9901
} ) ;
9826
9902
} ) ;
9827
-
9828
- it ( 'should not overwrite the contents of an `ng-transclude` element, if the matching optional slot is not filled' , function ( ) {
9829
- module ( function ( ) {
9830
- directive ( 'minionComponent' , function ( ) {
9831
- return {
9832
- restrict : 'E' ,
9833
- scope : { } ,
9834
- transclude : {
9835
- minionSlot : 'minion' ,
9836
- bossSlot : '?boss'
9837
- } ,
9838
- template :
9839
- '<div class="boss" ng-transclude="bossSlot">default boss content</div>' +
9840
- '<div class="minion" ng-transclude="minionSlot">default minion content</div>' +
9841
- '<div class="other" ng-transclude>default content</div>'
9842
- } ;
9843
- } ) ;
9844
- } ) ;
9845
- inject ( function ( $rootScope , $compile ) {
9846
- element = $compile (
9847
- '<minion-component>' +
9848
- '<minion>stuart</minion>' +
9849
- '<span>dorothy</span>' +
9850
- '<minion>kevin</minion>' +
9851
- '</minion-component>' ) ( $rootScope ) ;
9852
- $rootScope . $apply ( ) ;
9853
- expect ( element . children ( ) . eq ( 0 ) . text ( ) ) . toEqual ( 'default boss content' ) ;
9854
- expect ( element . children ( ) . eq ( 1 ) . text ( ) ) . toEqual ( 'stuartkevin' ) ;
9855
- expect ( element . children ( ) . eq ( 2 ) . text ( ) ) . toEqual ( 'dorothy' ) ;
9856
- } ) ;
9857
- } ) ;
9858
9903
} ) ;
9859
9904
9860
9905
0 commit comments