Enhance: allow matcher to return verbose info

This commit is contained in:
eight 2018-10-12 03:10:26 +08:00
parent 0ea7ada48f
commit 3ae0c4dd13

View File

@ -16,9 +16,10 @@ const styleManager = (() => {
const preparing = prepare(); const preparing = prepare();
const styles = new Map(); const styles = new Map();
const cachedStyleForUrl = createCache(); const cachedStyleForUrl = createCache();
const compiledRe = createCache();
const compiledExclusion = createCache();
const BAD_MATCHER = {test: () => false}; const BAD_MATCHER = {test: () => false};
const compileRe = createCompiler(text => `^(${text})$`);
const compileSloppyRe = createCompiler(text => `^${text}$`);
const compileExclusion = createCompiler(buildGlob);
handleLivePreviewConnections(); handleLivePreviewConnections();
@ -345,15 +346,13 @@ const styleManager = (() => {
// TODO: report excluded styles and sloppy regexps? // TODO: report excluded styles and sloppy regexps?
function getAppliedCode(url, data) { function getAppliedCode(url, data) {
if (!urlMatchStyle(url, data)) { if (urlMatchStyle(url, data) !== true) {
return; return;
} }
let code = ''; let code = '';
for (const section of data.sections) { for (const section of data.sections) {
if (urlMatchSection(url, section)) { if (urlMatchSection(url, section) === true && !styleCodeEmpty(section.code)) {
if (!styleCodeEmpty(section.code)) { code += section.code;
code += section.code;
}
} }
} }
return code; return code;
@ -378,9 +377,8 @@ const styleManager = (() => {
} }
function urlMatchStyle(url, style) { function urlMatchStyle(url, style) {
// TODO: show excluded style in popup?
if (style.exclusions && style.exclusions.some(e => compileExclusion(e).test(url))) { if (style.exclusions && style.exclusions.some(e => compileExclusion(e).test(url))) {
return false; return 'excluded';
} }
return true; return true;
} }
@ -406,6 +404,9 @@ const styleManager = (() => {
if (section.regexps && section.regexps.some(r => compileRe(r).test(url))) { if (section.regexps && section.regexps.some(r => compileRe(r).test(url))) {
return true; return true;
} }
if (section.regexps && section.regexps.some(r => compileSloppyRe(r).test(url))) {
return 'sloppy';
}
if ( if (
(!section.regexps || !section.regexps.length) && (!section.regexps || !section.regexps.length) &&
(!section.urlPrefixes || !section.urlPrefixes.length) && (!section.urlPrefixes || !section.urlPrefixes.length) &&
@ -417,28 +418,19 @@ const styleManager = (() => {
return false; return false;
} }
function compileRe(text) { function createCompiler(compile) {
let re = compiledRe.get(text); const cache = createCache();
if (!re) { return text => {
re = tryRegExp(`^(${text})$`); let re = cache.get(text);
if (!re) { if (!re) {
re = BAD_MATCHER; re = tryRegExp(compile(text));
if (!re) {
re = BAD_MATCHER;
}
cache.set(text, re);
} }
compiledRe.set(text, re); return re;
} };
return re;
}
function compileExclusion(text) {
let re = compiledExclusion.get(text);
if (!re) {
re = tryRegExp(buildGlob(text));
if (!re) {
re = BAD_MATCHER;
}
compiledExclusion.set(text, re);
}
return re;
} }
function buildGlob(text) { function buildGlob(text) {