postcss-selector-parser.d.ts 21 KB

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