Skip to content

Commit

Permalink
Run tests in IE 10.
Browse files Browse the repository at this point in the history
IE 10 doesn't support the charset=x-user-defined trick and doesn't
interpret conditional comment. The only way do download a zip file is
to use the responseType option... which isn't compatible with jQuery.

Good bye, jQuery :-)
  • Loading branch information
dduponchel committed Mar 3, 2013
1 parent 4a9da0a commit bb6820a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 46 deletions.
4 changes: 3 additions & 1 deletion examples/get-binary-files-xhr2.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
//========================= //=========================
var xhr1 = new XMLHttpRequest(); var xhr1 = new XMLHttpRequest();
xhr1.open('GET', '../test/ref/text.zip', true); xhr1.open('GET', '../test/ref/text.zip', true);
xhr1.overrideMimeType('text/plain; charset=x-user-defined'); if (xhr1.overrideMimeType) {
xhr1.overrideMimeType('text/plain; charset=x-user-defined');
}


xhr1.onreadystatechange = function(e) { xhr1.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ <h3>Browser support</h3>
<td>Tested with 3.0 / 3.6 / latest version</td> <td>Tested with 3.0 / 3.6 / latest version</td>
<td>Tested with the latest version</td> <td>Tested with the latest version</td>
<td>Tested with the latest version</td> <td>Tested with the latest version</td>
<td>Tested with IE 6 / 7 / 8 / 9</td> <td>Tested with IE 6 / 7 / 8 / 9 / 10</td>
</tr> </tr>
</table> </table>


Expand Down
137 changes: 93 additions & 44 deletions test/index.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
<title>JavaScript Zip Testing</title> <title>JavaScript Zip Testing</title>
<link media="screen" href="qunit.css" type="text/css" rel="stylesheet"> <link media="screen" href="qunit.css" type="text/css" rel="stylesheet">


<!--
Only used to avoid cross browser xhr madness.
-->
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>

<script type="text/javascript" src="../jszip.js"></script> <script type="text/javascript" src="../jszip.js"></script>
<script type="text/javascript" src="../jszip-load.js"></script> <script type="text/javascript" src="../jszip-load.js"></script>
<script type="text/javascript" src="../jszip-deflate.js"></script> <script type="text/javascript" src="../jszip-deflate.js"></script>
Expand Down Expand Up @@ -45,7 +40,7 @@
<script type="text/javascript"> <script type="text/javascript">
// IE-specific logic here // IE-specific logic here
// helper to convert from responseBody to a "responseText" like thing // helper to convert from responseBody to a "responseText" like thing
window.getBinaryFromXHR = function (responseText, xhr) { window.getBinaryFromXHR = function (xhr) {
var binary = xhr.responseBody; var binary = xhr.responseBody;
var byteMapping = {}; var byteMapping = {};
for ( var i = 0; i < 256; i++ ) { for ( var i = 0; i < 256; i++ ) {
Expand All @@ -68,17 +63,19 @@
// https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data // https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data
// Note : if you just want to give it to JSZip.load, you can skip this part // Note : if you just want to give it to JSZip.load, you can skip this part
// (this transformation is already done there) and just return responseText. // (this transformation is already done there) and just return responseText.
window.getBinaryFromXHR = window.getBinaryFromXHR || function (responseText, xhr) { window.getBinaryFromXHR = window.getBinaryFromXHR || function (xhr) {
// we skip the & 0xFF part, since this must be tested in the JSZip tests. // we skip the & 0xFF part, since this must be tested in the JSZip tests.
return responseText; if (xhr.response) {
if (JSZip.support.arraybuffer && xhr.response instanceof ArrayBuffer) {
return JSZip.utils.uint8Array2String(new Uint8Array(xhr.response));
}
return xhr.response;
} else {
return xhr.responseText;
}
// if you check this method to know how to load binay data, remove this return :) // if you check this method to know how to load binay data, remove this return :)


var result = ""; return JSZip.utils.string2binary(xhr.responseText);
for (var i = 0; i < responseText.length; i++) {
var code = responseText.charCodeAt(i) & 0xff;
result += String.fromCharCode(code);
}
return result;
}; };
</script> </script>


Expand Down Expand Up @@ -121,43 +118,95 @@
testFunction.call(this, refZips[zipName]); testFunction.call(this, refZips[zipName]);
} else { } else {
stop(); stop();

/* /*
* Here is the tricky part : getting the data. * Here is the tricky part : getting the data.
* In firefox/chrome/opera/... setting the mimeType to 'text/plain; charset=x-user-defined' * In firefox/chrome/opera/... setting the mimeType to 'text/plain; charset=x-user-defined'
* is enough, the result is in the standard xhr.responseText. * is enough, the result is in the standard xhr.responseText.
* cf https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data_in_older_browsers * cf https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data_in_older_browsers
* In IE, we must use (the IE only) attribute responseBody * In IE <= 9, we must use (the IE only) attribute responseBody
* (for binary data, its content is different from responseText). * (for binary data, its content is different from responseText).
* With jQuery 1.5+, we don't have access directly to the native xhr * In IE 10, the 'charset=x-user-defined' trick doesn't work, only the
* object in the callback. * responseType will work :
* The workaround is to use the default method of jQuery to create it, * http://msdn.microsoft.com/en-us/library/ie/hh673569%28v=vs.85%29.aspx#Binary_Object_upload_and_download
* put the xhr into a context accessible in the callbacks and define *
* a custom method for getting the xhr object (will return the known xhr). * I'd like to use jQuery to avoid this XHR madness, but it doesn't support
* the responseType attribute : http://bugs.jquery.com/ticket/11461
*/ */
var context = {
xhr:jQuery.ajaxSettings.xhr() // taken from jQuery
}; function createStandardXHR() {
$.ajax(zipName, { try {
cache: false, return new window.XMLHttpRequest();
context: context, } catch( e ) {}
xhr:function(){return context.xhr}, }
mimeType:'text/plain; charset=x-user-defined'
}) function createActiveXHR() {
.done(function(data, textStatus, jqXHR){ try {
start(); return new window.ActiveXObject("Microsoft.XMLHTTP");
try { } catch( e ) {}
// this.xhr comes from the context }
var file = getBinaryFromXHR(jqXHR.responseText, this.xhr);
refZips[zipName] = file; var isLocal = window.location.protocol === "file:";
testFunction.call(this, file);
} catch(e) { // Create the request object
ok(false, e.message||e) var createXHR = window.ActiveXObject ?
} /* Microsoft failed to properly
}) * implement the XMLHttpRequest in IE7 (can't request local files),
.fail(function(){ * so we use the ActiveXObject when it is available
start(); * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
ok(false, "Ajax error for " + zipName + " : " + arguments); * we need a fallback.
}); */
function() {
return !isLocal && createStandardXHR() || createActiveXHR();
} :
// For all other browsers, use the standard XMLHttpRequest object
createStandardXHR;



try {

var xhr = createXHR();

xhr.open('GET', zipName + "?_=" + ( new Date() ).getTime(), true);

// recent browsers
if ("responseType" in xhr) {
xhr.responseType = "arraybuffer";
}

// older browser
if(xhr.overrideMimeType) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}

xhr.onreadystatechange = function(e) {
// use `xhr` and not `this`... thanks IE
if (xhr.readyState === 4) {
if (xhr.status === 200 || isLocal) {
start();
try {
// this.xhr comes from the context
var file = getBinaryFromXHR(xhr);
refZips[zipName] = file;
testFunction.call(this, file);
} catch(e) {
ok(false, e.message||e);
}
} else {
start();
ok(false, "Ajax error for " + zipName + " : " + this.status);
}
}
};

xhr.send();

} catch (e) {
start();
ok(false, "Ajax error for " + zipName + " : " + (e.message||e));
}
} }
}); });
} }
Expand Down

0 comments on commit bb6820a

Please sign in to comment.