Move styleCodeEmpty to util
This commit is contained in:
parent
41ac66a137
commit
a004bc3c7d
|
@ -1,9 +1,8 @@
|
||||||
/* global getStyleWithNoCode styleSectionsEqual */
|
/*
|
||||||
|
global getStyleWithNoCode styleSectionsEqual styleCodeEmpty RX_CSS_COMMENTS
|
||||||
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const RX_NAMESPACE = /\s*(@namespace\s+(?:\S+\s+)?url\(http:\/\/.*?\);)\s*/g;
|
|
||||||
const RX_CHARSET = /\s*@charset\s+(['"]).*?\1\s*;\s*/g;
|
|
||||||
const RX_CSS_COMMENTS = /\/\*[\s\S]*?(?:\*\/|$)/g;
|
|
||||||
// eslint-disable-next-line no-var
|
// eslint-disable-next-line no-var
|
||||||
var SLOPPY_REGEXP_PREFIX = '\0';
|
var SLOPPY_REGEXP_PREFIX = '\0';
|
||||||
|
|
||||||
|
@ -527,26 +526,6 @@ function getApplicableSections({
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function styleCodeEmpty(code) {
|
|
||||||
// Collect the global section if it's not empty, not comment-only, not namespace-only.
|
|
||||||
const cmtOpen = code && code.indexOf('/*');
|
|
||||||
if (cmtOpen >= 0) {
|
|
||||||
const cmtCloseLast = code.lastIndexOf('*/');
|
|
||||||
if (cmtCloseLast < 0) {
|
|
||||||
code = code.substr(0, cmtOpen);
|
|
||||||
} else {
|
|
||||||
code = code.substr(0, cmtOpen) +
|
|
||||||
code.substring(cmtOpen, cmtCloseLast + 2).replace(RX_CSS_COMMENTS, '') +
|
|
||||||
code.substr(cmtCloseLast + 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!code || !code.trim()) return true;
|
|
||||||
if (code.includes('@namespace')) code = code.replace(RX_NAMESPACE, '').trim();
|
|
||||||
if (code.includes('@charset')) code = code.replace(RX_CHARSET, '').trim();
|
|
||||||
return !code;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function invalidateCache({added, updated, deletedId} = {}) {
|
function invalidateCache({added, updated, deletedId} = {}) {
|
||||||
if (!cachedStyles.list) return;
|
if (!cachedStyles.list) return;
|
||||||
const id = added ? added.id : updated ? updated.id : deletedId;
|
const id = added ? added.id : updated ? updated.id : deletedId;
|
||||||
|
|
24
background/util.js
Normal file
24
background/util.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const RX_NAMESPACE = /\s*(@namespace\s+(?:\S+\s+)?url\(http:\/\/.*?\);)\s*/g;
|
||||||
|
const RX_CHARSET = /\s*@charset\s+(['"]).*?\1\s*;\s*/g;
|
||||||
|
const RX_CSS_COMMENTS = /\/\*[\s\S]*?(?:\*\/|$)/g;
|
||||||
|
|
||||||
|
function styleCodeEmpty(code) {
|
||||||
|
// Collect the global section if it's not empty, not comment-only, not namespace-only.
|
||||||
|
const cmtOpen = code && code.indexOf('/*');
|
||||||
|
if (cmtOpen >= 0) {
|
||||||
|
const cmtCloseLast = code.lastIndexOf('*/');
|
||||||
|
if (cmtCloseLast < 0) {
|
||||||
|
code = code.substr(0, cmtOpen);
|
||||||
|
} else {
|
||||||
|
code = code.substr(0, cmtOpen) +
|
||||||
|
code.substring(cmtOpen, cmtCloseLast + 2).replace(RX_CSS_COMMENTS, '') +
|
||||||
|
code.substr(cmtCloseLast + 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!code || !code.trim()) return true;
|
||||||
|
if (code.includes('@namespace')) code = code.replace(RX_NAMESPACE, '').trim();
|
||||||
|
if (code.includes('@charset')) code = code.replace(RX_CHARSET, '').trim();
|
||||||
|
return !code;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user