@@ -1201,41 +1201,52 @@ 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 ( directiveTemplate ) {
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 ) {
1225
+ templateVar = directiveTemplate ;
1226
+ expect ( function ( ) {
1227
+ $compile ( '<p template></p>' ) ;
1228
+ } ) . toThrowMinErr ( '$compile' , 'tplrt' ,
1229
+ 'Template for directive \'template\' must have exactly one root element.'
1230
+ ) ;
1231
+ } ) ;
1232
+ } ) ;
1234
1233
1235
- // ws is ok
1236
- expect ( function ( ) {
1237
- $compile ( '<p single-root-with-white-space></p>' ) ;
1238
- } ) . not . toThrow ( ) ;
1234
+ they ( 'should not throw if the root element is accompanied by: $prop' ,
1235
+ {
1236
+ 'whitespace' : ' <div>Hello World!</div> \n' ,
1237
+ 'comments' : '<!-- oh hi --><div>Hello World!</div> \n' ,
1238
+ 'comments + whitespace' : ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
1239
+ } , function ( directiveTemplate ) {
1240
+
1241
+ inject ( function ( $compile , $rootScope ) {
1242
+ templateVar = directiveTemplate ;
1243
+ var element ;
1244
+ expect ( function ( ) {
1245
+ element = $compile ( '<p template></p>' ) ( $rootScope ) ;
1246
+ } ) . not . toThrow ( ) ;
1247
+ expect ( element . length ) . toBe ( 1 ) ;
1248
+ expect ( element . text ( ) ) . toBe ( 'Hello World!' ) ;
1249
+ } ) ;
1239
1250
} ) ;
1240
1251
} ) ;
1241
1252
@@ -1348,38 +1359,6 @@ describe('$compile', function() {
1348
1359
} ) ;
1349
1360
}
1350
1361
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
1362
it ( 'should keep prototype properties on directive' , function ( ) {
1384
1363
module ( function ( ) {
1385
1364
function DirectiveClass ( ) {
@@ -2078,57 +2057,55 @@ describe('$compile', function() {
2078
2057
}
2079
2058
) ) ;
2080
2059
2060
+ describe ( 'replace and not exactly one root element' , function ( ) {
2081
2061
2082
- it ( 'should fail if replacing and template doesn\'t have a single root element' , function ( ) {
2083
- module ( function ( $exceptionHandlerProvider ) {
2084
- $exceptionHandlerProvider . mode ( 'log' ) ;
2062
+ beforeEach ( module ( function ( ) {
2085
2063
2086
2064
directive ( 'template' , function ( ) {
2087
2065
return {
2088
2066
replace : true ,
2089
2067
templateUrl : 'template.html'
2090
2068
} ;
2091
2069
} ) ;
2092
- } ) ;
2070
+ } ) ) ;
2093
2071
2094
- inject ( function ( $compile , $templateCache , $rootScope , $exceptionHandler ) {
2095
- // no root element
2096
- $templateCache . put ( 'template.html' , 'dada' ) ;
2097
- $compile ( '<p template></p>' ) ;
2098
- $rootScope . $digest ( ) ;
2099
- expect ( $exceptionHandler . errors . pop ( ) ) . toEqualMinErr ( '$compile' , 'tplrt' ,
2100
- 'Template for directive \'template\' must have exactly one root element. ' +
2101
- 'template.html' ) ;
2072
+ they ( 'should throw if: $prop' ,
2073
+ {
2074
+ 'no root element' : 'dada' ,
2075
+ 'multiple root elements' : '<div></div><div></div>'
2076
+ } , function ( directiveTemplate ) {
2102
2077
2103
- // multi root
2104
- $templateCache . put ( 'template.html' , '<div></div><div></div>' ) ;
2105
- $compile ( '<p template></p>' ) ;
2106
- $rootScope . $digest ( ) ;
2107
- expect ( $exceptionHandler . errors . pop ( ) ) . toEqualMinErr ( '$compile' , 'tplrt' ,
2108
- 'Template for directive \'template\' must have exactly one root element. ' +
2109
- 'template.html' ) ;
2078
+ inject ( function ( $compile , $templateCache , $rootScope , $exceptionHandler ) {
2079
+ $templateCache . put ( 'template.html' , directiveTemplate ) ;
2080
+ $compile ( '<p template></p>' ) ( $rootScope ) ;
2081
+ $rootScope . $digest ( ) ;
2110
2082
2111
- // ws is ok
2112
- $templateCache . put ( 'template.html' , ' <div></div> \n' ) ;
2113
- $compile ( '<p template></p>' ) ;
2114
- $rootScope . $apply ( ) ;
2115
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2083
+ expect ( $exceptionHandler . errors . pop ( ) ) . toEqualMinErr ( '$compile' , 'tplrt' ,
2084
+ 'Template for directive \'template\' must have exactly one root element. ' +
2085
+ 'template.html'
2086
+ ) ;
2087
+ } ) ;
2088
+ } ) ;
2116
2089
2117
- // comments are ok
2118
- $templateCache . put ( 'template.html' , '<!-- oh hi --><div></div> \n' ) ;
2119
- $compile ( '<p template></p>' ) ;
2120
- $rootScope . $apply ( ) ;
2121
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2090
+ they ( 'should not throw if the root element is accompanied by: $prop' ,
2091
+ {
2092
+ 'whitespace' : ' <div>Hello World!</div> \n' ,
2093
+ 'comments' : '<!-- oh hi --><div>Hello World!</div> \n' ,
2094
+ 'comments + whitespace' : ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
2095
+ } , function ( directiveTemplate ) {
2122
2096
2123
- // white space around comments is ok
2124
- $templateCache . put ( 'template.html' , ' <!-- oh hi --> <div></div> <!-- oh hi -->\n' ) ;
2125
- $compile ( '<p template></p>' ) ;
2126
- $rootScope . $apply ( ) ;
2127
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2097
+ inject ( function ( $compile , $templateCache , $rootScope ) {
2098
+ $templateCache . put ( 'template.html' , directiveTemplate ) ;
2099
+ element = $compile ( '<p template></p>' ) ( $rootScope ) ;
2100
+ expect ( function ( ) {
2101
+ $rootScope . $digest ( ) ;
2102
+ } ) . not . toThrow ( ) ;
2103
+ expect ( element . length ) . toBe ( 1 ) ;
2104
+ expect ( element . text ( ) ) . toBe ( 'Hello World!' ) ;
2105
+ } ) ;
2128
2106
} ) ;
2129
2107
} ) ;
2130
2108
2131
-
2132
2109
it ( 'should resume delayed compilation without duplicates when in a repeater' , function ( ) {
2133
2110
// this is a test for a regression
2134
2111
// scope creation, isolate watcher setup, controller instantiation, etc should happen
@@ -2317,45 +2294,6 @@ describe('$compile', function() {
2317
2294
} ) ;
2318
2295
}
2319
2296
2320
- it ( 'should ignore comment nodes when replacing with a templateUrl' , function ( ) {
2321
- module ( function ( ) {
2322
- directive ( 'replaceWithComments' , valueFn ( {
2323
- replace : true ,
2324
- templateUrl : 'templateWithComments.html'
2325
- } ) ) ;
2326
- } ) ;
2327
- inject ( function ( $compile , $rootScope , $httpBackend ) {
2328
- $httpBackend . whenGET ( 'templateWithComments.html' ) .
2329
- respond ( '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->' ) ;
2330
- expect ( function ( ) {
2331
- element = $compile ( '<div><div replace-with-comments></div></div>' ) ( $rootScope ) ;
2332
- } ) . not . toThrow ( ) ;
2333
- $httpBackend . flush ( ) ;
2334
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
2335
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
2336
- } ) ;
2337
- } ) ;
2338
-
2339
- it ( 'should ignore whitespace between comment and root node when replacing with a templateUrl' , function ( ) {
2340
- module ( function ( ) {
2341
- directive ( 'replaceWithWhitespace' , valueFn ( {
2342
- replace : true ,
2343
- templateUrl : 'templateWithWhitespace.html'
2344
- } ) ) ;
2345
- } ) ;
2346
- inject ( function ( $compile , $rootScope , $httpBackend ) {
2347
- $httpBackend . whenGET ( 'templateWithWhitespace.html' ) .
2348
- respond ( '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->' ) ;
2349
- expect ( function ( ) {
2350
- element = $compile ( '<div><div replace-with-whitespace></div></div>' ) ( $rootScope ) ;
2351
- } ) . not . toThrow ( ) ;
2352
- $httpBackend . flush ( ) ;
2353
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
2354
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
2355
- } ) ;
2356
- } ) ;
2357
-
2358
-
2359
2297
it ( 'should keep prototype properties on sync version of async directive' , function ( ) {
2360
2298
module ( function ( ) {
2361
2299
function DirectiveClass ( ) {
0 commit comments