only global sections are skipped if empty/comment/namespace

This commit is contained in:
tophf 2017-05-05 17:21:17 +03:00
parent 71f4a53b41
commit d3d3a1f9b5

View File

@ -14,7 +14,6 @@ var cachedStyles = {
filters: new Map(), // filterStyles() parameters mapped to the returned results, 10k max filters: new Map(), // filterStyles() parameters mapped to the returned results, 10k max
regexps: new Map(), // compiled style regexps regexps: new Map(), // compiled style regexps
urlDomains: new Map(), // getDomain() results for 100 last checked urls urlDomains: new Map(), // getDomain() results for 100 last checked urls
emptyCode: new Map(), // entire code is comments/whitespace/@namespace
mutex: { mutex: {
inProgress: false, // while getStyles() is reading IndexedDB all subsequent calls inProgress: false, // while getStyles() is reading IndexedDB all subsequent calls
onDone: [], // to getStyles() are queued and resolved when the first one finishes onDone: [], // to getStyles() are queued and resolved when the first one finishes
@ -331,16 +330,17 @@ function getApplicableSections({style, matchUrl, strictRegexp = true, stopOnFirs
const sections = []; const sections = [];
for (const section of style.sections) { for (const section of style.sections) {
const {urls, domains, urlPrefixes, regexps, code} = section; const {urls, domains, urlPrefixes, regexps, code} = section;
if ((!urls.length && !urlPrefixes.length && !domains.length && !regexps.length const isGlobal = !urls.length && !urlPrefixes.length && !domains.length && !regexps.length;
|| urls.length const isMatching = !isGlobal && (
urls.length
&& urls.indexOf(matchUrl) >= 0 && urls.indexOf(matchUrl) >= 0
|| urlPrefixes.length || urlPrefixes.length
&& arraySomeIsPrefix(urlPrefixes, matchUrl) && arraySomeIsPrefix(urlPrefixes, matchUrl)
|| domains.length || domains.length
&& arraySomeIn(cachedStyles.urlDomains.get(matchUrl) || getDomains(matchUrl), domains) && arraySomeIn(cachedStyles.urlDomains.get(matchUrl) || getDomains(matchUrl), domains)
|| regexps.length || regexps.length
&& arraySomeMatches(regexps, matchUrl, strictRegexp) && arraySomeMatches(regexps, matchUrl, strictRegexp));
) && !styleCodeEmpty(code)) { if (isGlobal && !styleCodeEmpty(code) || isMatching) {
sections.push(section); sections.push(section);
if (stopOnFirst) { if (stopOnFirst) {
break; break;
@ -396,20 +396,21 @@ function getApplicableSections({style, matchUrl, strictRegexp = true, stopOnFirs
function styleCodeEmpty(code) { function styleCodeEmpty(code) {
// Collect the section if not empty or namespace-only. // Collect the global section if it's not empty, not comment-only, not namespace-only.
// We don't check long code as it's slow both for emptyCode declared as Object const cmtOpen = code && code.indexOf('/*');
// and as Map in case the string is not the same reference used to add the item if (cmtOpen >= 0) {
let isEmpty = code !== null && const cmtCloseLast = code.lastIndexOf('*/');
code.length < 1000 && if (cmtCloseLast < 0) {
cachedStyles.emptyCode.get(code); code = code.substr(0, cmtOpen);
if (isEmpty !== undefined) { } else {
return isEmpty; code = code.substr(0, cmtOpen) +
code.substring(cmtOpen, cmtCloseLast + 2).replace(RX_CSS_COMMENTS, '') +
code.substr(cmtCloseLast + 2);
} }
isEmpty = !code || !code.trim() }
|| code.indexOf('@namespace') >= 0 return !code
&& code.replace(RX_CSS_COMMENTS, '').replace(RX_NAMESPACE, '').trim() == ''; || !code.trim()
cachedStyles.emptyCode.set(code, isEmpty); || code.includes('@namespace') && !code.replace(RX_NAMESPACE, '').trim();
return isEmpty;
} }