Skip to content

Commit 3910926

Browse files
authored
fix(joinRelativeURL): handle base with protocol (#222)
1 parent fffbcd4 commit 3910926

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const PROTOCOL_RELATIVE_REGEX = /^([/\\]\s*){2,}[^/\\]/;
1414
const PROTOCOL_SCRIPT_RE = /^[\s\0]*(blob|data|javascript|vbscript):$/i;
1515
const TRAILING_SLASH_RE = /\/$|\/\?|\/#/;
1616
const JOIN_LEADING_SLASH_RE = /^\.?\//;
17+
const JOIN_SEGMENT_SPLIT_RE = /(?<!\/)\/(?!\/)/;
1718

1819
/**
1920
* Check if a path starts with `./` or `../`.
@@ -355,11 +356,14 @@ export function joinRelativeURL(..._input: string[]): string {
355356
if (!i || i === "/") {
356357
continue;
357358
}
358-
for (const s of i.split("/")) {
359+
for (const s of i.split(JOIN_SEGMENT_SPLIT_RE)) {
359360
if (!s || s === ".") {
360361
continue;
361362
}
362363
if (s === "..") {
364+
if (segments.length === 1 && hasProtocol(segments[0])) {
365+
continue;
366+
}
363367
segments.pop();
364368
segmentsDepth--;
365369
continue;

test/join.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const joinURLTests = [
1616
{ input: ["/", "./foo"], out: "/foo" },
1717
{ input: ["/", "./foo/"], out: "/foo/" },
1818
{ input: ["/", "./foo", "bar"], out: "/foo/bar" },
19+
{
20+
input: ["https://google.com/", "./foo", "/bar"],
21+
out: "https://google.com/foo/bar",
22+
},
1923
] as const;
2024

2125
describe("joinURL", () => {
@@ -42,6 +46,10 @@ describe("joinRelativeURL", () => {
4246
{ input: ["../a", "../../../b"], out: "../../b" },
4347
{ input: ["../a", "../../../../b"], out: "../../../b" },
4448
{ input: ["../a/", "../b"], out: "b" },
49+
{
50+
input: ["https://google.com/", "../foo"],
51+
out: "https://google.com/foo",
52+
},
4553
];
4654

4755
for (const t of relativeTests) {

0 commit comments

Comments
 (0)