Skip to content

Commit

Permalink
path: fix path.relative() for prefixes at device root
Browse files Browse the repository at this point in the history
Fixes #5485
  • Loading branch information
omsmith committed Feb 29, 2016
1 parent 3c79bbd commit d1e13c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
34 changes: 22 additions & 12 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ const win32 = {
// We get here if `from` is the exact base path for `to`.
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
return toOrig.slice(toStart + i + 1);
} else if (lastCommonSep === 2) {
} else if (i === 2) {
// We get here if `from` is the device root.
// For example: from='C:\\'; to='C:\\foo'
return toOrig.slice(toStart + i);
Expand All @@ -629,7 +629,7 @@ const win32 = {
// We get here if `to` is the exact base path for `from`.
// For example: from='C:\\foo\\bar'; to='C:\\foo'
lastCommonSep = i;
} else if (lastCommonSep === 2) {
} else if (i === 2) {
// We get here if `to` is the device root.
// For example: from='C:\\foo\\bar'; to='C:\\'
lastCommonSep = 3;
Expand Down Expand Up @@ -1300,16 +1300,26 @@ const posix = {
var i = 0;
for (; i <= length; ++i) {
if (i === length) {
if (lastCommonSep === -1) {
lastCommonSep = i;
} else if (toLen > length && to.charCodeAt(i + 1) === 47/*/*/) {
// We get here if `from` is the exact base path for `to`.
// For example: from='/foo/bar'; to='/foo/bar/baz'
return to.slice(i + 2);
} else if (fromLen > length && from.charCodeAt(i + 1) === 47/*/*/) {
// We get here if `to` is the exact base path for `from`.
// For example: from='/foo/bar/baz'; to='/foo/bar'
lastCommonSep = i;
if (toLen > length) {
if (to.charCodeAt(toStart + i) === 47/*/*/) {
// We get here if `from` is the exact base path for `to`.
// For example: from='/foo/bar'; to='/foo/bar/baz'
return to.slice(toStart + i + 1);
} else if (i === 0) {
// We get here if `from` is the root
// For example: from='/'; to='/foo'
return to.slice(toStart + 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;
}
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,11 @@ const relativeTests = [
['\\\\foo\\bar', '\\\\foo\\bar\\baz', 'baz'],
['\\\\foo\\bar\\baz', '\\\\foo\\bar', '..'],
['\\\\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,
Expand All @@ -490,7 +494,9 @@ const relativeTests = [
['/foo/test', '/foo/test/bar/package.json', 'bar/package.json'],
['/Users/a/web/b/test/mails', '/Users/a/web/b', '../..'],
['/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']
]
]
];
Expand Down

0 comments on commit d1e13c2

Please sign in to comment.