2
2
3
3
var d3 = require ( 'd3' ) ;
4
4
5
+ var ATTRS = [ 'x' , 'y' , 'width' , 'height' ] ;
6
+
5
7
6
8
// In-house implementation of SVG getBBox that takes clip paths into account
7
9
module . exports = function getBBox ( element ) {
@@ -14,21 +16,42 @@ module.exports = function getBBox(element) {
14
16
15
17
// only supports 'url(#<id>)' at the moment
16
18
var clipPathId = clipPathAttr . substring ( 5 , clipPathAttr . length - 1 ) ;
17
- var clipPath = d3 . select ( '#' + clipPathId ) . node ( ) ;
19
+ var clipBBox = getClipBBox ( clipPathId ) ;
18
20
19
- return minBBox ( elementBBox , clipPath . getBBox ( ) ) ;
21
+ return minBBox ( elementBBox , clipBBox ) ;
20
22
} ;
21
23
24
+ function getClipBBox ( clipPathId ) {
25
+ var clipPath = d3 . select ( '#' + clipPathId ) ;
26
+ var clipBBox ;
27
+
28
+ try {
29
+ // this line throws an error in FF (38 and 45 at least)
30
+ clipBBox = clipPath . node ( ) . getBBox ( ) ;
31
+ }
32
+ catch ( e ) {
33
+ // use DOM attributes as fallback
34
+ var path = d3 . select ( clipPath . node ( ) . firstChild ) ;
35
+
36
+ clipBBox = { } ;
37
+
38
+ ATTRS . forEach ( function ( attr ) {
39
+ clipBBox [ attr ] = path . attr ( attr ) ;
40
+ } ) ;
41
+ }
42
+
43
+ return clipBBox ;
44
+ }
45
+
22
46
function minBBox ( bbox1 , bbox2 ) {
23
- var keys = [ 'x' , 'y' , 'width' , 'height' ] ;
24
47
var out = { } ;
25
48
26
49
function min ( attr ) {
27
50
return Math . min ( bbox1 [ attr ] , bbox2 [ attr ] ) ;
28
51
}
29
52
30
- keys . forEach ( function ( key ) {
31
- out [ key ] = min ( key ) ;
53
+ ATTRS . forEach ( function ( attr ) {
54
+ out [ attr ] = min ( attr ) ;
32
55
} ) ;
33
56
34
57
return out ;
0 commit comments