@@ -1201,41 +1201,50 @@ describe('$compile', function() {
1201
1201
} ) ;
1202
1202
} ) ;
1203
1203
1204
- it ( 'should fail if replacing and template doesn\'t have a single root element' , function ( ) {
1205
- module ( function ( ) {
1206
- directive ( 'noRootElem' , function ( ) {
1207
- return {
1208
- replace : true ,
1209
- template : 'dada'
1210
- } ;
1211
- } ) ;
1212
- directive ( 'multiRootElem' , function ( ) {
1213
- return {
1214
- replace : true ,
1215
- template : '<div></div><div></div>'
1216
- } ;
1217
- } ) ;
1218
- directive ( 'singleRootWithWhiteSpace' , function ( ) {
1204
+ describe ( 'replace and not exactly one root element' , function ( ) {
1205
+ var templateVar ;
1206
+
1207
+ beforeEach ( module ( function ( ) {
1208
+ directive ( 'template' , function ( ) {
1219
1209
return {
1220
1210
replace : true ,
1221
- template : ' <div></div> \n'
1211
+ template : function ( ) {
1212
+ return templateVar ;
1213
+ }
1222
1214
} ;
1223
1215
} ) ;
1224
- } ) ;
1216
+ } ) ) ;
1225
1217
1226
- inject ( function ( $compile ) {
1227
- expect ( function ( ) {
1228
- $compile ( '<p no-root-elem></p>' ) ;
1229
- } ) . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'noRootElem\' must have exactly one root element. ' ) ;
1218
+ they ( 'should throw if: $prop' ,
1219
+ {
1220
+ 'no root element' : 'dada' ,
1221
+ 'multiple root elements' : '<div></div><div></div>'
1222
+ } , function ( $prop ) {
1230
1223
1231
- expect ( function ( ) {
1232
- $compile ( '<p multi-root-elem></p>' ) ;
1233
- } ) . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'multiRootElem\' must have exactly one root element. ' ) ;
1224
+ inject ( function ( $compile , $templateCache , $rootScope ) {
1225
+ templateVar = $prop ;
1226
+ expect ( function ( ) {
1227
+ $compile ( '<p template></p>' ) ;
1228
+ } ) . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'template\' must have exactly one root element.' ) ;
1229
+ } ) ;
1230
+ } ) ;
1234
1231
1235
- // ws is ok
1236
- expect ( function ( ) {
1237
- $compile ( '<p single-root-with-white-space></p>' ) ;
1238
- } ) . not . toThrow ( ) ;
1232
+ they ( 'should not throw if the root element is accompanied by: $prop' ,
1233
+ {
1234
+ 'whitespace' : ' <div>Hello World!</div> \n' ,
1235
+ 'comments' : '<!-- oh hi --><div>Hello World!</div> \n' ,
1236
+ 'comments + whitespace' : ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
1237
+ } , function ( $prop ) {
1238
+
1239
+ inject ( function ( $compile , $templateCache , $rootScope ) {
1240
+ templateVar = $prop ;
1241
+ var element ;
1242
+ expect ( function ( ) {
1243
+ element = $compile ( '<p template></p>' ) ( $rootScope ) ;
1244
+ } ) . not . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'template\' must have exactly one root element.' ) ;
1245
+ expect ( element . length ) . toBe ( 1 ) ;
1246
+ expect ( element . text ( ) ) . toBe ( 'Hello World!' ) ;
1247
+ } ) ;
1239
1248
} ) ;
1240
1249
} ) ;
1241
1250
@@ -1348,38 +1357,6 @@ describe('$compile', function() {
1348
1357
} ) ;
1349
1358
}
1350
1359
1351
- it ( 'should ignore comment nodes when replacing with a template' , function ( ) {
1352
- module ( function ( ) {
1353
- directive ( 'replaceWithComments' , valueFn ( {
1354
- replace : true ,
1355
- template : '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
1356
- } ) ) ;
1357
- } ) ;
1358
- inject ( function ( $compile , $rootScope ) {
1359
- expect ( function ( ) {
1360
- element = $compile ( '<div><div replace-with-comments></div></div>' ) ( $rootScope ) ;
1361
- } ) . not . toThrow ( ) ;
1362
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
1363
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
1364
- } ) ;
1365
- } ) ;
1366
-
1367
- it ( 'should ignore whitespace betwee comment and root node when replacing with a template' , function ( ) {
1368
- module ( function ( ) {
1369
- directive ( 'replaceWithWhitespace' , valueFn ( {
1370
- replace : true ,
1371
- template : '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
1372
- } ) ) ;
1373
- } ) ;
1374
- inject ( function ( $compile , $rootScope ) {
1375
- expect ( function ( ) {
1376
- element = $compile ( '<div><div replace-with-whitespace></div></div>' ) ( $rootScope ) ;
1377
- } ) . not . toThrow ( ) ;
1378
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
1379
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
1380
- } ) ;
1381
- } ) ;
1382
-
1383
1360
it ( 'should keep prototype properties on directive' , function ( ) {
1384
1361
module ( function ( ) {
1385
1362
function DirectiveClass ( ) {
@@ -2075,55 +2052,53 @@ describe('$compile', function() {
2075
2052
}
2076
2053
) ) ;
2077
2054
2055
+ describe ( 'replace and not exactly one root element' , function ( ) {
2078
2056
2079
- it ( 'should fail if replacing and template doesn\'t have a single root element' , function ( ) {
2080
- module ( function ( $exceptionHandlerProvider ) {
2081
- $exceptionHandlerProvider . mode ( 'log' ) ;
2082
-
2057
+ beforeEach ( module ( function ( ) {
2083
2058
directive ( 'template' , function ( ) {
2084
2059
return {
2085
2060
replace : true ,
2086
2061
templateUrl : 'template.html'
2087
2062
} ;
2088
2063
} ) ;
2089
- } ) ;
2090
-
2091
- inject ( function ( $compile , $templateCache , $rootScope , $exceptionHandler ) {
2092
- // no root element
2093
- $templateCache . put ( 'template.html' , 'dada' ) ;
2094
- $compile ( '<p template></p>' ) ;
2095
- $rootScope . $digest ( ) ;
2096
- expect ( $exceptionHandler . errors . pop ( ) . message ) .
2097
- toMatch ( / \[ \$ c o m p i l e : t p l r t \] T e m p l a t e f o r d i r e c t i v e ' t e m p l a t e ' m u s t h a v e e x a c t l y o n e r o o t e l e m e n t \. t e m p l a t e \. h t m l / ) ;
2064
+ } ) ) ;
2098
2065
2099
- // multi root
2100
- $templateCache . put ( 'template.html' , '<div></div><div></div>' ) ;
2101
- $compile ( '<p template></p>' ) ;
2102
- $rootScope . $digest ( ) ;
2103
- expect ( $exceptionHandler . errors . pop ( ) . message ) .
2104
- toMatch ( / \[ \$ c o m p i l e : t p l r t \] T e m p l a t e f o r d i r e c t i v e ' t e m p l a t e ' m u s t h a v e e x a c t l y o n e r o o t e l e m e n t \. t e m p l a t e \. h t m l / ) ;
2066
+ they ( 'should throw if: $prop' ,
2067
+ {
2068
+ 'no root element' : 'dada' ,
2069
+ 'multiple root elements' : '<div></div><div></div>'
2070
+ } , function ( $prop ) {
2105
2071
2106
- // ws is ok
2107
- $templateCache . put ( 'template.html' , ' <div></div> \n' ) ;
2108
- $compile ( '<p template></p>' ) ;
2109
- $rootScope . $apply ( ) ;
2110
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2072
+ inject ( function ( $compile , $templateCache , $rootScope ) {
2073
+ // no root element
2074
+ $templateCache . put ( 'template.html' , $prop ) ;
2075
+ $compile ( '<p template></p>' ) ( $rootScope ) ;
2076
+ expect ( function ( ) {
2077
+ $rootScope . $digest ( ) ;
2078
+ } ) . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'template\' must have exactly one root element. template.html' ) ;
2079
+ } ) ;
2080
+ } ) ;
2111
2081
2112
- // comments are ok
2113
- $templateCache . put ( 'template.html' , '<!-- oh hi --><div></div> \n' ) ;
2114
- $compile ( '<p template></p>' ) ;
2115
- $rootScope . $apply ( ) ;
2116
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2082
+ they ( 'should not throw if the root element is accompanied by: $prop' ,
2083
+ {
2084
+ 'whitespace' : ' <div>Hello World!</div> \n' ,
2085
+ 'comments' : '<!-- oh hi --><div>Hello World!</div> \n' ,
2086
+ 'comments + whitespace' : ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
2087
+ } , function ( $prop ) {
2117
2088
2118
- // white space around comments is ok
2119
- $templateCache . put ( 'template.html' , ' <!-- oh hi --> <div></div> <!-- oh hi -->\n' ) ;
2120
- $compile ( '<p template></p>' ) ;
2121
- $rootScope . $apply ( ) ;
2122
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2089
+ inject ( function ( $compile , $templateCache , $rootScope ) {
2090
+ // no root element
2091
+ $templateCache . put ( 'template.html' , $prop ) ;
2092
+ element = $compile ( '<p template></p>' ) ( $rootScope ) ;
2093
+ expect ( function ( ) {
2094
+ $rootScope . $digest ( ) ;
2095
+ } ) . not . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'template\' must have exactly one root element. template.html' ) ;
2096
+ expect ( element . length ) . toBe ( 1 ) ;
2097
+ expect ( element . text ( ) ) . toBe ( 'Hello World!' ) ;
2098
+ } ) ;
2123
2099
} ) ;
2124
2100
} ) ;
2125
2101
2126
-
2127
2102
it ( 'should resume delayed compilation without duplicates when in a repeater' , function ( ) {
2128
2103
// this is a test for a regression
2129
2104
// scope creation, isolate watcher setup, controller instantiation, etc should happen
@@ -2312,45 +2287,6 @@ describe('$compile', function() {
2312
2287
} ) ;
2313
2288
}
2314
2289
2315
- it ( 'should ignore comment nodes when replacing with a templateUrl' , function ( ) {
2316
- module ( function ( ) {
2317
- directive ( 'replaceWithComments' , valueFn ( {
2318
- replace : true ,
2319
- templateUrl : 'templateWithComments.html'
2320
- } ) ) ;
2321
- } ) ;
2322
- inject ( function ( $compile , $rootScope , $httpBackend ) {
2323
- $httpBackend . whenGET ( 'templateWithComments.html' ) .
2324
- respond ( '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->' ) ;
2325
- expect ( function ( ) {
2326
- element = $compile ( '<div><div replace-with-comments></div></div>' ) ( $rootScope ) ;
2327
- } ) . not . toThrow ( ) ;
2328
- $httpBackend . flush ( ) ;
2329
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
2330
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
2331
- } ) ;
2332
- } ) ;
2333
-
2334
- it ( 'should ignore whitespace between comment and root node when replacing with a templateUrl' , function ( ) {
2335
- module ( function ( ) {
2336
- directive ( 'replaceWithWhitespace' , valueFn ( {
2337
- replace : true ,
2338
- templateUrl : 'templateWithWhitespace.html'
2339
- } ) ) ;
2340
- } ) ;
2341
- inject ( function ( $compile , $rootScope , $httpBackend ) {
2342
- $httpBackend . whenGET ( 'templateWithWhitespace.html' ) .
2343
- respond ( '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->' ) ;
2344
- expect ( function ( ) {
2345
- element = $compile ( '<div><div replace-with-whitespace></div></div>' ) ( $rootScope ) ;
2346
- } ) . not . toThrow ( ) ;
2347
- $httpBackend . flush ( ) ;
2348
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
2349
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
2350
- } ) ;
2351
- } ) ;
2352
-
2353
-
2354
2290
it ( 'should keep prototype properties on sync version of async directive' , function ( ) {
2355
2291
module ( function ( ) {
2356
2292
function DirectiveClass ( ) {
0 commit comments