Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: fix win32.relative() for some Unicode paths #27662

Closed
wants to merge 1 commit into from
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
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');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't be NFC?

from = fromOrig.toLowerCase();
}
let toNormalized;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚙️ (optional) Let's use false rather than undefined since it's boolean type:

Suggested change
let toNormalized;
let toNormalized = false;

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