Search


Search something to see results

findByValue

Search the ast tree by checking each node's value token and return the first match

 // find the first ast node which contains the length token '30px'
import { findByValue, EnumToken, transform } from "@tbela99/css-parser";
import type { AstNode } from "@tbela99/css-parser";

const css = `

button {
aspect-ratio: 1;
width: if(media(any-pointer: fine): 30px; else: 44px);
}
`;

// find declaration which contain the length token '30px'
const nodeMatcher = (value: Token) =>
return value.typ == EnumToken.LengthTokenType && (value as LengthToken).val == 30 && (value as LengthToken).unit == 'px' ;

const result = await transform(css);
const { node, value } = findByValue(result.ast, nodeMatcher) ?? {};

console.log({node, value});

Parameters

Returns { node: any; value: TokenSearchResult } | null