Skip to content

Commit

Permalink
fix(joinRelativeURL): handle base with protocol (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 15, 2024
1 parent fffbcd4 commit 3910926
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utils.ts
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
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

0 comments on commit 3910926

Please sign in to comment.