2018-09-25 13:18:39 +00:00
|
|
|
/* global usercssMeta colorConverter */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var metaParser = (() => {
|
2018-09-26 02:33:02 +00:00
|
|
|
const {createParser, ParseError} = usercssMeta;
|
|
|
|
const PREPROCESSORS = new Set(['default', 'uso', 'stylus', 'less']);
|
2018-10-01 15:14:56 +00:00
|
|
|
const options = {
|
2018-09-26 02:33:02 +00:00
|
|
|
validateKey: {
|
|
|
|
preprocessor: state => {
|
|
|
|
if (!PREPROCESSORS.has(state.value)) {
|
|
|
|
throw new ParseError({
|
|
|
|
code: 'unknownPreprocessor',
|
|
|
|
args: [state.value],
|
|
|
|
index: state.valueIndex
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-09-25 13:18:39 +00:00
|
|
|
validateVar: {
|
|
|
|
select: state => {
|
2018-09-26 02:33:02 +00:00
|
|
|
if (state.varResult.options.every(o => o.name !== state.value)) {
|
|
|
|
throw new ParseError({
|
|
|
|
code: 'invalidSelectValueMismatch',
|
|
|
|
index: state.valueIndex
|
|
|
|
});
|
2018-09-25 13:18:39 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
color: state => {
|
2018-09-26 02:33:02 +00:00
|
|
|
const color = colorConverter.parse(state.value);
|
|
|
|
if (!color) {
|
|
|
|
throw new ParseError({
|
|
|
|
code: 'invalidColor',
|
|
|
|
args: [state.value],
|
|
|
|
index: state.valueIndex
|
|
|
|
});
|
2018-09-25 13:18:39 +00:00
|
|
|
}
|
2018-09-26 02:33:02 +00:00
|
|
|
state.value = colorConverter.format(color, 'rgb');
|
2018-09-25 13:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-01 15:14:56 +00:00
|
|
|
};
|
|
|
|
const parser = createParser(options);
|
|
|
|
const looseParser = createParser(Object.assign({}, options, {allowErrors: true, unknownKey: 'throw'}));
|
|
|
|
return {
|
|
|
|
parse,
|
|
|
|
lint,
|
|
|
|
nullifyInvalidVars
|
|
|
|
};
|
2018-09-25 13:18:39 +00:00
|
|
|
|
|
|
|
function parse(text, indexOffset) {
|
|
|
|
try {
|
|
|
|
return parser.parse(text);
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err.index === 'number') {
|
|
|
|
err.index += indexOffset;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:14:56 +00:00
|
|
|
function lint(text) {
|
|
|
|
return looseParser.parse(text);
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:18:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
})();
|