420733b93a
* add Patch CSP option * show style version, size, and update age in manager * add scope selector to style search in manager * keep scroll position and selections in tab's session * directly install usercss from raw github links * ditch localStorage, use on-demand SessionStore proxy * simplify localization * allow <code> tag in i18n-html * keep nodes in HTML templates * API.getAllStyles is actually faster with code untouched * fix fitToContent when applies-to is taller than window * dedupe linter.enableForEditor calls * prioritize visible CMs in refreshOnViewListener * don't scroll to last style on editing a new one * delay colorview for invisible CMs * eslint comma-dangle error + autofix files * styleViaXhr: also toggle for disableAll pref * styleViaXhr: allow cookies for sandbox CSP * simplify notes in options * simplify getStylesViaXhr * oldUI fixups: * remove separator before 1st applies-to * center name bubbles * fix updateToc focus on a newly added section * fix fitToContent when cloning section * remove CSS `contain` as it makes no difference * replace overrides with declarative CSS + code cosmetics * simplify adjustWidth and make it work in FF
79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
/* global usercssMeta colorConverter */
|
|
/* exported metaParser */
|
|
'use strict';
|
|
|
|
const metaParser = (() => {
|
|
const {createParser, ParseError} = usercssMeta;
|
|
const PREPROCESSORS = new Set(['default', 'uso', 'stylus', 'less']);
|
|
const options = {
|
|
validateKey: {
|
|
preprocessor: state => {
|
|
if (!PREPROCESSORS.has(state.value)) {
|
|
throw new ParseError({
|
|
code: 'unknownPreprocessor',
|
|
args: [state.value],
|
|
index: state.valueIndex,
|
|
});
|
|
}
|
|
},
|
|
},
|
|
validateVar: {
|
|
select: state => {
|
|
if (state.varResult.options.every(o => o.name !== state.value)) {
|
|
throw new ParseError({
|
|
code: 'invalidSelectValueMismatch',
|
|
index: state.valueIndex,
|
|
});
|
|
}
|
|
},
|
|
color: state => {
|
|
const color = colorConverter.parse(state.value);
|
|
if (!color) {
|
|
throw new ParseError({
|
|
code: 'invalidColor',
|
|
args: [state.value],
|
|
index: state.valueIndex,
|
|
});
|
|
}
|
|
state.value = colorConverter.format(color, 'rgb');
|
|
},
|
|
},
|
|
};
|
|
const parser = createParser(options);
|
|
const looseParser = createParser(Object.assign({}, options, {allowErrors: true, unknownKey: 'throw'}));
|
|
return {
|
|
parse,
|
|
lint,
|
|
nullifyInvalidVars,
|
|
};
|
|
|
|
function parse(text, indexOffset) {
|
|
try {
|
|
return parser.parse(text);
|
|
} catch (err) {
|
|
if (typeof err.index === 'number') {
|
|
err.index += indexOffset;
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
function lint(text) {
|
|
return looseParser.parse(text);
|
|
}
|
|
|
|
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;
|
|
}
|
|
})();
|