@tbela99/css-parser
Version:
CSS parser, minifier and validator for node and the browser
2,510 lines (2,252 loc) • 82.9 kB
TypeScript
/**
* syntax validation enum
*/
declare enum SyntaxValidationResult {
/** valid syntax */
Valid = 0,
/** drop invalid syntax */
Drop = 1,
/** preserve unknown at-rules, declarations and pseudo-classes */
Lenient = 2
}
/**
* enum of validation levels
*/
declare enum ValidationLevel {
/**
* disable validation
*/
None = 0,
/**
* validate selectors
*/
Selector = 1,
/**
* validate at-rules
*/
AtRule = 2,
/**
* validate declarations
*/
Declaration = 4,
/**
* validate selectors and at-rules
*/
Default = 3,// selectors + at-rules
/**
* validate selectors, at-rules and declarations
*/
All = 7
}
/**
* enum of all token types
*/
declare enum EnumToken {
/**
* comment token
*/
CommentTokenType = 0,
/**
* cdata section token
*/
CDOCOMMTokenType = 1,
/**
* style sheet node type
*/
StyleSheetNodeType = 2,
/**
* at-rule node type
*/
AtRuleNodeType = 3,
/**
* rule node type
*/
RuleNodeType = 4,
/**
* declaration node type
*/
DeclarationNodeType = 5,
/**
* literal token type
*/
LiteralTokenType = 6,
/**
* identifier token type
*/
IdenTokenType = 7,
/**
* dashed identifier token type
*/
DashedIdenTokenType = 8,
/**
* comma token type
*/
CommaTokenType = 9,
/**
* colon token type
*/
ColonTokenType = 10,
/**
* semicolon token type
*/
SemiColonTokenType = 11,
/**
* number token type
*/
NumberTokenType = 12,
/**
* at-rule token type
*/
AtRuleTokenType = 13,
/**
* percentage token type
*/
PercentageTokenType = 14,
/**
* function token type
*/
FunctionTokenType = 15,
/**
* timeline function token type
*/
TimelineFunctionTokenType = 16,
/**
* timing function token type
*/
TimingFunctionTokenType = 17,
/**
* url function token type
*/
UrlFunctionTokenType = 18,
/**
* image function token type
*/
ImageFunctionTokenType = 19,
/**
* string token type
*/
StringTokenType = 20,
/**
* unclosed string token type
*/
UnclosedStringTokenType = 21,
/**
* dimension token type
*/
DimensionTokenType = 22,
/**
* length token type
*/
LengthTokenType = 23,
/**
* angle token type
*/
AngleTokenType = 24,
/**
* time token type
*/
TimeTokenType = 25,
/**
* frequency token type
*/
FrequencyTokenType = 26,
/**
* resolution token type
*/
ResolutionTokenType = 27,
/**
* hash token type
*/
HashTokenType = 28,
/**
* block start token type
*/
BlockStartTokenType = 29,
/**
* block end token type
*/
BlockEndTokenType = 30,
/**
* attribute start token type
*/
AttrStartTokenType = 31,
/**
* attribute end token type
*/
AttrEndTokenType = 32,
/**
* start parentheses token type
*/
StartParensTokenType = 33,
/**
* end parentheses token type
*/
EndParensTokenType = 34,
/**
* parentheses token type
*/
ParensTokenType = 35,
/**
* whitespace token type
*/
WhitespaceTokenType = 36,
/**
* include match token type
*/
IncludeMatchTokenType = 37,
/**
* dash match token type
*/
DashMatchTokenType = 38,
/**
* equal match token type
*/
EqualMatchTokenType = 39,
/**
* less than token type
*/
LtTokenType = 40,
/**
* less than or equal to token type
*/
LteTokenType = 41,
/**
* greater than token type
*/
GtTokenType = 42,
/**
* greater than or equal to token type
*/
GteTokenType = 43,
/**
* pseudo-class token type
*/
PseudoClassTokenType = 44,
/**
* pseudo-class function token type
*/
PseudoClassFuncTokenType = 45,
/**
* delimiter token type
*/
DelimTokenType = 46,
/**
* URL token type
*/
UrlTokenTokenType = 47,
/**
* end of file token type
*/
EOFTokenType = 48,
/**
* important token type
*/
ImportantTokenType = 49,
/**
* color token type
*/
ColorTokenType = 50,
/**
* attribute token type
*/
AttrTokenType = 51,
/**
* bad comment token type
*/
BadCommentTokenType = 52,
/**
* bad cdo token type
*/
BadCdoTokenType = 53,
/**
* bad URL token type
*/
BadUrlTokenType = 54,
/**
* bad string token type
*/
BadStringTokenType = 55,
/**
* binary expression token type
*/
BinaryExpressionTokenType = 56,
/**
* unary expression token type
*/
UnaryExpressionTokenType = 57,
/**
* flex token type
*/
FlexTokenType = 58,
/**
* token list token type
*/
ListToken = 59,
/**
* addition token type
*/
Add = 60,
/**
* multiplication token type
*/
Mul = 61,
/**
* division token type
*/
Div = 62,
/**
* subtraction token type
*/
Sub = 63,
/**
* column combinator token type
*/
ColumnCombinatorTokenType = 64,
/**
* contain match token type
*/
ContainMatchTokenType = 65,
/**
* start match token type
*/
StartMatchTokenType = 66,
/**
* end match token type
*/
EndMatchTokenType = 67,
/**
* match expression token type
*/
MatchExpressionTokenType = 68,
/**
* namespace attribute token type
*/
NameSpaceAttributeTokenType = 69,
/**
* fraction token type
*/
FractionTokenType = 70,
/**
* identifier list token type
*/
IdenListTokenType = 71,
/**
* grid template function token type
*/
GridTemplateFuncTokenType = 72,
/**
* keyframe rule node type
*/
KeyFramesRuleNodeType = 73,
/**
* class selector token type
*/
ClassSelectorTokenType = 74,
/**
* universal selector token type
*/
UniversalSelectorTokenType = 75,
/**
* child combinator token type
*/
ChildCombinatorTokenType = 76,// >
/**
* descendant combinator token type
*/
DescendantCombinatorTokenType = 77,// whitespace
/**
* next sibling combinator token type
*/
NextSiblingCombinatorTokenType = 78,// +
/**
* subsequent sibling combinator token type
*/
SubsequentSiblingCombinatorTokenType = 79,// ~
/**
* nesting selector token type
*/
NestingSelectorTokenType = 80,// &
/**
* invalid rule token type
*/
InvalidRuleTokenType = 81,
/**
* invalid class selector token type
*/
InvalidClassSelectorTokenType = 82,
/**
* invalid attribute token type
*/
InvalidAttrTokenType = 83,
/**
* invalid at rule token type
*/
InvalidAtRuleTokenType = 84,
/**
* media query condition token type
*/
MediaQueryConditionTokenType = 85,
/**
* media feature token type
*/
MediaFeatureTokenType = 86,
/**
* media feature only token type
*/
MediaFeatureOnlyTokenType = 87,
/**
* media feature not token type
*/
MediaFeatureNotTokenType = 88,
/**
* media feature and token type
*/
MediaFeatureAndTokenType = 89,
/**
* media feature or token type
*/
MediaFeatureOrTokenType = 90,
/**
* pseudo page token type
*/
PseudoPageTokenType = 91,
/**
* pseudo element token type
*/
PseudoElementTokenType = 92,
/**
* keyframe at rule node type
*/
KeyframesAtRuleNodeType = 93,
/**
* invalid declaration node type
*/
InvalidDeclarationNodeType = 94,
/**
* alias for time token type
*/
Time = 25,
/**
* alias for identifier token type
*/
Iden = 7,
/**
* alias for end of file token type
*/
EOF = 48,
/**
* alias for hash token type
*/
Hash = 28,
/**
* alias for flex token type
*/
Flex = 58,
/**
* alias for angle token type
*/
Angle = 24,
/**
* alias for color token type
*/
Color = 50,
/**
* alias for comma token type
*/
Comma = 9,
/**
* alias for string token type
*/
String = 20,
/**
* alias for length token type
*/
Length = 23,
/**
* alias for number token type
*/
Number = 12,
/**
* alias for percentage token type
*/
Perc = 14,
/**
* alias for literal token type
*/
Literal = 6,
/**
* alias for comment token type
*/
Comment = 0,
/**
* alias for url function token type
*/
UrlFunc = 18,
/**
* alias for dimension token type
*/
Dimension = 22,
/**
* alias for frequency token type
*/
Frequency = 26,
/**
* alias for resolution token type
*/
Resolution = 27,
/**
* alias for whitespace token type
*/
Whitespace = 36,
/**
* alias for identifier list token type
*/
IdenList = 71,
/**
* alias for dashed identifier token type
*/
DashedIden = 8,
/**
* alias for grid template function token type
*/
GridTemplateFunc = 72,
/**
* alias for image function token type
*/
ImageFunc = 19,
/**
* alias for comment node type
*/
CommentNodeType = 0,
/**
* alias for cdata section node type
*/
CDOCOMMNodeType = 1,
/**
* alias for timing function token type
*/
TimingFunction = 17,
/**
* alias for timeline function token type
*/
TimelineFunction = 16
}
/**
* supported color types enum
*/
declare enum ColorType {
/**
* system colors
*/
SYS = 0,
/**
* deprecated system colors
*/
DPSYS = 1,
/**
* colors as literals
*/
LIT = 2,
/**
* colors as hex values
*/
HEX = 3,
/**
* colors as rgb values
*/
RGBA = 4,
/**
* colors as hsl values
*/
HSLA = 5,
/**
* colors as hwb values
*/
HWB = 6,
/**
* colors as cmyk values
*/
CMYK = 7,
/**
* colors as oklab values
* */
OKLAB = 8,
/**
* colors as oklch values
* */
OKLCH = 9,
/**
* colors as lab values
*/
LAB = 10,
/**
* colors as lch values
*/
LCH = 11,
/**
* colors using color() function
*/
COLOR = 12,
/**
* color using srgb values
*/
SRGB = 13,
/**
* color using prophoto-rgb values
*/
PROPHOTO_RGB = 14,
/**
* color using a98-rgb values
*/
A98_RGB = 15,
/**
* color using rec2020 values
*/
REC2020 = 16,
/**
* color using display-p3 values
*/
DISPLAY_P3 = 17,
/**
* color using srgb-linear values
*/
SRGB_LINEAR = 18,
/**
* color using xyz-d50 values
*/
XYZ_D50 = 19,
/**
* color using xyz-d65 values
*/
XYZ_D65 = 20,
/**
* light-dark() color function
*/
LIGHT_DARK = 21,
/**
* color-mix() color function
*/
COLOR_MIX = 22,
/**
* alias for rgba
*/
RGB = 4,
/**
* alias for hsl
*/
HSL = 5,
/**
* alias for xyz-d65
*/
XYZ = 20,
/**
* alias for cmyk
*/
DEVICE_CMYK = 7
}
/**
* apply minification rules to the ast tree
* @param ast
* @param options
* @param recursive
* @param errors
* @param nestingContent
*
* @private
*/
declare function minify(ast: AstNode$1, options: ParserOptions | MinifyFeatureOptions, recursive: boolean, errors?: ErrorDescription[], nestingContent?: boolean): AstNode$1;
/**
* options for the walk function
*/
declare enum WalkerOptionEnum {
/**
* ignore the current node and its children
*/
Ignore = 1,
/**
* stop walking the tree
*/
Stop = 2,
/**
* ignore the current node and process its children
*/
Children = 4,
/**
* ignore the current node children
*/
IgnoreChildren = 8
}
/**
* event types for the walkValues function
*/
declare enum WalkerEvent {
/**
* enter node
*/
Enter = 1,
/**
* leave node
*/
Leave = 2
}
/**
* walk ast nodes
* @param node initial node
* @param filter control the walk process
* @param reverse walk in reverse order
*
* ```ts
*
* import {walk} from '@tbela99/css-parser';
*
* const css = `
* body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
*
* html,
* body {
* line-height: 1.474;
* }
*
* .ruler {
*
* height: 10px;
* }
* `;
*
* for (const {node, parent, root} of walk(ast)) {
*
* // do something with node
* }
* ```
*
* Using a {@link filter} function to control the ast traversal. the filter function returns a value of type {@link WalkerOption}.
*
* ```ts
* import {EnumToken, transform, walk, WalkerOptionEnum} from '@tbela99/css-parser';
*
* const css = `
* body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
*
* html,
* body {
* line-height: 1.474;
* }
*
* .ruler {
*
* height: 10px;
* }
* `;
*
* function filter(node) {
*
* if (node.typ == EnumToken.AstRule && node.sel.includes('html')) {
*
* // skip the children of the current node
* return WalkerOptionEnum.IgnoreChildren;
* }
* }
*
* const result = await transform(css);
* for (const {node} of walk(result.ast, filter)) {
*
* console.error([EnumToken[node.typ]]);
* }
*
* // [ "StyleSheetNodeType" ]
* // [ "RuleNodeType" ]
* // [ "DeclarationNodeType" ]
* // [ "RuleNodeType" ]
* // [ "DeclarationNodeType" ]
* // [ "RuleNodeType" ]
* // [ "DeclarationNodeType" ]
* ```
*/
declare function walk(node: AstNode$1, filter?: WalkerFilter | null, reverse?: boolean): Generator<WalkResult>;
/**
* walk ast node value tokens
* @param values
* @param root
* @param filter
* @param reverse
*
* Example:
*
* ```ts
*
* import {AstDeclaration, EnumToken, transform, walkValues} from '@tbela99/css-parser';
*
* const css = `
* body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
* `;
*
* const result = await transform(css);
* const declaration = result.ast.chi[0].chi[0] as AstDeclaration;
*
* // walk the node attribute's tokens in reverse order
* for (const {value} of walkValues(declaration.val, null, null,true)) {
*
* console.error([EnumToken[value.typ], value.val]);
* }
*
* // [ "Color", "color" ]
* // [ "FunctionTokenType", "calc" ]
* // [ "Number", 0.15 ]
* // [ "Add", undefined ]
* // [ "Iden", "b" ]
* // [ "Whitespace", undefined ]
* // [ "FunctionTokenType", "calc" ]
* // [ "Number", 0.24 ]
* // [ "Add", undefined ]
* // [ "Iden", "g" ]
* // [ "Whitespace", undefined ]
* // [ "Iden", "r" ]
* // [ "Whitespace", undefined ]
* // [ "Iden", "display-p3" ]
* // [ "Whitespace", undefined ]
* // [ "FunctionTokenType", "var" ]
* // [ "DashedIden", "--base-color" ]
* // [ "Whitespace", undefined ]
* // [ "Iden", "from" ]
* ```
*/
declare function walkValues(values: Token$1[], root?: AstNode$1 | Token$1 | null, filter?: WalkerValueFilter | null | {
event?: WalkerEvent;
fn?: WalkerValueFilter;
type?: EnumToken | EnumToken[] | ((token: Token$1) => boolean);
}, reverse?: boolean): Generator<WalkAttributesResult>;
/**
* expand css nesting ast nodes
* @param ast
*
* @private
*/
declare function expand(ast: AstStyleSheet | AstAtRule | AstRule): AstNode$1;
/**
*
* @param token
* @param options
*
* @private
*/
declare function renderToken(token: Token$1, options?: RenderOptions, cache?: {
[key: string]: any;
}, reducer?: (acc: string, curr: Token$1) => string, errors?: ErrorDescription[]): string;
/**
* Source map class
* @internal
*/
declare class SourceMap {
#private;
/**
* Last location
*/
lastLocation: Location | null;
/**
* Add a location
* @param source
* @param original
*/
add(source: Location, original: Location): void;
/**
* Convert to URL encoded string
*/
toUrl(): string;
/**
* Convert to JSON object
*/
toJSON(): SourceMapObject;
}
/**
* parse a string as an array of declaration nodes
* @param declaration
*
* Example:
* ````ts
*
* const declarations = await parseDeclarations('color: red; background: blue');
* console.log(declarations);
* ```
*/
declare function parseDeclarations(declaration: string): Promise<Array<AstDeclaration | AstComment>>;
/**
* parse css string and return an array of tokens
* @param src
* @param options
*
* @private
*
* Example:
*
* ```ts
*
* import {parseString} from '@tbela99/css-parser';
*
* let tokens = parseString('body { color: red; }');
* console.log(tokens);
*
* tokens = parseString('#c322c980');
* console.log(tokens);
* ```
*/
declare function parseString(src: string, options?: {
location: boolean;
}): Token$1[];
/**
* parse function tokens in a token array
* @param tokens
* @param options
*
* Example:
*
* ```ts
*
* import {parseString, parseTokens} from '@tbela99/css-parser';
*
* let tokens = parseString('body { color: red; }');
* console.log(parseTokens(tokens));
*
* tokens = parseString('#c322c980');
* console.log(parseTokens(tokens));
* ```
*
* @private
*/
declare function parseTokens(tokens: Token$1[], options?: ParseTokenOptions): Token$1[];
/**
* Literal token
*/
export declare interface LiteralToken extends BaseToken {
typ: EnumToken.LiteralTokenType;
val: string;
}
/**
* Class selector token
*/
export declare interface ClassSelectorToken extends BaseToken {
typ: EnumToken.ClassSelectorTokenType;
val: string;
}
/**
* Invalid class selector token
*/
export declare interface InvalidClassSelectorToken extends BaseToken {
typ: EnumToken.InvalidClassSelectorTokenType;
val: string;
}
/**
* Universal selector token
*/
export declare interface UniversalSelectorToken extends BaseToken {
typ: EnumToken.UniversalSelectorTokenType;
}
/**
* Ident token
*/
export declare interface IdentToken extends BaseToken {
typ: EnumToken.IdenTokenType,
val: string;
}
/**
* Ident list token
*/
export declare interface IdentListToken extends BaseToken {
typ: EnumToken.IdenListTokenType,
val: string;
}
/**
* Dashed ident token
*/
export declare interface DashedIdentToken extends BaseToken {
typ: EnumToken.DashedIdenTokenType,
val: string;
}
/**
* Comma token
*/
export declare interface CommaToken extends BaseToken {
typ: EnumToken.CommaTokenType
}
/**
* Colon token
*/
export declare interface ColonToken extends BaseToken {
typ: EnumToken.ColonTokenType
}
/**
* Semicolon token
*/
export declare interface SemiColonToken extends BaseToken {
typ: EnumToken.SemiColonTokenType
}
/**
* Nesting selector token
*/
export declare interface NestingSelectorToken extends BaseToken {
typ: EnumToken.NestingSelectorTokenType
}
/**
* Number token
*/
export declare interface NumberToken extends BaseToken {
typ: EnumToken.NumberTokenType,
val: number | FractionToken;
}
/**
* At rule token
*/
export declare interface AtRuleToken extends BaseToken {
typ: EnumToken.AtRuleTokenType,
val: string;
pre: string;
}
/**
* Percentage token
*/
export declare interface PercentageToken extends BaseToken {
typ: EnumToken.PercentageTokenType,
val: number | FractionToken;
}
/**
* Flex token
*/
export declare interface FlexToken extends BaseToken {
typ: EnumToken.FlexTokenType,
val: number | FractionToken;
}
/**
* Function token
*/
export declare interface FunctionToken extends BaseToken {
typ: EnumToken.FunctionTokenType,
val: string;
chi: Token$1[];
}
/**
* Grid template function token
*/
export declare interface GridTemplateFuncToken extends BaseToken {
typ: EnumToken.GridTemplateFuncTokenType,
val: string;
chi: Token$1[];
}
/**
* Function URL token
*/
export declare interface FunctionURLToken extends BaseToken {
typ: EnumToken.UrlFunctionTokenType,
val: 'url';
chi: Array<UrlToken | CommentToken>;
}
/**
* Function image token
*/
export declare interface FunctionImageToken extends BaseToken {
typ: EnumToken.ImageFunctionTokenType,
val: 'linear-gradient' | 'radial-gradient' | 'repeating-linear-gradient' | 'repeating-radial-gradient' | 'conic-gradient' | 'image' | 'image-set' | 'element' | 'cross-fade';
chi: Array<UrlToken | CommentToken>;
}
/**
* Timing function token
*/
export declare interface TimingFunctionToken extends BaseToken {
typ: EnumToken.TimingFunctionTokenType;
val: string;
chi: Token$1[];
}
/**
* Timeline function token
*/
export declare interface TimelineFunctionToken extends BaseToken {
typ: EnumToken.TimelineFunctionTokenType;
val: string;
chi: Token$1[];
}
/**
* String token
*/
export declare interface StringToken extends BaseToken {
typ: EnumToken.StringTokenType;
val: string;
}
/**
* Bad string token
*/
export declare interface BadStringToken extends BaseToken {
typ: EnumToken.BadStringTokenType;
val: string;
}
/**
* Unclosed string token
*/
export declare interface UnclosedStringToken extends BaseToken {
typ: EnumToken.UnclosedStringTokenType;
val: string;
}
/**
* Dimension token
*/
export declare interface DimensionToken extends BaseToken {
typ: EnumToken.DimensionTokenType;
val: number | FractionToken;
unit: string;
}
/**
* Length token
*/
export declare interface LengthToken extends BaseToken {
typ: EnumToken.LengthTokenType;
val: number | FractionToken;
unit: string;
}
/**
* Angle token
*/
export declare interface AngleToken extends BaseToken {
typ: EnumToken.AngleTokenType;
val: number | FractionToken;
unit: string;
}
/**
* Time token
*/
export declare interface TimeToken extends BaseToken {
typ: EnumToken.TimeTokenType;
val: number | FractionToken;
unit: 'ms' | 's';
}
/**
* Frequency token
*/
export declare interface FrequencyToken extends BaseToken {
typ: EnumToken.FrequencyTokenType;
val: number | FractionToken;
unit: 'Hz' | 'Khz';
}
/**
* Resolution token
*/
export declare interface ResolutionToken extends BaseToken {
typ: EnumToken.ResolutionTokenType;
val: number | FractionToken;
unit: 'dpi' | 'dpcm' | 'dppx' | 'x';
}
/**
* Hash token
*/
export declare interface HashToken extends BaseToken {
typ: EnumToken.HashTokenType;
val: string;
}
/**
* Block start token
*/
export declare interface BlockStartToken extends BaseToken {
typ: EnumToken.BlockStartTokenType
}
/**
* Block end token
*/
export declare interface BlockEndToken extends BaseToken {
typ: EnumToken.BlockEndTokenType
}
/**
* Attribute start token
*/
export declare interface AttrStartToken extends BaseToken {
typ: EnumToken.AttrStartTokenType;
chi?: Token$1[];
}
/**
* Attribute end token
*/
export declare interface AttrEndToken extends BaseToken {
typ: EnumToken.AttrEndTokenType
}
/**
* Parenthesis start token
*/
export declare interface ParensStartToken extends BaseToken {
typ: EnumToken.StartParensTokenType;
}
/**
* Parenthesis end token
*/
export declare interface ParensEndToken extends BaseToken {
typ: EnumToken.EndParensTokenType
}
/**
* Parenthesis token
*/
export declare interface ParensToken extends BaseToken {
typ: EnumToken.ParensTokenType;
chi: Token$1[];
}
/**
* Whitespace token
*/
export declare interface WhitespaceToken extends BaseToken {
typ: EnumToken.WhitespaceTokenType
}
/**
* Comment token
*/
export declare interface CommentToken extends BaseToken {
typ: EnumToken.CommentTokenType;
val: string;
}
/**
* Bad comment token
*/
export declare interface BadCommentToken extends BaseToken {
typ: EnumToken.BadCommentTokenType;
val: string;
}
/**
* CDO comment token
*/
export declare interface CDOCommentToken extends BaseToken {
typ: EnumToken.CDOCOMMTokenType;
val: string;
}
/**
* Bad CDO comment token
*/
export declare interface BadCDOCommentToken extends BaseToken {
typ: EnumToken.BadCdoTokenType;
val: string;
}
/**
* Include match token
*/
export declare interface IncludeMatchToken extends BaseToken {
typ: EnumToken.IncludeMatchTokenType;
// val: '~=';
}
/**
* Dash match token
*/
export declare interface DashMatchToken extends BaseToken {
typ: EnumToken.DashMatchTokenType;
// val: '|=';
}
/**
* Equal match token
*/
export declare interface EqualMatchToken extends BaseToken {
typ: EnumToken.EqualMatchTokenType;
// val: '|=';
}
/**
* Start match token
*/
export declare interface StartMatchToken extends BaseToken {
typ: EnumToken.StartMatchTokenType;
// val: '^=';
}
/**
* End match token
*/
export declare interface EndMatchToken extends BaseToken {
typ: EnumToken.EndMatchTokenType;
// val: '|=';
}
/**
* Contain match token
*/
export declare interface ContainMatchToken extends BaseToken {
typ: EnumToken.ContainMatchTokenType;
// val: '|=';
}
/**
* Less than token
*/
export declare interface LessThanToken extends BaseToken {
typ: EnumToken.LtTokenType;
}
/**
* Less than or equal token
*/
export declare interface LessThanOrEqualToken extends BaseToken {
typ: EnumToken.LteTokenType;
}
/**
* Greater than token
*/
export declare interface GreaterThanToken extends BaseToken {
typ: EnumToken.GtTokenType;
}
/**
* Greater than or equal token
*/
export declare interface GreaterThanOrEqualToken extends BaseToken {
typ: EnumToken.GteTokenType;
}
/**
* Column combinator token
*/
export declare interface ColumnCombinatorToken extends BaseToken {
typ: EnumToken.ColumnCombinatorTokenType;
}
/**
* Pseudo class token
*/
export declare interface PseudoClassToken extends BaseToken {
typ: EnumToken.PseudoClassTokenType;
val: string;
}
/**
* Pseudo element token
*/
export declare interface PseudoElementToken extends BaseToken {
typ: EnumToken.PseudoElementTokenType;
val: string;
}
/**
* Pseudo page token
*/
export declare interface PseudoPageToken extends BaseToken {
typ: EnumToken.PseudoPageTokenType;
val: string;
}
/**
* Pseudo class function token
*/
export declare interface PseudoClassFunctionToken extends BaseToken {
typ: EnumToken.PseudoClassFuncTokenType;
val: string;
chi: Token$1[];
}
/**
* Delim token
*/
export declare interface DelimToken extends BaseToken {
typ: EnumToken.DelimTokenType;
}
/**
* Bad URL token
*/
export declare interface BadUrlToken extends BaseToken {
typ: EnumToken.BadUrlTokenType,
val: string;
}
/**
* URL token
*/
export declare interface UrlToken extends BaseToken {
typ: EnumToken.UrlTokenTokenType,
val: string;
}
/**
* EOF token
*/
export declare interface EOFToken extends BaseToken {
typ: EnumToken.EOFTokenType;
}
/**
* Important token
*/
export declare interface ImportantToken extends BaseToken {
typ: EnumToken.ImportantTokenType;
}
/**
* Color token
*/
export declare interface ColorToken extends BaseToken {
typ: EnumToken.ColorTokenType;
val: string;
kin: ColorType;
chi?: Token$1[];
/* calculated */
cal?: 'rel' | 'mix';
}
/**
* Attribute token
*/
export declare interface AttrToken extends BaseToken {
typ: EnumToken.AttrTokenType,
chi: Token$1[]
}
/**
* Invalid attribute token
*/
export declare interface InvalidAttrToken extends BaseToken {
typ: EnumToken.InvalidAttrTokenType,
chi: Token$1[]
}
/**
* Child combinator token
*/
export declare interface ChildCombinatorToken extends BaseToken {
typ: EnumToken.ChildCombinatorTokenType
}
/**
* Media feature token
*/
export declare interface MediaFeatureToken extends BaseToken {
typ: EnumToken.MediaFeatureTokenType,
val: string;
}
/**
* Media feature not token
*/
export declare interface MediaFeatureNotToken extends BaseToken {
typ: EnumToken.MediaFeatureNotTokenType,
val: Token$1;
}
/**
* Media feature only token
*/
export declare interface MediaFeatureOnlyToken extends BaseToken {
typ: EnumToken.MediaFeatureOnlyTokenType,
val: Token$1;
}
/**
* Media feature and token
*/
export declare interface MediaFeatureAndToken extends BaseToken {
typ: EnumToken.MediaFeatureAndTokenType;
}
/**
* Media feature or token
*/
export declare interface MediaFeatureOrToken extends BaseToken {
typ: EnumToken.MediaFeatureOrTokenType;
}
/**
* Media query condition token
*/
export declare interface MediaQueryConditionToken extends BaseToken {
typ: EnumToken.MediaQueryConditionTokenType,
l: Token$1,
op: ColonToken | GreaterThanToken | LessThanToken | GreaterThanOrEqualToken | LessThanOrEqualToken,
r: Token$1[]
}
/**
* Descendant combinator token
*/
export declare interface DescendantCombinatorToken extends BaseToken {
typ: EnumToken.DescendantCombinatorTokenType
}
/**
* Next sibling combinator token
*/
export declare interface NextSiblingCombinatorToken extends BaseToken {
typ: EnumToken.NextSiblingCombinatorTokenType
}
/**
* Subsequent sibling combinator token
*/
export declare interface SubsequentCombinatorToken extends BaseToken {
typ: EnumToken.SubsequentSiblingCombinatorTokenType
}
/**
* Add token
*/
export declare interface AddToken extends BaseToken {
typ: EnumToken.Add;
}
/**
* Sub token
*/
export declare interface SubToken extends BaseToken {
typ: EnumToken.Sub;
}
/**
* Div token
*/
export declare interface DivToken extends BaseToken {
typ: EnumToken.Div;
}
/**
* Mul token
*/
export declare interface MulToken extends BaseToken {
typ: EnumToken.Mul;
}
/**
* Unary expression token
*/
export declare interface UnaryExpression extends BaseToken {
typ: EnumToken.UnaryExpressionTokenType
sign: EnumToken.Add | EnumToken.Sub;
val: UnaryExpressionNode;
}
/**
* Fraction token
*/
export declare interface FractionToken extends BaseToken {
typ: EnumToken.FractionTokenType;
l: NumberToken;
r: NumberToken;
}
/**
* Binary expression token
*/
export declare interface BinaryExpressionToken extends BaseToken {
typ: EnumToken.BinaryExpressionTokenType
op: EnumToken.Add | EnumToken.Sub | EnumToken.Div | EnumToken.Mul;
l: BinaryExpressionNode | Token$1;
r: BinaryExpressionNode | Token$1;
}
/**
* Match expression token
*/
export declare interface MatchExpressionToken extends BaseToken {
typ: EnumToken.MatchExpressionTokenType
op: EqualMatchToken | DashMatchToken | StartMatchToken | ContainMatchToken | EndMatchToken | IncludeMatchToken;
l: Token$1;
r: Token$1;
attr?: 'i' | 's';
}
/**
* Name space attribute token
*/
export declare interface NameSpaceAttributeToken extends BaseToken {
typ: EnumToken.NameSpaceAttributeTokenType
l?: Token$1;
r: Token$1;
}
/**
* List token
*/
export declare interface ListToken extends BaseToken {
typ: EnumToken.ListToken
chi: Token$1[];
}
/**
* Unary expression node
*/
export declare type UnaryExpressionNode =
BinaryExpressionNode
| NumberToken
| DimensionToken
| TimeToken
| LengthToken
| AngleToken
| FrequencyToken;
/**
* Binary expression node
*/
export declare type BinaryExpressionNode = NumberToken | DimensionToken | PercentageToken | FlexToken | FractionToken |
AngleToken | LengthToken | FrequencyToken | BinaryExpressionToken | FunctionToken | ParensToken;
/**
* Token
*/
export declare type Token$1 =
InvalidClassSelectorToken
| InvalidAttrToken
|
LiteralToken
| IdentToken
| IdentListToken
| DashedIdentToken
| CommaToken
| ColonToken
| SemiColonToken
| ClassSelectorToken
| UniversalSelectorToken
| ChildCombinatorToken
| DescendantCombinatorToken
| NextSiblingCombinatorToken
| SubsequentCombinatorToken
| ColumnCombinatorToken
| NestingSelectorToken
|
MediaQueryConditionToken
| MediaFeatureToken
| MediaFeatureNotToken
| MediaFeatureOnlyToken
| MediaFeatureAndToken
| MediaFeatureOrToken
| AstDeclaration
|
NumberToken
| AtRuleToken
| PercentageToken
| FlexToken
| FunctionURLToken
| FunctionImageToken
| TimingFunctionToken
| TimelineFunctionToken
| FunctionToken
| GridTemplateFuncToken
| DimensionToken
| LengthToken
|
AngleToken
| StringToken
| TimeToken
| FrequencyToken
| ResolutionToken
|
UnclosedStringToken
| HashToken
| BadStringToken
| BlockStartToken
| BlockEndToken
|
AttrStartToken
| AttrEndToken
| ParensStartToken
| ParensEndToken
| ParensToken
| CDOCommentToken
|
BadCDOCommentToken
| CommentToken
| BadCommentToken
| WhitespaceToken
| IncludeMatchToken
| StartMatchToken
| EndMatchToken
| ContainMatchToken
| MatchExpressionToken
| NameSpaceAttributeToken
|
DashMatchToken
| EqualMatchToken
| LessThanToken
| LessThanOrEqualToken
| GreaterThanToken
| GreaterThanOrEqualToken
|
ListToken
| PseudoClassToken
| PseudoPageToken
| PseudoElementToken
| PseudoClassFunctionToken
| DelimToken
| BinaryExpressionToken
| UnaryExpression
| FractionToken
|
AddToken
| SubToken
| DivToken
| MulToken
|
BadUrlToken
| UrlToken
| ImportantToken
| ColorToken
| AttrToken
| EOFToken;
declare enum ValidationTokenEnum {
Root = 0,
Keyword = 1,
PropertyType = 2,
DeclarationType = 3,
AtRule = 4,
ValidationFunctionDefinition = 5,
OpenBracket = 6,
CloseBracket = 7,
OpenParenthesis = 8,
CloseParenthesis = 9,
Comma = 10,
Pipe = 11,
Column = 12,
Star = 13,
OpenCurlyBrace = 14,
CloseCurlyBrace = 15,
HashMark = 16,
QuestionMark = 17,
Function = 18,
Number = 19,
Whitespace = 20,
Parenthesis = 21,
Bracket = 22,
Block = 23,
AtLeastOnce = 24,
Separator = 25,
Exclamation = 26,
Ampersand = 27,
PipeToken = 28,
ColumnToken = 29,
AmpersandToken = 30,
Parens = 31,
PseudoClassToken = 32,
PseudoClassFunctionToken = 33,
StringToken = 34,
AtRuleDefinition = 35,
DeclarationNameToken = 36,
DeclarationDefinitionToken = 37,
SemiColon = 38,
Character = 39,
InfinityToken = 40
}
declare const enum ValidationSyntaxGroupEnum {
Declarations = "declarations",
Functions = "functions",
Syntaxes = "syntaxes",
Selectors = "selectors",
AtRules = "atRules"
}
interface Position$1 {
ind: number;
lin: number;
col: number;
}
interface ValidationToken$1 {
typ: ValidationTokenEnum;
pos: Position$1;
isList?: boolean;
text?: string;
isRepeatable?: boolean;
atLeastOnce?: boolean;
isOptional?: boolean;
isRepeatableGroup?: boolean;
occurence?: {
min: number;
max: number | null;
}
}
export declare interface ValidationSyntaxNode {
syntax: string;
ast?: ValidationToken$1[];
}
interface ValidationSelectorOptions extends ValidationOptions {
nestedSelector?: boolean;
}
export declare interface ValidationConfiguration {
[ValidationSyntaxGroupEnum.Declarations]: ValidationSyntaxNode;
[ValidationSyntaxGroupEnum.Functions]: ValidationSyntaxNode;
[ValidationSyntaxGroupEnum.Syntaxes]: ValidationSyntaxNode;
[ValidationSyntaxGroupEnum.Selectors]: ValidationSyntaxNode;
[ValidationSyntaxGroupEnum.AtRules]: ValidationSyntaxNode;
}
//= Record<keyof ValidationSyntaxGroupEnum, ValidationSyntaxNode>;
interface ValidationResult {
valid: SyntaxValidationResult;
node: AstNode$1 | Token$1 | null;
syntax: ValidationToken$1 | string | null;
error: string;
cycle?: boolean;
}
interface ValidationSyntaxResult extends ValidationResult {
syntax: ValidationToken$1 | string | null;
context: Context<Token$1> | Token$1[];
}
interface Context<Type> {
index: number;
/**
* The length of the context tokens to be consumed
*/
readonly length: number;
current<Type>(): Type | null;
update<Type>(context: Context<Type>): void;
consume<Type>(token: Type, howMany?: number): boolean;
peek<Type>(): Type | null;
// tokens<Type>(): Type[];
next<Type>(): Type | null;
consume<Type>(token: Type, howMany?: number): boolean;
slice<Type>(): Type[];
clone<Type>(): Context<Type>;
done(): boolean;
}
/**
* Converts a color to another color space
* @param token
* @param to
*
* @private
*
* <code>
*
* const token = {typ: EnumToken.ColorTokenType, kin: ColorType.HEX, val: '#F00'}
* const result = convertColor(token, ColorType.LCH);
*
* </code>
*/
declare function convertColor(token: ColorToken, to: ColorType): ColorToken | null;
/**
* Calculate the distance between two okLab colors.
* @param okLab1
* @param okLab2
*
* @private
*/
declare function okLabDistance(okLab1: [number, number, number], okLab2: [number, number, number]): number;
/**
* Check if two colors are close in okLab space.
* @param color1
* @param color2
* @param threshold
*
* @private
*/
declare function isOkLabClose(color1: ColorToken, color2: ColorToken, threshold?: number): boolean;
/**
* Position
*/
export declare interface Position {
/**
* index in the source
*/
ind: number;
/**
* line number
*/
lin: number;
/**
* column number
*/
col: number;
}
/**
* token or node location
*/
export declare interface Location {
/**
* start position
*/
sta: Position;
/**
* end position
*/
end: Position;
/**
* source file
*/
src: string;
}
export declare interface BaseToken {
/**
* token type
*/
typ: EnumToken;
/**
* location info
*/
loc?: Location;
/**
* prelude or selector tokens
*/
tokens?: Token$1[] | null;
/**
* parent node
*/
parent?: AstAtRule | astRule | AstKeyframesAtRule | AstKeyFrameRule | AstInvalidRule | AstInvalidAtRule | null;
/**
* @private
*/
validSyntax?: boolean;
}
/**
* comment node
*/
export declare interface AstComment extends BaseToken {
typ: EnumToken.CommentNodeType | EnumToken.CDOCOMMNodeType,
tokens?: null;
val: string;
}
/**
* declaration node
*/
export declare interface AstDeclaration extends BaseToken {
nam: string,
tokens?: null;
val: Token$1[];
typ: EnumToken.DeclarationNodeType
}
/**
* rule node
*/
export declare interface AstRule extends BaseToken {
typ: EnumToken.RuleNodeType;
sel: string;
chi: Array<AstDeclaration | AstComment | AstRule | AstAtRule | AstInvalidRule | AstInvalidDeclaration | AstInvalidAtRule>;
optimized?: OptimizedSelector | null;
raw?: RawSelectorTokens | null;
}
/**
* invalid rule node
*/
export declare interface AstInvalidRule extends BaseToken {
typ: EnumToken.InvalidRuleTokenType;
sel: string;
chi: Array<AstNode$1>;
}
/**
* invalid declaration node
*/
export declare interface AstInvalidDeclaration extends BaseToken {
typ: EnumToken.InvalidDeclarationNodeType;
tokens?: null;
val: Array<Token$1>;
}
/**
* invalid at rule node
*/
export declare interface AstInvalidAtRule extends BaseToken {
typ: EnumToken.InvalidAtRuleTokenType;
nam: string;
val: string;
chi?: Array<AstNode$1>;
}
/**
* keyframe rule node
*/
export declare interface AstKeyFrameRule extends BaseToken {
typ: EnumToken.KeyFramesRuleNodeType;
sel: string;
chi: Array<AstDeclaration | AstComment | AstInvalidDeclaration>;
optimized?: OptimizedSelector;
raw?: RawSelectorTokens;
tokens?: Token$1[]
}
/**
* raw selector tokens
*/
export declare type RawSelectorTokens = string[][];
/**
* optimized selector
*
* @private
*/
export declare interface OptimizedSelector {
match: boolean;
optimized: string[];
selector: string[][],
reducible: boolean;
}
/**
* optimized selector token
*
* @private
*/
export declare interface OptimizedSelectorToken {
match: boolean;
optimized: Token$1[];
selector: Token$1[][],
reducible: boolean;
}
/**
* at rule node
*/
export declare interface AstAtRule extends BaseToken {
typ: EnumToken.AtRuleNodeType,
nam: string;
val: string;
chi?: Array<AstDeclaration | AstInvalidDeclaration | AstComment> | Array<AstRule | AstComment>
}
/**
* keyframe rule node
*/
export declare interface AstKeyframesRule extends BaseToken {
typ: EnumToken.KeyFramesRuleNodeType;
sel: string;
chi: Array<AstDeclaration | AstInvalidDeclaration | AstComment | AstRuleList>;
optimized?: OptimizedSelector;
raw?: RawSelectorTokens;
}
/**
* keyframe at rule node
*/
export declare interface AstKeyframesAtRule extends BaseToken {
typ: EnumToken.KeyframesAtRuleNodeType,
nam: string;
val: string;
chi: Array<AstKeyframesRule | AstComment>;
}
/**
* rule list node
*/
export declare type AstRuleList =
AstStyleSheet
| AstAtRule
| AstRule
| AstKeyframesAtRule
| AstKeyFrameRule
| AstInvalidRule;
/**
* stylesheet node
*/
export declare interface AstStyleSheet extends BaseToken {
typ: EnumToken.StyleSheetNodeType,
chi: Array<AstRule | AstAtRule | astKeyframesAtRule | AstComment | AstInvalidAtRule | AstInvalidRule>;
tokens?: null;
}
/**
* ast node
*/
export declare type AstNode$1 =
AstStyleSheet
| AstRuleList
| AstComment
| AstAtRule
| AstRule
| AstDeclaration
| AstKeyframesAtRule
| AstKeyFrameRule
| AstInvalidRule
| AstInvalidAtRule
| AstInvalidDeclaration;
export declare type GenericVisitorResult<T> = T | T[] | Promise<T> | Promise<T[]> | null | Promise<null>;
export declare type GenericVisitorHandler<T> = ((node: T, parent?: AstNode | Token, root?: AstNode | Token) => GenericVisitorResult<T>);
export declare type GenericVisitorAstNodeHandlerMap<T> =
Record<string, GenericVisitorHandler<T>>
| GenericVisitorHandler<T>
| { type: WalkerEvent, handler: GenericVisitorHandler<T> }
| { type: WalkerEvent, handler: Record<string, GenericVisitorHandler<T>> };
export declare type ValueVisitorHandler = GenericVisitorHandler<Token>;
/**
* Declaration visitor handler
*/
export declare type DeclarationVisitorHandler = GenericVisitorHandler<AstDeclaration>;
/**
* Rule visitor handler
*/
export declare type RuleVisitorHandler = GenericVisitorHandler<AstRule>;
/**
* AtRule visitor handler
*/
export declare type AtRuleVisitorHandler = GenericVisitorHandler<AstAtRule>;
/**
* node visitor callback map
*
*/
export declare interface VisitorNodeMap {
/**
* at rule visitor
*
* Example: change media at-rule prelude
*
* ```ts
*
* import {transform, AstAtRule, ParserOptions} from "@tbela99/css-parser";
*
* const options: ParserOptions = {
*
* visitor: {
*
* AtRule: {
*
* media: (node: AstAtRule): AstAtRule => {
*
* node.val = '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)}}
* ```
*/
AtRule?: GenericVisitorAstNodeHandlerMap<AstAtRule>;
/**
* declaration visitor
*
* Example: add 'width: 3px' everytime a declaration with the name 'height' is found
*
* ```ts
*
* import {transform, parseDeclarations} from "@tbela99/css-parser";
*
* const options: ParserOptions = {
*
* removeEmpty: false,
* visitor: {
*
* Declaration: {
*
* // called only for height declaration
* height: (node: AstDeclaration): AstDeclaration[] => {
*
*
* return [
* node,
* {
*
* typ: EnumToken.DeclarationNodeType,
* nam: 'width',
* val: [
* <LengthToken>{
* typ: EnumToken.LengthTokenType,
* val: 3,
* unit: 'px'
* }
* ]
* }
* ];
* }
* }
* }
* };
*
* 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'
*
* ```ts
* import {AstDeclaration, ParserOptions, transform} from "../src/node.ts";
*
* const options: ParserOptions = {
*
* visitor: {
*
* // called for every declaration
* Declaration: (node: AstDeclaration): null => {
*
*
* 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}
* ```
*/
Declaration?: GenericVisitorAstNodeHandlerMap<AstDeclaration>;
/**
* rule visitor
*
* Example: add 'width: 3px' to every rule with the selector '.foo'
*
* ```ts
*
* import {transform, parseDeclarations} from "@tbela99/css-parser";
*
* const options: ParserOptions = {
*
* removeEmpty: false,
* visitor: {
*
* Rule: async (node: AstRule): Promise<AstRule | null> => {
*
* if (node.sel == '.foo') {
*
* node.chi.push(...await parseDeclarations('width: 3px'));
* return node;
* }
*
* return null;
* }
* }
* };
*
* const css = `
*
* .foo {
* .foo {
* }
* }
* `;
*
* console.debug(await transform(css, options));
*
* // .foo{width:3px;.foo{width:3px}}
* ```
*/
Rule?: GenericVisitorAstNodeHandlerMap<AstRule>;
KeyframesRule?: GenericVisitorAstNodeHandlerMap<AstKeyframesRule>;
KeyframesAtRule?: GenericVisitorAstNodeHandlerMap<AstKeyframesAtRule>;
/**
* value visitor
*/
Value?: GenericVisitorAstNodeHandlerMap<Token>;
/**
* generic token visitor. the key name is of type keyof EnumToken.
* generic tokens are called for every token of the specified type.
*
* ```ts
*
* import {transform, parse, parseDeclarations} from "@tbela99/css-parser";
*
* const options: ParserOptions = {
*
* inlineCssVariables: true,
* visitor: {
*
* // Stylesheet node visitor
* StyleSheetNodeType: async (node) => {
*
* // insert a new rule
* node.chi.unshift(await parse('html {--base-color: pink}').then(result => result.ast.chi[0]))
* },
* ColorTokenType: (node) => {
*
* // dump all color tokens
* // console.debug(node);
* },
* FunctionTokenType: (node) => {
*
* // dump all function tokens
* // console.debug(node);
* },
* DeclarationNodeType: (node) => {
*
* // dump all declaration nodes
* // console.debug(node);
* }
* }
* };
*
* const css = `
*
* body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); }
* `;
*
* console.debug(await transform(css, options));
*
* // body {color:#f3fff0}
* ```
*/
[key: keyof typeof EnumToken]: GenericVisitorAstNodeHandlerMap<Token> | GenericVisitorAstNodeHandlerMap<AstNode>;
}
export declare interface PropertyListOptions {
removeD