Skip to content

Commit 246915c

Browse files
committed
fix convert URLs
See #17
1 parent 3b3338c commit 246915c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/utils/absolute-urls.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ function absolute(path, $) {
4040

4141
if (~src.indexOf('://')) {
4242
return;
43-
} else if (src[0] == '/') {
44-
src = remote + src;
4543
} else {
46-
src = remote + '/' + parts.pathname.replace(/^\//, '') + '/' + src
44+
var current = url.resolve(remote, parts.pathname);
45+
src = url.resolve(current, src);
4746
}
4847

4948
$el.attr(key, src);

test/x-ray.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var isArray = Array.isArray;
1010
var subs = require('subs');
1111
var xray = require('..');
1212
var fs = require('fs');
13+
var cheerio = require('cheerio');
14+
var absolute = require('../lib/utils/absolute-urls');
1315

1416
/**
1517
* Tests
@@ -252,6 +254,26 @@ describe('x-ray', function() {
252254
done();
253255
})
254256
})
257+
258+
describe('absolute URLs', function(){
259+
var $el;
260+
var path = 'http://example.com/foo.html';
261+
262+
it('should not convert URL', function(){
263+
$el = cheerio.load('<a href="http://example.com/bar.html"></a>');
264+
assert.equal('<a href="http://example.com/bar.html"></a>', absolute(path, $el).html());
265+
});
266+
267+
it('should convert absolute URL', function(){
268+
$el = cheerio.load('<a href="/bar.html"></a>');
269+
assert.equal('<a href="http://example.com/bar.html"></a>', absolute(path, $el).html());
270+
});
271+
272+
it('should convert relative URL', function(){
273+
$el = cheerio.load('<a href="bar.html"></a>');
274+
assert.equal('<a href="http://example.com/bar.html"></a>', absolute(path, $el).html());
275+
});
276+
});
255277
})
256278

257279
/**

0 commit comments

Comments
 (0)