You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

499 lines
19 KiB

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