Skip to content

Commit

Permalink
feat: Allow custom syntax
Browse files Browse the repository at this point in the history
fixes #37
  • Loading branch information
nzakas committed Jan 4, 2025
1 parent 37984bd commit 633b8be
Showing 1 changed file with 100 additions and 1 deletion.
101 changes: 100 additions & 1 deletion typings/css-tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ declare module "css-tree" {
name: string;
}

export interface Condition extends CssNodeCommon {
type: "Condition";
kind: string;
children: List<CssNode>;
}

export interface ConditionPlain extends CssNodeCommon {
type: "Condition";
kind: string;
children: CssNodePlain[];
}

export interface Comment extends CssNodeCommon {
type: "Comment";
value: string;
Expand Down Expand Up @@ -470,6 +482,7 @@ declare module "css-tree" {
| ClassSelector
| Combinator
| Comment
| Condition
| Declaration
| DeclarationList
| Dimension
Expand Down Expand Up @@ -513,6 +526,7 @@ declare module "css-tree" {
| ClassSelector
| Combinator
| Comment
| ConditionPlain
| DeclarationPlain
| DeclarationListPlain
| Dimension
Expand Down Expand Up @@ -569,6 +583,10 @@ declare module "css-tree" {
}

export function parse(text: string, options?: ParseOptions): CssNode;
export function tokenize(
source: string,
onToken: (type: number, start: number, offset?: number) => void,
): void;

export interface GenerateHandlers {
children: (node: CssNode, delimiter?: (node: CssNode) => void) => void;
Expand Down Expand Up @@ -993,5 +1011,86 @@ declare module "css-tree" {
atrules?: Record<string, string> | undefined;
properties?: Record<string, string> | undefined;
types?: Record<string, string> | undefined;
}): { lexer: Lexer };
}): Syntax;

export interface ParseContext {
default: string;
stylesheet: string;
atrule: string;
atrulePrelude: (options: { atrule?: string }) => AtrulePrelude;
mediaQueryList: string;
mediaQuery: string;
condition: (options: { kind: string }) => Condition;
rule: string;
selectorList: string;
selector: string;
block: () => Block;
declarationList: string;
declaration: string;
value: string;
}

export interface NodeDefinition {
name: string;
walkContext?: string;
structure: Record<string, unknown>;
parse(...args: unknown[]): CssNode;
generate(node: CssNode): void;
}

export interface ScopeDefinition {
getNode(context: unknown): CssNode;
onWhitespace?(next: CssNode, children: NodeList): void;
}

export interface MDNSyntaxDefinition {
comment?: string;
references?: string[];
syntax: string;
}

export interface MDNAtruleDefinition {
prelude?: string;
descriptors?: Record<string, string | MDNSyntaxDefinition>;
comment?: string;
}

export interface SyntaxConfig {
parseContext: ParseContext;
generic: boolean;
cssWideKeywords: string[];
units: Record<string, string[]>;
types: Record<string, MDNSyntaxDefinition>;
atrules: Record<string, MDNAtruleDefinition>;
properties: Record<string, MDNSyntaxDefinition>;
node: Record<string, NodeDefinition>;
features: Record<
string,
{
selector?(): Selector;
style?(): Declaration;
}
>;
scope: Record<string, ScopeDefinition>;
}

export interface Syntax {
lexer: Lexer | null;
createLexer(config: SyntaxConfig): Lexer;

tokenize: typeof tokenize;
parse: typeof parse;
generate: typeof generate;

walk: typeof walk;
find: typeof find;
findLast: typeof findLast;
findAll: typeof findAll;

fromPlainObject: typeof fromPlainObject;
toPlainObject: typeof toPlainObject;
fork: typeof fork;
}

export function createSyntax(config: SyntaxConfig): Syntax;
}

0 comments on commit 633b8be

Please sign in to comment.