simplify/modularize getApplicableSections

This commit is contained in:
tophf 2017-04-14 00:44:23 +03:00
parent 8f784a19d4
commit db1dc0df7b

View File

@ -246,42 +246,49 @@ function deleteStyle({id, notify = true}) {
function getApplicableSections({style, matchUrl, strictRegexp = true, stopOnFirst}) { function getApplicableSections({style, matchUrl, strictRegexp = true, stopOnFirst}) {
//let t0 = 0;
const sections = [];
checkingSections:
for (const section of style.sections) {
andCollect:
do {
// only http, https, file, ftp, and chrome-extension://OWN_EXTENSION_ID allowed
if (!matchUrl.startsWith('http') if (!matchUrl.startsWith('http')
&& !matchUrl.startsWith('ftp') && !matchUrl.startsWith('ftp')
&& !matchUrl.startsWith('file') && !matchUrl.startsWith('file')
&& !matchUrl.startsWith(URLS.ownOrigin)) { && !matchUrl.startsWith(URLS.ownOrigin)) {
continue checkingSections; return [];
} }
if (section.urls.length == 0 const sections = [];
&& section.domains.length == 0 for (const section of style.sections) {
&& section.urlPrefixes.length == 0 const {urls, domains, urlPrefixes, regexps, code} = section;
&& section.regexps.length == 0) { if ((!urls.length && !urlPrefixes.length && !domains.length && !regexps.length
break andCollect; || urls.length && urls.indexOf(matchUrl) >= 0
} || urlPrefixes.length && arraySomeIsPrefix(urlPrefixes, matchUrl)
if (section.urls.indexOf(matchUrl) != -1) { || domains.length && arraySomeIn(cachedStyles.urlDomains.get(matchUrl) || getDomains(matchUrl), domains)
break andCollect; || regexps.length && arraySomeMatches(regexps, matchUrl, strictRegexp)
} ) && !styleCodeEmpty(code)) {
for (const urlPrefix of section.urlPrefixes) { sections.push(section);
if (matchUrl.startsWith(urlPrefix)) { if (stopOnFirst) {
break andCollect; break;
}
}
if (section.domains.length) {
const urlDomains = cachedStyles.urlDomains.get(matchUrl) || getDomains(matchUrl);
for (const domain of urlDomains) {
if (section.domains.indexOf(domain) != -1) {
break andCollect;
} }
} }
} }
for (const regexp of section.regexps) { return sections;
function arraySomeIsPrefix(array, string) {
for (const prefix of array) {
if (string.startsWith(prefix)) {
return true;
}
}
return false;
}
function arraySomeIn(array, haystack) {
for (const el of array) {
if (haystack.indexOf(el) >= 0) {
return true;
}
}
return false;
}
function arraySomeMatches(array, matchUrl, strictRegexp) {
for (const regexp of array) {
for (let pass = 1; pass <= (strictRegexp ? 1 : 2); pass++) { for (let pass = 1; pass <= (strictRegexp ? 1 : 2); pass++) {
const cacheKey = pass == 1 ? regexp : SLOPPY_REGEXP_PREFIX + regexp; const cacheKey = pass == 1 ? regexp : SLOPPY_REGEXP_PREFIX + regexp;
let rx = cachedStyles.regexps.get(cacheKey); let rx = cachedStyles.regexps.get(cacheKey);
@ -299,35 +306,30 @@ function getApplicableSections({style, matchUrl, strictRegexp = true, stopOnFirs
} }
} }
if (rx.test(matchUrl)) { if (rx.test(matchUrl)) {
break andCollect; return true;
} }
} }
} }
continue checkingSections; return false;
} while (0); }
}
function styleCodeEmpty(code) {
// Collect the section if not empty or namespace-only. // Collect the section if not empty or namespace-only.
// We don't check long code as it's slow both for emptyCode declared as Object // We don't check long code as it's slow both for emptyCode declared as Object
// and as Map in case the string is not the same reference used to add the item // and as Map in case the string is not the same reference used to add the item
//const t0start = performance.now(); let isEmpty = code !== null &&
const code = section.code; code.length < 1000 &&
let isEmpty = code !== null && code.length < 1000 && cachedStyles.emptyCode.get(code); cachedStyles.emptyCode.get(code);
if (isEmpty === undefined) { if (isEmpty !== undefined) {
return isEmpty;
}
isEmpty = !code || !code.trim() isEmpty = !code || !code.trim()
|| code.indexOf('@namespace') >= 0 || code.indexOf('@namespace') >= 0
&& code.replace(RX_CSS_COMMENTS, '').replace(RX_NAMESPACE, '').trim() == ''; && code.replace(RX_CSS_COMMENTS, '').replace(RX_NAMESPACE, '').trim() == '';
cachedStyles.emptyCode.set(code, isEmpty); cachedStyles.emptyCode.set(code, isEmpty);
} return isEmpty;
//t0 += performance.now() - t0start;
if (!isEmpty) {
sections.push(section);
if (stopOnFirst) {
//t0 >= 0.1 && console.debug('%s emptyCode', t0.toFixed(1)); // eslint-disable-line no-unused-expressions
return sections;
}
}
}
//t0 >= 0.1 && console.debug('%s emptyCode', t0.toFixed(1)); // eslint-disable-line no-unused-expressions
return sections;
} }