Skip to content
Merged
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
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const PROTOCOL_RELATIVE_REGEX = /^([/\\]\s*){2,}[^/\\]/;
const PROTOCOL_SCRIPT_RE = /^[\s\0]*(blob|data|javascript|vbscript):$/i;
const TRAILING_SLASH_RE = /\/$|\/\?|\/#/;
const JOIN_LEADING_SLASH_RE = /^\.?\//;
const JOIN_SEGMENT_SPLIT_RE = /(?<!\/)\/(?!\/)/;

/**
* Check if a path starts with `./` or `../`.
Expand Down Expand Up @@ -355,11 +356,14 @@ export function joinRelativeURL(..._input: string[]): string {
if (!i || i === "/") {
continue;
}
for (const s of i.split("/")) {
for (const s of i.split(JOIN_SEGMENT_SPLIT_RE)) {
if (!s || s === ".") {
continue;
}
if (s === "..") {
if (segments.length === 1 && hasProtocol(segments[0])) {
continue;
}
segments.pop();
segmentsDepth--;
continue;
Expand Down
8 changes: 8 additions & 0 deletions test/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const joinURLTests = [
{ input: ["/", "./foo"], out: "/foo" },
{ input: ["/", "./foo/"], out: "/foo/" },
{ input: ["/", "./foo", "bar"], out: "/foo/bar" },
{
input: ["https://google.com/", "./foo", "/bar"],
out: "https://google.com/foo/bar",
},
] as const;

describe("joinURL", () => {
Expand All @@ -42,6 +46,10 @@ describe("joinRelativeURL", () => {
{ input: ["../a", "../../../b"], out: "../../b" },
{ input: ["../a", "../../../../b"], out: "../../../b" },
{ input: ["../a/", "../b"], out: "b" },
{
input: ["https://google.com/", "../foo"],
out: "https://google.com/foo",
},
];

for (const t of relativeTests) {
Expand Down