Skip to content

Commit 4e24ed2

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 f021398 commit 4e24ed2

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
@@ -44,23 +45,23 @@ function fileSaver(url, name, format) {
4445
if(Lib.isIE()) {
4546
// At this point we are only dealing with a decoded SVG as
4647
// a data URL (since IE only supports SVG)
47-
blob = createBlob(url, format);
48+
blob = helpers.createBlob(url, 'svg');
4849
window.navigator.msSaveBlob(blob, name);
4950
blob = null;
5051
return resolve(name);
5152
}
5253

5354
if(canUseSaveLink) {
54-
blob = createBlob(url, format);
55-
objectUrl = window.URL.createObjectURL(blob);
55+
blob = helpers.createBlob(url, format);
56+
objectUrl = helpers.createObjectURL(blob);
5657

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

6263
document.body.removeChild(saveLink);
63-
window.URL.revokeObjectURL(objectUrl);
64+
helpers.revokeObjectURL(objectUrl);
6465
blob = null;
6566

6667
return resolve(name);
@@ -80,24 +81,4 @@ function isIE9orBelow() {
8081
);
8182
}
8283

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