@@ -7970,17 +7970,52 @@ describe('$compile', function() {
79707970 } ) ;
79717971
79727972
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' ) ;
79758012
79768013 module ( function ( ) {
79778014 directive ( 'inner' , function ( ) {
79788015 return {
79798016 restrict : 'E' ,
79808017 template : 'old stuff! ' ,
7981- link : function ( ) {
7982- contentsDidLink = true ;
7983- }
8018+ link : linkSpy
79848019 } ;
79858020 } ) ;
79868021
@@ -7995,21 +8030,19 @@ describe('$compile', function() {
79958030 element = $compile ( '<div trans>unicorn!</div>' ) ( $rootScope ) ;
79968031 $rootScope . $apply ( ) ;
79978032 expect ( sortedHtml ( element . html ( ) ) ) . toEqual ( '<div ng-transclude="">unicorn!</div>' ) ;
7998- expect ( contentsDidLink ) . toBe ( false ) ;
8033+ expect ( linkSpy ) . not . toHaveBeenCalled ( ) ;
79998034 } ) ;
80008035 } ) ;
80018036
80028037 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' ) ;
80048039
80058040 module ( function ( ) {
80068041 directive ( 'inner' , function ( ) {
80078042 return {
80088043 restrict : 'E' ,
80098044 template : 'old stuff! ' ,
8010- link : function ( ) {
8011- contentsDidLink = true ;
8012- }
8045+ link : linkSpy
80138046 } ;
80148047 } ) ;
80158048
@@ -8024,7 +8057,50 @@ describe('$compile', function() {
80248057 element = $compile ( '<div trans></div>' ) ( $rootScope ) ;
80258058 $rootScope . $apply ( ) ;
80268059 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>' ) ;
80288104 } ) ;
80298105 } ) ;
80308106
@@ -9824,37 +9900,6 @@ describe('$compile', function() {
98249900 expect ( element . children ( ) . eq ( 2 ) . text ( ) ) . toEqual ( 'dorothy' ) ;
98259901 } ) ;
98269902 } ) ;
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- } ) ;
98589903 } ) ;
98599904
98609905
0 commit comments