From bdae1c3697c698a84417886b0bfaaa54c975f5d4 Mon Sep 17 00:00:00 2001 From: eight Date: Thu, 11 Oct 2018 20:00:25 +0800 Subject: [PATCH] Change: simpler styleCodeEmpty --- background/background.js | 2 +- background/style-manager.js | 14 ++------------ edit/edit.js | 4 ++-- edit/linter-meta.js | 2 +- edit/sections-editor.js | 2 +- js/meta-parser.js | 4 ++-- js/sections-util.js | 25 ++++++------------------- js/usercss.js | 2 +- js/worker-util.js | 4 ++-- 9 files changed, 18 insertions(+), 41 deletions(-) diff --git a/background/background.js b/background/background.js index f5dbce9a..208fb7e5 100644 --- a/background/background.js +++ b/background/background.js @@ -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 diff --git a/background/style-manager.js b/background/style-manager.js index 653ad29d..a30f2d66 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -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; diff --git a/edit/edit.js b/edit/edit.js index a0bad638..42fa5980 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -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({ diff --git a/edit/linter-meta.js b/edit/linter-meta.js index 99ab04f6..bd5b0399 100644 --- a/edit/linter-meta.js +++ b/edit/linter-meta.js @@ -1,4 +1,4 @@ -/* global linter API editorWorker */ +/* global linter editorWorker */ /* exported createMetaCompiler */ 'use strict'; diff --git a/edit/sections-editor.js b/edit/sections-editor.js index 1fdb39b3..0734221d 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -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 */ diff --git a/js/meta-parser.js b/js/meta-parser.js index e660effb..2f5bec83 100644 --- a/js/meta-parser.js +++ b/js/meta-parser.js @@ -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 = { diff --git a/js/sections-util.js b/js/sections-util.js index e85c4ea6..79413a7c 100644 --- a/js/sections-util.js +++ b/js/sections-util.js @@ -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; } /** diff --git a/js/usercss.js b/js/usercss.js index 3d2bb5a8..5d58da59 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -1,4 +1,4 @@ -/* global loadScript semverCompare colorConverter styleCodeEmpty backgroundWorker */ +/* global backgroundWorker */ /* exported usercss */ 'use strict'; diff --git a/js/worker-util.js b/js/worker-util.js index 5e081959..4e226a27 100644 --- a/js/worker-util.js +++ b/js/worker-util.js @@ -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};