Skip to content

Commit e0802b1

Browse files
committed
move (create|revoke)ObjectURL & createBlob to helpers
- so that when we can fallback to window.webkitURL if window.URL isn't defined
1 parent f2044f2 commit e0802b1

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

src/snapshot/filesaver.js

+5-24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var Lib = require('../lib');
12+
var helpers = require('./helpers');
1213

1314
/*
1415
* substantial portions of this code from FileSaver.js
@@ -45,23 +46,23 @@ function fileSaver(url, name, format) {
4546
if(Lib.isIE()) {
4647
// At this point we are only dealing with a decoded SVG as
4748
// a data URL (since IE only supports SVG)
48-
blob = createBlob(url, format);
49+
blob = helpers.createBlob(url, 'svg');
4950
window.navigator.msSaveBlob(blob, name);
5051
blob = null;
5152
return resolve(name);
5253
}
5354

5455
if(canUseSaveLink) {
55-
blob = createBlob(url, format);
56-
objectUrl = window.URL.createObjectURL(blob);
56+
blob = helpers.createBlob(url, format);
57+
objectUrl = helpers.createObjectURL(blob);
5758

5859
saveLink.href = objectUrl;
5960
saveLink.download = name;
6061
document.body.appendChild(saveLink);
6162
saveLink.click();
6263

6364
document.body.removeChild(saveLink);
64-
window.URL.revokeObjectURL(objectUrl);
65+
helpers.revokeObjectURL(objectUrl);
6566
blob = null;
6667

6768
return resolve(name);
@@ -81,24 +82,4 @@ function isIE9orBelow() {
8182
);
8283
}
8384

84-
// Taken from https://bl.ocks.org/nolanlawson/0eac306e4dac2114c752
85-
function fixBinary(b) {
86-
var len = b.length;
87-
var buf = new ArrayBuffer(len);
88-
var arr = new Uint8Array(buf);
89-
for(var i = 0; i < len; i++) {
90-
arr[i] = b.charCodeAt(i);
91-
}
92-
return buf;
93-
}
94-
95-
function createBlob(url, format) {
96-
if(format === 'svg') {
97-
return new window.Blob([url]);
98-
} else {
99-
var binary = fixBinary(window.atob(url));
100-
return new window.Blob([binary], {type: 'image/' + format});
101-
}
102-
}
103-
10485
module.exports = fileSaver;

src/snapshot/helpers.js

+30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ exports.encodeSVG = function(svg) {
3636
return 'data:image/svg+xml,' + encodeURIComponent(svg);
3737
};
3838

39+
var DOM_URL = window.URL || window.webkitURL;
40+
41+
exports.createObjectURL = function(blob) {
42+
return DOM_URL.createObjectURL(blob);
43+
};
44+
45+
exports.revokeObjectURL = function(url) {
46+
return DOM_URL.revokeObjectURL(url);
47+
};
48+
49+
exports.createBlob = function(url, format) {
50+
if(format === 'svg') {
51+
return new window.Blob([url], {type: 'image/svg+xml;charset=utf-8'});
52+
} else {
53+
var binary = fixBinary(window.atob(url));
54+
return new window.Blob([binary], {type: 'image/' + format});
55+
}
56+
};
57+
58+
// Taken from https://bl.ocks.org/nolanlawson/0eac306e4dac2114c752
59+
function fixBinary(b) {
60+
var len = b.length;
61+
var buf = new ArrayBuffer(len);
62+
var arr = new Uint8Array(buf);
63+
for(var i = 0; i < len; i++) {
64+
arr[i] = b.charCodeAt(i);
65+
}
66+
return buf;
67+
}
68+
3969
exports.IMAGE_URL_PREFIX = /^data:image\/\w+;base64,/;
4070

4171
exports.MSG_IE_BAD_FORMAT = 'Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.';

0 commit comments

Comments
 (0)