@@ -649,44 +649,74 @@ declare enum WalkerOptionEnum {
649649 /**
650650 * ignore the current node and its children
651651 */
652- Ignore = 0 ,
652+ Ignore = 1 ,
653653 /**
654654 * stop walking the tree
655655 */
656- Stop = 1 ,
656+ Stop = 2 ,
657657 /**
658658 * ignore node and process children
659659 */
660- Children = 2 ,
660+ Children = 4 ,
661661 /**
662662 * ignore children
663663 */
664- IgnoreChildren = 3
664+ IgnoreChildren = 8
665665}
666666declare enum WalkerValueEvent {
667667 /**
668668 * enter node
669669 */
670- Enter = 0 ,
670+ Enter = 1 ,
671671 /**
672672 * leave node
673673 */
674- Leave = 1
674+ Leave = 2
675675}
676676/**
677677 * walk ast nodes
678- * @param node
679- * @param filter
678+ * @param node initial node
679+ * @param filter control the walk process
680+ * @param reverse walk in reverse order
681+ *
682+ * ```ts
683+ *
684+ * import {walk} from '@tbela99/css-parser';
685+ *
686+ * for (const {node, parent, root} of walk(ast)) {
687+ *
688+ * // do something with node
689+ * }
690+ * ```
691+ *
692+ * Using a filter to control the walk process:
693+ *
694+ * ```ts
695+ *
696+ * import {walk} from '@tbela99/css-parser';
697+ *
698+ * for (const {node, parent, root} of walk(ast, (node) => {
699+ *
700+ * if (node.typ == EnumToken.AstRule && node.sel.includes('html')) {
701+ *
702+ * // skip the children of the current node
703+ * return WalkerOptionEnum.IgnoreChildren;
704+ * }
705+ * })) {
706+ *
707+ * // do something with node
708+ * }
709+ * ```
680710 */
681- declare function walk ( node : AstNode , filter ?: WalkerFilter ) : Generator < WalkResult > ;
711+ declare function walk ( node : AstNode , filter ?: WalkerFilter | null , reverse ?: boolean ) : Generator < WalkResult > ;
682712/**
683713 * walk ast node value tokens
684714 * @param values
685715 * @param root
686716 * @param filter
687717 * @param reverse
688718 */
689- declare function walkValues ( values : Token [ ] , root ?: AstNode | Token | null , filter ?: WalkerValueFilter | {
719+ declare function walkValues ( values : Token [ ] , root ?: AstNode | Token | null , filter ?: WalkerValueFilter | null | {
690720 event ?: WalkerValueEvent ;
691721 fn ?: WalkerValueFilter ;
692722 type ?: EnumToken | EnumToken [ ] | ( ( token : Token ) => boolean ) ;
@@ -748,7 +778,7 @@ declare class SourceMap {
748778 * console.log(declarations);
749779 * ```
750780 */
751- declare function parseDeclarations ( declaration : string ) : Promise < AstDeclaration [ ] > ;
781+ declare function parseDeclarations ( declaration : string ) : Promise < Array < AstDeclaration | AstComment > > ;
752782/**
753783 * parse css string and return an array of tokens
754784 * @param src
@@ -1565,6 +1595,8 @@ interface Context<Type> {
15651595 * @param token
15661596 * @param to
15671597 *
1598+ * @private
1599+ *
15681600 * <code>
15691601 *
15701602 * const token = {typ: EnumToken.ColorTokenType, kin: ColorType.HEX, val: '#F00'}
@@ -1980,16 +2012,18 @@ export declare interface ParseInfo {
19802012
19812013/**
19822014 * feature walk mode
2015+ *
2016+ * @internal
19832017 */
19842018declare enum FeatureWalkMode {
19852019 /**
19862020 * pre process
19872021 */
1988- Pre = 0 ,
2022+ Pre = 1 ,
19892023 /**
19902024 * post process
19912025 */
1992- Post = 1
2026+ Post = 2
19932027}
19942028
19952029/**
@@ -2401,24 +2435,23 @@ interface BorderRadius {
24012435 keywords : string [ ] ;
24022436}
24032437
2438+ /**
2439+ * node walker option
2440+ */
24042441export declare type WalkerOption = WalkerOptionEnum | Token | null ;
24052442/**
24062443 * returned value:
2407- * - WalkerOptionEnum.Ignore: ignore this node and its children
2408- * - WalkerOptionEnum.Stop: stop walking the tree
2409- * - WalkerOptionEnum.Children: walk the children and ignore the node itself
2410- * - WalkerOptionEnum.IgnoreChildren: walk the node and ignore children
2444+ * - { @link WalkerOptionEnum.Ignore} : ignore this node and its children
2445+ * - { @link WalkerOptionEnum.Stop} : stop walking the tree
2446+ * - { @link WalkerOptionEnum.Children} : walk the children and ignore the current node
2447+ * - { @link WalkerOptionEnum.IgnoreChildren} : walk the node and ignore children
24112448 */
24122449export declare type WalkerFilter = ( node : AstNode ) => WalkerOption ;
24132450
24142451/**
2415- * returned value:
2416- * - 'ignore': ignore this node and its children
2417- * - 'stop': stop walking the tree
2418- * - 'children': walk the children and ignore the node itself
2419- * - 'ignore-children': walk the node and ignore children
2452+ * filter nod
24202453 */
2421- export declare type WalkerValueFilter = ( node : AstNode | Token , parent ?: FunctionToken | ParensToken | BinaryExpressionToken , event ?: WalkerValueEvent ) => WalkerOption | null ;
2454+ export declare type WalkerValueFilter = ( node : AstNode | Token , parent ?: AstNode | Token | null , event ?: WalkerValueEvent ) => WalkerOption | null ;
24222455
24232456export declare interface WalkResult {
24242457 node : AstNode ;
@@ -2430,9 +2463,8 @@ export declare interface WalkAttributesResult {
24302463 value : Token ;
24312464 previousValue : Token | null ;
24322465 nextValue : Token | null ;
2433- root ?: AstNode ;
2466+ root ?: AstNode | Token | null ;
24342467 parent : AstNode | Token | null ;
2435- list : Token [ ] | null ;
24362468}
24372469
24382470/**
@@ -2695,13 +2727,9 @@ export declare interface MinifyFeature {
26952727 */
26962728 ordering : number ;
26972729 /**
2698- * use in pre process
2699- */
2700- preProcess : boolean ;
2701- /**
2702- * use in post process
2730+ * process mode
27032731 */
2704- postProcess : boolean ;
2732+ processMode : FeatureWalkMode ;
27052733 /**
27062734 * register feature
27072735 * @param options
@@ -3043,7 +3071,8 @@ export declare interface SourceMapObject {
30433071/**
30443072 * return the directory name of a path
30453073 * @param path
3046- * @internal
3074+ *
3075+ * @private
30473076 */
30483077declare function dirname ( path : string ) : string ;
30493078/**
@@ -3059,19 +3088,15 @@ declare function resolve(url: string, currentDirectory: string, cwd?: string): {
30593088 relative : string ;
30603089} ;
30613090
3062- /**
3063- * node module entry point
3064- * @module node
3065- */
3066-
30673091/**
30683092 * load file or url as stream
30693093 * @param url
30703094 * @param currentFile
3095+ * @throws Error file not found
30713096 *
30723097 * @private
30733098 */
3074- declare function getStream ( url : string , currentFile ?: string ) : Promise < ReadableStream < string > > ;
3099+ declare function getStream ( url : string , currentFile ?: string ) : Promise < ReadableStream < Uint8Array > | string > ;
30753100/**
30763101 * render ast tree
30773102 * @param data
@@ -3083,13 +3108,21 @@ declare function getStream(url: string, currentFile?: string): Promise<ReadableS
30833108 *
30843109 * import {render, ColorType} from '@tbela99/css-parser';
30853110 *
3086- * // remote file
3087- * let result = render(ast);
3088- * console.log(result.code);
3111+ * const css = 'body { color: color(from hsl(0 100% 50%) xyz x y z); }';
3112+ * const parseResult = await parse(css);
30893113 *
3090- * // local file
3091- * result = await parseFile(ast, {beatify: true, convertColor: ColorType.SRGB});
3114+ * let renderResult = render(parseResult.ast);
30923115 * console.log(result.code);
3116+ *
3117+ * // body{color:red}
3118+ *
3119+ *
3120+ * renderResult = render(parseResult.ast, {beautify: true, convertColor: ColorType.SRGB});
3121+ * console.log(renderResult.code);
3122+ *
3123+ * // body {
3124+ * // color: color(srgb 1 0 0)
3125+ * // }
30933126 * ```
30943127 */
30953128declare function render ( data : AstNode , options ?: RenderOptions ) : RenderResult ;
@@ -3098,6 +3131,8 @@ declare function render(data: AstNode, options?: RenderOptions): RenderResult;
30983131 * @param file url or path
30993132 * @param options
31003133 *
3134+ * @throws Error file not found
3135+ *
31013136 * Example:
31023137 *
31033138 * ```ts
@@ -3123,11 +3158,11 @@ declare function parseFile(file: string, options?: ParserOptions): Promise<Parse
31233158 *
31243159 * ```ts
31253160 *
3126- * import {transform } from '@tbela99/css-parser';
3161+ * import {parse } from '@tbela99/css-parser';
31273162 *
31283163 * // css string
3129- * let result = await transform (css);
3130- * console.log(result.code );
3164+ * let result = await parse (css);
3165+ * console.log(result.ast );
31313166 * ```
31323167 *
31333168 * Example using stream
@@ -3157,12 +3192,14 @@ declare function parseFile(file: string, options?: ParserOptions): Promise<Parse
31573192 * console.log(result.ast);
31583193 * ```
31593194 */
3160- declare function parse ( stream : string | ReadableStream < string > , opt ?: ParserOptions ) : Promise < ParseResult > ;
3195+ declare function parse ( stream : string | ReadableStream < Uint8Array > , opt ?: ParserOptions ) : Promise < ParseResult > ;
31613196/**
31623197 * transform css file
31633198 * @param file url or path
31643199 * @param options
31653200 *
3201+ * @throws Error file not found
3202+ *
31663203 * Example:
31673204 *
31683205 * ```ts
@@ -3222,7 +3259,7 @@ declare function transformFile(file: string, options?: TransformOptions): Promis
32223259 * console.log(result.code);
32233260 * ```
32243261 */
3225- declare function transform ( css : string | ReadableStream < string > , options ?: TransformOptions ) : Promise < TransformResult > ;
3262+ declare function transform ( css : string | ReadableStream < Uint8Array > , options ?: TransformOptions ) : Promise < TransformResult > ;
32263263
32273264export { ColorType , EnumToken , FeatureWalkMode , SourceMap , ValidationLevel , WalkerOptionEnum , WalkerValueEvent , convertColor , dirname , expand , getStream , isOkLabClose , mathFuncs , minify , okLabDistance , parse , parseDeclarations , parseFile , parseString , parseTokens , render , renderToken , resolve , transform , transformFile , transformFunctions , walk , walkValues } ;
32283265export type { AddToken , AngleToken , AstAtRule , AstComment , AstDeclaration , AstInvalidAtRule , AstInvalidDeclaration , AstInvalidRule , AstKeyFrameRule , AstKeyframAtRule , AstKeyframeRule , AstNode , AstRule , AstRuleList , AstRuleStyleSheet , AtRuleToken , AtRuleVisitorHandler , AttrEndToken , AttrStartToken , AttrToken , Background , BackgroundAttachmentMapping , BackgroundPosition , BackgroundPositionClass , BackgroundPositionConstraints , BackgroundPositionMapping , BackgroundProperties , BackgroundRepeat , BackgroundRepeatMapping , BackgroundSize , BackgroundSizeMapping , BadCDOCommentToken , BadCommentToken , BadStringToken , BadUrlToken , BaseToken , BinaryExpressionNode , BinaryExpressionToken , BlockEndToken , BlockStartToken , Border , BorderColor , BorderColorClass , BorderProperties , BorderRadius , CDOCommentToken , ChildCombinatorToken , ClassSelectorToken , ColonToken , ColorToken , ColumnCombinatorToken , CommaToken , CommentToken , ConstraintsMapping , ContainMatchToken , Context , DashMatchToken , DashedIdentToken , DeclarationVisitorHandler , DelimToken , DescendantCombinatorToken , DimensionToken , DivToken , EOFToken , EndMatchToken , EqualMatchToken , ErrorDescription , FlexToken , Font , FontFamily , FontProperties , FontWeight , FontWeightConstraints , FontWeightMapping , FractionToken , FrequencyToken , FunctionImageToken , FunctionToken , FunctionURLToken , GreaterThanOrEqualToken , GreaterThanToken , GridTemplateFuncToken , HashToken , IdentListToken , IdentToken , ImportantToken , IncludeMatchToken , InvalidAttrToken , InvalidClassSelectorToken , LengthToken , LessThanOrEqualToken , LessThanToken , LineHeight , ListToken , LiteralToken , Location , Map$1 as Map , MatchExpressionToken , MatchedSelector , MediaFeatureAndToken , MediaFeatureNotToken , MediaFeatureOnlyToken , MediaFeatureOrToken , MediaFeatureToken , MediaQueryConditionToken , MinifyFeature , MinifyFeatureOptions , MinifyOptions , MulToken , NameSpaceAttributeToken , NestingSelectorToken , NextSiblingCombinatorToken , NumberToken , OptimizedSelector , OptimizedSelectorToken , Outline , OutlineProperties , ParensEndToken , ParensStartToken , ParensToken , ParseInfo , ParseResult , ParseResultStats , ParseTokenOptions , ParserOptions , PercentageToken , Position , Prefix , PropertiesConfig , PropertiesConfigProperties , PropertyListOptions , PropertyMapType , PropertySetType , PropertyType , PseudoClassFunctionToken , PseudoClassToken , PseudoElementToken , PseudoPageToken , PurpleBackgroundAttachment , RawSelectorTokens , RenderOptions , RenderResult , ResolutionToken , ResolvedPath , RuleVisitorHandler , SemiColonToken , Separator , ShorthandDef , ShorthandMapType , ShorthandProperties , ShorthandPropertyType , ShorthandType , SourceMapObject , StartMatchToken , StringToken , SubToken , SubsequentCombinatorToken , TimeToken , TimelineFunctionToken , TimingFunctionToken , Token , TokenizeResult , TransformOptions , TransformResult , UnaryExpression , UnaryExpressionNode , UnclosedStringToken , UniversalSelectorToken , UrlToken , ValidationConfiguration , ValidationOptions , ValidationResult , ValidationSelectorOptions , ValidationSyntaxNode , ValidationSyntaxResult , Value , VariableScopeInfo , VisitorNodeMap , WalkAttributesResult , WalkResult , WalkerFilter , WalkerOption , WalkerValueFilter , WhitespaceToken } ;
0 commit comments