Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,12 @@ const win32 = {
if (from.charCodeAt(fromStart) !== 92/*\*/)
break;
}
// Trim trailing backslashes (applicable to UNC paths only)
var fromEnd = from.length;
for (; fromEnd - 1 > fromStart; --fromEnd) {
if (from.charCodeAt(fromEnd - 1) !== 92/*\*/)
break;
}
var fromLen = (fromEnd - fromStart);

// Trim any leading backslashes
Expand All @@ -594,7 +599,12 @@ const win32 = {
if (to.charCodeAt(toStart) !== 92/*\*/)
break;
}
// Trim trailing backslashes (applicable to UNC paths only)
var toEnd = to.length;
for (; toEnd - 1 > toStart; --toEnd) {
if (to.charCodeAt(toEnd - 1) !== 92/*\*/)
break;
}
var toLen = (toEnd - toStart);

// Compare paths to find the longest common path from root
Expand All @@ -603,14 +613,28 @@ const win32 = {
var i = 0;
for (; i <= length; ++i) {
if (i === length) {
if (lastCommonSep > 2 && // ignore separator match following device root
toLen > length &&
to.charCodeAt(i) === 92/*\*/) {
// 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(i + 1);
if (toLen > length) {
if (to.charCodeAt(toStart + i) === 92/*\*/) {
// 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) {
// We get here if `from` is the device root.
// For example: from='C:\\'; to='C:\\foo'
return toOrig.slice(toStart + i);
}
}
if (fromLen > length) {
if (from.charCodeAt(fromStart + i) === 92/*\*/) {
// 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) {
// We get here if `to` is the device root.
// For example: from='C:\\foo\\bar'; to='C:\\'
lastCommonSep = 3;
}
}
lastCommonSep = i;
break;
}
var fromCode = from.charCodeAt(fromStart + i);
Expand Down Expand Up @@ -648,12 +672,12 @@ const win32 = {
// Lastly, append the rest of the destination (`to`) path that comes after
// the common path parts
if (out.length > 0)
return out + toOrig.slice(toStart + lastCommonSep);
return out + toOrig.slice(toStart + lastCommonSep, toEnd);
else {
toStart += lastCommonSep;
if (toOrig.charCodeAt(toStart) === 92/*\*/)
++toStart;
return toOrig.slice(toStart);
return toOrig.slice(toStart, toEnd);
}
},

Expand Down
12 changes: 10 additions & 2 deletions test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,13 @@ const relativeTests = [
['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''],
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json']
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json'],
['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz'],
['C:\\foo\\bar\\baz', 'C:\\foo\\bar\\baz-quux', '..\\baz-quux'],
['\\\\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']
]
],
[ path.posix.relative,
Expand All @@ -482,7 +488,9 @@ const relativeTests = [
['/var/', '/var/lib', 'lib'],
['/', '/var/lib', 'var/lib'],
['/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', '/foo/bar/baz-quux', '../baz-quux']
]
]
];
Expand Down