Skip to content

Fix unknown filename when exporting charts using new versions of Safari #5838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions draftlogs/5838_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- 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)],
with thanks to @rlreamy for the contribution!
14 changes: 7 additions & 7 deletions src/snapshot/filesaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ function fileSaver(url, name, format) {
var blob;
var objectUrl;

// Safari doesn't allow downloading of blob urls
if(Lib.isSafari()) {
var prefix = format === 'svg' ? ',' : ';base64,';
helpers.octetStream(prefix + encodeURIComponent(url));
return resolve(name);
}

// IE 10+ (native saveAs)
if(Lib.isIE()) {
// At this point we are only dealing with a decoded SVG as
Expand All @@ -56,6 +49,13 @@ function fileSaver(url, name, format) {
return resolve(name);
}

// Older versions of Safari did not allow downloading of blob urls
if(Lib.isSafari()) {
var prefix = format === 'svg' ? ',' : ';base64,';
helpers.octetStream(prefix + encodeURIComponent(url));
return resolve(name);
}

reject(new Error('download error'));
});

Expand Down
48 changes: 0 additions & 48 deletions test/jasmine/tests/download_test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
var Plotly = require('@lib/index');
var Lib = require('@src/lib');

var helpers = require('@src/snapshot/helpers');
var getImageSize = require('@src/traces/image/helpers').getImageSize;

var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');

Expand Down Expand Up @@ -137,51 +134,6 @@ describe('Plotly.downloadImage', function() {
})
.then(done, done.fail);
}, LONG_TIMEOUT_INTERVAL);

it('should produce right output in Safari', function(done) {
spyOn(Lib, 'isSafari').and.callFake(function() { return true; });
spyOn(helpers, 'octetStream');

Plotly.newPlot(gd, textchartMock.data, textchartMock.layout)
.then(function() { return Plotly.downloadImage(gd, {format: 'svg'}); })
.then(function() { return Plotly.downloadImage(gd, {format: 'png'}); })
.then(function() { return Plotly.downloadImage(gd, {format: 'jpeg'}); })
.then(function() { return Plotly.downloadImage(gd, {format: 'webp'}); })
.then(function() {
var args = helpers.octetStream.calls.allArgs();
expect(args[0][0].slice(0, 15)).toBe(',%3Csvg%20class', 'format:svg');
expect(args[1][0].slice(0, 8)).toBe(';base64,', 'format:png');
expect(args[2][0].slice(0, 8)).toBe(';base64,', 'format:jpeg');
expect(args[3][0].slice(0, 8)).toBe(';base64,', 'format:webp');
})
.then(done, done.fail);
});

it('should default width & height for downloadImage to match with the live graph', function(done) {
spyOn(Lib, 'isSafari').and.callFake(function() { return true; });
spyOn(helpers, 'octetStream');

var fig = {
data: [{y: [0, 1]}]
};

gd.style.width = '500px';
gd.style.height = '300px';

Plotly.newPlot(gd, fig)
.then(function() { return Plotly.downloadImage(gd, {format: 'png'}); })
.then(function() {
var args = helpers.octetStream.calls.allArgs();
var blob = args[0][0];
expect(blob.slice(0, 8)).toBe(';base64,', 'format:png');
var size = getImageSize('data:image/png' + blob);
expect(size.width).toBe(gd._fullLayout.width, 'fullLayout width');
expect(size.height).toBe(gd._fullLayout.height, 'fullLayout height');
expect(size.width).toBe(500, 'div width');
expect(size.height).toBe(300, 'div height');
})
.then(done, done.fail);
});
});

function downloadTest(gd, format) {
Expand Down