22
33var d3 = require ( 'd3' ) ;
44
5+ var ATTRS = [ 'x' , 'y' , 'width' , 'height' ] ;
6+
57
68// In-house implementation of SVG getBBox that takes clip paths into account
79module . exports = function getBBox ( element ) {
@@ -14,21 +16,42 @@ module.exports = function getBBox(element) {
1416
1517 // only supports 'url(#<id>)' at the moment
1618 var clipPathId = clipPathAttr . substring ( 5 , clipPathAttr . length - 1 ) ;
17- var clipPath = d3 . select ( '#' + clipPathId ) . node ( ) ;
19+ var clipBBox = getClipBBox ( clipPathId ) ;
1820
19- return minBBox ( elementBBox , clipPath . getBBox ( ) ) ;
21+ return minBBox ( elementBBox , clipBBox ) ;
2022} ;
2123
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+
2246function minBBox ( bbox1 , bbox2 ) {
23- var keys = [ 'x' , 'y' , 'width' , 'height' ] ;
2447 var out = { } ;
2548
2649 function min ( attr ) {
2750 return Math . min ( bbox1 [ attr ] , bbox2 [ attr ] ) ;
2851 }
2952
30- keys . forEach ( function ( key ) {
31- out [ key ] = min ( key ) ;
53+ ATTRS . forEach ( function ( attr ) {
54+ out [ attr ] = min ( attr ) ;
3255 } ) ;
3356
3457 return out ;
0 commit comments