mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-13 03:50:02 -04:00
Updated a few more things
This commit is contained in:
parent
c827e9f7e1
commit
73cdcb3186
47 changed files with 35805 additions and 1102 deletions
70
lib/css/index.js
Normal file
70
lib/css/index.js
Normal file
|
@ -0,0 +1,70 @@
|
|||
// -------------------------------------------------------------
|
||||
// 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;
|
Loading…
Add table
Add a link
Reference in a new issue