Change: simpler styleCodeEmpty

This commit is contained in:
eight 2018-10-11 20:00:25 +08:00
parent bd4a453f45
commit bdae1c3697
9 changed files with 18 additions and 41 deletions

View File

@ -1,6 +1,6 @@
/* global detectSloppyRegexps download prefs openURL FIREFOX CHROME VIVALDI
openEditor debounce URLS ignoreChromeError queryTabs getTab
usercss styleManager db msg navigatorUtil iconUtil */
styleManager db msg navigatorUtil iconUtil workerUtil */
'use strict';
// eslint-disable-next-line no-var

View File

@ -1,5 +1,5 @@
/* eslint no-eq-null: 0, eqeqeq: [2, "smart"] */
/* global createCache db calcStyleDigest db tryRegExp
/* global createCache db calcStyleDigest db tryRegExp styleCodeEmpty
getStyleWithNoCode msg */
/* exported styleManager */
'use strict';
@ -351,7 +351,7 @@ const styleManager = (() => {
let code = '';
for (const section of data.sections) {
if (urlMatchSection(url, section)) {
if (!isCodeEmpty(section.code)) {
if (!styleCodeEmpty(section.code)) {
code += section.code;
}
}
@ -359,16 +359,6 @@ const styleManager = (() => {
return code;
}
function isCodeEmpty(code) {
const rx = /\s+|\/\*[\s\S]*?\*\/|@namespace[^;]+;|@charset[^;]+;/giy;
while (rx.exec(code)) {
if (rx.lastIndex === code.length) {
return true;
}
}
return false;
}
function prepare() {
return db.exec('getAll').then(event => {
const styleList = event.target.result;

View File

@ -1,9 +1,9 @@
/* global CodeMirror onDOMready prefs setupLivePrefs $ $$ $create t tHTML
createSourceEditor queryTabs sessionStorageHash getOwnTab FIREFOX API tryCatch
closeCurrentTab messageBox debounce
closeCurrentTab messageBox debounce workerUtil
beautify
moveFocus msg createSectionsEditor rerouteHotkeys */
/* exported showCodeMirrorPopup */
/* exported showCodeMirrorPopup editorWorker */
'use strict';
const editorWorker = workerUtil.createWorker({

View File

@ -1,4 +1,4 @@
/* global linter API editorWorker */
/* global linter editorWorker */
/* exported createMetaCompiler */
'use strict';

View File

@ -1,7 +1,7 @@
/* global dirtyReporter showHelp prefs ignoreChromeError
CodeMirror propertyToCss
regExpTester linter createLivePreview showCodeMirrorPopup
sectionsToMozFormat editorWorker messageBox clipString beautify
sectionsToMozFormat messageBox clipString beautify
rerouteHotkeys cmFactory CssToProperty template $ $$ $create t FIREFOX API
debounce tryRegExp
*/

View File

@ -1,8 +1,8 @@
/* global usercssMeta colorConverter */
/* exported metaParser */
'use strict';
// eslint-disable-next-line no-var
var metaParser = (() => {
const metaParser = (() => {
const {createParser, ParseError} = usercssMeta;
const PREPROCESSORS = new Set(['default', 'uso', 'stylus', 'less']);
const options = {

View File

@ -1,27 +1,14 @@
/* exported styleSectionsEqual */
/* exported styleSectionsEqual styleCodeEmpty */
'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);
const rx = /\s+|\/\*[\s\S]*?\*\/|@namespace[^;]+;|@charset[^;]+;/giy;
while (rx.exec(code)) {
if (rx.lastIndex === code.length) {
return true;
}
}
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;
return false;
}
/**

View File

@ -1,4 +1,4 @@
/* global loadScript semverCompare colorConverter styleCodeEmpty backgroundWorker */
/* global backgroundWorker */
/* exported usercss */
'use strict';

View File

@ -1,8 +1,8 @@
/* global importScripts */
/* exported workerUtil */
'use strict';
// eslint-disable-next-line no-var
var workerUtil = (() => {
const workerUtil = (() => {
const loadedScripts = new Set();
return {createWorker, createAPI, loadScript, cloneError};