mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-12 11:30:01 -04:00
70 lines
2.4 KiB
JavaScript
70 lines
2.4 KiB
JavaScript
// -------------------------------------------------------------
|
|
// WARNING: this file is used by both the client and the server.
|
|
// Do not use any browser or node-specific API!
|
|
// -------------------------------------------------------------
|
|
const csstree = require('css-tree');
|
|
const rawMap = [
|
|
require('./import'),
|
|
require('./url-string'),
|
|
require('./url-raw'),
|
|
];
|
|
|
|
class CSSRewriter {
|
|
constructor(ctx) {
|
|
this.ctx = ctx;
|
|
this.map = new Map();
|
|
rawMap.forEach(({ type, ...keys }) => {
|
|
if (!this.map.has(type)) this.map.set(type, []);
|
|
this.map.get(type).push(keys);
|
|
});
|
|
};
|
|
process(source, config = {}) {
|
|
try {
|
|
const ast = csstree.parse(source, {
|
|
context: config.context || 'stylesheet',
|
|
parseCustomProperty: true,
|
|
});
|
|
csstree.walk(ast, node => {
|
|
if (this.map.has(node.type)) {
|
|
const data = {
|
|
ctx: this.ctx,
|
|
meta: config.meta || {},
|
|
}
|
|
const transformers = this.map.get(node.type);
|
|
for (const transformer of transformers) {
|
|
if (!transformer.condition || transformer.condition(node, data)) {
|
|
if (transformer.rewrite) transformer.rewrite(node, data);
|
|
};
|
|
};
|
|
};
|
|
});
|
|
return csstree.generate(ast);
|
|
} catch(e) {
|
|
return source;
|
|
};
|
|
};
|
|
source(processed, config = {}) {
|
|
const ast = csstree.parse(processed, {
|
|
context: config.context || 'stylesheet',
|
|
parseCustomProperty: true,
|
|
});
|
|
csstree.walk(ast, node => {
|
|
if (this.map.has(node.type)) {
|
|
const data = {
|
|
ctx: this.ctx,
|
|
meta: config.meta || {},
|
|
}
|
|
const transformers = this.map.get(node.type);
|
|
for (const transformer of transformers) {
|
|
if (!transformer.condition || transformer.condition(node, data)) {
|
|
console.log(transformer)
|
|
if (transformer.source) transformer.source(node, data);
|
|
};
|
|
};
|
|
};
|
|
});
|
|
return csstree.generate(ast);
|
|
};
|
|
};
|
|
|
|
module.exports = CSSRewriter;
|