Fix: vars might be empty

This commit is contained in:
eight 2018-09-25 21:38:40 +08:00
parent 7d75dd8754
commit 3d32b0428b
2 changed files with 17 additions and 13 deletions

View File

@ -2,9 +2,7 @@
'use strict'; 'use strict';
importScripts('/js/worker-util.js'); importScripts('/js/worker-util.js');
const {loadScript, createAPI} = workerUtil; const {loadScript, createAPI} = workerUtil;
const BUILDER = usercssBuilder();
createAPI({ createAPI({
parseMozFormat(arg) { parseMozFormat(arg) {
@ -35,15 +33,7 @@ createAPI({
function compileUsercss(preprocessor, code, vars) { function compileUsercss(preprocessor, code, vars) {
loadScript('/vendor-overwrites/csslint/parserlib.js', '/js/moz-parser.js'); loadScript('/vendor-overwrites/csslint/parserlib.js', '/js/moz-parser.js');
let builder; const builder = getUsercssCompiler(preprocessor);
if (preprocessor) {
if (!BUILDER[preprocessor]) {
throw new Error('unknwon preprocessor');
}
builder = BUILDER[preprocessor];
} else {
builder = BUILDER.default;
}
vars = simpleVars(vars); vars = simpleVars(vars);
return Promise.resolve(builder.preprocess ? builder.preprocess(code, vars) : code) return Promise.resolve(builder.preprocess ? builder.preprocess(code, vars) : code)
.then(code => parseMozFormat({code})) .then(code => parseMozFormat({code}))
@ -55,6 +45,9 @@ function compileUsercss(preprocessor, code, vars) {
}); });
function simpleVars(vars) { function simpleVars(vars) {
if (!vars) {
return {};
}
// simplify vars by merging `va.default` to `va.value`, so BUILDER don't // simplify vars by merging `va.default` to `va.value`, so BUILDER don't
// need to test each va's default value. // need to test each va's default value.
return Object.keys(vars).reduce((output, key) => { return Object.keys(vars).reduce((output, key) => {
@ -79,9 +72,9 @@ function compileUsercss(preprocessor, code, vars) {
} }
} }
function usercssBuilder() { function getUsercssCompiler(preprocessor) {
/* global colorConverter styleCodeEmpty */ /* global colorConverter styleCodeEmpty */
return { const BUILDER = {
default: { default: {
postprocess(sections, vars) { postprocess(sections, vars) {
loadScript('/background/util.js'); loadScript('/background/util.js');
@ -166,4 +159,12 @@ function usercssBuilder() {
} }
} }
}; };
if (preprocessor) {
if (!BUILDER[preprocessor]) {
throw new Error('unknwon preprocessor');
}
return BUILDER[preprocessor];
}
return BUILDER.default;
} }

View File

@ -64,6 +64,9 @@ var usercss = (() => {
function assignVars(style, oldStyle) { function assignVars(style, oldStyle) {
const {usercssData: {vars}} = style; const {usercssData: {vars}} = style;
const {usercssData: {vars: oldVars}} = oldStyle; const {usercssData: {vars: oldVars}} = oldStyle;
if (!vars || !oldVars) {
return Promise.resolve();
}
// The type of var might be changed during the update. Set value to null if the value is invalid. // The type of var might be changed during the update. Set value to null if the value is invalid.
for (const key of Object.keys(vars)) { for (const key of Object.keys(vars)) {
if (oldVars[key] && oldVars[key].value) { if (oldVars[key] && oldVars[key].value) {