2018-09-25 13:18:39 +00:00
|
|
|
/* global usercssMeta colorConverter */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var metaParser = (() => {
|
|
|
|
const parser = usercssMeta.createParser({
|
|
|
|
validateVar: {
|
|
|
|
select: state => {
|
|
|
|
if (state.value !== null && state.varResult.options.every(o => o.name !== state.value)) {
|
|
|
|
throw new Error('select value mismatch');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
color: state => {
|
|
|
|
if (state.value !== null) {
|
2018-09-25 13:56:38 +00:00
|
|
|
const color = colorConverter.parse(state.value);
|
|
|
|
if (!color) {
|
|
|
|
throw new Error(`invalid color: ${state.value}`);
|
|
|
|
}
|
|
|
|
state.value = colorConverter.format(color, 'rgb');
|
2018-09-25 13:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {parse, nullifyInvalidVars};
|
|
|
|
|
|
|
|
function parse(text, indexOffset) {
|
|
|
|
try {
|
|
|
|
return parser.parse(text);
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err.index === 'number') {
|
|
|
|
err.index += indexOffset;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function nullifyInvalidVars(vars) {
|
|
|
|
for (const va of Object.values(vars)) {
|
|
|
|
if (va.value === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
parser.validateVar(va);
|
|
|
|
} catch (err) {
|
|
|
|
va.value = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vars;
|
|
|
|
}
|
|
|
|
})();
|