@@ -1189,41 +1189,52 @@ describe('$compile', function() {
1189
1189
} ) ;
1190
1190
} ) ;
1191
1191
1192
- it ( 'should fail if replacing and template doesn\'t have a single root element' , function ( ) {
1193
- module ( function ( ) {
1194
- directive ( 'noRootElem' , function ( ) {
1195
- return {
1196
- replace : true ,
1197
- template : 'dada'
1198
- } ;
1199
- } ) ;
1200
- directive ( 'multiRootElem' , function ( ) {
1201
- return {
1202
- replace : true ,
1203
- template : '<div></div><div></div>'
1204
- } ;
1205
- } ) ;
1206
- directive ( 'singleRootWithWhiteSpace' , function ( ) {
1192
+ describe ( 'replace and not exactly one root element' , function ( ) {
1193
+ var templateVar ;
1194
+
1195
+ beforeEach ( module ( function ( ) {
1196
+ directive ( 'template' , function ( ) {
1207
1197
return {
1208
1198
replace : true ,
1209
- template : ' <div></div> \n'
1199
+ template : function ( ) {
1200
+ return templateVar ;
1201
+ }
1210
1202
} ;
1211
1203
} ) ;
1212
- } ) ;
1204
+ } ) ) ;
1213
1205
1214
- inject ( function ( $compile ) {
1215
- expect ( function ( ) {
1216
- $compile ( '<p no-root-elem></p>' ) ;
1217
- } ) . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'noRootElem\' must have exactly one root element. ' ) ;
1206
+ they ( 'should throw if: $prop' ,
1207
+ {
1208
+ 'no root element' : 'dada' ,
1209
+ 'multiple root elements' : '<div></div><div></div>'
1210
+ } , function ( directiveTemplate ) {
1218
1211
1219
- expect ( function ( ) {
1220
- $compile ( '<p multi-root-elem></p>' ) ;
1221
- } ) . toThrowMinErr ( '$compile' , 'tplrt' , 'Template for directive \'multiRootElem\' must have exactly one root element. ' ) ;
1212
+ inject ( function ( $compile ) {
1213
+ templateVar = directiveTemplate ;
1214
+ expect ( function ( ) {
1215
+ $compile ( '<p template></p>' ) ;
1216
+ } ) . toThrowMinErr ( '$compile' , 'tplrt' ,
1217
+ 'Template for directive \'template\' must have exactly one root element.'
1218
+ ) ;
1219
+ } ) ;
1220
+ } ) ;
1222
1221
1223
- // ws is ok
1224
- expect ( function ( ) {
1225
- $compile ( '<p single-root-with-white-space></p>' ) ;
1226
- } ) . not . toThrow ( ) ;
1222
+ they ( 'should not throw if the root element is accompanied by: $prop' ,
1223
+ {
1224
+ 'whitespace' : ' <div>Hello World!</div> \n' ,
1225
+ 'comments' : '<!-- oh hi --><div>Hello World!</div> \n' ,
1226
+ 'comments + whitespace' : ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
1227
+ } , function ( directiveTemplate ) {
1228
+
1229
+ inject ( function ( $compile , $rootScope ) {
1230
+ templateVar = directiveTemplate ;
1231
+ var element ;
1232
+ expect ( function ( ) {
1233
+ element = $compile ( '<p template></p>' ) ( $rootScope ) ;
1234
+ } ) . not . toThrow ( ) ;
1235
+ expect ( element . length ) . toBe ( 1 ) ;
1236
+ expect ( element . text ( ) ) . toBe ( 'Hello World!' ) ;
1237
+ } ) ;
1227
1238
} ) ;
1228
1239
} ) ;
1229
1240
@@ -1336,38 +1347,6 @@ describe('$compile', function() {
1336
1347
} ) ;
1337
1348
}
1338
1349
1339
- it ( 'should ignore comment nodes when replacing with a template' , function ( ) {
1340
- module ( function ( ) {
1341
- directive ( 'replaceWithComments' , valueFn ( {
1342
- replace : true ,
1343
- template : '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->'
1344
- } ) ) ;
1345
- } ) ;
1346
- inject ( function ( $compile , $rootScope ) {
1347
- expect ( function ( ) {
1348
- element = $compile ( '<div><div replace-with-comments></div></div>' ) ( $rootScope ) ;
1349
- } ) . not . toThrow ( ) ;
1350
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
1351
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
1352
- } ) ;
1353
- } ) ;
1354
-
1355
- it ( 'should ignore whitespace betwee comment and root node when replacing with a template' , function ( ) {
1356
- module ( function ( ) {
1357
- directive ( 'replaceWithWhitespace' , valueFn ( {
1358
- replace : true ,
1359
- template : '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->'
1360
- } ) ) ;
1361
- } ) ;
1362
- inject ( function ( $compile , $rootScope ) {
1363
- expect ( function ( ) {
1364
- element = $compile ( '<div><div replace-with-whitespace></div></div>' ) ( $rootScope ) ;
1365
- } ) . not . toThrow ( ) ;
1366
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
1367
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
1368
- } ) ;
1369
- } ) ;
1370
-
1371
1350
it ( 'should keep prototype properties on directive' , function ( ) {
1372
1351
module ( function ( ) {
1373
1352
function DirectiveClass ( ) {
@@ -2063,57 +2042,55 @@ describe('$compile', function() {
2063
2042
}
2064
2043
) ) ;
2065
2044
2045
+ describe ( 'replace and not exactly one root element' , function ( ) {
2066
2046
2067
- it ( 'should fail if replacing and template doesn\'t have a single root element' , function ( ) {
2068
- module ( function ( $exceptionHandlerProvider ) {
2069
- $exceptionHandlerProvider . mode ( 'log' ) ;
2047
+ beforeEach ( module ( function ( ) {
2070
2048
2071
2049
directive ( 'template' , function ( ) {
2072
2050
return {
2073
2051
replace : true ,
2074
2052
templateUrl : 'template.html'
2075
2053
} ;
2076
2054
} ) ;
2077
- } ) ;
2055
+ } ) ) ;
2078
2056
2079
- inject ( function ( $compile , $templateCache , $rootScope , $exceptionHandler ) {
2080
- // no root element
2081
- $templateCache . put ( 'template.html' , 'dada' ) ;
2082
- $compile ( '<p template></p>' ) ;
2083
- $rootScope . $digest ( ) ;
2084
- expect ( $exceptionHandler . errors . pop ( ) ) . toEqualMinErr ( '$compile' , 'tplrt' ,
2085
- 'Template for directive \'template\' must have exactly one root element. ' +
2086
- 'template.html' ) ;
2057
+ they ( 'should throw if: $prop' ,
2058
+ {
2059
+ 'no root element' : 'dada' ,
2060
+ 'multiple root elements' : '<div></div><div></div>'
2061
+ } , function ( directiveTemplate ) {
2087
2062
2088
- // multi root
2089
- $templateCache . put ( 'template.html' , '<div></div><div></div>' ) ;
2090
- $compile ( '<p template></p>' ) ;
2091
- $rootScope . $digest ( ) ;
2092
- expect ( $exceptionHandler . errors . pop ( ) ) . toEqualMinErr ( '$compile' , 'tplrt' ,
2093
- 'Template for directive \'template\' must have exactly one root element. ' +
2094
- 'template.html' ) ;
2063
+ inject ( function ( $compile , $templateCache , $rootScope , $exceptionHandler ) {
2064
+ $templateCache . put ( 'template.html' , directiveTemplate ) ;
2065
+ $compile ( '<p template></p>' ) ( $rootScope ) ;
2066
+ $rootScope . $digest ( ) ;
2095
2067
2096
- // ws is ok
2097
- $templateCache . put ( 'template.html' , ' <div></div> \n' ) ;
2098
- $compile ( '<p template></p>' ) ;
2099
- $rootScope . $apply ( ) ;
2100
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2068
+ expect ( $exceptionHandler . errors . pop ( ) ) . toEqualMinErr ( '$compile' , 'tplrt' ,
2069
+ 'Template for directive \'template\' must have exactly one root element. ' +
2070
+ 'template.html'
2071
+ ) ;
2072
+ } ) ;
2073
+ } ) ;
2101
2074
2102
- // comments are ok
2103
- $templateCache . put ( 'template.html' , '<!-- oh hi --><div></div> \n' ) ;
2104
- $compile ( '<p template></p>' ) ;
2105
- $rootScope . $apply ( ) ;
2106
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2075
+ they ( 'should not throw if the root element is accompanied by: $prop' ,
2076
+ {
2077
+ 'whitespace' : ' <div>Hello World!</div> \n' ,
2078
+ 'comments' : '<!-- oh hi --><div>Hello World!</div> \n' ,
2079
+ 'comments + whitespace' : ' <!-- oh hi --> <div>Hello World!</div> <!-- oh hi -->\n'
2080
+ } , function ( directiveTemplate ) {
2107
2081
2108
- // white space around comments is ok
2109
- $templateCache . put ( 'template.html' , ' <!-- oh hi --> <div></div> <!-- oh hi -->\n' ) ;
2110
- $compile ( '<p template></p>' ) ;
2111
- $rootScope . $apply ( ) ;
2112
- expect ( $exceptionHandler . errors ) . toEqual ( [ ] ) ;
2082
+ inject ( function ( $compile , $templateCache , $rootScope ) {
2083
+ $templateCache . put ( 'template.html' , directiveTemplate ) ;
2084
+ element = $compile ( '<p template></p>' ) ( $rootScope ) ;
2085
+ expect ( function ( ) {
2086
+ $rootScope . $digest ( ) ;
2087
+ } ) . not . toThrow ( ) ;
2088
+ expect ( element . length ) . toBe ( 1 ) ;
2089
+ expect ( element . text ( ) ) . toBe ( 'Hello World!' ) ;
2090
+ } ) ;
2113
2091
} ) ;
2114
2092
} ) ;
2115
2093
2116
-
2117
2094
it ( 'should resume delayed compilation without duplicates when in a repeater' , function ( ) {
2118
2095
// this is a test for a regression
2119
2096
// scope creation, isolate watcher setup, controller instantiation, etc should happen
@@ -2302,45 +2279,6 @@ describe('$compile', function() {
2302
2279
} ) ;
2303
2280
}
2304
2281
2305
- it ( 'should ignore comment nodes when replacing with a templateUrl' , function ( ) {
2306
- module ( function ( ) {
2307
- directive ( 'replaceWithComments' , valueFn ( {
2308
- replace : true ,
2309
- templateUrl : 'templateWithComments.html'
2310
- } ) ) ;
2311
- } ) ;
2312
- inject ( function ( $compile , $rootScope , $httpBackend ) {
2313
- $httpBackend . whenGET ( 'templateWithComments.html' ) .
2314
- respond ( '<!-- ignored comment --><p>Hello, world!</p><!-- ignored comment-->' ) ;
2315
- expect ( function ( ) {
2316
- element = $compile ( '<div><div replace-with-comments></div></div>' ) ( $rootScope ) ;
2317
- } ) . not . toThrow ( ) ;
2318
- $httpBackend . flush ( ) ;
2319
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
2320
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
2321
- } ) ;
2322
- } ) ;
2323
-
2324
- it ( 'should ignore whitespace between comment and root node when replacing with a templateUrl' , function ( ) {
2325
- module ( function ( ) {
2326
- directive ( 'replaceWithWhitespace' , valueFn ( {
2327
- replace : true ,
2328
- templateUrl : 'templateWithWhitespace.html'
2329
- } ) ) ;
2330
- } ) ;
2331
- inject ( function ( $compile , $rootScope , $httpBackend ) {
2332
- $httpBackend . whenGET ( 'templateWithWhitespace.html' ) .
2333
- respond ( '<!-- ignored comment --> <p>Hello, world!</p> <!-- ignored comment-->' ) ;
2334
- expect ( function ( ) {
2335
- element = $compile ( '<div><div replace-with-whitespace></div></div>' ) ( $rootScope ) ;
2336
- } ) . not . toThrow ( ) ;
2337
- $httpBackend . flush ( ) ;
2338
- expect ( element . find ( 'p' ) . length ) . toBe ( 1 ) ;
2339
- expect ( element . find ( 'p' ) . text ( ) ) . toBe ( 'Hello, world!' ) ;
2340
- } ) ;
2341
- } ) ;
2342
-
2343
-
2344
2282
it ( 'should keep prototype properties on sync version of async directive' , function ( ) {
2345
2283
module ( function ( ) {
2346
2284
function DirectiveClass ( ) {
0 commit comments