postcss-selector-parser.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Type definitions for postcss-selector-parser 2.2.3
  2. // Definitions by: Chris Eppstein <chris@eppsteins.net>
  3. /*~ Note that ES6 modules cannot directly export callable functions.
  4. *~ This file should be imported using the CommonJS-style:
  5. *~ import x = require('someLibrary');
  6. *~
  7. *~ Refer to the documentation to understand common
  8. *~ workarounds for this limitation of ES6 modules.
  9. */
  10. /*~ This declaration specifies that the function
  11. *~ is the exported object from the file
  12. */
  13. export = parser;
  14. declare function parser(): parser.Processor<never>;
  15. declare function parser<Transform extends any>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, never>;
  16. declare function parser(processor: parser.AsyncProcessor): parser.Processor<never>;
  17. declare function parser<Transform extends any>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform, never>;
  18. declare function parser(processor: parser.SyncProcessor): parser.Processor<never>;
  19. declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform>;
  20. /*~ If you want to expose types from your module as well, you can
  21. *~ place them in this block. Often you will want to describe the
  22. *~ shape of the return type of the function; that type should
  23. *~ be declared in here, as this example shows.
  24. */
  25. declare namespace parser {
  26. /* copied from postcss -- so we don't need to add a dependency */
  27. type ErrorOptions = {
  28. plugin?: string;
  29. word?: string;
  30. index?: number
  31. };
  32. /* the bits we use of postcss.Rule, copied from postcss -- so we don't need to add a dependency */
  33. type PostCSSRuleNode = {
  34. selector: string
  35. /**
  36. * @returns postcss.CssSyntaxError but it's a complex object, caller
  37. * should cast to it if they have a dependency on postcss.
  38. */
  39. error(message: string, options?: ErrorOptions): Error;
  40. };
  41. /** Accepts a string */
  42. type Selectors = string | PostCSSRuleNode
  43. type SyncProcessor<Transform = void> = (root: parser.Root) => Transform
  44. type AsyncProcessor<Transform = void> = (root: parser.Root) => Transform | PromiseLike<Transform>
  45. const TAG: "tag";
  46. const STRING: "string";
  47. const SELECTOR: "selector";
  48. const ROOT: "root";
  49. const PSEUDO: "pseudo";
  50. const NESTING: "nesting";
  51. const ID: "id";
  52. const COMMENT: "comment";
  53. const COMBINATOR: "combinator";
  54. const CLASS: "class";
  55. const ATTRIBUTE: "attribute";
  56. const UNIVERSAL: "universal";
  57. interface NodeTypes {
  58. tag: Tag,
  59. string: String,
  60. selector: Selector,
  61. root: Root,
  62. pseudo: Pseudo,
  63. nesting: Nesting,
  64. id: Identifier,
  65. comment: Comment,
  66. combinator: Combinator,
  67. class: ClassName,
  68. attribute: Attribute,
  69. universal: Universal
  70. }
  71. type Node = NodeTypes[keyof NodeTypes];
  72. function isNode(node: any): node is Node;
  73. interface Options {
  74. /**
  75. * Preserve whitespace when true. Default: false;
  76. */
  77. lossless: boolean;
  78. /**
  79. * When true and a postcss.Rule is passed, set the result of
  80. * processing back onto the rule when done. Default: false.
  81. */
  82. updateSelector: boolean;
  83. }
  84. class Processor<
  85. TransformType = never,
  86. SyncSelectorsType extends Selectors | never = Selectors
  87. > {
  88. res: Root;
  89. readonly result: String;
  90. ast(selectors: Selectors, options?: Partial<Options>): Promise<Root>;
  91. astSync(selectors: SyncSelectorsType, options?: Partial<Options>): Root;
  92. transform(selectors: Selectors, options?: Partial<Options>): Promise<TransformType>;
  93. transformSync(selectors: SyncSelectorsType, options?: Partial<Options>): TransformType;
  94. process(selectors: Selectors, options?: Partial<Options>): Promise<string>;
  95. processSync(selectors: SyncSelectorsType, options?: Partial<Options>): string;
  96. }
  97. interface ParserOptions {
  98. css: string;
  99. error: (message: string, options: ErrorOptions) => Error;
  100. options: Options;
  101. }
  102. class Parser {
  103. input: ParserOptions;
  104. lossy: boolean;
  105. position: number;
  106. root: Root;
  107. selectors: string;
  108. current: Selector;
  109. constructor(input: ParserOptions);
  110. /**
  111. * Raises an error, if the processor is invoked on
  112. * a postcss Rule node, a better error message is raised.
  113. */
  114. error(message: string, options?: ErrorOptions): void;
  115. }
  116. interface NodeSource {
  117. start?: {
  118. line: number,
  119. column: number
  120. },
  121. end?: {
  122. line: number,
  123. column: number
  124. }
  125. }
  126. interface SpaceAround {
  127. before: string;
  128. after: string;
  129. }
  130. interface Spaces extends SpaceAround {
  131. [spaceType: string]: string | Partial<SpaceAround> | undefined;
  132. }
  133. interface NodeOptions<Value = string> {
  134. value: Value;
  135. spaces?: Partial<Spaces>;
  136. source?: NodeSource;
  137. sourceIndex?: number;
  138. }
  139. interface Base<
  140. Value extends string | undefined = string,
  141. ParentType extends Container | undefined = Container | undefined
  142. > {
  143. type: keyof NodeTypes;
  144. parent: ParentType;
  145. value: Value;
  146. spaces: Spaces;
  147. source?: NodeSource;
  148. sourceIndex: number;
  149. remove(): Node;
  150. replaceWith(...nodes: Node[]): Node;
  151. next(): Node;
  152. prev(): Node;
  153. clone(opts: {[override: string]:any}): Node;
  154. toString(): string;
  155. }
  156. interface ContainerOptions extends NodeOptions {
  157. nodes?: Array<Node>;
  158. }
  159. interface Container<Value extends string | undefined = string> extends Base<Value> {
  160. nodes: Array<Node>;
  161. append(selector: Selector): Container;
  162. prepend(selector: Selector): Container;
  163. at(index: number): Node;
  164. index(child: Node): number;
  165. readonly first: Node;
  166. readonly last: Node;
  167. readonly length: number;
  168. removeChild(child: Node): Container;
  169. removeAll(): Container;
  170. empty(): Container;
  171. insertAfter(oldNode: Node, newNode: Node): Container;
  172. insertBefore(oldNode: Node, newNode: Node): Container;
  173. each(callback: (node: Node) => boolean | void): boolean | undefined;
  174. walk(callback: (node: Node) => boolean | void): boolean | undefined;
  175. walkAttributes(callback: (node: Node) => boolean | void): boolean | undefined;
  176. walkClasses(callback: (node: Node) => boolean | void): boolean | undefined;
  177. walkCombinators(callback: (node: Node) => boolean | void): boolean | undefined;
  178. walkComments(callback: (node: Node) => boolean | void): boolean | undefined;
  179. walkIds(callback: (node: Node) => boolean | void): boolean | undefined;
  180. walkNesting(callback: (node: Node) => boolean | void): boolean | undefined;
  181. walkPseudos(callback: (node: Node) => boolean | void): boolean | undefined;
  182. walkTags(callback: (node: Node) => boolean | void): boolean | undefined;
  183. split(callback: (node: Node) => boolean): [Node[], Node[]];
  184. map(callback: (node: Node) => Node): Node[];
  185. reduce<T>(callback: (node: Node) => Node, memo: T): T;
  186. every(callback: (node: Node) => boolean): boolean;
  187. some(callback: (node: Node) => boolean): boolean;
  188. filter(callback: (node: Node) => boolean): Node[];
  189. sort(callback: (nodeA: Node, nodeB: Node) => number): Node[];
  190. toString(): string;
  191. }
  192. function isContainer(node: any): node is Root | Selector | Pseudo;
  193. interface NamespaceOptions<Value extends string | undefined = string> extends NodeOptions<Value> {
  194. namespace?: string | true;
  195. }
  196. interface Namespace<Value extends string | undefined = string> extends Base<Value> {
  197. /** alias for namespace */
  198. ns: string | true;
  199. /**
  200. * namespace prefix.
  201. */
  202. namespace: string | true;
  203. /**
  204. * If a namespace exists, prefix the value provided with it, separated by |.
  205. */
  206. qualifiedName(value: string): string;
  207. /**
  208. * A string representing the namespace suitable for output.
  209. */
  210. readonly namespaceString: string;
  211. }
  212. function isNamespace(node: any): node is ClassName | Attribute | Tag;
  213. interface Root extends Container<undefined> {
  214. type: "root";
  215. /**
  216. * Raises an error, if the processor is invoked on
  217. * a postcss Rule node, a better error message is raised.
  218. */
  219. error(message: string, options?: ErrorOptions): Error;
  220. }
  221. function root(opts: ContainerOptions): Root;
  222. function isRoot(node: any): node is Root;
  223. interface Selector extends Container {
  224. type: "selector";
  225. }
  226. function selector(opts: ContainerOptions): Selector;
  227. function isSelector(node: any): node is Selector;
  228. interface Combinator extends Base {
  229. type: "combinator"
  230. }
  231. function combinator(opts: NodeOptions): Combinator;
  232. function isCombinator(node: any): node is Combinator;
  233. interface ClassName extends Namespace {
  234. type: "class";
  235. }
  236. function className(opts: NamespaceOptions): ClassName;
  237. function isClassName(node: any): node is ClassName;
  238. type AttributeOperator = "=" | "~=" | "|=" | "^=" | "$=" | "*=";
  239. interface AttributeOptions extends NamespaceOptions<string | undefined> {
  240. attribute: string;
  241. operator?: AttributeOperator;
  242. insensitive?: boolean;
  243. quoted?: boolean;
  244. spaces?: {
  245. before?: string;
  246. after?: string;
  247. attribute?: Partial<SpaceAround>;
  248. operator?: Partial<SpaceAround>;
  249. value?: Partial<SpaceAround>;
  250. insensitive?: Partial<SpaceAround>;
  251. }
  252. raws: {
  253. unquoted?: string;
  254. attribute?: string;
  255. operator?: string;
  256. value?: string;
  257. insensitive?: string;
  258. spaces?: {
  259. attribute?: Partial<Spaces>;
  260. operator?: Partial<Spaces>;
  261. value?: Partial<Spaces>;
  262. insensitive?: Partial<Spaces>;
  263. }
  264. };
  265. }
  266. interface Attribute extends Namespace<string | undefined> {
  267. type: "attribute";
  268. attribute: string;
  269. operator?: AttributeOperator;
  270. insensitive?: boolean;
  271. quoted?: boolean;
  272. spaces: {
  273. before: string;
  274. after: string;
  275. attribute?: Partial<Spaces>;
  276. operator?: Partial<Spaces>;
  277. value?: Partial<Spaces>;
  278. insensitive?: Partial<Spaces>;
  279. }
  280. raws: {
  281. unquoted?: string;
  282. attribute?: string;
  283. operator?: string;
  284. value?: string;
  285. insensitive?: string;
  286. spaces?: {
  287. attribute?: Partial<Spaces>;
  288. operator?: Partial<Spaces>;
  289. value?: Partial<Spaces>;
  290. insensitive?: Partial<Spaces>;
  291. }
  292. };
  293. /**
  294. * The attribute name after having been qualified with a namespace.
  295. */
  296. readonly qualifiedAttribute: string;
  297. /**
  298. * returns the offset of the attribute part specified relative to the
  299. * start of the node of the output string.
  300. *
  301. * * "ns" - alias for "namespace"
  302. * * "namespace" - the namespace if it exists.
  303. * * "attribute" - the attribute name
  304. * * "attributeNS" - the start of the attribute or its namespace
  305. * * "operator" - the match operator of the attribute
  306. * * "value" - The value (string or identifier)
  307. * * "insensitive" - the case insensitivity flag;
  308. * @param part One of the possible values inside an attribute.
  309. * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
  310. */
  311. offsetOf(part: "ns" | "namespace" | "attribute" | "attributeNS" | "operator" | "value" | "insensitive"): number;
  312. }
  313. function attribute(opts: AttributeOptions): Attribute;
  314. function isAttribute(node: any): node is Attribute;
  315. interface Pseudo extends Container {
  316. type: "pseudo";
  317. }
  318. function pseudo(opts: ContainerOptions): Pseudo;
  319. /**
  320. * Checks wether the node is the Psuedo subtype of node.
  321. */
  322. function isPseudo(node: any): node is Pseudo;
  323. /**
  324. * Checks wether the node is, specifically, a pseudo element instead of
  325. * pseudo class.
  326. */
  327. function isPseudoElement(node: any): node is Pseudo;
  328. /**
  329. * Checks wether the node is, specifically, a pseudo class instead of
  330. * pseudo element.
  331. */
  332. function isPseudoClass(node: any): node is Pseudo;
  333. interface Tag extends Namespace {
  334. type: "tag";
  335. }
  336. function tag(opts: NamespaceOptions): Tag;
  337. function isTag(node: any): node is Tag;
  338. interface Comment extends Base {
  339. type: "comment";
  340. }
  341. function comment(opts: NodeOptions): Comment;
  342. function isComment(node: any): node is Comment;
  343. interface Identifier extends Base {
  344. type: "id";
  345. }
  346. function id(opts: any): any;
  347. function isIdentifier(node: any): node is Identifier;
  348. interface Nesting extends Base {
  349. type: "nesting";
  350. }
  351. function nesting(opts: any): any;
  352. function isNesting(node: any): node is Nesting;
  353. interface String extends Base {
  354. type: "string";
  355. }
  356. function string(opts: NodeOptions): String;
  357. function isString(node: any): node is String;
  358. interface Universal extends Base {
  359. type: "universal";
  360. }
  361. function universal(opts?: NamespaceOptions): any;
  362. function isUniversal(node: any): node is Universal;
  363. }