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
8 changes: 8 additions & 0 deletions packages/shared/src/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import {
} from "./preview.ts";

describe("resolveAddressBarInput", () => {
it.each([
["localhost:3000/?next=https://example.com", "http://localhost:3000/?next=https://example.com"],
["127.0.0.1:3000/#https://example.com", "http://127.0.0.1:3000/#https://example.com"],
["example.com/https://example.org", "https://example.com/https://example.org"],
])("opens the outer address when it contains another URL: %s", (input, expected) => {
expect(resolveAddressBarInput(input)).toBe(expected);
});

it("opens what users type as addresses", () => {
expect(resolveAddressBarInput("localhost:5173")).toBe("http://localhost:5173/");
expect(resolveAddressBarInput("127.0.0.1:3000/path")).toBe("http://127.0.0.1:3000/path");
Expand Down
5 changes: 3 additions & 2 deletions packages/shared/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function previewUrlProtocol(rawUrl: string): string | undefined {
const SEARCH_URL = "https://duckduckgo.com/?q=";
const ADDRESS_HOST_PATTERN = /^(?:localhost|\d{1,3}(?:\.\d{1,3}){3}|\[[\da-f:.]+\])$/i;
const BARE_HOST_PORT_PATTERN = /^[^/?#:@]+:\d+(?:[/?#]|$)/;
const URL_SCHEME_WITH_AUTHORITY_PATTERN = /^[a-z][a-z\d+.-]*:\/\//i;
const KNOWN_NON_WEB_SCHEMES: ReadonlySet<string> = new Set([
"about:",
"blob:",
Expand All @@ -68,7 +69,7 @@ const KNOWN_NON_WEB_SCHEMES: ReadonlySet<string> = new Set([

export function resolveAddressBarInput(rawInput: string): string {
const trimmed = rawInput.trim();
if (/^[a-z][a-z\d+.-]*:\/\//i.test(trimmed) || trimmed.length === 0)
if (URL_SCHEME_WITH_AUTHORITY_PATTERN.test(trimmed) || trimmed.length === 0)
return normalizePreviewUrl(trimmed);
if (!/\s/.test(trimmed)) {
// `mailto:x` or `data:...` is an explicit scheme; `example.com:8080` or
Expand Down Expand Up @@ -106,7 +107,7 @@ export function normalizePreviewUrl(rawUrl: string): string {
throw new PreviewUrlNormalizationError({ inputLength: rawUrl.length, reason: "empty" });
}
const useHttp = LOOPBACK_PREFIX_PATTERN.test(trimmed);
const candidate = trimmed.includes("://")
const candidate = URL_SCHEME_WITH_AUTHORITY_PATTERN.test(trimmed)
? trimmed
: `${useHttp ? "http" : "https"}://${trimmed}`;
let parsed: URL;
Expand Down
Loading