postcss.d.ts 49 KB

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