mirror of
https://github.com/titaniumnetwork-dev/Ultraviolet.git
synced 2025-05-16 04:50:01 -04:00
uv
This commit is contained in:
parent
b9b6aee734
commit
82f5f76588
66 changed files with 74967 additions and 1 deletions
37
rewrite/css.js
Normal file
37
rewrite/css.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { parse, walk, generate } from "css-tree";
|
||||
import EventEmitter from "./events.js";
|
||||
import parsel from "./parsel.js";
|
||||
|
||||
class CSS extends EventEmitter {
|
||||
constructor(ctx) {
|
||||
super();
|
||||
this.ctx = ctx;
|
||||
this.meta = ctx.meta;
|
||||
this.parsel = parsel;
|
||||
this.parse = parse;
|
||||
this.walk = walk;
|
||||
this.generate = generate;
|
||||
};
|
||||
rewrite(str, options) {
|
||||
return this.recast(str, options, 'rewrite');
|
||||
};
|
||||
source(str, options) {
|
||||
return this.recast(str, options, 'source');
|
||||
};
|
||||
recast(str, options, type) {
|
||||
if (!str) return str;
|
||||
str = new String(str).toString();
|
||||
try {
|
||||
const ast = this.parse(str, { ...options, parseCustomProperty: true });
|
||||
this.walk(ast, node => {
|
||||
this.emit(node.type, node, options, type);
|
||||
});
|
||||
return this.generate(ast);
|
||||
} catch(e) {
|
||||
console.log(e)
|
||||
return str;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export default CSS;
|
Loading…
Add table
Add a link
Reference in a new issue