Search


Search something to see results

VisitorSyncNodeMap

node visitor callback map

interface VisitorSyncNodeMap {
    AtRule?: any;
    Declaration?: any;
    KeyframesAtRule?: any;
    KeyframesRule?: any;
    Rule?: any;
    Value?: any;
}

Index

Properties

AtRule?: any

at rule visitor

Example: change media at-rule prelude


import {transform, AstArena, AstAtRule, ParserOptions} from "@tbela99/css-parser";

const options: ParserOptions = {

visitor: {

AtRule: {

media: (arena: AstArena, node: AstAtRule): AstAtRule => {

arena.setValueString(node, 'tv,screen');
return node
}
}
}
};

const css = `

@media screen {

.foo {

height: calc(100px * 2/ 15);
}
}
`;

const result = await transform(css, options);

console.debug(result.code);

// @media tv,screen{.foo{height:calc(40px/3)}}
Declaration?: any

declaration visitor

Example: add 'width: 3px' everytime a declaration with the name 'height' is found


import {transform, AstArena, AstDeclaration, EnumToken, LengthToken, ParserOptions} from "@tbela99/css-parser";

const options: ParserOptions = {

removeEmpty: false,
visitor: {

Declaration: {

// called only for height declaration
height: (arena: AstArena, node: AstDeclaration): AstDeclaration[] => {


return [
node,
// new nodes are allocated in the arena
arena.createDeclaration(EnumToken.DeclarationNodeType, 'width', [
<LengthToken>{
typ: EnumToken.LengthTokenType,
val: 3,
unit: 'px'
}
], null, null)
];
}
}
}
};

const css = `

.foo {
height: calc(100px * 2/ 15);
}
.selector {
color: lch(from peru calc(l * 0.8) calc(c * 0.7) calc(h + 180))
}
`;

console.debug(await transform(css, options));

// .foo{height:calc(40px/3);width:3px}.selector{color:#0880b0}

Example: rename 'margin' to 'padding' and 'height' to 'width'

import {AstArena, AstDeclaration, ParserOptions, transform} from "@tbela99/css-parser";

const options: ParserOptions = {

visitor: {

// called for every declaration
Declaration: (arena: AstArena, id: AstDeclaration): null => {

// object view of the declaration: the properties read and write the arena
const node = arena.toObject(id);

if (node.nam == 'height') {

node.nam = 'width';
}

else if (node.nam == 'margin') {

node.nam = 'padding';
}

return null;
}
}
};

const css = `

.foo {
height: calc(100px * 2/ 15);
margin: 10px;
}
.selector {

margin: 20px;}
`;

const result = await transform(css, options);

console.debug(result.code);

// .foo{width:calc(40px/3);padding:10px}.selector{padding:20px}
KeyframesAtRule?: any

keyframes at-rule visitor

KeyframesRule?: any

keyframes rule visitor

Rule?: any

rule visitor

Example: add 'width: 3px' to every rule with the selector '.foo'


import {transform, parseDeclarations, AstArena, AstRule, ParserOptions} from "@tbela99/css-parser";

const options: ParserOptions = {

removeEmpty: false,
visitor: {

Rule: (arena: AstArena, node: AstRule): AstRule | null => {

if (arena.getSelector(node) == '.foo') {

// the declarations are parsed in the same arena
arena.appendChildren(node, ...parseDeclarations('width: 3px', arena).nodes);
return node;
}

return null;
}
}
};

const css = `

.foo {
.foo {
}
}
`;

console.debug(await transform(css, options));

// .foo{width:3px;.foo{width:3px}}
Value?: any

value visitor