11import { XmlDocument } from "xmldoc" ;
2+ import { isString } from './helpers/variableType' ;
23
34/**
45 * Strip unit postfix, parse number, but return undefined instead of NaN for bad input
@@ -26,11 +27,11 @@ const parseSVG = (svgString) => {
2627 try {
2728 doc = new XmlDocument ( svgString ) ;
2829 } catch ( err ) {
29- throw new Error ( 'SVGMeasure: ' + err ) ;
30+ throw new Error ( 'Invalid svg document ( ' + err + ')' ) ;
3031 }
3132
3233 if ( doc . name !== "svg" ) {
33- throw new Error ( 'SVGMeasure: expected <svg> document ' ) ;
34+ throw new Error ( 'Invalid svg document ( expected <svg>) ' ) ;
3435 }
3536
3637 return doc ;
@@ -41,16 +42,30 @@ class SVGMeasure {
4142
4243 }
4344
44- measureSVG ( svgString ) {
45- let doc = parseSVG ( svgString ) ;
45+ measureSVG ( svg ) {
46+ let width , height , viewBox ;
4647
47- let docWidth = stripUnits ( doc . attr . width ) ;
48- let docHeight = stripUnits ( doc . attr . height ) ;
48+ if ( isString ( svg ) ) {
49+ let doc = parseSVG ( svg ) ;
4950
50- if ( ( docWidth === undefined || docHeight === undefined ) && typeof doc . attr . viewBox === 'string' ) {
51- let viewBoxParts = doc . attr . viewBox . split ( / [ , \s ] + / ) ;
51+ width = doc . attr . width ;
52+ height = doc . attr . height ;
53+ viewBox = doc . attr . viewBox ;
54+ } else if ( typeof SVGElement !== 'undefined' && svg instanceof SVGElement && typeof getComputedStyle === 'function' ) {
55+ width = svg . getAttribute ( "width" ) ;
56+ height = svg . getAttribute ( "height" ) ;
57+ viewBox = svg . getAttribute ( "viewBox" ) ;
58+ } else {
59+ throw new Error ( 'Invalid SVG document' ) ;
60+ }
61+
62+ let docWidth = stripUnits ( width ) ;
63+ let docHeight = stripUnits ( height ) ;
64+
65+ if ( ( docWidth === undefined || docHeight === undefined ) && typeof viewBox === 'string' ) {
66+ let viewBoxParts = viewBox . split ( / [ , \s ] + / ) ;
5267 if ( viewBoxParts . length !== 4 ) {
53- throw new Error ( "Unexpected svg viewbox format, should have 4 entries but found: '" + doc . attr . viewBox + "'" ) ;
68+ throw new Error ( "Unexpected svg viewBox format, should have 4 entries but found: '" + viewBox + "'" ) ;
5469 }
5570 if ( docWidth === undefined ) {
5671 docWidth = stripUnits ( viewBoxParts [ 2 ] ) ;
@@ -66,17 +81,28 @@ class SVGMeasure {
6681 } ;
6782 }
6883
69- writeDimensions ( svgString , dimensions ) {
70- let doc = parseSVG ( svgString ) ;
71-
72- if ( typeof doc . attr . viewBox !== 'string' ) {
73- doc . attr . viewBox = `0 0 ${ stripUnits ( doc . attr . width ) } ${ stripUnits ( doc . attr . height ) } ` ;
84+ writeDimensions ( svg , dimensions ) {
85+ if ( isString ( svg ) ) {
86+ let doc = parseSVG ( svg ) ;
87+
88+ if ( typeof doc . attr . viewBox !== 'string' ) {
89+ doc . attr . viewBox = `0 0 ${ stripUnits ( doc . attr . width ) } ${ stripUnits ( doc . attr . height ) } ` ;
90+ }
91+
92+ doc . attr . width = "" + dimensions . width ;
93+ doc . attr . height = "" + dimensions . height ;
94+
95+ return doc . toString ( ) ;
96+ }
97+
98+ if ( ! svg . hasAttribute ( 'viewBox' ) ) {
99+ svg . setAttribute ( 'viewBox' , `0 0 ${ stripUnits ( svg . getAttribute ( 'width' ) ) } ${ stripUnits ( svg . getAttribute ( 'height' ) ) } ` ) ;
74100 }
75101
76- doc . attr . width = "" + dimensions . width ;
77- doc . attr . height = "" + dimensions . height ;
102+ svg . setAttribute ( ' width' , "" + dimensions . width ) ;
103+ svg . setAttribute ( ' height' , "" + dimensions . height ) ;
78104
79- return doc . toString ( ) ;
105+ return svg ;
80106 }
81107}
82108
0 commit comments