From 3d32b0428bee83b04bc4f813e1f4a49efc2ca488 Mon Sep 17 00:00:00 2001 From: eight Date: Tue, 25 Sep 2018 21:38:40 +0800 Subject: [PATCH] Fix: vars might be empty --- background/background-worker.js | 27 ++++++++++++++------------- js/usercss.js | 3 +++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/background/background-worker.js b/background/background-worker.js index 65e82888..2d767962 100644 --- a/background/background-worker.js +++ b/background/background-worker.js @@ -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; } diff --git a/js/usercss.js b/js/usercss.js index 0d196165..afb0ea47 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -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) {