Skip to content

Commit 4ec3e0f

Browse files
committed
move IE-specific SVG string tweaks to toSVG
- make Plotly.toImage bypass svgToImg even when imageDataOnly is false.
1 parent 3c42efb commit 4ec3e0f

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

src/plot_api/to_image.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ function toImage(gd, opts) {
144144
return new Promise(function(resolve, reject) {
145145
var svg = toSVG(clonedGd);
146146

147-
if(format === 'svg' && imageDataOnly) {
148-
return resolve(svg);
147+
if(format === 'svg') {
148+
if(imageDataOnly) {
149+
return resolve(svg);
150+
} else {
151+
return resolve('data:image/svg+xml,' + encodeURIComponent(svg));
152+
}
149153
}
150154

151155
var canvas = document.createElement('canvas');

src/snapshot/svgtoimg.js

+10-32
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,27 @@ var Lib = require('../lib');
1212
var EventEmitter = require('events').EventEmitter;
1313

1414
function svgToImg(opts) {
15-
1615
var ev = opts.emitter || new EventEmitter();
1716

1817
var promise = new Promise(function(resolve, reject) {
19-
2018
var Image = window.Image;
21-
2219
var svg = opts.svg;
2320
var format = opts.format || 'png';
2421

25-
// IE is very strict, so we will need to clean
26-
// svg with the following regex
27-
// yes this is messy, but do not know a better way
28-
// Even with this IE will not work due to tainted canvas
29-
// see https://github.com/kangax/fabric.js/issues/1957
30-
// http://stackoverflow.com/questions/18112047/canvas-todataurl-working-in-all-browsers-except-ie10
31-
// Leave here just in case the CORS/tainted IE issue gets resolved
32-
if(Lib.isIE()) {
33-
// replace double quote with single quote
34-
svg = svg.replace(/"/gi, '\'');
35-
// url in svg are single quoted
36-
// since we changed double to single
37-
// we'll need to change these to double-quoted
38-
svg = svg.replace(/(\('#)([^']*)('\))/gi, '(\"$2\")');
39-
// font names with spaces will be escaped single-quoted
40-
// we'll need to change these to double-quoted
41-
svg = svg.replace(/(\\')/gi, '\"');
42-
// IE only support svg
43-
if(format !== 'svg') {
44-
var ieSvgError = new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.');
45-
reject(ieSvgError);
46-
// eventually remove the ev
47-
// in favor of promises
48-
if(!opts.promise) {
49-
return ev.emit('error', ieSvgError);
50-
} else {
51-
return promise;
52-
}
22+
// IE only support svg
23+
if(Lib.isIE() && format !== 'svg') {
24+
var ieSvgError = new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.');
25+
reject(ieSvgError);
26+
// eventually remove the ev
27+
// in favor of promises
28+
if(!opts.promise) {
29+
return ev.emit('error', ieSvgError);
30+
} else {
31+
return promise;
5332
}
5433
}
5534

5635
var canvas = opts.canvas;
57-
5836
var ctx = canvas.getContext('2d');
5937
var img = new Image();
6038

src/snapshot/tosvg.js

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var d3 = require('d3');
1313

14+
var Lib = require('../lib');
1415
var Drawing = require('../components/drawing');
1516
var Color = require('../components/color');
1617

@@ -143,5 +144,24 @@ module.exports = function toSVG(gd, format) {
143144
// Fix quotations around font strings and gradient URLs
144145
s = s.replace(DUMMY_REGEX, '\'');
145146

147+
// IE is very strict, so we will need to clean
148+
// svg with the following regex
149+
// yes this is messy, but do not know a better way
150+
// Even with this IE will not work due to tainted canvas
151+
// see https://github.com/kangax/fabric.js/issues/1957
152+
// http://stackoverflow.com/questions/18112047/canvas-todataurl-working-in-all-browsers-except-ie10
153+
// Leave here just in case the CORS/tainted IE issue gets resolved
154+
if(Lib.isIE()) {
155+
// replace double quote with single quote
156+
s = s.replace(/"/gi, '\'');
157+
// url in svg are single quoted
158+
// since we changed double to single
159+
// we'll need to change these to double-quoted
160+
s = s.replace(/(\('#)([^']*)('\))/gi, '(\"$2\")');
161+
// font names with spaces will be escaped single-quoted
162+
// we'll need to change these to double-quoted
163+
s = s.replace(/(\\')/gi, '\"');
164+
}
165+
146166
return s;
147167
};

0 commit comments

Comments
 (0)