Commit 43bbc5ab authored by Douglas's avatar Douglas Committed by Romain Courteaud

local storage: fixes test transformation from data-uri to blob

Webkit has issues when trying to load data-uri based images with
XMLHTTPRequest this is changed for the sake of compatibility with all
browsers.
parent 05d4c9b5
/*jslint nomen: true */ /*jslint nomen: true */
/*global sessionStorage, localStorage, Blob, document, btoa, /*global sessionStorage, localStorage, Blob, document, btoa, atob, Uint8Array,
unescape, HTMLCanvasElement, XMLHttpRequest*/ unescape, HTMLCanvasElement, XMLHttpRequest*/
(function (jIO, sessionStorage, localStorage, QUnit, Blob, document, (function (jIO, sessionStorage, localStorage, QUnit, Blob, document,
btoa, unescape, HTMLCanvasElement, XMLHttpRequest) { btoa, unescape, HTMLCanvasElement) {
"use strict"; "use strict";
var test = QUnit.test, var test = QUnit.test,
stop = QUnit.stop, stop = QUnit.stop,
...@@ -365,13 +365,22 @@ ...@@ -365,13 +365,22 @@
'toBlob', 'toBlob',
{ {
value: function (callback, type, quality) { value: function (callback, type, quality) {
var xhr = new XMLHttpRequest(); var byte_string, ia, i,
xhr.open('GET', this.toDataURL(type, quality)); data_uri = this.toDataURL(type, quality);
xhr.responseType = 'arraybuffer';
xhr.onload = function () { if (data_uri.split(',')[0].indexOf('base64') >= 0) {
callback(new Blob([this.response], {type: type || 'image/png'})); byte_string = atob(data_uri.split(',')[1]);
}; } else {
xhr.send(); byte_string = unescape(data_uri.split(',')[1]);
}
// write the bytes of the string to a typed array
ia = new Uint8Array(byte_string.length);
for (i = 0; i < byte_string.length; i += 1) {
ia[i] = byte_string.charCodeAt(i);
}
return callback(new Blob([ia], {type: type || 'image/png'}));
} }
} }
); );
...@@ -496,4 +505,4 @@ ...@@ -496,4 +505,4 @@
}); });
}(jIO, sessionStorage, localStorage, QUnit, Blob, document, }(jIO, sessionStorage, localStorage, QUnit, Blob, document,
btoa, unescape, HTMLCanvasElement, XMLHttpRequest)); btoa, unescape, HTMLCanvasElement));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment