Fix: handle invalid URLs

This commit is contained in:
eight 2019-03-18 23:27:29 +08:00
parent a16cf9aa41
commit dd3334ed21

View File

@ -45,6 +45,21 @@ const styleManager = (() => {
const compileSloppyRe = createCompiler(text => `^${text}$`); const compileSloppyRe = createCompiler(text => `^${text}$`);
const compileExclusion = createCompiler(buildGlob); const compileExclusion = createCompiler(buildGlob);
const DUMMY_URL = {
hash: '',
host: '',
hostname: '',
href: '',
origin: '',
password: '',
pathname: '',
port: '',
protocol: '',
search: '',
searchParams: new URLSearchParams(),
username: ''
};
handleLivePreviewConnections(); handleLivePreviewConnections();
return ensurePrepared({ return ensurePrepared({
@ -555,18 +570,26 @@ const styleManager = (() => {
}, },
get urlWithoutParams() { get urlWithoutParams() {
if (!urlWithoutParams) { if (!urlWithoutParams) {
const u = new URL(url); const u = createURL(url);
urlWithoutParams = u.origin + u.pathname; urlWithoutParams = u.origin + u.pathname;
} }
return urlWithoutParams; return urlWithoutParams;
}, },
get domain() { get domain() {
if (!domain) { if (!domain) {
const u = new URL(url); const u = createURL(url);
domain = u.hostname; domain = u.hostname;
} }
return domain; return domain;
} }
}; };
} }
function createURL(url) {
try {
return new URL(url);
} catch (err) {
return DUMMY_URL;
}
}
})(); })();