Skip to content

Commit 18c56df

Browse files
BridgeARtargos
authored andcommitted
path: using .relative() should not return a trailing slash
Resolving a path against root with `path.relative()` should not include a trailing slash. Fixes: #28549 PR-URL: #28556 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
1 parent 17df75f commit 18c56df

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/path.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1090,11 +1090,16 @@ const posix = {
10901090
// For example: from='/'; to='/foo'
10911091
return to.slice(toStart + i);
10921092
}
1093-
} else if (fromLen > length &&
1094-
from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
1095-
// We get here if `to` is the exact base path for `from`.
1096-
// For example: from='/foo/bar/baz'; to='/foo/bar'
1097-
lastCommonSep = i;
1093+
} else if (fromLen > length) {
1094+
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
1095+
// We get here if `to` is the exact base path for `from`.
1096+
// For example: from='/foo/bar/baz'; to='/foo/bar'
1097+
lastCommonSep = i;
1098+
} else if (i === 0) {
1099+
// We get here if `to` is the root.
1100+
// For example: from='/foo/bar'; to='/'
1101+
lastCommonSep = 0;
1102+
}
10981103
}
10991104
}
11001105

test/parallel/test-path-relative.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const relativeTests = [
4747
['/foo/bar/baz-quux', '/foo/bar/baz', '../baz'],
4848
['/foo/bar/baz', '/foo/bar/baz-quux', '../baz-quux'],
4949
['/baz-quux', '/baz', '../baz'],
50-
['/baz', '/baz-quux', '../baz-quux']
50+
['/baz', '/baz-quux', '../baz-quux'],
51+
['/page1/page2/foo', '/', '../../..']
5152
]
5253
]
5354
];

0 commit comments

Comments
 (0)