Skip to content

Commit

Permalink
Move RetinaImagePath.set_options to #configure() method on Retina obj…
Browse files Browse the repository at this point in the history
…ect, provides an interface for overriding default configuration. Update tests.
  • Loading branch information
Casey O'Hara committed Oct 8, 2012
1 parent 40dc758 commit 415b1e1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 45 deletions.
67 changes: 33 additions & 34 deletions src/retina.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@

var root = (typeof exports == 'undefined' ? window : exports);

var default_options = {
var config = {
// Ensure Content-Type is an image before trying to load @2x image
// https://github.com/imulus/retinajs/pull/45)
check_mime_type: true
};



root.Retina = Retina;

function Retina() {}

Retina.configure = function(options) {
if (options == null) options = {};
for (var prop in options) config[prop] = options[prop];
};

Retina.init = function(context) {
if (context == null) context = root;

var existing_onload = context.onload || new Function;

context.onload = function() {
var images = document.getElementsByTagName("img"), retinaImages = [], i, image;
for (i = 0; i < images.length; i++) {
image = images[i];
retinaImages.push(new RetinaImage(image));
}
existing_onload();
}
}




function RetinaImagePath(path) {
this.path = path;
this.at_2x_path = path.replace(/\.\w+$/, function(match) { return "@2x" + match; });
Expand All @@ -15,13 +46,6 @@

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) )
}
Expand All @@ -41,7 +65,7 @@
}

if (http.status >= 200 && http.status <= 399) {
if (default_options.check_mime_type) {
if (config.check_mime_type) {
var type = http.getResponseHeader('Content-Type');
if (type == null || !type.match(/^image/i)) {
return callback(false);
Expand All @@ -60,8 +84,6 @@





function RetinaImage(el) {
this.el = el;
this.path = new RetinaImagePath(this.el.getAttribute('src'));
Expand Down Expand Up @@ -91,29 +113,6 @@





function Retina() {}

root.Retina = Retina;

Retina.init = function(context){
if (context == null) context = root;

var existing_onload = context.onload || new Function;

context.onload = function() {
var images = document.getElementsByTagName("img"), retinaImages = [], i, image;
for (i = 0; i < images.length; i++) {
image = images[i];
retinaImages.push(new RetinaImage(image));
}
existing_onload();
}
}



if (root.devicePixelRatio > 1) {
Retina.init(root);
}
Expand Down
18 changes: 10 additions & 8 deletions test/retina.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Create a document object because we don't have one
// in our Node test environment
delete global.document;

global.document = {
// stub out the getElementsByTagName method
getElementsByTagName : function(){
return [];
}
}

global.document = {};

var Retina = require('../').Retina;

describe('Retina', function() {

before(function(){
// stub out the getElementsByTagName method
global.document = {
getElementsByTagName : function(){
return [];
}
}
});

describe('init', function(){
it('stashes the existing onload and executes it later', function(){
var existingOnloadExecutions = 0;
Expand Down
19 changes: 16 additions & 3 deletions test/retina_image_path.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// Create a document object because we don't have one
// in our Node test environment
delete global.document;
global.document = {domain: null};
global.document = {};
global.Image = require('./fixtures/image').Image;
global.XMLHttpRequest = require('./fixtures/xml_http_request').XMLHttpRequest;
global.Retina = require('../').Retina;
global.RetinaImage = require('../').RetinaImage;
global.RetinaImagePath = require('../').RetinaImagePath;


describe('RetinaImagePath', function() {
var path = null;

before(function(){
global.document = {domain: null};
});

describe('@at_2x_path', function(){
it('adds "@2x" before the extension', function(){
path = new RetinaImagePath("/path/to/image.png");
Expand Down Expand Up @@ -119,11 +124,19 @@ 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

Retina.configure({
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});

Retina.configure({
check_mime_type: true
});

done();
});
});
Expand Down

0 comments on commit 415b1e1

Please sign in to comment.