From 763be51c607eceb3c468e9ecc8dcf7625f55f352 Mon Sep 17 00:00:00 2001 From: John Gozde Date: Sun, 27 Jul 2014 16:41:59 -0600 Subject: [PATCH] Handle empty root in util.relative. Re #128, an empty root passed to util.relative should return 'aPath' as-is. This avoids stripping the leading '/' off of absolute paths. This treats root paths of '' and '.' the same. Root path of '/' will strip the leading '/' from any path. --- lib/source-map/util.js | 4 ++++ test/source-map/test-util.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/source-map/util.js b/lib/source-map/util.js index 3bc13640..976f6cab 100644 --- a/lib/source-map/util.js +++ b/lib/source-map/util.js @@ -192,6 +192,10 @@ define(function (require, exports, module) { * @param aPath The path or URL to be made relative to aRoot. */ function relative(aRoot, aPath) { + if (aRoot === "") { + aRoot = "."; + } + aRoot = aRoot.replace(/\/$/, ''); // XXX: It is possible to remove this block, and the tests still pass! diff --git a/test/source-map/test-util.js b/test/source-map/test-util.js index 44f7b7f6..997d1a26 100644 --- a/test/source-map/test-util.js +++ b/test/source-map/test-util.js @@ -203,6 +203,14 @@ define(function (require, exports, module) { exports['test relative()'] = function (assert, util) { assert.equal(libUtil.relative('/the/root', '/the/root/one.js'), 'one.js'); assert.equal(libUtil.relative('/the/root', '/the/rootone.js'), '/the/rootone.js'); + + assert.equal(libUtil.relative('', '/the/root/one.js'), '/the/root/one.js'); + assert.equal(libUtil.relative('.', '/the/root/one.js'), '/the/root/one.js'); + assert.equal(libUtil.relative('', 'the/root/one.js'), 'the/root/one.js'); + assert.equal(libUtil.relative('.', 'the/root/one.js'), 'the/root/one.js'); + + assert.equal(libUtil.relative('/', '/the/root/one.js'), 'the/root/one.js'); + assert.equal(libUtil.relative('/', 'the/root/one.js'), 'the/root/one.js'); }; });