Change: move styleCodeEmpty to sections-util, load colorConverter in background worker

This commit is contained in:
eight 2018-09-26 09:34:57 +08:00
parent d5ade807f0
commit ab0ef239cf
5 changed files with 28 additions and 33 deletions

View File

@ -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));

View File

@ -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;
}

View File

@ -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

View File

@ -163,7 +163,7 @@
<script src="manage/import-export.js" async></script>
<script src="manage/incremental-search.js" async></script>
<script src="msgbox/msgbox.js" async></script>
<script src="js/sections-equal.js" async></script>
<script src="js/sections-util.js" async></script>
<script src="js/storage-util.js" async></script>
</head>

View File

@ -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",