postcss-selector-parser.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. // A type that's T but not U.
  15. type Diff<T, U> = T extends U ? never : T;
  16. // TODO: Conditional types in TS 1.8 will really clean this up.
  17. declare function parser(): parser.Processor<never>;
  18. declare function parser<Transform>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, never>;
  19. declare function parser(processor: parser.AsyncProcessor<void>): parser.Processor<never, never>;
  20. declare function parser<Transform>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform>;
  21. declare function parser(processor: parser.SyncProcessor<void>): parser.Processor<never>;
  22. declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform>;
  23. /*~ If you want to expose types from your module as well, you can
  24. *~ place them in this block. Often you will want to describe the
  25. *~ shape of the return type of the function; that type should
  26. *~ be declared in here, as this example shows.
  27. */
  28. declare namespace parser {
  29. /* copied from postcss -- so we don't need to add a dependency */
  30. type ErrorOptions = {
  31. plugin?: string;
  32. word?: string;
  33. index?: number
  34. };
  35. /* the bits we use of postcss.Rule, copied from postcss -- so we don't need to add a dependency */
  36. type PostCSSRuleNode = {
  37. selector: string
  38. /**
  39. * @returns postcss.CssSyntaxError but it's a complex object, caller
  40. * should cast to it if they have a dependency on postcss.
  41. */
  42. error(message: string, options?: ErrorOptions): Error;
  43. };
  44. /** Accepts a string */
  45. type Selectors = string | PostCSSRuleNode
  46. type ProcessorFn<ReturnType = void> = (root: parser.Root) => ReturnType;
  47. type SyncProcessor<Transform = void> = ProcessorFn<Transform>;
  48. type AsyncProcessor<Transform = void> = ProcessorFn<PromiseLike<Transform>>;
  49. const TAG: "tag";
  50. const STRING: "string";
  51. const SELECTOR: "selector";
  52. const ROOT: "root";
  53. const PSEUDO: "pseudo";
  54. const NESTING: "nesting";
  55. const ID: "id";
  56. const COMMENT: "comment";
  57. const COMBINATOR: "combinator";
  58. const CLASS: "class";
  59. const ATTRIBUTE: "attribute";
  60. const UNIVERSAL: "universal";
  61. interface NodeTypes {
  62. tag: Tag,
  63. string: String,
  64. selector: Selector,
  65. root: Root,
  66. pseudo: Pseudo,
  67. nesting: Nesting,
  68. id: Identifier,
  69. comment: Comment,
  70. combinator: Combinator,
  71. class: ClassName,
  72. attribute: Attribute,
  73. universal: Universal
  74. }
  75. type Node = NodeTypes[keyof NodeTypes];
  76. function isNode(node: any): node is Node;
  77. interface Options {
  78. /**
  79. * Preserve whitespace when true. Default: false;
  80. */
  81. lossless: boolean;
  82. /**
  83. * When true and a postcss.Rule is passed, set the result of
  84. * processing back onto the rule when done. Default: false.
  85. */
  86. updateSelector: boolean;
  87. }
  88. class Processor<
  89. TransformType = never,
  90. SyncSelectorsType extends Selectors | never = Selectors
  91. > {
  92. res: Root;
  93. readonly result: String;
  94. ast(selectors: Selectors, options?: Partial<Options>): Promise<Root>;
  95. astSync(selectors: SyncSelectorsType, options?: Partial<Options>): Root;
  96. transform(selectors: Selectors, options?: Partial<Options>): Promise<TransformType>;
  97. transformSync(selectors: SyncSelectorsType, options?: Partial<Options>): TransformType;
  98. process(selectors: Selectors, options?: Partial<Options>): Promise<string>;
  99. processSync(selectors: SyncSelectorsType, options?: Partial<Options>): string;
  100. }
  101. interface ParserOptions {
  102. css: string;
  103. error: (message: string, options: ErrorOptions) => Error;
  104. options: Options;
  105. }
  106. class Parser {
  107. input: ParserOptions;
  108. lossy: boolean;
  109. position: number;
  110. root: Root;
  111. selectors: string;
  112. current: Selector;
  113. constructor(input: ParserOptions);
  114. /**
  115. * Raises an error, if the processor is invoked on
  116. * a postcss Rule node, a better error message is raised.
  117. */
  118. error(message: string, options?: ErrorOptions): void;
  119. }
  120. interface NodeSource {
  121. start?: {
  122. line: number,
  123. column: number
  124. },
  125. end?: {
  126. line: number,
  127. column: number
  128. }
  129. }
  130. interface SpaceAround {
  131. before: string;
  132. after: string;
  133. }
  134. interface Spaces extends SpaceAround {
  135. [spaceType: string]: string | Partial<SpaceAround> | undefined;
  136. }
  137. interface NodeOptions<Value = string> {
  138. value: Value;
  139. spaces?: Partial<Spaces>;
  140. source?: NodeSource;
  141. sourceIndex?: number;
  142. }
  143. interface Base<
  144. Value extends string | undefined = string,
  145. ParentType extends Container | undefined = Container | undefined
  146. > {
  147. type: keyof NodeTypes;
  148. parent: ParentType;
  149. value: Value;
  150. spaces: Spaces;
  151. source?: NodeSource;
  152. sourceIndex: number;
  153. rawSpaceBefore: string;
  154. rawSpaceAfter: string;
  155. remove(): Node;
  156. replaceWith(...nodes: Node[]): Node;
  157. next(): Node;
  158. prev(): Node;
  159. clone(opts: {[override: string]:any}): Node;
  160. /**
  161. * Return whether this node includes the character at the position of the given line and column.
  162. * Returns undefined if the nodes lack sufficient source metadata to determine the position.
  163. * @param line 1-index based line number relative to the start of the selector.
  164. * @param column 1-index based column number relative to the start of the selector.
  165. */
  166. isAtPosition(line: number, column: number): boolean | undefined;
  167. /**
  168. * Some non-standard syntax doesn't follow normal escaping rules for css,
  169. * this allows the escaped value to be specified directly, allowing illegal characters to be
  170. * directly inserted into css output.
  171. * @param name the property to set
  172. * @param value the unescaped value of the property
  173. * @param valueEscaped optional. the escaped value of the property.
  174. */
  175. setPropertyAndEscape(name: string, value: any, valueEscaped: string): void;
  176. /**
  177. * When you want a value to passed through to CSS directly. This method
  178. * deletes the corresponding raw value causing the stringifier to fallback
  179. * to the unescaped value.
  180. * @param name the property to set.
  181. * @param value The value that is both escaped and unescaped.
  182. */
  183. setPropertyWithoutEscape(name: string, value: any): void;
  184. /**
  185. * Some non-standard syntax doesn't follow normal escaping rules for css.
  186. * This allows non standard syntax to be appended to an existing property
  187. * by specifying the escaped value. By specifying the escaped value,
  188. * illegal characters are allowed to be directly inserted into css output.
  189. * @param {string} name the property to set
  190. * @param {any} value the unescaped value of the property
  191. * @param {string} valueEscaped optional. the escaped value of the property.
  192. */
  193. appendToPropertyAndEscape(name: string, value: any, valueEscaped: string): void;
  194. toString(): string;
  195. }
  196. interface ContainerOptions extends NodeOptions {
  197. nodes?: Array<Node>;
  198. }
  199. interface Container<
  200. Value extends string | undefined = string,
  201. Child extends Node = Node
  202. > extends Base<Value> {
  203. nodes: Array<Child>;
  204. append(selector: Selector): this;
  205. prepend(selector: Selector): this;
  206. at(index: number): Child;
  207. /**
  208. * Return the most specific node at the line and column number given.
  209. * The source location is based on the original parsed location, locations aren't
  210. * updated as selector nodes are mutated.
  211. *
  212. * Note that this location is relative to the location of the first character
  213. * of the selector, and not the location of the selector in the overall document
  214. * when used in conjunction with postcss.
  215. *
  216. * If not found, returns undefined.
  217. * @param line The line number of the node to find. (1-based index)
  218. * @param col The column number of the node to find. (1-based index)
  219. */
  220. atPosition(line: number, column: number): Child;
  221. index(child: Child): number;
  222. readonly first: Child;
  223. readonly last: Child;
  224. readonly length: number;
  225. removeChild(child: Child): this;
  226. removeAll(): Container;
  227. empty(): Container;
  228. insertAfter(oldNode: Child, newNode: Child): this;
  229. insertBefore(oldNode: Child, newNode: Child): this;
  230. each(callback: (node: Child) => boolean | void): boolean | undefined;
  231. walk(
  232. callback: (node: Node) => boolean | void
  233. ): boolean | undefined;
  234. walkAttributes(
  235. callback: (node: Attribute) => boolean | void
  236. ): boolean | undefined;
  237. walkClasses(
  238. callback: (node: ClassName) => boolean | void
  239. ): boolean | undefined;
  240. walkCombinators(
  241. callback: (node: Combinator) => boolean | void
  242. ): boolean | undefined;
  243. walkComments(
  244. callback: (node: Comment) => boolean | void
  245. ): boolean | undefined;
  246. walkIds(
  247. callback: (node: Identifier) => boolean | void
  248. ): boolean | undefined;
  249. walkNesting(
  250. callback: (node: Nesting) => boolean | void
  251. ): boolean | undefined;
  252. walkPseudos(
  253. callback: (node: Pseudo) => boolean | void
  254. ): boolean | undefined;
  255. walkTags(callback: (node: Tag) => boolean | void): boolean | undefined;
  256. split(callback: (node: Child) => boolean): [Child[], Child[]];
  257. map<T>(callback: (node: Child) => T): T[];
  258. reduce(
  259. callback: (
  260. previousValue: Child,
  261. currentValue: Child,
  262. currentIndex: number,
  263. array: readonly Child[]
  264. ) => Child
  265. ): Child;
  266. reduce(
  267. callback: (
  268. previousValue: Child,
  269. currentValue: Child,
  270. currentIndex: number,
  271. array: readonly Child[]
  272. ) => Child,
  273. initialValue: Child
  274. ): Child;
  275. reduce<T>(
  276. callback: (
  277. previousValue: T,
  278. currentValue: Child,
  279. currentIndex: number,
  280. array: readonly Child[]
  281. ) => T,
  282. initialValue: T
  283. ): T;
  284. every(callback: (node: Child) => boolean): boolean;
  285. some(callback: (node: Child) => boolean): boolean;
  286. filter(callback: (node: Child) => boolean): Child[];
  287. sort(callback: (nodeA: Child, nodeB: Child) => number): Child[];
  288. toString(): string;
  289. }
  290. function isContainer(node: any): node is Root | Selector | Pseudo;
  291. interface NamespaceOptions<Value extends string | undefined = string> extends NodeOptions<Value> {
  292. namespace?: string | true;
  293. }
  294. interface Namespace<Value extends string | undefined = string> extends Base<Value> {
  295. /** alias for namespace */
  296. ns: string | true;
  297. /**
  298. * namespace prefix.
  299. */
  300. namespace: string | true;
  301. /**
  302. * If a namespace exists, prefix the value provided with it, separated by |.
  303. */
  304. qualifiedName(value: string): string;
  305. /**
  306. * A string representing the namespace suitable for output.
  307. */
  308. readonly namespaceString: string;
  309. }
  310. function isNamespace(node: any): node is Attribute | Tag;
  311. interface Root extends Container<undefined, Selector> {
  312. type: "root";
  313. /**
  314. * Raises an error, if the processor is invoked on
  315. * a postcss Rule node, a better error message is raised.
  316. */
  317. error(message: string, options?: ErrorOptions): Error;
  318. nodeAt(line: number, column: number): Node
  319. }
  320. function root(opts: ContainerOptions): Root;
  321. function isRoot(node: any): node is Root;
  322. interface _Selector<S> extends Container<string, Diff<Node, S>> {
  323. type: "selector";
  324. }
  325. type Selector = _Selector<Selector>;
  326. function selector(opts: ContainerOptions): Selector;
  327. function isSelector(node: any): node is Selector;
  328. interface CombinatorRaws {
  329. value?: string;
  330. spaces?: {
  331. before?: string;
  332. after?: string;
  333. };
  334. }
  335. interface Combinator extends Base {
  336. type: "combinator";
  337. raws?: CombinatorRaws;
  338. }
  339. function combinator(opts: NodeOptions): Combinator;
  340. function isCombinator(node: any): node is Combinator;
  341. interface ClassName extends Base {
  342. type: "class";
  343. }
  344. function className(opts: NamespaceOptions): ClassName;
  345. function isClassName(node: any): node is ClassName;
  346. type AttributeOperator = "=" | "~=" | "|=" | "^=" | "$=" | "*=";
  347. type QuoteMark = '"' | "'" | null;
  348. interface PreferredQuoteMarkOptions {
  349. quoteMark?: QuoteMark;
  350. preferCurrentQuoteMark?: boolean;
  351. }
  352. interface SmartQuoteMarkOptions extends PreferredQuoteMarkOptions {
  353. smart?: boolean;
  354. }
  355. interface AttributeOptions extends NamespaceOptions<string | undefined> {
  356. attribute: string;
  357. operator?: AttributeOperator;
  358. insensitive?: boolean;
  359. quoteMark?: QuoteMark;
  360. /** @deprecated Use quoteMark instead. */
  361. quoted?: boolean;
  362. spaces?: {
  363. before?: string;
  364. after?: string;
  365. attribute?: Partial<SpaceAround>;
  366. operator?: Partial<SpaceAround>;
  367. value?: Partial<SpaceAround>;
  368. insensitive?: Partial<SpaceAround>;
  369. }
  370. raws: {
  371. unquoted?: string;
  372. attribute?: string;
  373. operator?: string;
  374. value?: string;
  375. insensitive?: string;
  376. spaces?: {
  377. attribute?: Partial<Spaces>;
  378. operator?: Partial<Spaces>;
  379. value?: Partial<Spaces>;
  380. insensitive?: Partial<Spaces>;
  381. }
  382. };
  383. }
  384. interface Attribute extends Namespace<string | undefined> {
  385. type: "attribute";
  386. attribute: string;
  387. operator?: AttributeOperator;
  388. insensitive?: boolean;
  389. quoteMark: QuoteMark;
  390. quoted?: boolean;
  391. spaces: {
  392. before: string;
  393. after: string;
  394. attribute?: Partial<Spaces>;
  395. operator?: Partial<Spaces>;
  396. value?: Partial<Spaces>;
  397. insensitive?: Partial<Spaces>;
  398. }
  399. raws: {
  400. /** @deprecated The attribute value is unquoted, use that instead.. */
  401. unquoted?: string;
  402. attribute?: string;
  403. operator?: string;
  404. /** The value of the attribute with quotes and escapes. */
  405. value?: string;
  406. insensitive?: string;
  407. spaces?: {
  408. attribute?: Partial<Spaces>;
  409. operator?: Partial<Spaces>;
  410. value?: Partial<Spaces>;
  411. insensitive?: Partial<Spaces>;
  412. }
  413. };
  414. /**
  415. * The attribute name after having been qualified with a namespace.
  416. */
  417. readonly qualifiedAttribute: string;
  418. /**
  419. * The case insensitivity flag or an empty string depending on whether this
  420. * attribute is case insensitive.
  421. */
  422. readonly insensitiveFlag : 'i' | '';
  423. /**
  424. * Returns the attribute's value quoted such that it would be legal to use
  425. * in the value of a css file. The original value's quotation setting
  426. * used for stringification is left unchanged. See `setValue(value, options)`
  427. * if you want to control the quote settings of a new value for the attribute or
  428. * `set quoteMark(mark)` if you want to change the quote settings of the current
  429. * value.
  430. *
  431. * You can also change the quotation used for the current value by setting quoteMark.
  432. **/
  433. getQuotedValue(options?: SmartQuoteMarkOptions): string;
  434. /**
  435. * Set the unescaped value with the specified quotation options. The value
  436. * provided must not include any wrapping quote marks -- those quotes will
  437. * be interpreted as part of the value and escaped accordingly.
  438. * @param value
  439. */
  440. setValue(value: string, options?: SmartQuoteMarkOptions): void;
  441. /**
  442. * Intelligently select a quoteMark value based on the value's contents. If
  443. * the value is a legal CSS ident, it will not be quoted. Otherwise a quote
  444. * mark will be picked that minimizes the number of escapes.
  445. *
  446. * If there's no clear winner, the quote mark from these options is used,
  447. * then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
  448. * true). If the quoteMark is unspecified, a double quote is used.
  449. **/
  450. smartQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark;
  451. /**
  452. * Selects the preferred quote mark based on the options and the current quote mark value.
  453. * If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
  454. * instead.
  455. */
  456. preferredQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark
  457. /**
  458. * returns the offset of the attribute part specified relative to the
  459. * start of the node of the output string.
  460. *
  461. * * "ns" - alias for "namespace"
  462. * * "namespace" - the namespace if it exists.
  463. * * "attribute" - the attribute name
  464. * * "attributeNS" - the start of the attribute or its namespace
  465. * * "operator" - the match operator of the attribute
  466. * * "value" - The value (string or identifier)
  467. * * "insensitive" - the case insensitivity flag;
  468. * @param part One of the possible values inside an attribute.
  469. * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
  470. */
  471. offsetOf(part: "ns" | "namespace" | "attribute" | "attributeNS" | "operator" | "value" | "insensitive"): number;
  472. }
  473. function attribute(opts: AttributeOptions): Attribute;
  474. function isAttribute(node: any): node is Attribute;
  475. interface Pseudo extends Container<string, Selector> {
  476. type: "pseudo";
  477. }
  478. function pseudo(opts: ContainerOptions): Pseudo;
  479. /**
  480. * Checks wether the node is the Psuedo subtype of node.
  481. */
  482. function isPseudo(node: any): node is Pseudo;
  483. /**
  484. * Checks wether the node is, specifically, a pseudo element instead of
  485. * pseudo class.
  486. */
  487. function isPseudoElement(node: any): node is Pseudo;
  488. /**
  489. * Checks wether the node is, specifically, a pseudo class instead of
  490. * pseudo element.
  491. */
  492. function isPseudoClass(node: any): node is Pseudo;
  493. interface Tag extends Namespace {
  494. type: "tag";
  495. }
  496. function tag(opts: NamespaceOptions): Tag;
  497. function isTag(node: any): node is Tag;
  498. interface Comment extends Base {
  499. type: "comment";
  500. }
  501. function comment(opts: NodeOptions): Comment;
  502. function isComment(node: any): node is Comment;
  503. interface Identifier extends Base {
  504. type: "id";
  505. }
  506. function id(opts: any): any;
  507. function isIdentifier(node: any): node is Identifier;
  508. interface Nesting extends Base {
  509. type: "nesting";
  510. }
  511. function nesting(opts: any): any;
  512. function isNesting(node: any): node is Nesting;
  513. interface String extends Base {
  514. type: "string";
  515. }
  516. function string(opts: NodeOptions): String;
  517. function isString(node: any): node is String;
  518. interface Universal extends Base {
  519. type: "universal";
  520. }
  521. function universal(opts?: NamespaceOptions): any;
  522. function isUniversal(node: any): node is Universal;
  523. }