1 // Type definitions for postcss-selector-parser 2.2.3
2 // Definitions by: Chris Eppstein <chris@eppsteins.net>
4 /*~ Note that ES6 modules cannot directly export callable functions.
5 *~ This file should be imported using the CommonJS-style:
6 *~ import x = require('someLibrary');
8 *~ Refer to the documentation to understand common
9 *~ workarounds for this limitation of ES6 modules.
12 /*~ This declaration specifies that the function
13 *~ is the exported object from the file
17 declare function parser(): parser.Processor<never>;
18 declare function parser<Transform extends any>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, never>;
19 declare function parser(processor: parser.AsyncProcessor): parser.Processor<never>;
20 declare function parser<Transform extends any>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform, never>;
21 declare function parser(processor: parser.SyncProcessor): parser.Processor<never>;
22 declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform>;
24 /*~ If you want to expose types from your module as well, you can
25 *~ place them in this block. Often you will want to describe the
26 *~ shape of the return type of the function; that type should
27 *~ be declared in here, as this example shows.
29 declare namespace parser {
30 /* copied from postcss -- so we don't need to add a dependency */
36 /* the bits we use of postcss.Rule, copied from postcss -- so we don't need to add a dependency */
37 type PostCSSRuleNode = {
40 * @returns postcss.CssSyntaxError but it's a complex object, caller
41 * should cast to it if they have a dependency on postcss.
43 error(message: string, options?: ErrorOptions): Error;
45 /** Accepts a string */
46 type Selectors = string | PostCSSRuleNode
47 type SyncProcessor<Transform = void> = (root: parser.Root) => Transform
48 type AsyncProcessor<Transform = void> = (root: parser.Root) => Transform | PromiseLike<Transform>
51 const STRING: "string";
52 const SELECTOR: "selector";
54 const PSEUDO: "pseudo";
55 const NESTING: "nesting";
57 const COMMENT: "comment";
58 const COMBINATOR: "combinator";
60 const ATTRIBUTE: "attribute";
61 const UNIVERSAL: "universal";
72 combinator: Combinator,
78 type Node = NodeTypes[keyof NodeTypes];
80 function isNode(node: any): node is Node;
84 * Preserve whitespace when true. Default: false;
88 * When true and a postcss.Rule is passed, set the result of
89 * processing back onto the rule when done. Default: false.
91 updateSelector: boolean;
94 TransformType = never,
95 SyncSelectorsType extends Selectors | never = Selectors
98 readonly result: String;
99 ast(selectors: Selectors, options?: Partial<Options>): Promise<Root>;
100 astSync(selectors: SyncSelectorsType, options?: Partial<Options>): Root;
101 transform(selectors: Selectors, options?: Partial<Options>): Promise<TransformType>;
102 transformSync(selectors: SyncSelectorsType, options?: Partial<Options>): TransformType;
103 process(selectors: Selectors, options?: Partial<Options>): Promise<string>;
104 processSync(selectors: SyncSelectorsType, options?: Partial<Options>): string;
106 interface ParserOptions {
108 error: (message: string, options: ErrorOptions) => Error;
112 input: ParserOptions;
118 constructor(input: ParserOptions);
120 * Raises an error, if the processor is invoked on
121 * a postcss Rule node, a better error message is raised.
123 error(message: string, options?: ErrorOptions): void;
125 interface NodeSource {
135 interface SpaceAround {
139 interface Spaces extends SpaceAround {
140 [spaceType: string]: string | Partial<SpaceAround> | undefined;
142 interface NodeOptions<Value = string> {
144 spaces?: Partial<Spaces>;
146 sourceIndex?: number;
149 Value extends string | undefined = string,
150 ParentType extends Container | undefined = Container | undefined
152 type: keyof NodeTypes;
159 replaceWith(...nodes: Node[]): Node;
162 clone(opts: {[override: string]:any}): Node;
165 interface ContainerOptions extends NodeOptions {
168 interface Container<Value extends string | undefined = string> extends Base<Value> {
170 append(selector: Selector): Container;
171 prepend(selector: Selector): Container;
172 at(index: number): Node;
173 index(child: Node): number;
174 readonly first: Node;
176 readonly length: number;
177 removeChild(child: Node): Container;
178 removeAll(): Container;
180 insertAfter(oldNode: Node, newNode: Node): Container;
181 insertBefore(oldNode: Node, newNode: Node): Container;
182 each(callback: (node: Node) => boolean | void): boolean | undefined;
183 walk(callback: (node: Node) => boolean | void): boolean | undefined;
184 walkAttributes(callback: (node: Node) => boolean | void): boolean | undefined;
185 walkClasses(callback: (node: Node) => boolean | void): boolean | undefined;
186 walkCombinators(callback: (node: Node) => boolean | void): boolean | undefined;
187 walkComments(callback: (node: Node) => boolean | void): boolean | undefined;
188 walkIds(callback: (node: Node) => boolean | void): boolean | undefined;
189 walkNesting(callback: (node: Node) => boolean | void): boolean | undefined;
190 walkPseudos(callback: (node: Node) => boolean | void): boolean | undefined;
191 walkTags(callback: (node: Node) => boolean | void): boolean | undefined;
192 split(callback: (node: Node) => boolean): [Node[], Node[]];
193 map(callback: (node: Node) => Node): Node[];
194 reduce<T>(callback: (node: Node) => Node, memo: T): T;
195 every(callback: (node: Node) => boolean): boolean;
196 some(callback: (node: Node) => boolean): boolean;
197 filter(callback: (node: Node) => boolean): Node[];
198 sort(callback: (nodeA: Node, nodeB: Node) => number): Node[];
201 function isContainer(node: any): node is Root | Selector | Pseudo;
203 interface NamespaceOptions<Value extends string | undefined = string> extends NodeOptions<Value> {
204 namespace?: string | true;
206 interface Namespace<Value extends string | undefined = string> extends Base<Value> {
207 /** alias for namespace */
212 namespace: string | true;
214 * If a namespace exists, prefix the value provided with it, separated by |.
216 qualifiedName(value: string): string;
218 * A string representing the namespace suitable for output.
220 readonly namespaceString: string;
222 function isNamespace(node: any): node is ClassName | Attribute | Tag;
224 interface Root extends Container<undefined> {
227 * Raises an error, if the processor is invoked on
228 * a postcss Rule node, a better error message is raised.
230 error(message: string, options?: ErrorOptions): Error;
232 function root(opts: ContainerOptions): Root;
233 function isRoot(node: any): node is Root;
235 interface Selector extends Container {
238 function selector(opts: ContainerOptions): Selector;
239 function isSelector(node: any): node is Selector;
241 interface Combinator extends Base {
244 function combinator(opts: NodeOptions): Combinator;
245 function isCombinator(node: any): node is Combinator;
247 interface ClassName extends Namespace {
250 function className(opts: NamespaceOptions): ClassName;
251 function isClassName(node: any): node is ClassName;
253 type AttributeOperator = "=" | "~=" | "|=" | "^=" | "$=" | "*=";
254 interface AttributeOptions extends NamespaceOptions<string | undefined> {
256 operator?: AttributeOperator;
257 insensitive?: boolean;
262 attribute?: Partial<SpaceAround>;
263 operator?: Partial<SpaceAround>;
264 value?: Partial<SpaceAround>;
265 insensitive?: Partial<SpaceAround>;
272 insensitive?: string;
274 attribute?: Partial<Spaces>;
275 operator?: Partial<Spaces>;
276 value?: Partial<Spaces>;
277 insensitive?: Partial<Spaces>;
281 interface Attribute extends Namespace<string | undefined> {
284 operator?: AttributeOperator;
285 insensitive?: boolean;
290 attribute?: Partial<Spaces>;
291 operator?: Partial<Spaces>;
292 value?: Partial<Spaces>;
293 insensitive?: Partial<Spaces>;
300 insensitive?: string;
302 attribute?: Partial<Spaces>;
303 operator?: Partial<Spaces>;
304 value?: Partial<Spaces>;
305 insensitive?: Partial<Spaces>;
309 * The attribute name after having been qualified with a namespace.
311 readonly qualifiedAttribute: string;
313 * returns the offset of the attribute part specified relative to the
314 * start of the node of the output string.
316 * * "ns" - alias for "namespace"
317 * * "namespace" - the namespace if it exists.
318 * * "attribute" - the attribute name
319 * * "attributeNS" - the start of the attribute or its namespace
320 * * "operator" - the match operator of the attribute
321 * * "value" - The value (string or identifier)
322 * * "insensitive" - the case insensitivity flag;
323 * @param part One of the possible values inside an attribute.
324 * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
326 offsetOf(part: "ns" | "namespace" | "attribute" | "attributeNS" | "operator" | "value" | "insensitive"): number;
328 function attribute(opts: AttributeOptions): Attribute;
329 function isAttribute(node: any): node is Attribute;
331 interface Pseudo extends Container {
334 function pseudo(opts: ContainerOptions): Pseudo;
336 * Checks wether the node is the Psuedo subtype of node.
338 function isPseudo(node: any): node is Pseudo;
341 * Checks wether the node is, specifically, a pseudo element instead of
344 function isPseudoElement(node: any): node is Pseudo;
347 * Checks wether the node is, specifically, a pseudo class instead of
350 function isPseudoClass(node: any): node is Pseudo;
353 interface Tag extends Namespace {
356 function tag(opts: NamespaceOptions): Tag;
357 function isTag(node: any): node is Tag;
359 interface Comment extends Base {
362 function comment(opts: NodeOptions): Comment;
363 function isComment(node: any): node is Comment;
365 interface Identifier extends Base {
368 function id(opts: any): any;
369 function isIdentifier(node: any): node is Identifier;
371 interface Nesting extends Base {
374 function nesting(opts: any): any;
375 function isNesting(node: any): node is Nesting;
377 interface String extends Base {
380 function string(opts: NodeOptions): String;
381 function isString(node: any): node is String;
383 interface Universal extends Base {
386 function universal(opts?: NamespaceOptions): any;
387 function isUniversal(node: any): node is Universal;