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