Skip to content

Commit

Permalink
fix: preserve protocol-relative state internally
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 5, 2024
1 parent 2c27cf5 commit e9bb5d6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { decode } from "./encoding";
import { hasProtocol } from "./utils";
const protocolRelative = Symbol.for("ufo:protocolRelative");
export interface ParsedURL {
protocol?: string;
host?: string;
Expand All @@ -8,6 +9,7 @@ export interface ParsedURL {
pathname: string;
hash: string;
search: string;
[protocolRelative]?: boolean;
}

export interface ParsedAuth {
Expand Down Expand Up @@ -64,6 +66,7 @@ export function parseURL(input = "", defaultProto?: string): ParsedURL {
pathname,
search,
hash,
[protocolRelative]: !protocol,
};
}

Expand Down Expand Up @@ -125,7 +128,7 @@ export function stringifyParsedURL(parsed: Partial<ParsedURL>): string {
const hash = parsed.hash || "";
const auth = parsed.auth ? parsed.auth + "@" : "";
const host = parsed.host || "";
const proto = parsed.protocol || host ? (parsed.protocol || "") + "//" : "";
const proto = parsed.protocol || parsed[protocolRelative] ? (parsed.protocol || "") + "//" : "";
return proto + auth + host + pathname + search + hash;
}

Expand Down
2 changes: 1 addition & 1 deletion test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe("parseURL", () => {

for (const t of tests) {
test(t.input.toString(), () => {
expect(parseURL(t.input)).toEqual(t.out);
expect(JSON.parse(JSON.stringify(parseURL(t.input)))).toEqual(t.out);
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe("stringifyParsedURL", () => {
},
{
input: { host: "google.com" },
out: "//google.com",
out: "google.com",
},
{
input: { protocol: "https:", host: "google.com" },
Expand Down

0 comments on commit e9bb5d6

Please sign in to comment.