@@ -242,6 +242,15 @@ util.plainText = function(_str) {
242242 return ( _str || '' ) . replace ( STRIP_TAGS , ' ' ) ;
243243} ;
244244
245+ function encodeForHTML ( _str ) {
246+ return ( _str || '' ) . replace ( / & / g, '&' )
247+ . replace ( / < / g, '<' )
248+ . replace ( / > / g, '>' )
249+ . replace ( / " / g, '"' )
250+ . replace ( / ' / g, ''' )
251+ . replace ( / \/ / g, '/' ) ;
252+ }
253+
245254function convertToSVG ( _str ) {
246255 var htmlEntitiesDecoded = Plotly . util . html_entity_decode ( _str ) ;
247256 var result = htmlEntitiesDecoded
@@ -270,15 +279,14 @@ function convertToSVG(_str) {
270279 // remove quotes, leading '=', replace '&' with '&'
271280 var href = extra . substr ( 4 )
272281 . replace ( / [ " ' ] / g, '' )
273- . replace ( / = / , '' )
274- . replace ( / & / g, '&' ) ;
282+ . replace ( / = / , '' ) ;
275283
276284 // check protocol
277285 var dummyAnchor = document . createElement ( 'a' ) ;
278286 dummyAnchor . href = href ;
279287 if ( PROTOCOLS . indexOf ( dummyAnchor . protocol ) === - 1 ) return '<a>' ;
280288
281- return '<a xlink:show="new" xlink:href="' + href + '">' ;
289+ return '<a xlink:show="new" xlink:href="' + encodeForHTML ( href ) + '">' ;
282290 }
283291 }
284292 else if ( tag === 'br' ) return '<br>' ;
@@ -302,7 +310,7 @@ function convertToSVG(_str) {
302310 // most of the svg css users will care about is just like html,
303311 // but font color is different. Let our users ignore this.
304312 extraStyle = extraStyle [ 1 ] . replace ( / ( ^ | ; ) \s * c o l o r : / , '$1 fill:' ) ;
305- style = ( style ? style + ';' : '' ) + extraStyle ;
313+ style = ( style ? style + ';' : '' ) + encodeForHTML ( extraStyle ) ;
306314 }
307315
308316 return tspanStart + ( style ? ' style="' + style + '"' : '' ) + '>' ;
0 commit comments