From ab0ef239cf234fd4b6cefc2eed6e8fb442478069 Mon Sep 17 00:00:00 2001 From: eight Date: Wed, 26 Sep 2018 09:34:57 +0800 Subject: [PATCH] Change: move styleCodeEmpty to sections-util, load colorConverter in background worker --- background/background-worker.js | 9 +++------ background/util.js | 24 ------------------------ js/sections-equal.js | 23 +++++++++++++++++++++++ manage.html | 2 +- manifest.json | 3 +-- 5 files changed, 28 insertions(+), 33 deletions(-) delete mode 100644 background/util.js diff --git a/background/background-worker.js b/background/background-worker.js index a1de9e76..630c33b0 100644 --- a/background/background-worker.js +++ b/background/background-worker.js @@ -1,4 +1,4 @@ -/* global workerUtil importScripts */ +/* global workerUtil importScripts parseMozFormat metaParser styleCodeEmpty colorConverter */ 'use strict'; importScripts('/js/worker-util.js'); @@ -6,13 +6,11 @@ const {loadScript, createAPI} = workerUtil; createAPI({ parseMozFormat(arg) { - /* global parseMozFormat */ loadScript('/vendor-overwrites/csslint/parserlib.js', '/js/moz-parser.js'); return parseMozFormat(arg); }, compileUsercss, parseUsercssMeta(text, indexOffset = 0) { - /* global metaParser */ loadScript( '/vendor/usercss-meta/usercss-meta.min.js', '/vendor-overwrites/colorpicker/colorconverter.js', @@ -21,7 +19,6 @@ createAPI({ return metaParser.parse(text, indexOffset); }, nullifyInvalidVars(vars) { - /* global metaParser */ loadScript( '/vendor/usercss-meta/usercss-meta.min.js', '/vendor-overwrites/colorpicker/colorconverter.js', @@ -73,11 +70,10 @@ function compileUsercss(preprocessor, code, vars) { } function getUsercssCompiler(preprocessor) { - /* global colorConverter styleCodeEmpty */ const BUILDER = { default: { postprocess(sections, vars) { - loadScript('/background/util.js'); + loadScript('/js/sections-util.js'); let varDef = Object.keys(vars).map(k => ` --${k}: ${vars[k].value};\n`).join(''); if (!varDef) return; varDef = ':root {\n' + varDef + '}\n'; @@ -120,6 +116,7 @@ function getUsercssCompiler(preprocessor) { }, uso: { preprocess(source, vars) { + loadScript('/vendor-overwrites/colorpicker/colorconverter.js'); const pool = new Map(); return Promise.resolve(doReplace(source)); diff --git a/background/util.js b/background/util.js deleted file mode 100644 index 1bd2f0f5..00000000 --- a/background/util.js +++ /dev/null @@ -1,24 +0,0 @@ -'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; -} diff --git a/js/sections-equal.js b/js/sections-equal.js index d01c4bfe..95dcf68f 100644 --- a/js/sections-equal.js +++ b/js/sections-equal.js @@ -1,5 +1,28 @@ '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; +} + /** * @param {Style} a - first style object * @param {Style} b - second style object diff --git a/manage.html b/manage.html index df581f3f..6c3bf5ab 100644 --- a/manage.html +++ b/manage.html @@ -163,7 +163,7 @@ - + diff --git a/manifest.json b/manifest.json index fe679032..e0c393c0 100644 --- a/manifest.json +++ b/manifest.json @@ -25,9 +25,8 @@ "scripts": [ "js/messaging.js", "js/storage-util.js", - "js/sections-equal.js", + "js/sections-util.js", "js/worker-util.js", - "background/util.js", "background/storage-dummy.js", "background/storage.js", "js/prefs.js",