4.29 kBTypeScriptView Raw
1declare namespace postcssValueParser {
2 interface BaseNode {
3 /**
4 * The offset, inclusive, inside the CSS value at which the node starts.
5 */
6 sourceIndex: number;
7
8 /**
9 * The offset, exclusive, inside the CSS value at which the node ends.
10 */
11 sourceEndIndex: number;
12
13 /**
14 * The node's characteristic value
15 */
16 value: string;
17 }
18
19 interface ClosableNode {
20 /**
21 * Whether the parsed CSS value ended before the node was properly closed
22 */
23 unclosed?: true;
24 }
25
26 interface AdjacentAwareNode {
27 /**
28 * The token at the start of the node
29 */
30 before: string;
31
32 /**
33 * The token at the end of the node
34 */
35 after: string;
36 }
37
38 interface CommentNode extends BaseNode, ClosableNode {
39 type: "comment";
40 }
41
42 interface DivNode extends BaseNode, AdjacentAwareNode {
43 type: "div";
44 }
45
46 interface FunctionNode extends BaseNode, ClosableNode, AdjacentAwareNode {
47 type: "function";
48
49 /**
50 * Nodes inside the function
51 */
52 nodes: Node[];
53 }
54
55 interface SpaceNode extends BaseNode {
56 type: "space";
57 }
58
59 interface StringNode extends BaseNode, ClosableNode {
60 type: "string";
61
62 /**
63 * The quote type delimiting the string
64 */
65 quote: '"' | "'";
66 }
67
68 interface UnicodeRangeNode extends BaseNode {
69 type: "unicode-range";
70 }
71
72 interface WordNode extends BaseNode {
73 type: "word";
74 }
75
76 /**
77 * Any node parsed from a CSS value
78 */
79 type Node =
80 | CommentNode
81 | DivNode
82 | FunctionNode
83 | SpaceNode
84 | StringNode
85 | UnicodeRangeNode
86 | WordNode;
87
88 interface CustomStringifierCallback {
89 /**
90 * @param node The node to stringify
91 * @returns The serialized CSS representation of the node
92 */
93 (nodes: Node): string | undefined;
94 }
95
96 interface WalkCallback {
97 /**
98 * @param node The currently visited node
99 * @param index The index of the node in the series of parsed nodes
100 * @param nodes The series of parsed nodes
101 * @returns Returning `false` will prevent traversal of descendant nodes (only applies if `bubble` was set to `true` in the `walk()` call)
102 */
103 (node: Node, index: number, nodes: Node[]): void | boolean;
104 }
105
106 /**
107 * A CSS dimension, decomposed into its numeric and unit parts
108 */
109 interface Dimension {
110 number: string;
111 unit: string;
112 }
113
114 /**
115 * A wrapper around a parsed CSS value that allows for inspecting and walking nodes
116 */
117 interface ParsedValue {
118 /**
119 * The series of parsed nodes
120 */
121 nodes: Node[];
122
123 /**
124 * Walk all parsed nodes, applying a callback
125 *
126 * @param callback A visitor callback that will be executed for each node
127 * @param bubble When set to `true`, walking will be done inside-out instead of outside-in
128 */
129 walk(callback: WalkCallback, bubble?: boolean): this;
130 }
131
132 interface ValueParser {
133 /**
134 * Decompose a CSS dimension into its numeric and unit part
135 *
136 * @param value The dimension to decompose
137 * @returns An object representing `number` and `unit` part of the dimension or `false` if the decomposing fails
138 */
139 unit(value: string): Dimension | false;
140
141 /**
142 * Serialize a series of nodes into a CSS value
143 *
144 * @param nodes The nodes to stringify
145 * @param custom A custom stringifier callback
146 * @returns The generated CSS value
147 */
148 stringify(nodes: Node | Node[], custom?: CustomStringifierCallback): string;
149
150 /**
151 * Walk a series of nodes, applying a callback
152 *
153 * @param nodes The nodes to walk
154 * @param callback A visitor callback that will be executed for each node
155 * @param bubble When set to `true`, walking will be done inside-out instead of outside-in
156 */
157 walk(nodes: Node[], callback: WalkCallback, bubble?: boolean): void;
158
159 /**
160 * Parse a CSS value into a series of nodes to operate on
161 *
162 * @param value The value to parse
163 */
164 new (value: string): ParsedValue;
165
166 /**
167 * Parse a CSS value into a series of nodes to operate on
168 *
169 * @param value The value to parse
170 */
171 (value: string): ParsedValue;
172 }
173}
174
175declare const postcssValueParser: postcssValueParser.ValueParser;
176
177export = postcssValueParser;