Skip to content

Commit

Permalink
Add XHR responseType tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed Jul 30, 2013
1 parent 8444f83 commit 2805f47
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ module.exports = function( grunt ) {
// Remove `define("modernizr-init" ...)` and `define("modernizr-build" ...)`
var mod = grunt.file.read(filepath).replace(/define\("modernizr-(init|build)", function\(\)\{\}\);/g, '');

// Hack the prefix into place. Anything is way to big for something so small.
// Hack the prefix into place. Anything is way too big for something so small.
if ( modConfig && modConfig.classPrefix ) {
mod = mod.replace("classPrefix : '',", "classPrefix : '" + modConfig.classPrefix.replace(/"/g, '\\"') + "',");
}
Expand Down
1 change: 1 addition & 0 deletions feature-detects/network/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/* DOC
Tests for server sent events aka eventsource.
*/
define(['Modernizr'], function( Modernizr ) {
Modernizr.addTest('eventsource', !!window.EventSource);
Expand Down
19 changes: 19 additions & 0 deletions feature-detects/network/xhr-responsetype-arraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
{
"name": "XMLHttpRequest xhr.responseType='arraybuffer'",
"property": "xhrresponsetypearraybuffer",
"tags": ["network"],
"notes": [{
"name": "XMLHttpRequest Living Standard",
"href": "http://xhr.spec.whatwg.org/#the-responsetype-attribute"
}]
}
!*/
/* DOC
Tests for XMLHttpRequest xhr.responseType='arraybuffer'.
*/
define(['Modernizr', 'testXhrType'], function( Modernizr, testXhrType ) {
Modernizr.addTest('xhrresponsetypearraybuffer', testXhrType('arraybuffer'));
});
19 changes: 19 additions & 0 deletions feature-detects/network/xhr-responsetype-blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
{
"name": "XMLHttpRequest xhr.responseType='blob'",
"property": "xhrresponsetypeblob",
"tags": ["network"],
"notes": [{
"name": "XMLHttpRequest Living Standard",
"href": "http://xhr.spec.whatwg.org/#the-responsetype-attribute"
}]
}
!*/
/* DOC
Tests for XMLHttpRequest xhr.responseType='blob'.
*/
define(['Modernizr', 'testXhrType'], function( Modernizr, testXhrType ) {
Modernizr.addTest('xhrresponsetypeblob', testXhrType('blob'));
});
19 changes: 19 additions & 0 deletions feature-detects/network/xhr-responsetype-document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
{
"name": "XMLHttpRequest xhr.responseType='document'",
"property": "xhrresponsetypedocument",
"tags": ["network"],
"notes": [{
"name": "XMLHttpRequest Living Standard",
"href": "http://xhr.spec.whatwg.org/#the-responsetype-attribute"
}]
}
!*/
/* DOC
Tests for XMLHttpRequest xhr.responseType='document'.
*/
define(['Modernizr', 'testXhrType'], function( Modernizr, testXhrType ) {
Modernizr.addTest('xhrresponsetypedocument', testXhrType('document'));
});
22 changes: 22 additions & 0 deletions feature-detects/network/xhr-responsetype-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*!
{
"name": "XMLHttpRequest xhr.responseType='json'",
"property": "xhrresponsetypejson",
"tags": ["network"],
"notes": [{
"name": "XMLHttpRequest Living Standard",
"href": "http://xhr.spec.whatwg.org/#the-responsetype-attribute"
},{
"name": "Explanation of xhr.responseType='json'",
"href": "http://mathiasbynens.be/notes/xhr-responsetype-json"
}]
}
!*/
/* DOC
Tests for XMLHttpRequest xhr.responseType='json'.
*/
define(['Modernizr', 'testXhrType'], function( Modernizr, testXhrType ) {
Modernizr.addTest('xhrresponsetypejson', testXhrType('json'));
});
19 changes: 19 additions & 0 deletions feature-detects/network/xhr-responsetype-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
{
"name": "XMLHttpRequest xhr.responseType='text'",
"property": "xhrresponsetypetext",
"tags": ["network"],
"notes": [{
"name": "XMLHttpRequest Living Standard",
"href": "http://xhr.spec.whatwg.org/#the-responsetype-attribute"
}]
}
!*/
/* DOC
Tests for XMLHttpRequest xhr.responseType='text'.
*/
define(['Modernizr', 'testXhrType'], function( Modernizr, testXhrType ) {
Modernizr.addTest('xhrresponsetypetext', testXhrType('text'));
});
26 changes: 26 additions & 0 deletions feature-detects/network/xhr-responsetype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*!
{
"name": "XMLHttpRequest xhr.responseType",
"property": "xhrresponsetype",
"tags": ["network"],
"notes": [{
"name": "XMLHttpRequest Living Standard",
"href": "http://xhr.spec.whatwg.org/#the-responsetype-attribute"
}]
}
!*/
/* DOC
Tests for XMLHttpRequest xhr.responseType.
*/
define(['Modernizr'], function( Modernizr ) {
Modernizr.addTest('xhrresponsetype', (function() {
if (typeof XMLHttpRequest == 'undefined') {
return false;
}
var xhr = new XMLHttpRequest();
xhr.open('get', '/', true);
return 'response' in xhr;
}()));
});
6 changes: 6 additions & 0 deletions lib/config-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
"test/network/connection",
"test/network/eventsource",
"test/network/xhr2",
"test/network/xhr-responsetype-arraybuffer",
"test/network/xhr-responsetype-blob",
"test/network/xhr-responsetype-document",
"test/network/xhr-responsetype-json",
"test/network/xhr-responsetype-text",
"test/network/xhr-responsetype",
"test/notification",
"test/pagevisibility-api",
"test/performance",
Expand Down
18 changes: 18 additions & 0 deletions src/testXhrType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
define(function() {
// http://mathiasbynens.be/notes/xhr-responsetype-json#comment-4
var testXhrType = function(type) {
if (typeof XMLHttpRequest == 'undefined') {
return false;
}
var xhr = new XMLHttpRequest();
xhr.open('get', '/', true);
try {
xhr.responseType = type;
} catch(error) {
return false;
}
return 'response' in xhr && xhr.responseType == type;
};

return testXhrType;
});

0 comments on commit 2805f47

Please sign in to comment.