From 246915c46f7880bb4a1015775a4b0a6c99632a12 Mon Sep 17 00:00:00 2001 From: Hiroki Sato Date: Wed, 8 Apr 2015 15:09:57 +0900 Subject: [PATCH] fix convert URLs See https://github.com/lapwinglabs/x-ray/issues/17 --- lib/utils/absolute-urls.js | 5 ++--- test/x-ray.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/utils/absolute-urls.js b/lib/utils/absolute-urls.js index 4e84dc5..1a81f6b 100644 --- a/lib/utils/absolute-urls.js +++ b/lib/utils/absolute-urls.js @@ -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); diff --git a/test/x-ray.js b/test/x-ray.js index 4a3f6c1..0716e98 100644 --- a/test/x-ray.js +++ b/test/x-ray.js @@ -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 @@ -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(''); + assert.equal('', absolute(path, $el).html()); + }); + + it('should convert absolute URL', function(){ + $el = cheerio.load(''); + assert.equal('', absolute(path, $el).html()); + }); + + it('should convert relative URL', function(){ + $el = cheerio.load(''); + assert.equal('', absolute(path, $el).html()); + }); + }); }) /**