Skip to content

Commit

Permalink
Use better @2x path method from based on pull #39
Browse files Browse the repository at this point in the history
  • Loading branch information
Casey O'Hara committed Aug 18, 2012
1 parent aeefad3 commit c1f2d04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
20 changes: 7 additions & 13 deletions src/retina.js
Expand Up @@ -4,21 +4,15 @@ var root = (typeof exports == 'undefined' ? window : exports);


function RetinaImagePath(path) { function RetinaImagePath(path) {
this.path = path; this.path = path;
// Split the file extension off the image path, this.at_2x_path = path.replace(/\.\w+$/, function(match) { return "@2x" + match; });
// and put it back together with @2x before the extension.
// "/path/to/image.png" => "/path/to/image@2x.png"
var path_segments = this.path.split('.');
var path_without_extension = path_segments.slice(0, (path_segments.length - 1)).join(".");
var extension = path_segments[path_segments.length - 1];
this.at_2x_path = path_without_extension + "@2x." + extension;
} }

root.RetinaImagePath = RetinaImagePath; root.RetinaImagePath = RetinaImagePath;

// Class variable [Array] // Class variable [Array]
// cache of files we've checked on the server // cache of files we've checked on the server
RetinaImagePath.confirmed_paths = []; RetinaImagePath.confirmed_paths = [];

// Function to test if image is external // Function to test if image is external
RetinaImagePath.prototype.is_external = function() { RetinaImagePath.prototype.is_external = function() {
return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) ) return !!(this.path.match(/^https?\:/i) && !this.path.match('//' + document.domain) )
Expand Down Expand Up @@ -75,7 +69,7 @@ root.RetinaImage = RetinaImage;
RetinaImage.prototype.swap = function(path) { RetinaImage.prototype.swap = function(path) {
if (typeof path == 'undefined') path = this.path.at_2x_path; if (typeof path == 'undefined') path = this.path.at_2x_path;


// We wrap this in a named self-executer so we can reference // We wrap this in a named self-executer so we can reference
// it in a setTimeout if the image has not loaded yet. // it in a setTimeout if the image has not loaded yet.
var that = this; var that = this;
function load() { function load() {
Expand All @@ -88,7 +82,7 @@ RetinaImage.prototype.swap = function(path) {
// and still have the script detect image loads reliably and efficiently. // and still have the script detect image loads reliably and efficiently.
setTimeout(load, 5); setTimeout(load, 5);
} else { } else {
// Once the the image has loaded we know we // Once the the image has loaded we know we
// can grab the proper dimensions of the original image // can grab the proper dimensions of the original image
// and go ahead and swap in the new image path and apply the dimensions // and go ahead and swap in the new image path and apply the dimensions
that.el.setAttribute('width', that.el.offsetWidth); that.el.setAttribute('width', that.el.offsetWidth);
Expand Down
19 changes: 13 additions & 6 deletions test/retina_image_path.test.js
Expand Up @@ -9,7 +9,14 @@ global.RetinaImagePath = require('../').RetinaImagePath;


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


describe('@at_2x_path', function(){
it('adds "@2x" before the extension', function(){
path = new RetinaImagePath("/path/to/image.png");
path.at_2x_path.should.equal("/path/to/image@2x.png");
});
});

describe('#is_external()', function() { describe('#is_external()', function() {
it('should return true when image path references a remote domain with www', function() { it('should return true when image path references a remote domain with www', function() {
document.domain = "www.apple.com"; document.domain = "www.apple.com";
Expand Down Expand Up @@ -40,7 +47,7 @@ describe('RetinaImagePath', function() {
path = new RetinaImagePath("http://google.com/images/some_image.png") path = new RetinaImagePath("http://google.com/images/some_image.png")
path.is_external().should.equal(true); path.is_external().should.equal(true);
}); });

it('should return true when image path has www and domain does not', function() { it('should return true when image path has www and domain does not', function() {
document.domain = "apple.com"; document.domain = "apple.com";
path = new RetinaImagePath("http://www.apple.com/images/some_image.png"); path = new RetinaImagePath("http://www.apple.com/images/some_image.png");
Expand All @@ -64,20 +71,20 @@ describe('RetinaImagePath', function() {
path = new RetinaImagePath("/images/some_image.png"); path = new RetinaImagePath("/images/some_image.png");
path.is_external().should.equal(false); path.is_external().should.equal(false);
}); });

it('should return false when image path is relative to localhost', function() { it('should return false when image path is relative to localhost', function() {
document.domain = "localhost"; document.domain = "localhost";
path = new RetinaImagePath("/images/some_image.png"); path = new RetinaImagePath("/images/some_image.png");
path.is_external().should.equal(false); path.is_external().should.equal(false);
}); });

it('should return false when image path has same domain as current site with www', function() { it('should return false when image path has same domain as current site with www', function() {
document.domain = "www.apple.com"; document.domain = "www.apple.com";
path = new RetinaImagePath("http://www.apple.com/images/some_image.png"); path = new RetinaImagePath("http://www.apple.com/images/some_image.png");
path.is_external().should.equal(false); path.is_external().should.equal(false);
}); });
}); });

describe('#check_2x_variant()', function() { describe('#check_2x_variant()', function() {
it('should callback with false when #is_external() is true', function(done) { it('should callback with false when #is_external() is true', function(done) {
document.domain = "www.apple.com"; document.domain = "www.apple.com";
Expand All @@ -96,7 +103,7 @@ describe('RetinaImagePath', function() {
done(); done();
}); });
}); });

it('should callback with true when remote at2x image exists', function(done) { it('should callback with true when remote at2x image exists', function(done) {
XMLHttpRequest.status = 200; // simulate a proper request XMLHttpRequest.status = 200; // simulate a proper request
path = new RetinaImagePath("/images/some_image.png"); path = new RetinaImagePath("/images/some_image.png");
Expand Down

0 comments on commit c1f2d04

Please sign in to comment.