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.

1277 lines
45 KiB

4 years ago
  1. import * as mozilla from 'source-map';
  2. /**
  3. * @param plugins Can also be included with the Processor#use method.
  4. * @returns A processor that will apply plugins as CSS processors.
  5. */
  6. declare function postcss(plugins?: postcss.AcceptedPlugin[]): postcss.Processor;
  7. declare function postcss(...plugins: postcss.AcceptedPlugin[]): postcss.Processor;
  8. declare namespace postcss {
  9. type AcceptedPlugin = Plugin<any> | Transformer | {
  10. postcss: TransformCallback | Processor;
  11. } | Processor;
  12. /**
  13. * Creates a PostCSS plugin with a standard API.
  14. * @param name Plugin name. Same as in name property in package.json. It will
  15. * be saved in plugin.postcssPlugin property.
  16. * @param initializer Will receive plugin options and should return functions
  17. * to modify nodes in input CSS.
  18. */
  19. function plugin<T>(name: string, initializer: PluginInitializer<T>): Plugin<T>;
  20. interface Plugin<T> extends Transformer {
  21. (opts?: T): Transformer;
  22. postcss: Transformer;
  23. process: (css: string | {
  24. toString(): string;
  25. } | Result, opts?: any) => LazyResult;
  26. }
  27. interface Transformer extends TransformCallback {
  28. postcssPlugin?: string;
  29. postcssVersion?: string;
  30. }
  31. interface TransformCallback {
  32. /**
  33. * @returns A Promise that resolves when all work is complete. May return
  34. * synchronously, but that style of plugin is only meant for debugging and
  35. * development. In either case, the resolved or returned value is not used -
  36. * the "result" is the output.
  37. */
  38. (root: Root, result: Result): Promise<any> | any;
  39. }
  40. interface PluginInitializer<T> {
  41. (pluginOptions?: T): Transformer;
  42. }
  43. /**
  44. * Contains helpers for working with vendor prefixes.
  45. */
  46. export namespace vendor {
  47. /**
  48. * @returns The vendor prefix extracted from the input string.
  49. */
  50. function prefix(prop: string): string;
  51. /**
  52. * @returns The input string stripped of its vendor prefix.
  53. */
  54. function unprefixed(prop: string): string;
  55. }
  56. type ParserInput = string | { toString(): string };
  57. interface Parser {
  58. (css: ParserInput, opts?: Pick<ProcessOptions, 'map' | 'from'>): Root;
  59. }
  60. interface Builder {
  61. (part: string, node?: Node, type?: 'start' | 'end'): void;
  62. }
  63. interface Stringifier {
  64. (node: Node, builder: Builder): void;
  65. }
  66. /**
  67. * Default function to convert a node tree into a CSS string.
  68. */
  69. const stringify: Stringifier;
  70. /**
  71. * Parses source CSS.
  72. * @param css The CSS to parse.
  73. * @param options
  74. * @returns {} A new Root node, which contains the source CSS nodes.
  75. */
  76. const parse: Parser;
  77. /**
  78. * Contains helpers for safely splitting lists of CSS values, preserving
  79. * parentheses and quotes.
  80. */
  81. export namespace list {
  82. /**
  83. * Safely splits space-separated values (such as those for background,
  84. * border-radius and other shorthand properties).
  85. */
  86. function space(str: string): string[];
  87. /**
  88. * Safely splits comma-separated values (such as those for transition-* and
  89. * background properties).
  90. */
  91. function comma(str: string): string[];
  92. }
  93. /**
  94. * Creates a new Comment node.
  95. * @param defaults Properties for the new Comment node.
  96. * @returns The new node.
  97. */
  98. function comment(defaults?: CommentNewProps): Comment;
  99. /**
  100. * Creates a new AtRule node.
  101. * @param defaults Properties for the new AtRule node.
  102. * @returns The new node.
  103. */
  104. function atRule(defaults?: AtRuleNewProps): AtRule;
  105. /**
  106. * Creates a new Declaration node.
  107. * @param defaults Properties for the new Declaration node.
  108. * @returns The new node.
  109. */
  110. function decl(defaults?: DeclarationNewProps): Declaration;
  111. /**
  112. * Creates a new Rule node.
  113. * @param defaults Properties for the new Rule node.
  114. * @returns The new node.
  115. */
  116. function rule(defaults?: RuleNewProps): Rule;
  117. /**
  118. * Creates a new Root node.
  119. * @param defaults Properties for the new Root node.
  120. * @returns The new node.
  121. */
  122. function root(defaults?: object): Root;
  123. interface SourceMapOptions {
  124. /**
  125. * Indicates that the source map should be embedded in the output CSS as a
  126. * Base64-encoded comment. By default, it is true. But if all previous maps
  127. * are external, not inline, PostCSS will not embed the map even if you do
  128. * not set this option.
  129. *
  130. * If you have an inline source map, the result.map property will be empty,
  131. * as the source map will be contained within the text of result.css.
  132. */
  133. inline?: boolean;
  134. /**
  135. * Source map content from a previous processing step (e.g., Sass compilation).
  136. * PostCSS will try to read the previous source map automatically (based on comments
  137. * within the source CSS), but you can use this option to identify it manually.
  138. * If desired, you can omit the previous map with prev: false.
  139. */
  140. prev?: any;
  141. /**
  142. * Indicates that PostCSS should set the origin content (e.g., Sass source)
  143. * of the source map. By default, it is true. But if all previous maps do not
  144. * contain sources content, PostCSS will also leave it out even if you do not set
  145. * this option.
  146. */
  147. sourcesContent?: boolean;
  148. /**
  149. * Indicates that PostCSS should add annotation comments to the CSS. By default,
  150. * PostCSS will always add a comment with a path to the source map. PostCSS will
  151. * not add annotations to CSS files that do not contain any comments.
  152. *
  153. * By default, PostCSS presumes that you want to save the source map as
  154. * opts.to + '.map' and will use this path in the annotation comment. A different
  155. * path can be set by providing a string value for annotation.
  156. *
  157. * If you have set inline: true, annotation cannot be disabled.
  158. */
  159. annotation?: string | false;
  160. /**
  161. * Override "from" in map's sources.
  162. */
  163. from?: string;
  164. }
  165. /**
  166. * A Processor instance contains plugins to process CSS. Create one
  167. * Processor instance, initialize its plugins, and then use that instance
  168. * on numerous CSS files.
  169. */
  170. interface Processor {
  171. /**
  172. * Adds a plugin to be used as a CSS processor. Plugins can also be
  173. * added by passing them as arguments when creating a postcss instance.
  174. */
  175. use(plugin: AcceptedPlugin): Processor;
  176. /**
  177. * Parses source CSS. Because some plugins can be asynchronous it doesn't
  178. * make any transformations. Transformations will be applied in LazyResult's
  179. * methods.
  180. * @param css Input CSS or any object with toString() method, like a file
  181. * stream. If a Result instance is passed the processor will take the
  182. * existing Root parser from it.
  183. */
  184. process(css: ParserInput | Result | LazyResult | Root, options?: ProcessOptions): LazyResult;
  185. /**
  186. * Contains plugins added to this processor.
  187. */
  188. plugins: Plugin<any>[];
  189. /**
  190. * Contains the current version of PostCSS (e.g., "4.0.5").
  191. */
  192. version: string;
  193. }
  194. interface ProcessOptions {
  195. /**
  196. * The path of the CSS source file. You should always set "from", because it is
  197. * used in source map generation and syntax error messages.
  198. */
  199. from?: string;
  200. /**
  201. * The path where you'll put the output CSS file. You should always set "to"
  202. * to generate correct source maps.
  203. */
  204. to?: string;
  205. /**
  206. * Function to generate AST by string.
  207. */
  208. parser?: Parser;
  209. /**
  210. * Class to generate string by AST.
  211. */
  212. stringifier?: Stringifier;
  213. /**
  214. * Object with parse and stringify.
  215. */
  216. syntax?: Syntax;
  217. /**
  218. * Source map options
  219. */
  220. map?: SourceMapOptions | boolean;
  221. }
  222. interface Syntax {
  223. /**
  224. * Function to generate AST by string.
  225. */
  226. parse?: Parser;
  227. /**
  228. * Class to generate string by AST.
  229. */
  230. stringify?: Stringifier;
  231. }
  232. /**
  233. * A promise proxy for the result of PostCSS transformations.
  234. */
  235. interface LazyResult {
  236. /**
  237. * Processes input CSS through synchronous and asynchronous plugins.
  238. * @param onRejected Called if any plugin throws an error.
  239. */
  240. then: Promise<Result>["then"];
  241. /**
  242. * Processes input CSS through synchronous and asynchronous plugins.
  243. * @param onRejected Called if any plugin throws an error.
  244. */
  245. catch: Promise<Result>["catch"];
  246. /**
  247. * Alias for css property.
  248. */
  249. toString(): string;
  250. /**
  251. * Processes input CSS through synchronous plugins and converts Root to
  252. * CSS string. This property will only work with synchronous plugins. If
  253. * the processor contains any asynchronous plugins it will throw an error.
  254. * In this case, you should use LazyResult#then() instead.
  255. * @returns Result#css.
  256. */
  257. css: string;
  258. /**
  259. * Alias for css property to use when syntaxes generate non-CSS output.
  260. */
  261. content: string;
  262. /**
  263. * Processes input CSS through synchronous plugins. This property will
  264. * work only with synchronous plugins. If processor contains any
  265. * asynchronous plugins it will throw an error. You should use
  266. * LazyResult#then() instead.
  267. */
  268. map: ResultMap;
  269. /**
  270. * Processes input CSS through synchronous plugins. This property will work
  271. * only with synchronous plugins. If processor contains any asynchronous
  272. * plugins it will throw an error. You should use LazyResult#then() instead.
  273. */
  274. root: Root;
  275. /**
  276. * Processes input CSS through synchronous plugins and calls Result#warnings().
  277. * This property will only work with synchronous plugins. If the processor
  278. * contains any asynchronous plugins it will throw an error. In this case,
  279. * you should use LazyResult#then() instead.
  280. */
  281. warnings(): Warning[];
  282. /**
  283. * Processes input CSS through synchronous plugins. This property will work
  284. * only with synchronous plugins. If processor contains any asynchronous
  285. * plugins it will throw an error. You should use LazyResult#then() instead.
  286. */
  287. messages: ResultMessage[];
  288. /**
  289. * @returns A processor used for CSS transformations.
  290. */
  291. processor: Processor;
  292. /**
  293. * @returns Options from the Processor#process(css, opts) call that produced
  294. * this Result instance.
  295. */
  296. opts: ResultOptions;
  297. }
  298. /**
  299. * Provides the result of the PostCSS transformations.
  300. */
  301. interface Result {
  302. /**
  303. * Alias for css property.
  304. */
  305. toString(): string;
  306. /**
  307. * Creates an instance of Warning and adds it to messages.
  308. * @param message Used in the text property of the message object.
  309. * @param options Properties for Message object.
  310. */
  311. warn(message: string, options?: WarningOptions): void;
  312. /**
  313. * @returns Warnings from plugins, filtered from messages.
  314. */
  315. warnings(): Warning[];
  316. /**
  317. * A CSS string representing this Result's Root instance.
  318. */
  319. css: string;
  320. /**
  321. * Alias for css property to use with syntaxes that generate non-CSS output.
  322. */
  323. content: string;
  324. /**
  325. * An instance of the SourceMapGenerator class from the source-map library,
  326. * representing changes to the Result's Root instance.
  327. * This property will have a value only if the user does not want an inline
  328. * source map. By default, PostCSS generates inline source maps, written
  329. * directly into the processed CSS. The map property will be empty by default.
  330. * An external source map will be generated and assigned to map only if
  331. * the user has set the map.inline option to false, or if PostCSS was passed
  332. * an external input source map.
  333. */
  334. map: ResultMap;
  335. /**
  336. * Contains the Root node after all transformations.
  337. */
  338. root?: Root;
  339. /**
  340. * Contains messages from plugins (e.g., warnings or custom messages).
  341. * Add a warning using Result#warn() and get all warnings
  342. * using the Result#warnings() method.
  343. */
  344. messages: ResultMessage[];
  345. /**
  346. * The Processor instance used for this transformation.
  347. */
  348. processor?: Processor;
  349. /**
  350. * Options from the Processor#process(css, opts) or Root#toResult(opts) call
  351. * that produced this Result instance.
  352. */
  353. opts?: ResultOptions;
  354. }
  355. interface ResultOptions extends ProcessOptions {
  356. /**
  357. * The CSS node that was the source of the warning.
  358. */
  359. node?: postcss.Node;
  360. /**
  361. * Name of plugin that created this warning. Result#warn() will fill it
  362. * automatically with plugin.postcssPlugin value.
  363. */
  364. plugin?: string;
  365. }
  366. interface ResultMap {
  367. /**
  368. * Add a single mapping from original source line and column to the generated
  369. * source's line and column for this source map being created. The mapping
  370. * object should have the following properties:
  371. * @param mapping
  372. * @returns {}
  373. */
  374. addMapping(mapping: mozilla.Mapping): void;
  375. /**
  376. * Set the source content for an original source file.
  377. * @param sourceFile The URL of the original source file.
  378. * @param sourceContent The content of the source file.
  379. */
  380. setSourceContent(sourceFile: string, sourceContent: string): void;
  381. /**
  382. * Applies a SourceMap for a source file to the SourceMap. Each mapping to
  383. * the supplied source file is rewritten using the supplied SourceMap.
  384. * Note: The resolution for the resulting mappings is the minimum of this
  385. * map and the supplied map.
  386. * @param sourceMapConsumer The SourceMap to be applied.
  387. * @param sourceFile The filename of the source file. If omitted, sourceMapConsumer
  388. * file will be used, if it exists. Otherwise an error will be thrown.
  389. * @param sourceMapPath The dirname of the path to the SourceMap to be applied.
  390. * If relative, it is relative to the SourceMap. This parameter is needed when
  391. * the two SourceMaps aren't in the same directory, and the SourceMap to be
  392. * applied contains relative source paths. If so, those relative source paths
  393. * need to be rewritten relative to the SourceMap.
  394. * If omitted, it is assumed that both SourceMaps are in the same directory;
  395. * thus, not needing any rewriting (Supplying '.' has the same effect).
  396. */
  397. applySourceMap(
  398. sourceMapConsumer: mozilla.SourceMapConsumer,
  399. sourceFile?: string,
  400. sourceMapPath?: string
  401. ): void;
  402. /**
  403. * Renders the source map being generated to JSON.
  404. */
  405. toJSON: () => mozilla.RawSourceMap;
  406. /**
  407. * Renders the source map being generated to a string.
  408. */
  409. toString: () => string;
  410. }
  411. interface ResultMessage {
  412. type: string;
  413. plugin: string;
  414. [others: string]: any;
  415. }
  416. /**
  417. * Represents a plugin warning. It can be created using Result#warn().
  418. */
  419. interface Warning {
  420. /**
  421. * @returns Error position, message.
  422. */
  423. toString(): string;
  424. /**
  425. * Contains the warning message.
  426. */
  427. text: string;
  428. /**
  429. * Contains the name of the plugin that created this warning. When you
  430. * call Result#warn(), it will fill this property automatically.
  431. */
  432. plugin: string;
  433. /**
  434. * The CSS node that caused the warning.
  435. */
  436. node: Node;
  437. /**
  438. * The line in the input file with this warning's source.
  439. */
  440. line: number;
  441. /**
  442. * Column in the input file with this warning's source.
  443. */
  444. column: number;
  445. }
  446. interface WarningOptions extends ResultOptions {
  447. /**
  448. * A word inside a node's string that should be highlighted as source
  449. * of warning.
  450. */
  451. word?: string;
  452. /**
  453. * The index inside a node's string that should be highlighted as
  454. * source of warning.
  455. */
  456. index?: number;
  457. }
  458. /**
  459. * The CSS parser throws this error for broken CSS.
  460. */
  461. interface CssSyntaxError extends InputOrigin {
  462. name: string;
  463. /**
  464. * @returns Error position, message and source code of broken part.
  465. */
  466. toString(): string;
  467. /**
  468. * @param color Whether arrow should be colored red by terminal color codes.
  469. * By default, PostCSS will use process.stdout.isTTY and
  470. * process.env.NODE_DISABLE_COLORS.
  471. * @returns A few lines of CSS source that caused the error. If CSS has
  472. * input source map without sourceContent this method will return an empty
  473. * string.
  474. */
  475. showSourceCode(color?: boolean): string;
  476. /**
  477. * Contains full error text in the GNU error format.
  478. */
  479. message: string;
  480. /**
  481. * Contains only the error description.
  482. */
  483. reason: string;
  484. /**
  485. * Contains the PostCSS plugin name if the error didn't come from the
  486. * CSS parser.
  487. */
  488. plugin?: string;
  489. input?: InputOrigin;
  490. }
  491. interface InputOrigin {
  492. /**
  493. * If parser's from option is set, contains the absolute path to the
  494. * broken file. PostCSS will use the input source map to detect the
  495. * original error location. If you wrote a Sass file, then compiled it
  496. * to CSS and parsed it with PostCSS, PostCSS will show the original
  497. * position in the Sass file. If you need the position in the PostCSS
  498. * input (e.g., to debug the previous compiler), use error.input.file.
  499. */
  500. file?: string;
  501. /**
  502. * Contains the source line of the error. PostCSS will use the input
  503. * source map to detect the original error location. If you wrote a Sass
  504. * file, then compiled it to CSS and parsed it with PostCSS, PostCSS
  505. * will show the original position in the Sass file. If you need the
  506. * position in the PostCSS input (e.g., to debug the previous
  507. * compiler), use error.input.line.
  508. */
  509. line?: number;
  510. /**
  511. * Contains the source column of the error. PostCSS will use input
  512. * source map to detect the original error location. If you wrote a
  513. * Sass file, then compiled it to CSS and parsed it with PostCSS,
  514. * PostCSS will show the original position in the Sass file. If you
  515. * need the position in the PostCSS input (e.g., to debug the
  516. * previous compiler), use error.input.column.
  517. */
  518. column?: number;
  519. /**
  520. * Contains the source code of the broken file. PostCSS will use the
  521. * input source map to detect the original error location. If you wrote
  522. * a Sass file, then compiled it to CSS and parsed it with PostCSS,
  523. * PostCSS will show the original position in the Sass file. If you need
  524. * the position in the PostCSS input (e.g., to debug the previous
  525. * compiler), use error.input.source.
  526. */
  527. source?: string;
  528. }
  529. export class PreviousMap {
  530. private inline;
  531. annotation: string;
  532. root: string;
  533. private consumerCache;
  534. text: string;
  535. file: string;
  536. constructor(css: any, opts: any);
  537. consumer(): mozilla.SourceMapConsumer;
  538. withContent(): boolean;
  539. startWith(string: string, start: string): boolean;
  540. loadAnnotation(css: string): void;
  541. decodeInline(text: string): string;
  542. loadMap(
  543. file: any,
  544. prev: string | Function | mozilla.SourceMapConsumer | mozilla.SourceMapGenerator | mozilla.RawSourceMap
  545. ): string;
  546. isMap(map: any): boolean;
  547. }
  548. /**
  549. * Represents the source CSS.
  550. */
  551. interface Input {
  552. /**
  553. * The absolute path to the CSS source file defined with the "from" option.
  554. * Either this property or the "id" property are always defined.
  555. */
  556. file?: string;
  557. /**
  558. * The unique ID of the CSS source. Used if "from" option is not provided
  559. * (because PostCSS does not know the file path). Either this property
  560. * or the "file" property are always defined.
  561. */
  562. id?: string;
  563. /**
  564. * The CSS source identifier. Contains input.file if the user set the
  565. * "from" option, or input.id if they did not.
  566. */
  567. from: string;
  568. /**
  569. * Represents the input source map passed from a compilation step before
  570. * PostCSS (e.g., from the Sass compiler).
  571. */
  572. map: PreviousMap;
  573. /**
  574. * The flag to indicate whether or not the source code has Unicode BOM.
  575. */
  576. hasBOM: boolean;
  577. /**
  578. * Reads the input source map.
  579. * @returns A symbol position in the input source (e.g., in a Sass file
  580. * that was compiled to CSS before being passed to PostCSS):
  581. */
  582. origin(line: number, column: number): InputOrigin;
  583. }
  584. type ChildNode = AtRule | Rule | Declaration | Comment;
  585. type Node = Root | ChildNode;
  586. interface NodeBase {
  587. /**
  588. * Returns the input source of the node. The property is used in source
  589. * map generation. If you create a node manually
  590. * (e.g., with postcss.decl() ), that node will not have a source
  591. * property and will be absent from the source map. For this reason, the
  592. * plugin developer should consider cloning nodes to create new ones
  593. * (in which case the new node's source will reference the original,
  594. * cloned node) or setting the source property manually.
  595. */
  596. source?: NodeSource;
  597. /**
  598. * Contains information to generate byte-to-byte equal node string as it
  599. * was in origin input.
  600. */
  601. raws: NodeRaws;
  602. /**
  603. * @returns A CSS string representing the node.
  604. */
  605. toString(): string;
  606. /**
  607. * This method produces very useful error messages. If present, an input
  608. * source map will be used to get the original position of the source, even
  609. * from a previous compilation step (e.g., from Sass compilation).
  610. * @returns The original position of the node in the source, showing line
  611. * and column numbers and also a small excerpt to facilitate debugging.
  612. */
  613. error(
  614. /**
  615. * Error description.
  616. */
  617. message: string, options?: NodeErrorOptions): CssSyntaxError;
  618. /**
  619. * Creates an instance of Warning and adds it to messages. This method is
  620. * provided as a convenience wrapper for Result#warn.
  621. * Note that `opts.node` is automatically passed to Result#warn for you.
  622. * @param result The result that will receive the warning.
  623. * @param text Warning message. It will be used in the `text` property of
  624. * the message object.
  625. * @param opts Properties to assign to the message object.
  626. */
  627. warn(result: Result, text: string, opts?: WarningOptions): void;
  628. /**
  629. * @returns The next child of the node's parent; or, returns undefined if
  630. * the current node is the last child.
  631. */
  632. next(): ChildNode | void;
  633. /**
  634. * @returns The previous child of the node's parent; or, returns undefined
  635. * if the current node is the first child.
  636. */
  637. prev(): ChildNode | void;
  638. /**
  639. * Insert new node before current node to current nodes parent.
  640. *
  641. * Just an alias for `node.parent.insertBefore(node, newNode)`.
  642. *
  643. * @returns this node for method chaining.
  644. *
  645. * @example
  646. * decl.before('content: ""');
  647. */
  648. before(newNode: Node | object | string | Node[]): this;
  649. /**
  650. * Insert new node after current node to current nodes parent.
  651. *
  652. * Just an alias for `node.parent.insertAfter(node, newNode)`.
  653. *
  654. * @returns this node for method chaining.
  655. *
  656. * @example
  657. * decl.after('color: black');
  658. */
  659. after(newNode: Node | object | string | Node[]): this;
  660. /**
  661. * @returns The Root instance of the node's tree.
  662. */
  663. root(): Root;
  664. /**
  665. * Removes the node from its parent and cleans the parent property in the
  666. * node and its children.
  667. * @returns This node for chaining.
  668. */
  669. remove(): this;
  670. /**
  671. * Inserts node(s) before the current node and removes the current node.
  672. * @returns This node for chaining.
  673. */
  674. replaceWith(...nodes: (Node | object)[]): this;
  675. /**
  676. * @param overrides New properties to override in the clone.
  677. * @returns A clone of this node. The node and its (cloned) children will
  678. * have a clean parent and code style properties.
  679. */
  680. clone(overrides?: object): this;
  681. /**
  682. * Shortcut to clone the node and insert the resulting cloned node before
  683. * the current node.
  684. * @param overrides New Properties to override in the clone.
  685. * @returns The cloned node.
  686. */
  687. cloneBefore(overrides?: object): this;
  688. /**
  689. * Shortcut to clone the node and insert the resulting cloned node after
  690. * the current node.
  691. * @param overrides New Properties to override in the clone.
  692. * @returns The cloned node.
  693. */
  694. cloneAfter(overrides?: object): this;
  695. /**
  696. * @param prop Name or code style property.
  697. * @param defaultType Name of default value. It can be easily missed if the
  698. * value is the same as prop.
  699. * @returns A code style property value. If the node is missing the code
  700. * style property (because the node was manually built or cloned), PostCSS
  701. * will try to autodetect the code style property by looking at other nodes
  702. * in the tree.
  703. */
  704. raw(prop: string, defaultType?: string): any;
  705. }
  706. interface NodeNewProps {
  707. source?: NodeSource;
  708. raws?: NodeRaws;
  709. }
  710. interface NodeRaws {
  711. /**
  712. * The space symbols before the node. It also stores `*` and `_`
  713. * symbols before the declaration (IE hack).
  714. */
  715. before?: string;
  716. /**
  717. * The space symbols after the last child of the node to the end of
  718. * the node.
  719. */
  720. after?: string;
  721. /**
  722. * The symbols between the property and value for declarations,
  723. * selector and "{" for rules, last parameter and "{" for at-rules.
  724. */
  725. between?: string;
  726. /**
  727. * True if last child has (optional) semicolon.
  728. */
  729. semicolon?: boolean;
  730. /**
  731. * The space between the at-rule's name and parameters.
  732. */
  733. afterName?: string;
  734. /**
  735. * The space symbols between "/*" and comment's text.
  736. */
  737. left?: string;
  738. /**
  739. * The space symbols between comment's text and "*\/".
  740. */
  741. right?: string;
  742. /**
  743. * The content of important statement, if it is not just "!important".
  744. */
  745. important?: string;
  746. }
  747. interface NodeSource {
  748. input: Input;
  749. /**
  750. * The starting position of the node's source.
  751. */
  752. start?: {
  753. column: number;
  754. line: number;
  755. };
  756. /**
  757. * The ending position of the node's source.
  758. */
  759. end?: {
  760. column: number;
  761. line: number;
  762. };
  763. }
  764. interface NodeErrorOptions {
  765. /**
  766. * Plugin name that created this error. PostCSS will set it automatically.
  767. */
  768. plugin?: string;
  769. /**
  770. * A word inside a node's string, that should be highlighted as source
  771. * of error.
  772. */
  773. word?: string;
  774. /**
  775. * An index inside a node's string that should be highlighted as source
  776. * of error.
  777. */
  778. index?: number;
  779. }
  780. interface JsonNode {
  781. /**
  782. * Returns a string representing the node's type. Possible values are
  783. * root, atrule, rule, decl or comment.
  784. */
  785. type?: string;
  786. /**
  787. * Returns the node's parent node.
  788. */
  789. parent?: JsonContainer;
  790. /**
  791. * Returns the input source of the node. The property is used in source
  792. * map generation. If you create a node manually (e.g., with
  793. * postcss.decl() ), that node will not have a source property and
  794. * will be absent from the source map. For this reason, the plugin
  795. * developer should consider cloning nodes to create new ones (in which
  796. * case the new node's source will reference the original, cloned node)
  797. * or setting the source property manually.
  798. */
  799. source?: NodeSource;
  800. /**
  801. * Contains information to generate byte-to-byte equal node string as it
  802. * was in origin input.
  803. */
  804. raws?: NodeRaws;
  805. }
  806. type Container = Root | AtRule | Rule;
  807. /**
  808. * Containers can store any content. If you write a rule inside a rule,
  809. * PostCSS will parse it.
  810. */
  811. interface ContainerBase extends NodeBase {
  812. /**
  813. * Contains the container's children.
  814. */
  815. nodes?: ChildNode[];
  816. /**
  817. * @returns The container's first child.
  818. */
  819. first?: ChildNode;
  820. /**
  821. * @returns The container's last child.
  822. */
  823. last?: ChildNode;
  824. /**
  825. * @param overrides New properties to override in the clone.
  826. * @returns A clone of this node. The node and its (cloned) children will
  827. * have a clean parent and code style properties.
  828. */
  829. clone(overrides?: object): this;
  830. /**
  831. * @param child Child of the current container.
  832. * @returns The child's index within the container's "nodes" array.
  833. */
  834. index(child: ChildNode | number): number;
  835. /**
  836. * Determines whether all child nodes satisfy the specified test.
  837. * @param callback A function that accepts up to three arguments. The
  838. * every method calls the callback function for each node until the
  839. * callback returns false, or until the end of the array.
  840. * @returns True if the callback returns true for all of the container's
  841. * children.
  842. */
  843. every(callback: (node: ChildNode, index: number, nodes: ChildNode[]) => any, thisArg?: any): boolean;
  844. /**
  845. * Determines whether the specified callback returns true for any child node.
  846. * @param callback A function that accepts up to three arguments. The some
  847. * method calls the callback for each node until the callback returns true,
  848. * or until the end of the array.
  849. * @param thisArg An object to which the this keyword can refer in the
  850. * callback function. If thisArg is omitted, undefined is used as the
  851. * this value.
  852. * @returns True if callback returns true for (at least) one of the
  853. * container's children.
  854. */
  855. some(callback: (node: ChildNode, index: number, nodes: ChildNode[]) => boolean, thisArg?: any): boolean;
  856. /**
  857. * Iterates through the container's immediate children, calling the
  858. * callback function for each child. If you need to recursively iterate
  859. * through all the container's descendant nodes, use container.walk().
  860. * Unlike the for {} -cycle or Array#forEach() this iterator is safe if
  861. * you are mutating the array of child nodes during iteration.
  862. * @param callback Iterator. Returning false will break iteration. Safe
  863. * if you are mutating the array of child nodes during iteration. PostCSS
  864. * will adjust the current index to match the mutations.
  865. * @returns False if the callback returns false during iteration.
  866. */
  867. each(callback: (node: ChildNode, index: number) => any): boolean | void;
  868. /**
  869. * Traverses the container's descendant nodes, calling `callback` for each
  870. * node. Like container.each(), this method is safe to use if you are
  871. * mutating arrays during iteration. If you only need to iterate through
  872. * the container's immediate children, use container.each().
  873. * @param callback Iterator.
  874. */
  875. walk(callback: (node: ChildNode, index: number) => any): boolean | void;
  876. /**
  877. * Traverses the container's descendant nodes, calling `callback` for each
  878. * declaration. Like container.each(), this method is safe to use if you
  879. * are mutating arrays during iteration.
  880. * @param propFilter Filters declarations by property name. Only those
  881. * declarations whose property matches propFilter will be iterated over.
  882. * @param callback Called for each declaration node within the container.
  883. */
  884. walkDecls(propFilter: string | RegExp, callback?: (decl: Declaration, index: number) => any): boolean | void;
  885. walkDecls(callback: (decl: Declaration, index: number) => any): boolean | void;
  886. /**
  887. * Traverses the container's descendant nodes, calling `callback` for each
  888. * at-rule. Like container.each(), this method is safe to use if you are
  889. * mutating arrays during iteration.
  890. * @param nameFilter Filters at-rules by name. If provided, iteration
  891. * will only happen over at-rules that have matching names.
  892. * @param callback Iterator called for each at-rule node within the
  893. * container.
  894. */
  895. walkAtRules(nameFilter: string | RegExp, callback: (atRule: AtRule, index: number) => any): boolean | void;
  896. walkAtRules(callback: (atRule: AtRule, index: number) => any): boolean | void;
  897. /**
  898. * Traverses the container's descendant nodes, calling `callback` for each
  899. * rule. Like container.each(), this method is safe to use if you are
  900. * mutating arrays during iteration.
  901. * @param selectorFilter Filters rules by selector. If provided,
  902. * iteration will only happen over rules that have matching names.
  903. * @param callback Iterator called for each rule node within the
  904. * container.
  905. */
  906. walkRules(selectorFilter: string | RegExp, callback: (atRule: Rule, index: number) => any): boolean | void;
  907. walkRules(callback: (atRule: Rule, index: number) => any): boolean | void;
  908. walkRules(selectorFilter: any, callback?: (atRule: Rule, index: number) => any): boolean | void;
  909. /**
  910. * Traverses the container's descendant nodes, calling `callback` for each
  911. * comment. Like container.each(), this method is safe to use if you are
  912. * mutating arrays during iteration.
  913. * @param callback Iterator called for each comment node within the container.
  914. */
  915. walkComments(callback: (comment: Comment, indexed: number) => any): void | boolean;
  916. /**
  917. * Passes all declaration values within the container that match pattern
  918. * through the callback, replacing those values with the returned result of
  919. * callback. This method is useful if you are using a custom unit or
  920. * function and need to iterate through all values.
  921. * @param pattern Pattern that we need to replace.
  922. * @param options Options to speed up the search.
  923. * @param callbackOrReplaceValue String to replace pattern or callback
  924. * that will return a new value. The callback will receive the same
  925. * arguments as those passed to a function parameter of String#replace.
  926. */
  927. replaceValues(pattern: string | RegExp, options: {
  928. /**
  929. * Property names. The method will only search for values that match
  930. * regexp within declarations of listed properties.
  931. */
  932. props?: string[];
  933. /**
  934. * Used to narrow down values and speed up the regexp search. Searching
  935. * every single value with a regexp can be slow. If you pass a fast
  936. * string, PostCSS will first check whether the value contains the fast
  937. * string; and only if it does will PostCSS check that value against
  938. * regexp. For example, instead of just checking for /\d+rem/ on all
  939. * values, set fast: 'rem' to first check whether a value has the rem
  940. * unit, and only if it does perform the regexp check.
  941. */
  942. fast?: string;
  943. }, callbackOrReplaceValue: string | {
  944. (substring: string, ...args: any[]): string;
  945. }): this;
  946. replaceValues(pattern: string | RegExp, callbackOrReplaceValue: string | {
  947. (substring: string, ...args: any[]): string;
  948. }): this;
  949. /**
  950. * Inserts new nodes to the beginning of the container.
  951. * Because each node class is identifiable by unique properties, use the
  952. * following shortcuts to create nodes in insert methods:
  953. * root.prepend({ name: '@charset', params: '"UTF-8"' }); // at-rule
  954. * root.prepend({ selector: 'a' }); // rule
  955. * rule.prepend({ prop: 'color', value: 'black' }); // declaration
  956. * rule.prepend({ text: 'Comment' }) // comment
  957. * A string containing the CSS of the new element can also be used. This
  958. * approach is slower than the above shortcuts.
  959. * root.prepend('a {}');
  960. * root.first.prepend('color: black; z-index: 1');
  961. * @param nodes New nodes.
  962. * @returns This container for chaining.
  963. */
  964. prepend(...nodes: (Node | object | string)[]): this;
  965. /**
  966. * Inserts new nodes to the end of the container.
  967. * Because each node class is identifiable by unique properties, use the
  968. * following shortcuts to create nodes in insert methods:
  969. * root.append({ name: '@charset', params: '"UTF-8"' }); // at-rule
  970. * root.append({ selector: 'a' }); // rule
  971. * rule.append({ prop: 'color', value: 'black' }); // declaration
  972. * rule.append({ text: 'Comment' }) // comment
  973. * A string containing the CSS of the new element can also be used. This
  974. * approach is slower than the above shortcuts.
  975. * root.append('a {}');
  976. * root.first.append('color: black; z-index: 1');
  977. * @param nodes New nodes.
  978. * @returns This container for chaining.
  979. */
  980. append(...nodes: (Node | object | string)[]): this;
  981. /**
  982. * Insert newNode before oldNode within the container.
  983. * @param oldNode Child or child's index.
  984. * @returns This container for chaining.
  985. */
  986. insertBefore(oldNode: ChildNode | number, newNode: ChildNode | object | string): this;
  987. /**
  988. * Insert newNode after oldNode within the container.
  989. * @param oldNode Child or child's index.
  990. * @returns This container for chaining.
  991. */
  992. insertAfter(oldNode: ChildNode | number, newNode: ChildNode | object | string): this;
  993. /**
  994. * Removes the container from its parent and cleans the parent property in the
  995. * container and its children.
  996. * @returns This container for chaining.
  997. */
  998. remove(): this;
  999. /**
  1000. * Removes child from the container and cleans the parent properties
  1001. * from the node and its children.
  1002. * @param child Child or child's index.
  1003. * @returns This container for chaining.
  1004. */
  1005. removeChild(child: ChildNode | number): this;
  1006. /**
  1007. * Removes all children from the container and cleans their parent
  1008. * properties.
  1009. * @returns This container for chaining.
  1010. */
  1011. removeAll(): this;
  1012. }
  1013. interface ContainerNewProps extends NodeNewProps {
  1014. /**
  1015. * Contains the container's children.
  1016. */
  1017. nodes?: ChildNode[];
  1018. raws?: ContainerRaws;
  1019. }
  1020. interface ContainerRaws extends NodeRaws {
  1021. indent?: string;
  1022. }
  1023. interface JsonContainer extends JsonNode {
  1024. /**
  1025. * Contains the container's children.
  1026. */
  1027. nodes?: ChildNode[];
  1028. /**
  1029. * @returns The container's first child.
  1030. */
  1031. first?: ChildNode;
  1032. /**
  1033. * @returns The container's last child.
  1034. */
  1035. last?: ChildNode;
  1036. }
  1037. /**
  1038. * Represents a CSS file and contains all its parsed nodes.
  1039. */
  1040. interface Root extends ContainerBase {
  1041. type: 'root';
  1042. /**
  1043. * Inherited from Container. Should always be undefined for a Root node.
  1044. */
  1045. parent: void;
  1046. /**
  1047. * @param overrides New properties to override in the clone.
  1048. * @returns A clone of this node. The node and its (cloned) children will
  1049. * have a clean parent and code style properties.
  1050. */
  1051. clone(overrides?: object): this;
  1052. /**
  1053. * @returns A Result instance representing the root's CSS.
  1054. */
  1055. toResult(options?: {
  1056. /**
  1057. * The path where you'll put the output CSS file. You should always
  1058. * set "to" to generate correct source maps.
  1059. */
  1060. to?: string;
  1061. map?: SourceMapOptions;
  1062. }): Result;
  1063. /**
  1064. * Removes child from the root node, and the parent properties of node and
  1065. * its children.
  1066. * @param child Child or child's index.
  1067. * @returns This root node for chaining.
  1068. */
  1069. removeChild(child: ChildNode | number): this;
  1070. }
  1071. interface RootNewProps extends ContainerNewProps {
  1072. }
  1073. interface JsonRoot extends JsonContainer {
  1074. }
  1075. /**
  1076. * Represents an at-rule. If it's followed in the CSS by a {} block, this
  1077. * node will have a nodes property representing its children.
  1078. */
  1079. interface AtRule extends ContainerBase {
  1080. type: 'atrule';
  1081. /**
  1082. * Returns the atrule's parent node.
  1083. */
  1084. parent: Container;
  1085. /**
  1086. * The identifier that immediately follows the @.
  1087. */
  1088. name: string;
  1089. /**
  1090. * These are the values that follow the at-rule's name, but precede any {}
  1091. * block. The spec refers to this area as the at-rule's "prelude".
  1092. */
  1093. params: string;
  1094. /**
  1095. * @param overrides New properties to override in the clone.
  1096. * @returns A clone of this node. The node and its (cloned) children will
  1097. * have a clean parent and code style properties.
  1098. */
  1099. clone(overrides?: object): this;
  1100. }
  1101. interface AtRuleNewProps extends ContainerNewProps {
  1102. /**
  1103. * The identifier that immediately follows the @.
  1104. */
  1105. name?: string;
  1106. /**
  1107. * These are the values that follow the at-rule's name, but precede any {}
  1108. * block. The spec refers to this area as the at-rule's "prelude".
  1109. */
  1110. params?: string | number;
  1111. raws?: AtRuleRaws;
  1112. }
  1113. interface AtRuleRaws extends NodeRaws {
  1114. params?: string;
  1115. }
  1116. interface JsonAtRule extends JsonContainer {
  1117. /**
  1118. * The identifier that immediately follows the @.
  1119. */
  1120. name?: string;
  1121. /**
  1122. * These are the values that follow the at-rule's name, but precede any {}
  1123. * block. The spec refers to this area as the at-rule's "prelude".
  1124. */
  1125. params?: string;
  1126. }
  1127. /**
  1128. * Represents a CSS rule: a selector followed by a declaration block.
  1129. */
  1130. interface Rule extends ContainerBase {
  1131. type: 'rule';
  1132. /**
  1133. * Returns the rule's parent node.
  1134. */
  1135. parent: Container;
  1136. /**
  1137. * The rule's full selector. If there are multiple comma-separated selectors,
  1138. * the entire group will be included.
  1139. */
  1140. selector: string;
  1141. /**
  1142. * An array containing the rule's individual selectors.
  1143. * Groups of selectors are split at commas.
  1144. */
  1145. selectors: string[];
  1146. /**
  1147. * @param overrides New properties to override in the clone.
  1148. * @returns A clone of this node. The node and its (cloned) children will
  1149. * have a clean parent and code style properties.
  1150. */
  1151. clone(overrides?: object): this;
  1152. }
  1153. interface RuleNewProps extends ContainerNewProps {
  1154. /**
  1155. * The rule's full selector. If there are multiple comma-separated selectors,
  1156. * the entire group will be included.
  1157. */
  1158. selector?: string;
  1159. /**
  1160. * An array containing the rule's individual selectors. Groups of selectors
  1161. * are split at commas.
  1162. */
  1163. selectors?: string[];
  1164. raws?: RuleRaws;
  1165. }
  1166. interface RuleRaws extends ContainerRaws {
  1167. /**
  1168. * The rule's full selector. If there are multiple comma-separated selectors,
  1169. * the entire group will be included.
  1170. */
  1171. selector?: string;
  1172. }
  1173. interface JsonRule extends JsonContainer {
  1174. /**
  1175. * The rule's full selector. If there are multiple comma-separated selectors,
  1176. * the entire group will be included.
  1177. */
  1178. selector?: string;
  1179. /**
  1180. * An array containing the rule's individual selectors.
  1181. * Groups of selectors are split at commas.
  1182. */
  1183. selectors?: string[];
  1184. }
  1185. /**
  1186. * Represents a CSS declaration.
  1187. */
  1188. interface Declaration extends NodeBase {
  1189. type: 'decl';
  1190. /**
  1191. * Returns the declaration's parent node.
  1192. */
  1193. parent: Container;
  1194. /**
  1195. * The declaration's property name.
  1196. */
  1197. prop: string;
  1198. /**
  1199. * The declaration's value. This value will be cleaned of comments. If the
  1200. * source value contained comments, those comments will be available in the
  1201. * _value.raws property. If you have not changed the value, the result of
  1202. * decl.toString() will include the original raws value (comments and all).
  1203. */
  1204. value: string;
  1205. /**
  1206. * True if the declaration has an !important annotation.
  1207. */
  1208. important: boolean;
  1209. /**
  1210. * @param overrides New properties to override in the clone.
  1211. * @returns A clone of this node. The node and its (cloned) children will
  1212. * have a clean parent and code style properties.
  1213. */
  1214. clone(overrides?: object): this;
  1215. }
  1216. interface DeclarationNewProps {
  1217. /**
  1218. * The declaration's property name.
  1219. */
  1220. prop?: string;
  1221. /**
  1222. * The declaration's value. This value will be cleaned of comments. If the
  1223. * source value contained comments, those comments will be available in the
  1224. * _value.raws property. If you have not changed the value, the result of
  1225. * decl.toString() will include the original raws value (comments and all).
  1226. */
  1227. value?: string;
  1228. raws?: DeclarationRaws;
  1229. }
  1230. interface DeclarationRaws extends NodeRaws {
  1231. /**
  1232. * The declaration's value. This value will be cleaned of comments.
  1233. * If the source value contained comments, those comments will be
  1234. * available in the _value.raws property. If you have not changed the value, the result of
  1235. * decl.toString() will include the original raws value (comments and all).
  1236. */
  1237. value?: string;
  1238. }
  1239. interface JsonDeclaration extends JsonNode {
  1240. /**
  1241. * True if the declaration has an !important annotation.
  1242. */
  1243. important?: boolean;
  1244. }
  1245. /**
  1246. * Represents a comment between declarations or statements (rule and at-rules).
  1247. * Comments inside selectors, at-rule parameters, or declaration values will
  1248. * be stored in the Node#raws properties.
  1249. */
  1250. interface Comment extends NodeBase {
  1251. type: 'comment';
  1252. /**
  1253. * Returns the comment's parent node.
  1254. */
  1255. parent: Container;
  1256. /**
  1257. * The comment's text.
  1258. */
  1259. text: string;
  1260. /**
  1261. * @param overrides New properties to override in the clone.
  1262. * @returns A clone of this node. The node and its (cloned) children will
  1263. * have a clean parent and code style properties.
  1264. */
  1265. clone(overrides?: object): this;
  1266. }
  1267. interface CommentNewProps {
  1268. /**
  1269. * The comment's text.
  1270. */
  1271. text?: string;
  1272. }
  1273. interface JsonComment extends JsonNode {
  1274. }
  1275. }
  1276. export = postcss;