postcss.d.ts 50 KB

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