Skip to content
This repository
Browse code

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...
commit f2139b2b787874313ef57e9a091da32a4431b303 1 parent 85f73c4
Zack Dever zever authored

Showing 2 changed files with 27 additions and 3 deletions. Show diff stats Hide diff stats

  1. +15 3 src/retina.js
  2. +12 0 test/retina_image_path.test.js
18 src/retina.js
@@ -2,6 +2,9 @@
2 2
3 3 var root = (typeof exports == 'undefined' ? window : exports);
4 4
  5 + var default_options = {
  6 + check_mime_type: true
  7 + };
5 8
6 9 function RetinaImagePath(path) {
7 10 this.path = path;
@@ -12,6 +15,13 @@
12 15
13 16 RetinaImagePath.confirmed_paths = [];
14 17
  18 + // only for testing because this automatically starts when loaded.
  19 + // https://github.com/imulus/retinajs/pull/45#issuecomment-8037131
  20 + RetinaImagePath.set_options = function(options) {
  21 + if (options == null) options = {};
  22 + for (var prop in options) default_options[prop] = options[prop];
  23 + }
  24 +
15 25 RetinaImagePath.prototype.is_external = function() {
16 26 return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) )
17 27 }
@@ -31,9 +41,11 @@
31 41 }
32 42
33 43 if (http.status >= 200 && http.status <= 399) {
34   - var type = http.getResponseHeader('Content-Type');
35   - if (type == null || !type.match(/^image/i)) {
36   - return callback(false);
  44 + if (default_options.check_mime_type) {
  45 + var type = http.getResponseHeader('Content-Type');
  46 + if (type == null || !type.match(/^image/i)) {
  47 + return callback(false);
  48 + }
37 49 }
38 50
39 51 RetinaImagePath.confirmed_paths.push(that.at_2x_path);
12 test/retina_image_path.test.js
@@ -115,6 +115,18 @@ describe('RetinaImagePath', function() {
115 115 });
116 116 });
117 117
  118 + it('should callback with true when content-type is wrong, but check_mime_type is false', function(done) {
  119 + XMLHttpRequest.status = 200; // simulate a proper request
  120 + XMLHttpRequest.contentType = 'text/html'; // but with an incorrect content type
  121 + global.RetinaImagePath.set_options({check_mime_type: false}); // but ignore it
  122 + path = new RetinaImagePath("/images/some_image.png");
  123 + path.check_2x_variant(function(hasVariant) {
  124 + hasVariant.should.equal(true);
  125 + global.RetinaImagePath.set_options({check_mime_type: true});
  126 + done();
  127 + });
  128 + });
  129 +
118 130 it('should callback with true when remote at2x image exists', function(done) {
119 131 XMLHttpRequest.status = 200; // simulate a proper request
120 132 XMLHttpRequest.contentType = 'image/png'; // simulate a proper content type

0 comments on commit f2139b2

Please sign in to comment.
Something went wrong with that request. Please try again.