Permalink
Browse files

allow for default options to be set in one place.

included a function to manually set those options without changing the
source, but this is just useful for testing. tests are included.

Ultimately I think having to explicitly call a `new RetinaSwap( /*
options */ )` or `Retina.load( /* options */ )` is the way to go _if_
configuration is commonly needed, but that requires a larger change and
I'm not sure it is desired.

No matter what strategy is adopted, this may at least provide the
underlying functionality.

@caseyohara is this headed in the right direction?
  • Loading branch information...
1 parent 85f73c4 commit f2139b2b787874313ef57e9a091da32a4431b303 @zackdever zackdever committed Sep 1, 2012
Showing with 27 additions and 3 deletions.
  1. +15 −3 src/retina.js
  2. +12 −0 test/retina_image_path.test.js
View
18 src/retina.js
@@ -2,6 +2,9 @@
var root = (typeof exports == 'undefined' ? window : exports);
+ var default_options = {
+ check_mime_type: true
+ };
function RetinaImagePath(path) {
this.path = path;
@@ -12,6 +15,13 @@
RetinaImagePath.confirmed_paths = [];
+ // only for testing because this automatically starts when loaded.
+ // https://github.com/imulus/retinajs/pull/45#issuecomment-8037131
+ RetinaImagePath.set_options = function(options) {
+ if (options == null) options = {};
+ for (var prop in options) default_options[prop] = options[prop];
+ }
+
RetinaImagePath.prototype.is_external = function() {
return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) )
}
@@ -31,9 +41,11 @@
}
if (http.status >= 200 && http.status <= 399) {
- var type = http.getResponseHeader('Content-Type');
- if (type == null || !type.match(/^image/i)) {
- return callback(false);
+ if (default_options.check_mime_type) {
+ var type = http.getResponseHeader('Content-Type');
+ if (type == null || !type.match(/^image/i)) {
+ return callback(false);
+ }
}
RetinaImagePath.confirmed_paths.push(that.at_2x_path);
View
12 test/retina_image_path.test.js
@@ -115,6 +115,18 @@ describe('RetinaImagePath', function() {
});
});
+ it('should callback with true when content-type is wrong, but check_mime_type is false', function(done) {
+ XMLHttpRequest.status = 200; // simulate a proper request
+ XMLHttpRequest.contentType = 'text/html'; // but with an incorrect content type
+ global.RetinaImagePath.set_options({check_mime_type: false}); // but ignore it
+ path = new RetinaImagePath("/images/some_image.png");
+ path.check_2x_variant(function(hasVariant) {
+ hasVariant.should.equal(true);
+ global.RetinaImagePath.set_options({check_mime_type: true});
+ done();
+ });
+ });
+
it('should callback with true when remote at2x image exists', function(done) {
XMLHttpRequest.status = 200; // simulate a proper request
XMLHttpRequest.contentType = 'image/png'; // simulate a proper content type

0 comments on commit f2139b2

Please sign in to comment.