Skip to content

Commit

Permalink
path: fix win32.relative() for some Unicode paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed May 12, 2019
1 parent 5dd6113 commit d02c703
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
41 changes: 33 additions & 8 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,26 @@ const win32 = {
if (from === to)
return '';

const fromOrig = win32.resolve(from);
const toOrig = win32.resolve(to);
let fromOrig = win32.resolve(from);
let toOrig = win32.resolve(to);

if (fromOrig === toOrig)
return '';

from = fromOrig.toLowerCase();
to = toOrig.toLowerCase();

if (fromOrig.length !== from.length) {
fromOrig = fromOrig.normalize('NFD');
from = fromOrig.toLowerCase();
}
let toNormalized;
if (toOrig.length !== to.length) {
toNormalized = true;
toOrig = toOrig.normalize('NFD');
to = toOrig.toLowerCase();
}

if (from === to)
return '';

Expand Down Expand Up @@ -493,18 +504,24 @@ const win32 = {
// return the original `to`.
if (i !== length) {
if (lastCommonSep === -1)
return toOrig;
return (toNormalized ? toOrig.normalize('NFC') : toOrig);
} else {
if (toLen > length) {
if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
// 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);
return (
toNormalized ?
toOrig.slice(toStart + i + 1).normalize('NFC') :
toOrig.slice(toStart + i + 1));
}
if (i === 2) {
// We get here if `from` is the device root.
// For example: from='C:\\'; to='C:\\foo'
return toOrig.slice(toStart + i);
return (
toNormalized ?
toOrig.slice(toStart + i).normalize('NFC') :
toOrig.slice(toStart + i));
}
}
if (fromLen > length) {
Expand Down Expand Up @@ -535,12 +552,20 @@ 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, toEnd)}`;
if (out.length > 0) {
const slice = (
toNormalized ?
toOrig.slice(toStart, toEnd).normalize('NFC') :
toOrig.slice(toStart, toEnd));
return `${out}${slice}`;
}

if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH)
++toStart;
return toOrig.slice(toStart, toEnd);
return (
toNormalized ?
toOrig.slice(toStart, toEnd).normalize('NFC') :
toOrig.slice(toStart, toEnd));
},

toNamespacedPath(path) {
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-path-relative.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ const relativeTests = [
['\\\\foo\\baz-quux', '\\\\foo\\baz', '..\\baz'],
['\\\\foo\\baz', '\\\\foo\\baz-quux', '..\\baz-quux'],
['C:\\baz', '\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz'],
['\\\\foo\\bar\\baz', 'C:\\baz', 'C:\\baz']
['\\\\foo\\bar\\baz', 'C:\\baz', 'C:\\baz'],
['c:\\a\\İ', 'c:\\a\\İ\\test.txt', 'test.txt'],
['c:\\İ\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'],
['c:\\İ\\a\\i̇', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'],
['c:\\i̇\\a\\İ', 'c:\\İ\\b\\İ\\test.txt', '..\\..\\b\\İ\\test.txt'],
['c:\\ß\\a\\ß', 'c:\\ß\\b\\ß\\test.txt', '..\\..\\b\\ß\\test.txt']
]
],
[ path.posix.relative,
Expand Down

0 comments on commit d02c703

Please sign in to comment.