convert colors in uso preprocessor to match USO site (#997)

#rrggbb for /*[[color]]*/
r,g,b for /*[[color-rgb]]*/

(no alpha channel)
This commit is contained in:
tophf 2020-09-22 14:15:40 +03:00 committed by GitHub
parent 2b149f97a5
commit ee30aa1407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,7 +31,11 @@ createAPI({
}); });
function compileUsercss(preprocessor, code, vars) { function compileUsercss(preprocessor, code, vars) {
loadScript('/vendor-overwrites/csslint/parserlib.js', '/js/moz-parser.js'); loadScript(
'/vendor-overwrites/csslint/parserlib.js',
'/vendor-overwrites/colorpicker/colorconverter.js',
'/js/moz-parser.js'
);
const builder = getUsercssCompiler(preprocessor); const builder = getUsercssCompiler(preprocessor);
vars = simpleVars(vars); vars = simpleVars(vars);
return Promise.resolve(builder.preprocess ? builder.preprocess(code, vars) : code) return Promise.resolve(builder.preprocess ? builder.preprocess(code, vars) : code)
@ -122,28 +126,39 @@ function getUsercssCompiler(preprocessor) {
const pool = new Map(); const pool = new Map();
return Promise.resolve(doReplace(source)); return Promise.resolve(doReplace(source));
function getValue(name, rgb) { function getValue(name, rgbName) {
if (!vars.hasOwnProperty(name)) { if (!vars.hasOwnProperty(name)) {
if (name.endsWith('-rgb')) { if (name.endsWith('-rgb')) {
return getValue(name.slice(0, -4), true); return getValue(name.slice(0, -4), name);
} }
return null; return null;
} }
if (rgb) { const {type, value} = vars[name];
if (vars[name].type === 'color') { switch (type) {
const color = colorConverter.parse(vars[name].value); case 'color': {
if (!color) return null; let color = pool.get(rgbName || name);
if (color == null) {
color = colorConverter.parse(value);
if (color) {
if (color.type === 'hsl') {
color = colorConverter.HSVtoRGB(colorConverter.HSLtoHSV(color));
}
const {r, g, b} = color; const {r, g, b} = color;
return `${r}, ${g}, ${b}`; color = rgbName
? `${r}, ${g}, ${b}`
: `#${(0x1000000 + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
} }
return null; // the pool stores `false` for bad colors to differentiate from a yet unknown color
pool.set(rgbName || name, color || false);
} }
if (vars[name].type === 'dropdown' || vars[name].type === 'select') { return color || null;
// prevent infinite recursion }
case 'dropdown':
case 'select': // prevent infinite recursion
pool.set(name, ''); pool.set(name, '');
return doReplace(vars[name].value); return doReplace(value);
} }
return vars[name].value; return value;
} }
function doReplace(text) { function doReplace(text) {