Make exclusion rules work like match pattern and handle invalid URLs

This commit is contained in:
narcolepticinsomniac 2019-06-03 07:50:37 -04:00 committed by GitHub
parent 16854d0f3c
commit f7e789e4b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,7 +43,22 @@ const styleManager = (() => {
const BAD_MATCHER = {test: () => false}; const BAD_MATCHER = {test: () => false};
const compileRe = createCompiler(text => `^(${text})$`); const compileRe = createCompiler(text => `^(${text})$`);
const compileSloppyRe = createCompiler(text => `^${text}$`); const compileSloppyRe = createCompiler(text => `^${text}$`);
const compileExclusion = createCompiler(buildGlob); const compileExclusion = createCompiler(buildExclusion);
const DUMMY_URL = {
hash: '',
host: '',
hostname: '',
href: '',
origin: '',
password: '',
pathname: '',
port: '',
protocol: '',
search: '',
searchParams: new URLSearchParams(),
username: ''
};
handleLivePreviewConnections(); handleLivePreviewConnections();
@ -529,8 +544,22 @@ const styleManager = (() => {
}; };
} }
function buildGlob(text) { function compileGlob(text) {
return '^' + escapeRegExp(text).replace(/\\\\\\\*|\\\*/g, m => m.length > 2 ? m : '.*') + '$'; return escapeRegExp(text).replace(/\\\\\\\*|\\\*/g, m => m.length > 2 ? m : '.*');
}
function buildExclusion(text) {
// match pattern
const match = text.match(/^(\*|[\w-]+):\/\/(\*\.)?([\w.]+\/.*)/);
if (!match) {
return '^' + compileGlob(text) + '$';
}
return '^' +
(match[1] === '*' ? '[\\w-]+' : match[1]) +
'://' +
(match[2] ? '(?:[\\w.]+\\.)?' : '') +
compileGlob(match[3]) +
'$';
} }
// The md5Url provided by USO includes a duplicate "update" subdomain (see #523), // The md5Url provided by USO includes a duplicate "update" subdomain (see #523),
@ -555,18 +584,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;
}
}
})(); })();