Skip to content

Commit

Permalink
fix convert URLs
Browse files Browse the repository at this point in the history
See #17
  • Loading branch information
skahack committed Apr 8, 2015
1 parent 3b3338c commit 246915c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/utils/absolute-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ function absolute(path, $) {

if (~src.indexOf('://')) {
return;
} else if (src[0] == '/') {
src = remote + src;
} else {
src = remote + '/' + parts.pathname.replace(/^\//, '') + '/' + src
var current = url.resolve(remote, parts.pathname);
src = url.resolve(current, src);
}

$el.attr(key, src);
Expand Down
22 changes: 22 additions & 0 deletions test/x-ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var isArray = Array.isArray;
var subs = require('subs');
var xray = require('..');
var fs = require('fs');
var cheerio = require('cheerio');
var absolute = require('../lib/utils/absolute-urls');

/**
* Tests
Expand Down Expand Up @@ -252,6 +254,26 @@ describe('x-ray', function() {
done();
})
})

describe('absolute URLs', function(){
var $el;
var path = 'http://example.com/foo.html';

it('should not convert URL', function(){
$el = cheerio.load('<a href="http://example.com/bar.html"></a>');
assert.equal('<a href="http://example.com/bar.html"></a>', absolute(path, $el).html());
});

it('should convert absolute URL', function(){
$el = cheerio.load('<a href="/bar.html"></a>');
assert.equal('<a href="http://example.com/bar.html"></a>', absolute(path, $el).html());
});

it('should convert relative URL', function(){
$el = cheerio.load('<a href="bar.html"></a>');
assert.equal('<a href="http://example.com/bar.html"></a>', absolute(path, $el).html());
});
});
})

/**
Expand Down

0 comments on commit 246915c

Please sign in to comment.