mirror of
https://github.com/QuiteAFancyEmerald/Holy-Unblocker.git
synced 2025-05-13 03:50:02 -04:00
Minor Change (oops)
This commit is contained in:
parent
af2ca7e034
commit
6bd7e10c4d
31 changed files with 34771 additions and 1 deletions
67
src/Corrosion/lib/css.js
Normal file
67
src/Corrosion/lib/css.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
const csstree = require('css-tree');
|
||||
|
||||
class CSSRewriter {
|
||||
constructor(ctx) {
|
||||
this.ctx = ctx;
|
||||
};
|
||||
process(source, config = {}) {
|
||||
const ast = csstree.parse(source, {
|
||||
context: config.context || 'stylesheet',
|
||||
parseCustomProperty: true,
|
||||
});
|
||||
const urls = csstree.findAll(ast, node =>
|
||||
node.type == 'Url'
|
||||
);
|
||||
const imports = csstree.findAll(ast, node =>
|
||||
node.type == 'Atrule' && node.name == 'import' && node.prelude && node.prelude.type == 'AtrulePrelude' && node.prelude.children.head.data.type == 'String'
|
||||
);
|
||||
urls.forEach(({ value }) => {
|
||||
switch(value.type) {
|
||||
case 'String':
|
||||
const quote = value.value.substring(0, 1);
|
||||
value.value = quote + this.ctx.url.wrap(value.value.slice(1).slice(0, -1), config) + quote;
|
||||
break;
|
||||
case 'Raw':
|
||||
value.value = this.ctx.url.wrap(value.value, config);
|
||||
break;
|
||||
};
|
||||
});
|
||||
imports.forEach(({ prelude }) => {
|
||||
const { data } = prelude.children.head;
|
||||
const quote = data.value.substring(0, 1);
|
||||
data.value = quote + this.ctx.url.wrap(data.value.slice(1).slice(0, -1), config) + quote;
|
||||
});
|
||||
return csstree.generate(ast);
|
||||
};
|
||||
source(processed, config = {}) {
|
||||
const ast = csstree.parse(processed, {
|
||||
context: config.context || 'stylesheet',
|
||||
parseCustomProperty: true,
|
||||
});
|
||||
const urls = csstree.findAll(ast, node =>
|
||||
node.type == 'Url'
|
||||
);
|
||||
const imports = csstree.findAll(ast, node =>
|
||||
node.type == 'Atrule' && node.name == 'import' && node.prelude && node.prelude.type == 'AtrulePrelude' && node.prelude.children.head.data.type == 'String'
|
||||
);
|
||||
urls.forEach(({ value }) => {
|
||||
switch(value.type) {
|
||||
case 'String':
|
||||
const quote = value.value.substring(0, 1);
|
||||
value.value = quote + this.ctx.url.unwrap(value.value.slice(1).slice(0, -1), config) + quote;
|
||||
break;
|
||||
case 'Raw':
|
||||
value.value = this.ctx.url.unwrap(value.value, config);
|
||||
break;
|
||||
};
|
||||
});
|
||||
imports.forEach(({ prelude }) => {
|
||||
const { data } = prelude.children.head;
|
||||
const quote = data.value.substring(0, 1);
|
||||
data.value = quote + this.ctx.url.unwrap(data.value.slice(1).slice(0, -1), config) + quote;
|
||||
});
|
||||
return csstree.generate(ast);
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = CSSRewriter;
|
Loading…
Add table
Add a link
Reference in a new issue