Files
bare-operating-system/packages/bare-os-coreutils/lib/summon-url.js
T
Raven Scott 15afc148d7
Release rolling / release (push) Successful in 9m30s
Updates
2026-08-13 00:13:03 -04:00

80 lines
1.8 KiB
JavaScript

/** URL parse / join for summon (about:, file:, http:, https:). */
var BARE_SUMMON_SCHEMES = {
http: true,
https: true,
file: true,
about: true
}
function bareSummonTrim(s) {
return String(s == null ? '' : s).trim()
}
function bareSummonParseUrl(raw) {
var s = bareSummonTrim(raw)
if (!s) return null
if (s.indexOf('://') < 0 && s.indexOf(':') > 0) {
var sch0 = s.slice(0, s.indexOf(':')).toLowerCase()
if (sch0 === 'about') {
return {
href: 'about:' + s.slice(s.indexOf(':') + 1),
protocol: 'about:',
host: '',
hostname: '',
port: '',
pathname: s.slice(s.indexOf(':') + 1) || 'blank',
search: '',
hash: '',
origin: 'about:'
}
}
}
try {
var u = new URL(s)
return {
href: u.href,
protocol: u.protocol,
host: u.host,
hostname: u.hostname,
port: u.port,
pathname: u.pathname || '/',
search: u.search,
hash: u.hash,
origin: u.origin
}
} catch (e) {
return null
}
}
function bareSummonResolveUrl(ref, base) {
var r = bareSummonTrim(ref)
if (!r) return bareSummonParseUrl(base)
if (/^javascript:/i.test(r)) return null
if (r.charAt(0) === '#') {
var b0 = bareSummonParseUrl(base)
if (!b0) return null
b0.hash = r
b0.href = b0.href.split('#')[0] + r
return b0
}
if (/^about:/i.test(r)) return bareSummonParseUrl(r)
try {
var u = base ? new URL(r, String(base)) : new URL(r)
return bareSummonParseUrl(u.href)
} catch (e2) {
return null
}
}
function bareSummonUrlOk(u) {
if (!u || !u.protocol) return false
var p = u.protocol.replace(/:$/, '')
return !!BARE_SUMMON_SCHEMES[p]
}
function bareSummonIsHttp(u) {
return u && (u.protocol === 'http:' || u.protocol === 'https:')
}