UNPKG

6.11 kBTypeScriptView Raw
1/*
2 Copyright © 2019 Andrew Powell
3
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 The above copyright notice and this permission notice shall be
9 included in all copies or substantial portions of this Source Code Form.
10*/
11
12import * as postcss from "postcss";
13
14// Even though the concrete classes extend PostCSS classes, we can't extend
15// PostCSS Node types here because they refer to statements that aren't
16// compatible with our value nodes. This unfortunately means that we need to
17// replicate a bunch of PostCSS's method declarations here.
18
19export interface NodeBase {
20 // Inherited from postcss.ContainerBase, but with our Node type.
21 next(): ChildNode | void;
22 prev(): ChildNode | void;
23 before(newNode: ChildNode | object | string | ChildNode[]): this;
24 after(newNode: ChildNode | object | string | ChildNode[]): this;
25 root(): Root;
26 replaceWith(...nodes: Array<ChildNode | object>): this;
27
28 // Inherited from postcss.ContainerBase with no changes.
29 source?: postcss.NodeSource;
30 raws: postcss.NodeRaws;
31 toString(stringifier?: Stringifier | Syntax): string;
32 error(
33 message: string,
34 options?: postcss.NodeErrorOptions
35 ): postcss.CssSyntaxError;
36 warn(
37 result: postcss.Result,
38 text: string,
39 opts?: postcss.WarningOptions
40 ): void;
41 remove(): this;
42 clone(overrides?: object): this;
43 cloneBefore(overrides?: object): this;
44 cloneAfter(overrides?: object): this;
45 raw(prop: string, defaultType?: string): any;
46}
47
48export interface ContainerBase extends NodeBase {
49 walkFuncs(callback: (decl: Func, index: number) => any): boolean | void;
50 walkInterpolations(
51 callback: (interpolation: Interpolation, index: number) => any
52 ): boolean | void;
53 walkNumerics(
54 callback: (numeric: Numeric, index: number) => any
55 ): boolean | void;
56 walkOperators(
57 callback: (operator: Operator, index: number) => any
58 ): boolean | void;
59 walkPunctuations(
60 callback: (punctuation: Punctuation, index: number) => any
61 ): boolean | void;
62 walkQuoteds(callback: (quoted: Quoted, index: number) => any): boolean | void;
63 walkUnicodeRanges(
64 callback: (unicodeRange: UnicodeRange, index: number) => any
65 ): boolean | void;
66 walkWords(callback: (word: Word, index: number) => any): boolean | void;
67 walkType(
68 type: string,
69 callback: (node: ChildNode, index: number) => any
70 ): boolean | void;
71
72 // Inherited from postcss.ContainerBase, but with our Node type.
73 nodes: ChildNode[];
74 first?: ChildNode;
75 last?: ChildNode;
76 index(child: ChildNode | number): number;
77 every(
78 callback: (node: ChildNode, index: number, nodes: ChildNode[]) => any,
79 thisArg?: any
80 ): boolean;
81 some(
82 callback: (node: ChildNode, index: number, nodes: ChildNode[]) => boolean,
83 thisArg?: any
84 ): boolean;
85 each(callback: (node: ChildNode, index: number) => any): boolean | void;
86 walk(callback: (node: ChildNode, index: number) => any): boolean | void;
87 walkAtWords(callback: (atWord: AtWord, index: number) => any): boolean | void;
88 walkComments(
89 callback: (comment: Comment, index: number) => any
90 ): boolean | void;
91 prepend(...nodes: Array<ChildNode | object | string>): this;
92 append(...nodes: Array<ChildNode | object | string>): this;
93 insertBefore(
94 oldNode: ChildNode | number,
95 newNode: ChildNode | object | string
96 ): this;
97 insertAfter(
98 oldNode: ChildNode | number,
99 newNode: ChildNode | object | string
100 ): this;
101 removeChild(child: ChildNode | number): this;
102
103 // Inherited from postcss.ContainerBase with no changes.
104 clone(overrides?: object): this;
105 remove(): this;
106 removeAll(): this;
107}
108
109export interface Root extends ContainerBase {
110 type: "root";
111 parent: undefined;
112 toResult(options?: {
113 to?: string;
114 map?: postcss.SourceMapOptions;
115 }): postcss.Result;
116}
117
118export type Node = Root | ChildNode;
119
120export type ChildNode =
121 | AtWord
122 | Comment
123 | Func
124 | Interpolation
125 | Numeric
126 | Operator
127 | Punctuation
128 | Quoted
129 | UnicodeRange
130 | Word;
131
132export type Container = Root | Func | Interpolation;
133
134export interface AtWord extends NodeBase {
135 type: "atrule";
136 parent: Container;
137 name: string;
138 params: string;
139}
140
141export interface Comment extends NodeBase {
142 type: "comment";
143 parent: Container;
144 inline: boolean;
145 text: string;
146}
147
148export interface Func extends ContainerBase {
149 type: "func";
150 parent: Container;
151 isColor: boolean;
152 isVar: boolean;
153 name: string;
154 params: string;
155}
156
157export interface Interpolation extends ContainerBase {
158 type: "interpolation";
159 parent: Container;
160 params: string;
161 prefix: string;
162}
163
164export interface Numeric extends NodeBase {
165 type: "numeric";
166 parent: Container;
167 unit: string;
168 value: string;
169}
170
171export interface Operator extends NodeBase {
172 type: "operator";
173 parent: Container;
174 value: string;
175}
176
177export interface Punctuation extends NodeBase {
178 type: "punctuation";
179 parent: Container;
180 value: string;
181}
182
183export interface Quoted extends NodeBase {
184 type: "quoted";
185 parent: Container;
186 quote: string;
187 value: string;
188}
189
190export interface UnicodeRange extends NodeBase {
191 type: "unicodeRange";
192 parent: Container;
193 name: string;
194}
195
196export interface Word extends NodeBase {
197 type: "word";
198 parent: Container;
199 isColor: boolean;
200 isHex: boolean;
201 isUrl: boolean;
202 isVariable: boolean;
203 value: string;
204}
205
206export function parse(css: string, options?: ParseOptions): Root;
207
208export interface ParseOptions {
209 ignoreUnknownWords?: boolean;
210 interpolation?: boolean | InterpolationOptions;
211 variables?: VariablesOptions;
212}
213
214export interface InterpolationOptions {
215 prefix: string;
216}
217
218export interface VariablesOptions {
219 prefixes: string[];
220}
221
222interface Syntax {
223 stringify?: Stringifier;
224}
225
226interface Builder {
227 (part: string, node?: Node, type?: "start" | "end"): void;
228}
229
230export interface Stringifier {
231 (node: Node, builder: Builder): void;
232}
233
234export const stringify: Stringifier;
235
236export function nodeToString(node: Node): string;