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 /* global detectSloppyRegexps download prefs openURL FIREFOX CHROME VIVALDI
openEditor debounce URLS ignoreChromeError queryTabs getTab openEditor debounce URLS ignoreChromeError queryTabs getTab
usercss styleManager db msg navigatorUtil iconUtil */ styleManager db msg navigatorUtil iconUtil workerUtil */
'use strict'; 'use strict';
// eslint-disable-next-line no-var // eslint-disable-next-line no-var

View File

@ -1,5 +1,5 @@
/* eslint no-eq-null: 0, eqeqeq: [2, "smart"] */ /* eslint no-eq-null: 0, eqeqeq: [2, "smart"] */
/* global createCache db calcStyleDigest db tryRegExp /* global createCache db calcStyleDigest db tryRegExp styleCodeEmpty
getStyleWithNoCode msg */ getStyleWithNoCode msg */
/* exported styleManager */ /* exported styleManager */
'use strict'; 'use strict';
@ -351,7 +351,7 @@ const styleManager = (() => {
let code = ''; let code = '';
for (const section of data.sections) { for (const section of data.sections) {
if (urlMatchSection(url, section)) { if (urlMatchSection(url, section)) {
if (!isCodeEmpty(section.code)) { if (!styleCodeEmpty(section.code)) {
code += section.code; code += section.code;
} }
} }
@ -359,16 +359,6 @@ const styleManager = (() => {
return code; 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() { function prepare() {
return db.exec('getAll').then(event => { return db.exec('getAll').then(event => {
const styleList = event.target.result; const styleList = event.target.result;

View File

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

View File

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

View File

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

View File

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

View File

@ -1,27 +1,14 @@
/* exported styleSectionsEqual */ /* exported styleSectionsEqual styleCodeEmpty */
'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;
function styleCodeEmpty(code) { function styleCodeEmpty(code) {
// Collect the global section if it's not empty, not comment-only, not namespace-only. const rx = /\s+|\/\*[\s\S]*?\*\/|@namespace[^;]+;|@charset[^;]+;/giy;
const cmtOpen = code && code.indexOf('/*'); while (rx.exec(code)) {
if (cmtOpen >= 0) { if (rx.lastIndex === code.length) {
const cmtCloseLast = code.lastIndexOf('*/'); return true;
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; return false;
if (code.includes('@namespace')) code = code.replace(RX_NAMESPACE, '').trim();
if (code.includes('@charset')) code = code.replace(RX_CHARSET, '').trim();
return !code;
} }
/** /**

View File

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

View File

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