path: fix win32 relative() for UNC paths
win32 normalize() will output a trailing '\' for some UNC paths. trim them before processing Change by @mscdex Add basic UNC path tests to win32 relative() PR-URL: https://github.com/nodejs/node/pull/5456 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Roman Reiss <me@silverwind.io>
This commit is contained in:
parent
b33879d9e2
commit
e326950498
14
lib/path.js
14
lib/path.js
@ -585,7 +585,12 @@ const win32 = {
|
|||||||
if (from.charCodeAt(fromStart) !== 92/*\*/)
|
if (from.charCodeAt(fromStart) !== 92/*\*/)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// Trim trailing backslashes (applicable to UNC paths only)
|
||||||
var fromEnd = from.length;
|
var fromEnd = from.length;
|
||||||
|
for (; fromEnd - 1 > fromStart; --fromEnd) {
|
||||||
|
if (from.charCodeAt(fromEnd - 1) !== 92/*\*/)
|
||||||
|
break;
|
||||||
|
}
|
||||||
var fromLen = (fromEnd - fromStart);
|
var fromLen = (fromEnd - fromStart);
|
||||||
|
|
||||||
// Trim any leading backslashes
|
// Trim any leading backslashes
|
||||||
@ -594,7 +599,12 @@ const win32 = {
|
|||||||
if (to.charCodeAt(toStart) !== 92/*\*/)
|
if (to.charCodeAt(toStart) !== 92/*\*/)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// Trim trailing backslashes (applicable to UNC paths only)
|
||||||
var toEnd = to.length;
|
var toEnd = to.length;
|
||||||
|
for (; toEnd - 1 > toStart; --toEnd) {
|
||||||
|
if (to.charCodeAt(toEnd - 1) !== 92/*\*/)
|
||||||
|
break;
|
||||||
|
}
|
||||||
var toLen = (toEnd - toStart);
|
var toLen = (toEnd - toStart);
|
||||||
|
|
||||||
// Compare paths to find the longest common path from root
|
// Compare paths to find the longest common path from root
|
||||||
@ -662,12 +672,12 @@ const win32 = {
|
|||||||
// Lastly, append the rest of the destination (`to`) path that comes after
|
// Lastly, append the rest of the destination (`to`) path that comes after
|
||||||
// the common path parts
|
// the common path parts
|
||||||
if (out.length > 0)
|
if (out.length > 0)
|
||||||
return out + toOrig.slice(toStart + lastCommonSep);
|
return out + toOrig.slice(toStart + lastCommonSep, toEnd);
|
||||||
else {
|
else {
|
||||||
toStart += lastCommonSep;
|
toStart += lastCommonSep;
|
||||||
if (toOrig.charCodeAt(toStart) === 92/*\*/)
|
if (toOrig.charCodeAt(toStart) === 92/*\*/)
|
||||||
++toStart;
|
++toStart;
|
||||||
return toOrig.slice(toStart);
|
return toOrig.slice(toStart, toEnd);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -471,7 +471,12 @@ const relativeTests = [
|
|||||||
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
|
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
|
||||||
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
|
['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-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,
|
[ path.posix.relative,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user