33 lines
1022 B
JavaScript
33 lines
1022 B
JavaScript
/*
|
|
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
|
|
* Source-Available Code / Dual-Licensed.
|
|
*
|
|
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
|
|
* Commercial or production use requires a valid paid license.
|
|
* See LICENSE file in repository root for details.
|
|
*/
|
|
|
|
export function safeAppUrl(value, fallback = "/") {
|
|
try {
|
|
const url = new URL(String(value || ""), location.origin);
|
|
if (url.origin !== location.origin || !["http:", "https:"].includes(url.protocol)) return fallback;
|
|
return `${url.pathname}${url.search}${url.hash}`;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function safePublicUrl(value, { allowMailto = true } = {}) {
|
|
const raw = String(value || "").trim();
|
|
if (!raw || raw.startsWith("//")) return "#";
|
|
try {
|
|
const url = new URL(raw, location.origin);
|
|
if (url.protocol === "mailto:" && allowMailto) return url.href;
|
|
if (!["http:", "https:"].includes(url.protocol)) return "#";
|
|
return url.href;
|
|
} catch {
|
|
return "#";
|
|
}
|
|
}
|
|
|