@@ -21,9 +21,14 @@ describe('$compile', function() {
21
21
}
22
22
23
23
function supportsMathML ( ) {
24
- var d = document . createElement ( 'div' ) ;
25
- d . innerHTML = '<math></math>' ;
26
- return ! isUnknownElement ( d . firstChild ) ;
24
+ var d = document . createElementNS ( 'http://www.w3.org/1998/Math/MathML' , 'math' ) ;
25
+ return ! isUnknownElement ( d ) ;
26
+ }
27
+
28
+ // IE9-11 do not support foreignObject in svg...
29
+ function supportsForeignObject ( ) {
30
+ var d = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'foreignObject' ) ;
31
+ return ! ! d . toString ( ) . match ( / S V G F o r e i g n O b j e c t / ) ;
27
32
}
28
33
29
34
var element , directive , $compile , $rootScope ;
@@ -80,6 +85,45 @@ describe('$compile', function() {
80
85
terminal : true
81
86
} ) ) ;
82
87
88
+ directive ( 'svgContainer' , function ( ) {
89
+ return {
90
+ template : '<svg width="400" height="400" ng-transclude></svg>' ,
91
+ replace : true ,
92
+ transclude : true
93
+ } ;
94
+ } ) ;
95
+
96
+ directive ( 'svgCustomTranscludeContainer' , function ( ) {
97
+ return {
98
+ template : '<svg width="400" height="400"></svg>' ,
99
+ transclude : true ,
100
+ link : function ( scope , element , attr , ctrls , $transclude ) {
101
+ var futureParent = element . children ( ) . eq ( 0 ) ;
102
+ $transclude ( function ( clone ) {
103
+ futureParent . append ( clone ) ;
104
+ } , futureParent ) ;
105
+ }
106
+ } ;
107
+ } ) ;
108
+
109
+ directive ( 'svgCircle' , function ( ) {
110
+ return {
111
+ template : '<circle cx="2" cy="2" r="1"></circle>' ,
112
+ templateNamespace : 'svg' ,
113
+ replace : true
114
+ } ;
115
+ } ) ;
116
+
117
+ directive ( 'myForeignObject' , function ( ) {
118
+ return {
119
+ template : '<foreignObject width="100" height="100" ng-transclude></foreignObject>' ,
120
+ templateNamespace : 'svg' ,
121
+ replace : true ,
122
+ transclude : true
123
+ } ;
124
+ } ) ;
125
+
126
+
83
127
return function ( _$compile_ , _$rootScope_ ) {
84
128
$rootScope = _$rootScope_ ;
85
129
$compile = _$compile_ ;
@@ -154,6 +198,105 @@ describe('$compile', function() {
154
198
} ) ;
155
199
156
200
201
+ describe ( 'svg namespace transcludes' , function ( ) {
202
+ // this method assumes some sort of sized SVG element is being inspected.
203
+ function assertIsValidSvgCircle ( elem ) {
204
+ expect ( isUnknownElement ( elem ) ) . toBe ( false ) ;
205
+ expect ( isSVGElement ( elem ) ) . toBe ( true ) ;
206
+ var box = elem . getBoundingClientRect ( ) ;
207
+ expect ( box . width === 0 && box . height === 0 ) . toBe ( false ) ;
208
+ }
209
+
210
+ it ( 'should handle transcluded svg elements' , inject ( function ( $compile ) {
211
+ element = jqLite ( '<div><svg-container>' +
212
+ '<circle cx="4" cy="4" r="2"></circle>' +
213
+ '</svg-container></div>' ) ;
214
+ $compile ( element . contents ( ) ) ( $rootScope ) ;
215
+ document . body . appendChild ( element [ 0 ] ) ;
216
+
217
+ var circle = element . find ( 'circle' ) ;
218
+
219
+ assertIsValidSvgCircle ( circle [ 0 ] ) ;
220
+ } ) ) ;
221
+
222
+ it ( 'should handle custom svg elements inside svg tag' , inject ( function ( ) {
223
+ element = jqLite ( '<div><svg width="300" height="300">' +
224
+ '<svg-circle></svg-circle>' +
225
+ '</svg></div>' ) ;
226
+ $compile ( element . contents ( ) ) ( $rootScope ) ;
227
+ document . body . appendChild ( element [ 0 ] ) ;
228
+
229
+ var circle = element . find ( 'circle' ) ;
230
+ assertIsValidSvgCircle ( circle [ 0 ] ) ;
231
+ } ) ) ;
232
+
233
+ it ( 'should handle transcluded custom svg elements' , inject ( function ( ) {
234
+ element = jqLite ( '<div><svg-container>' +
235
+ '<svg-circle></svg-circle>' +
236
+ '</svg-container></div>' ) ;
237
+ $compile ( element . contents ( ) ) ( $rootScope ) ;
238
+ document . body . appendChild ( element [ 0 ] ) ;
239
+
240
+ var circle = element . find ( 'circle' ) ;
241
+ assertIsValidSvgCircle ( circle [ 0 ] ) ;
242
+ } ) ) ;
243
+
244
+ if ( supportsForeignObject ( ) ) {
245
+ it ( 'should handle foreignObject' , inject ( function ( ) {
246
+ element = jqLite ( '<div><svg-container>' +
247
+ '<foreignObject width="100" height="100"><div class="test" style="position:absolute;width:20px;height:20px">test</div></foreignObject>' +
248
+ '</svg-container></div>' ) ;
249
+ $compile ( element . contents ( ) ) ( $rootScope ) ;
250
+ document . body . appendChild ( element [ 0 ] ) ;
251
+
252
+ var testElem = element . find ( 'div' ) ;
253
+ expect ( isHTMLElement ( testElem [ 0 ] ) ) . toBe ( true ) ;
254
+ var bounds = testElem [ 0 ] . getBoundingClientRect ( ) ;
255
+ expect ( bounds . width === 20 && bounds . height === 20 ) . toBe ( true ) ;
256
+ } ) ) ;
257
+
258
+ it ( 'should handle custom svg containers that transclude to foreignObject that transclude html' , inject ( function ( ) {
259
+ element = jqLite ( '<div><svg-container>' +
260
+ '<my-foreign-object><div class="test" style="width:20px;height:20px">test</div></my-foreign-object>' +
261
+ '</svg-container></div>' ) ;
262
+ $compile ( element . contents ( ) ) ( $rootScope ) ;
263
+ document . body . appendChild ( element [ 0 ] ) ;
264
+
265
+ var testElem = element . find ( 'div' ) ;
266
+ expect ( isHTMLElement ( testElem [ 0 ] ) ) . toBe ( true ) ;
267
+ var bounds = testElem [ 0 ] . getBoundingClientRect ( ) ;
268
+ expect ( bounds . width === 20 && bounds . height === 20 ) . toBe ( true ) ;
269
+ } ) ) ;
270
+
271
+ // NOTE: This test may be redundant.
272
+ it ( 'should handle custom svg containers that transclude to foreignObject' +
273
+ ' that transclude to custom svg containers that transclude to custom elements' , inject ( function ( ) {
274
+ element = jqLite ( '<div><svg-container>' +
275
+ '<my-foreign-object><svg-container><svg-circle></svg-circle></svg-container></my-foreign-object>' +
276
+ '</svg-container></div>' ) ;
277
+ $compile ( element . contents ( ) ) ( $rootScope ) ;
278
+ document . body . appendChild ( element [ 0 ] ) ;
279
+
280
+ var circle = element . find ( 'circle' ) ;
281
+ assertIsValidSvgCircle ( circle [ 0 ] ) ;
282
+ } ) ) ;
283
+ }
284
+
285
+ it ( 'should handle directives with templates that manually add the transclude further down' , inject ( function ( ) {
286
+ element = jqLite ( '<div><svg-custom-transclude-container>' +
287
+ '<circle cx="2" cy="2" r="1"></circle></svg-custom-transclude-container>' +
288
+ '</div>' ) ;
289
+ $compile ( element . contents ( ) ) ( $rootScope ) ;
290
+ document . body . appendChild ( element [ 0 ] ) ;
291
+
292
+ var circle = element . find ( 'circle' ) ;
293
+ assertIsValidSvgCircle ( circle [ 0 ] ) ;
294
+
295
+ } ) ) ;
296
+
297
+ } ) ;
298
+
299
+
157
300
describe ( 'compile phase' , function ( ) {
158
301
159
302
it ( 'should attach scope to the document node when it is compiled explicitly' , inject ( function ( $document ) {
0 commit comments