Browse Source

path: fix path.relative() for prefixes at root

Fixes #5485

PR-URL: https://github.com/nodejs/node/pull/5490
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Brian White <mscdex@mscdex.net>
process-exit-stdio-flushing
Owen Smith 9 years ago
committed by Roman Reiss
parent
commit
f296a7f16f
  1. 34
      lib/path.js
  2. 10
      test/parallel/test-path.js

34
lib/path.js

@ -618,7 +618,7 @@ const win32 = {
// We get here if `from` is the exact base path for `to`. // We get here if `from` is the exact base path for `to`.
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz' // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
return toOrig.slice(toStart + i + 1); return toOrig.slice(toStart + i + 1);
} else if (lastCommonSep === 2) { } else if (i === 2) {
// We get here if `from` is the device root. // We get here if `from` is the device root.
// For example: from='C:\\'; to='C:\\foo' // For example: from='C:\\'; to='C:\\foo'
return toOrig.slice(toStart + i); return toOrig.slice(toStart + i);
@ -629,7 +629,7 @@ const win32 = {
// We get here if `to` is the exact base path for `from`. // We get here if `to` is the exact base path for `from`.
// For example: from='C:\\foo\\bar'; to='C:\\foo' // For example: from='C:\\foo\\bar'; to='C:\\foo'
lastCommonSep = i; lastCommonSep = i;
} else if (lastCommonSep === 2) { } else if (i === 2) {
// We get here if `to` is the device root. // We get here if `to` is the device root.
// For example: from='C:\\foo\\bar'; to='C:\\' // For example: from='C:\\foo\\bar'; to='C:\\'
lastCommonSep = 3; lastCommonSep = 3;
@ -1300,16 +1300,26 @@ const posix = {
var i = 0; var i = 0;
for (; i <= length; ++i) { for (; i <= length; ++i) {
if (i === length) { if (i === length) {
if (lastCommonSep === -1) { if (toLen > length) {
lastCommonSep = i; if (to.charCodeAt(toStart + i) === 47/*/*/) {
} else if (toLen > length && to.charCodeAt(i + 1) === 47/*/*/) { // We get here if `from` is the exact base path for `to`.
// We get here if `from` is the exact base path for `to`. // For example: from='/foo/bar'; to='/foo/bar/baz'
// For example: from='/foo/bar'; to='/foo/bar/baz' return to.slice(toStart + i + 1);
return to.slice(i + 2); } else if (i === 0) {
} else if (fromLen > length && from.charCodeAt(i + 1) === 47/*/*/) { // We get here if `from` is the root
// We get here if `to` is the exact base path for `from`. // For example: from='/'; to='/foo'
// For example: from='/foo/bar/baz'; to='/foo/bar' return to.slice(toStart + i);
lastCommonSep = i; }
} else if (fromLen > length) {
if (from.charCodeAt(fromStart + i) === 47/*/*/) {
// We get here if `to` is the exact base path for `from`.
// For example: from='/foo/bar/baz'; to='/foo/bar'
lastCommonSep = i;
} else if (i === 0) {
// We get here if `to` is the root.
// For example: from='/foo'; to='/'
lastCommonSep = 0;
}
} }
break; break;
} }

10
test/parallel/test-path.js

@ -476,7 +476,11 @@ const relativeTests = [
['\\\\foo\\bar', '\\\\foo\\bar\\baz', 'baz'], ['\\\\foo\\bar', '\\\\foo\\bar\\baz', 'baz'],
['\\\\foo\\bar\\baz', '\\\\foo\\bar', '..'], ['\\\\foo\\bar\\baz', '\\\\foo\\bar', '..'],
['\\\\foo\\bar\\baz-quux', '\\\\foo\\bar\\baz', '..\\baz'], ['\\\\foo\\bar\\baz-quux', '\\\\foo\\bar\\baz', '..\\baz'],
['\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz-quux', '..\\baz-quux'] ['\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz-quux', '..\\baz-quux'],
['C:\\baz-quux', 'C:\\baz', '..\\baz'],
['C:\\baz', 'C:\\baz-quux', '..\\baz-quux'],
['\\\\foo\\baz-quux', '\\\\foo\\baz', '..\\baz'],
['\\\\foo\\baz', '\\\\foo\\baz-quux', '..\\baz-quux']
] ]
], ],
[ path.posix.relative, [ path.posix.relative,
@ -490,7 +494,9 @@ const relativeTests = [
['/foo/test', '/foo/test/bar/package.json', 'bar/package.json'], ['/foo/test', '/foo/test/bar/package.json', 'bar/package.json'],
['/Users/a/web/b/test/mails', '/Users/a/web/b', '../..'], ['/Users/a/web/b/test/mails', '/Users/a/web/b', '../..'],
['/foo/bar/baz-quux', '/foo/bar/baz', '../baz'], ['/foo/bar/baz-quux', '/foo/bar/baz', '../baz'],
['/foo/bar/baz', '/foo/bar/baz-quux', '../baz-quux'] ['/foo/bar/baz', '/foo/bar/baz-quux', '../baz-quux'],
['/baz-quux', '/baz', '../baz'],
['/baz', '/baz-quux', '../baz-quux']
] ]
] ]
]; ];

Loading…
Cancel
Save