@@ -649,44 +649,74 @@ declare enum WalkerOptionEnum {
649
649
/**
650
650
* ignore the current node and its children
651
651
*/
652
- Ignore = 0 ,
652
+ Ignore = 1 ,
653
653
/**
654
654
* stop walking the tree
655
655
*/
656
- Stop = 1 ,
656
+ Stop = 2 ,
657
657
/**
658
658
* ignore node and process children
659
659
*/
660
- Children = 2 ,
660
+ Children = 4 ,
661
661
/**
662
662
* ignore children
663
663
*/
664
- IgnoreChildren = 3
664
+ IgnoreChildren = 8
665
665
}
666
666
declare enum WalkerValueEvent {
667
667
/**
668
668
* enter node
669
669
*/
670
- Enter = 0 ,
670
+ Enter = 1 ,
671
671
/**
672
672
* leave node
673
673
*/
674
- Leave = 1
674
+ Leave = 2
675
675
}
676
676
/**
677
677
* 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
+ * ```
680
710
*/
681
- declare function walk ( node : AstNode , filter ?: WalkerFilter ) : Generator < WalkResult > ;
711
+ declare function walk ( node : AstNode , filter ?: WalkerFilter | null , reverse ?: boolean ) : Generator < WalkResult > ;
682
712
/**
683
713
* walk ast node value tokens
684
714
* @param values
685
715
* @param root
686
716
* @param filter
687
717
* @param reverse
688
718
*/
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 | {
690
720
event ?: WalkerValueEvent ;
691
721
fn ?: WalkerValueFilter ;
692
722
type ?: EnumToken | EnumToken [ ] | ( ( token : Token ) => boolean ) ;
@@ -748,7 +778,7 @@ declare class SourceMap {
748
778
* console.log(declarations);
749
779
* ```
750
780
*/
751
- declare function parseDeclarations ( declaration : string ) : Promise < AstDeclaration [ ] > ;
781
+ declare function parseDeclarations ( declaration : string ) : Promise < Array < AstDeclaration | AstComment > > ;
752
782
/**
753
783
* parse css string and return an array of tokens
754
784
* @param src
@@ -1565,6 +1595,8 @@ interface Context<Type> {
1565
1595
* @param token
1566
1596
* @param to
1567
1597
*
1598
+ * @private
1599
+ *
1568
1600
* <code>
1569
1601
*
1570
1602
* const token = {typ: EnumToken.ColorTokenType, kin: ColorType.HEX, val: '#F00'}
@@ -1980,16 +2012,18 @@ export declare interface ParseInfo {
1980
2012
1981
2013
/**
1982
2014
* feature walk mode
2015
+ *
2016
+ * @internal
1983
2017
*/
1984
2018
declare enum FeatureWalkMode {
1985
2019
/**
1986
2020
* pre process
1987
2021
*/
1988
- Pre = 0 ,
2022
+ Pre = 1 ,
1989
2023
/**
1990
2024
* post process
1991
2025
*/
1992
- Post = 1
2026
+ Post = 2
1993
2027
}
1994
2028
1995
2029
/**
@@ -2401,24 +2435,23 @@ interface BorderRadius {
2401
2435
keywords : string [ ] ;
2402
2436
}
2403
2437
2438
+ /**
2439
+ * node walker option
2440
+ */
2404
2441
export declare type WalkerOption = WalkerOptionEnum | Token | null ;
2405
2442
/**
2406
2443
* 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
2411
2448
*/
2412
2449
export declare type WalkerFilter = ( node : AstNode ) => WalkerOption ;
2413
2450
2414
2451
/**
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
2420
2453
*/
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 ;
2422
2455
2423
2456
export declare interface WalkResult {
2424
2457
node : AstNode ;
@@ -2430,9 +2463,8 @@ export declare interface WalkAttributesResult {
2430
2463
value : Token ;
2431
2464
previousValue : Token | null ;
2432
2465
nextValue : Token | null ;
2433
- root ?: AstNode ;
2466
+ root ?: AstNode | Token | null ;
2434
2467
parent : AstNode | Token | null ;
2435
- list : Token [ ] | null ;
2436
2468
}
2437
2469
2438
2470
/**
@@ -2695,13 +2727,9 @@ export declare interface MinifyFeature {
2695
2727
*/
2696
2728
ordering : number ;
2697
2729
/**
2698
- * use in pre process
2699
- */
2700
- preProcess : boolean ;
2701
- /**
2702
- * use in post process
2730
+ * process mode
2703
2731
*/
2704
- postProcess : boolean ;
2732
+ processMode : FeatureWalkMode ;
2705
2733
/**
2706
2734
* register feature
2707
2735
* @param options
@@ -3043,7 +3071,8 @@ export declare interface SourceMapObject {
3043
3071
/**
3044
3072
* return the directory name of a path
3045
3073
* @param path
3046
- * @internal
3074
+ *
3075
+ * @private
3047
3076
*/
3048
3077
declare function dirname ( path : string ) : string ;
3049
3078
/**
@@ -3059,19 +3088,15 @@ declare function resolve(url: string, currentDirectory: string, cwd?: string): {
3059
3088
relative : string ;
3060
3089
} ;
3061
3090
3062
- /**
3063
- * node module entry point
3064
- * @module node
3065
- */
3066
-
3067
3091
/**
3068
3092
* load file or url as stream
3069
3093
* @param url
3070
3094
* @param currentFile
3095
+ * @throws Error file not found
3071
3096
*
3072
3097
* @private
3073
3098
*/
3074
- declare function getStream ( url : string , currentFile ?: string ) : Promise < ReadableStream < string > > ;
3099
+ declare function getStream ( url : string , currentFile ?: string ) : Promise < ReadableStream < Uint8Array > | string > ;
3075
3100
/**
3076
3101
* render ast tree
3077
3102
* @param data
@@ -3083,13 +3108,21 @@ declare function getStream(url: string, currentFile?: string): Promise<ReadableS
3083
3108
*
3084
3109
* import {render, ColorType} from '@tbela99/css-parser';
3085
3110
*
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);
3089
3113
*
3090
- * // local file
3091
- * result = await parseFile(ast, {beatify: true, convertColor: ColorType.SRGB});
3114
+ * let renderResult = render(parseResult.ast);
3092
3115
* 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
+ * // }
3093
3126
* ```
3094
3127
*/
3095
3128
declare function render ( data : AstNode , options ?: RenderOptions ) : RenderResult ;
@@ -3098,6 +3131,8 @@ declare function render(data: AstNode, options?: RenderOptions): RenderResult;
3098
3131
* @param file url or path
3099
3132
* @param options
3100
3133
*
3134
+ * @throws Error file not found
3135
+ *
3101
3136
* Example:
3102
3137
*
3103
3138
* ```ts
@@ -3123,11 +3158,11 @@ declare function parseFile(file: string, options?: ParserOptions): Promise<Parse
3123
3158
*
3124
3159
* ```ts
3125
3160
*
3126
- * import {transform } from '@tbela99/css-parser';
3161
+ * import {parse } from '@tbela99/css-parser';
3127
3162
*
3128
3163
* // css string
3129
- * let result = await transform (css);
3130
- * console.log(result.code );
3164
+ * let result = await parse (css);
3165
+ * console.log(result.ast );
3131
3166
* ```
3132
3167
*
3133
3168
* Example using stream
@@ -3157,12 +3192,14 @@ declare function parseFile(file: string, options?: ParserOptions): Promise<Parse
3157
3192
* console.log(result.ast);
3158
3193
* ```
3159
3194
*/
3160
- declare function parse ( stream : string | ReadableStream < string > , opt ?: ParserOptions ) : Promise < ParseResult > ;
3195
+ declare function parse ( stream : string | ReadableStream < Uint8Array > , opt ?: ParserOptions ) : Promise < ParseResult > ;
3161
3196
/**
3162
3197
* transform css file
3163
3198
* @param file url or path
3164
3199
* @param options
3165
3200
*
3201
+ * @throws Error file not found
3202
+ *
3166
3203
* Example:
3167
3204
*
3168
3205
* ```ts
@@ -3222,7 +3259,7 @@ declare function transformFile(file: string, options?: TransformOptions): Promis
3222
3259
* console.log(result.code);
3223
3260
* ```
3224
3261
*/
3225
- declare function transform ( css : string | ReadableStream < string > , options ?: TransformOptions ) : Promise < TransformResult > ;
3262
+ declare function transform ( css : string | ReadableStream < Uint8Array > , options ?: TransformOptions ) : Promise < TransformResult > ;
3226
3263
3227
3264
export { 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 } ;
3228
3265
export 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