Skip to content

Commit f1d4f48

Browse files
authored
Merge pull request #5838 from plotly/safari-download
Fix unknown filename when exporting charts using new versions of Safari
2 parents 55d4916 + cc9655a commit f1d4f48

File tree

3 files changed

+9
-55
lines changed

3 files changed

+9
-55
lines changed

draftlogs/5838_fix.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Fix unknown filename when exporting charts using new versions of Safari [[#5609](https://github.com/plotly/plotly.js/pull/5609), [5838](https://github.com/plotly/plotly.js/pull/5838)],
2+
with thanks to @rlreamy for the contribution!

src/snapshot/filesaver.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ function fileSaver(url, name, format) {
2323
var blob;
2424
var objectUrl;
2525

26-
// Safari doesn't allow downloading of blob urls
27-
if(Lib.isSafari()) {
28-
var prefix = format === 'svg' ? ',' : ';base64,';
29-
helpers.octetStream(prefix + encodeURIComponent(url));
30-
return resolve(name);
31-
}
32-
3326
// IE 10+ (native saveAs)
3427
if(Lib.isIE()) {
3528
// At this point we are only dealing with a decoded SVG as
@@ -56,6 +49,13 @@ function fileSaver(url, name, format) {
5649
return resolve(name);
5750
}
5851

52+
// Older versions of Safari did not allow downloading of blob urls
53+
if(Lib.isSafari()) {
54+
var prefix = format === 'svg' ? ',' : ';base64,';
55+
helpers.octetStream(prefix + encodeURIComponent(url));
56+
return resolve(name);
57+
}
58+
5959
reject(new Error('download error'));
6060
});
6161

test/jasmine/tests/download_test.js

-48
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
var Plotly = require('@lib/index');
22
var Lib = require('@src/lib');
33

4-
var helpers = require('@src/snapshot/helpers');
5-
var getImageSize = require('@src/traces/image/helpers').getImageSize;
6-
74
var createGraphDiv = require('../assets/create_graph_div');
85
var destroyGraphDiv = require('../assets/destroy_graph_div');
96

@@ -137,51 +134,6 @@ describe('Plotly.downloadImage', function() {
137134
})
138135
.then(done, done.fail);
139136
}, LONG_TIMEOUT_INTERVAL);
140-
141-
it('should produce right output in Safari', function(done) {
142-
spyOn(Lib, 'isSafari').and.callFake(function() { return true; });
143-
spyOn(helpers, 'octetStream');
144-
145-
Plotly.newPlot(gd, textchartMock.data, textchartMock.layout)
146-
.then(function() { return Plotly.downloadImage(gd, {format: 'svg'}); })
147-
.then(function() { return Plotly.downloadImage(gd, {format: 'png'}); })
148-
.then(function() { return Plotly.downloadImage(gd, {format: 'jpeg'}); })
149-
.then(function() { return Plotly.downloadImage(gd, {format: 'webp'}); })
150-
.then(function() {
151-
var args = helpers.octetStream.calls.allArgs();
152-
expect(args[0][0].slice(0, 15)).toBe(',%3Csvg%20class', 'format:svg');
153-
expect(args[1][0].slice(0, 8)).toBe(';base64,', 'format:png');
154-
expect(args[2][0].slice(0, 8)).toBe(';base64,', 'format:jpeg');
155-
expect(args[3][0].slice(0, 8)).toBe(';base64,', 'format:webp');
156-
})
157-
.then(done, done.fail);
158-
});
159-
160-
it('should default width & height for downloadImage to match with the live graph', function(done) {
161-
spyOn(Lib, 'isSafari').and.callFake(function() { return true; });
162-
spyOn(helpers, 'octetStream');
163-
164-
var fig = {
165-
data: [{y: [0, 1]}]
166-
};
167-
168-
gd.style.width = '500px';
169-
gd.style.height = '300px';
170-
171-
Plotly.newPlot(gd, fig)
172-
.then(function() { return Plotly.downloadImage(gd, {format: 'png'}); })
173-
.then(function() {
174-
var args = helpers.octetStream.calls.allArgs();
175-
var blob = args[0][0];
176-
expect(blob.slice(0, 8)).toBe(';base64,', 'format:png');
177-
var size = getImageSize('data:image/png' + blob);
178-
expect(size.width).toBe(gd._fullLayout.width, 'fullLayout width');
179-
expect(size.height).toBe(gd._fullLayout.height, 'fullLayout height');
180-
expect(size.width).toBe(500, 'div width');
181-
expect(size.height).toBe(300, 'div height');
182-
})
183-
.then(done, done.fail);
184-
});
185137
});
186138

187139
function downloadTest(gd, format) {

0 commit comments

Comments
 (0)