Permalink
Browse files

Move RetinaImagePath.set_options to #configure() method on Retina obj…

…ect, provides an interface for overriding default configuration. Update tests.
  • Loading branch information...
1 parent 40dc758 commit 415b1e1557c743cc105a48d1df7834349d3ef9a5 Casey O'Hara committed Oct 8, 2012
Showing with 59 additions and 45 deletions.
  1. +33 −34 src/retina.js
  2. +10 −8 test/retina.test.js
  3. +16 −3 test/retina_image_path.test.js
View
@@ -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; });
@@ -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) )
}
@@ -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);
@@ -60,8 +84,6 @@
-
-
function RetinaImage(el) {
this.el = el;
this.path = new RetinaImagePath(this.el.getAttribute('src'));
@@ -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);
}
View
@@ -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;
@@ -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");
@@ -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();
});
});

0 comments on commit 415b1e1

Please sign in to comment.