2021-01-01 14:27:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const BUILDERS = Object.assign(Object.create(null), {
|
|
|
|
|
|
|
|
default: {
|
|
|
|
post(sections, vars) {
|
|
|
|
require(['/js/sections-util']); /* global styleCodeEmpty */
|
|
|
|
let varDef = Object.keys(vars).map(k => ` --${k}: ${vars[k].value};\n`).join('');
|
|
|
|
if (!varDef) return;
|
|
|
|
varDef = ':root {\n' + varDef + '}\n';
|
|
|
|
for (const section of sections) {
|
|
|
|
if (!styleCodeEmpty(section.code)) {
|
|
|
|
section.code = varDef + section.code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
stylus: {
|
|
|
|
pre(source, vars) {
|
|
|
|
require(['/vendor/stylus-lang-bundle/stylus-renderer.min']); /* global StylusRenderer */
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const varDef = Object.keys(vars).map(key => `${key} = ${vars[key].value};\n`).join('');
|
|
|
|
new StylusRenderer(varDef + source)
|
|
|
|
.render((err, output) => err ? reject(err) : resolve(output));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
less: {
|
|
|
|
async pre(source, vars) {
|
|
|
|
if (!self.less) {
|
|
|
|
self.less = {
|
|
|
|
logLevel: 0,
|
|
|
|
useFileCache: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
require(['/vendor/less-bundle/less.min']); /* global less */
|
|
|
|
const varDefs = Object.keys(vars).map(key => `@${key}:${vars[key].value};\n`).join('');
|
2021-01-16 06:18:54 +00:00
|
|
|
const res = await less.render(varDefs + source, {
|
|
|
|
math: 'parens-division',
|
|
|
|
});
|
|
|
|
return res.css;
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
uso: {
|
2021-01-16 06:58:45 +00:00
|
|
|
pre(source, vars) {
|
2021-01-01 14:27:58 +00:00
|
|
|
require(['/js/color/color-converter']); /* global colorConverter */
|
2021-03-05 14:25:05 +00:00
|
|
|
const pool = Object.create(null);
|
2021-01-01 14:27:58 +00:00
|
|
|
return doReplace(source);
|
|
|
|
|
2021-03-05 14:25:05 +00:00
|
|
|
function doReplace(text) {
|
|
|
|
return text.replace(/(\/\*\[\[([\w-]+)]]\*\/)([0-9a-f]{2}(?=\W))?/gi, (_, cmt, name, alpha) => {
|
|
|
|
const key = alpha ? name + '[A]' : name;
|
|
|
|
let val = pool[key];
|
|
|
|
if (val === undefined) {
|
|
|
|
val = pool[key] = getValue(name, null, alpha);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-03-05 14:25:05 +00:00
|
|
|
return (val != null ? val : cmt) + (alpha || '');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getValue(name, isUsoRgb, alpha) {
|
|
|
|
const v = vars[name];
|
|
|
|
if (!v) {
|
|
|
|
return name.endsWith('-rgb')
|
|
|
|
? getValue(name.slice(0, -4), true)
|
|
|
|
: null;
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-03-05 14:25:05 +00:00
|
|
|
let {value} = v;
|
|
|
|
switch (v.type) {
|
|
|
|
case 'color':
|
|
|
|
value = colorConverter.parse(value) || null;
|
|
|
|
if (value) {
|
|
|
|
/* #rrggbb - inline alpha is present; an opaque hsl/a; #rrggbb originally
|
|
|
|
* rgba(r, g, b, a) - transparency <1 is present (Chrome pre-66 compatibility)
|
|
|
|
* rgb(r, g, b) - if color is rgb/a with a=1, note: r/g/b will be rounded
|
|
|
|
* r, g, b - if the var has `-rgb` suffix per USO specification
|
|
|
|
* TODO: when minimum_chrome_version >= 66 try to keep `value` intact */
|
|
|
|
if (alpha) delete value.a;
|
|
|
|
const isRgb = isUsoRgb || value.type === 'rgb' || value.a != null && value.a !== 1;
|
|
|
|
const usoMode = isUsoRgb || !isRgb;
|
|
|
|
value = colorConverter.format(value, isRgb ? 'rgb' : 'hex', undefined, usoMode);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-03-05 14:25:05 +00:00
|
|
|
return value;
|
2021-01-01 14:27:58 +00:00
|
|
|
case 'dropdown':
|
2021-03-05 14:25:05 +00:00
|
|
|
case 'select':
|
|
|
|
pool[name] = ''; // prevent infinite recursion
|
2021-01-01 14:27:58 +00:00
|
|
|
return doReplace(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/* exported compileUsercss */
|
2021-01-16 06:58:45 +00:00
|
|
|
/**
|
|
|
|
* @param {string} preprocessor
|
|
|
|
* @param {string} code
|
|
|
|
* @param {Object} [vars] - WARNING: each var's `value` will be overwritten
|
|
|
|
(not a problem currently as this code runs in a worker so `vars` is just a copy)
|
|
|
|
* @returns {Promise<{sections, errors}>}
|
|
|
|
*/
|
2021-01-01 14:27:58 +00:00
|
|
|
async function compileUsercss(preprocessor, code, vars) {
|
|
|
|
let builder = BUILDERS[preprocessor];
|
|
|
|
if (!builder) {
|
|
|
|
builder = BUILDERS.default;
|
|
|
|
if (preprocessor != null) console.warn(`Unknown preprocessor "${preprocessor}"`);
|
|
|
|
}
|
2021-01-16 06:58:45 +00:00
|
|
|
if (vars) {
|
|
|
|
simplifyUsercssVars(vars);
|
|
|
|
} else {
|
|
|
|
vars = {};
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (builder.pre) {
|
|
|
|
code = await builder.pre(code, vars);
|
|
|
|
}
|
|
|
|
require(['/js/moz-parser']); /* global extractSections */
|
|
|
|
const res = extractSections({code});
|
|
|
|
if (builder.post) {
|
|
|
|
builder.post(res.sections, vars);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2021-01-16 06:58:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds units and sets `null` values to their defaults
|
|
|
|
* WARNING: the old value is overwritten
|
|
|
|
*/
|
|
|
|
function simplifyUsercssVars(vars) {
|
|
|
|
for (const va of Object.values(vars)) {
|
|
|
|
let value = va.value != null ? va.value : va.default;
|
|
|
|
switch (va.type) {
|
|
|
|
case 'select':
|
|
|
|
case 'dropdown':
|
|
|
|
case 'image':
|
|
|
|
// TODO: handle customized image
|
2021-01-26 13:33:17 +00:00
|
|
|
for (const opt of va.options) {
|
|
|
|
if (opt.name === value) {
|
|
|
|
value = opt.value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 06:58:45 +00:00
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
case 'range':
|
|
|
|
value += va.units || '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va.value = value;
|
|
|
|
}
|
|
|
|
}
|