node.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. import AtRule = require('./at-rule.js')
  2. import { AtRuleProps } from './at-rule.js'
  3. import Comment, { CommentProps } from './comment.js'
  4. import Container, { NewChild } from './container.js'
  5. import CssSyntaxError from './css-syntax-error.js'
  6. import Declaration, { DeclarationProps } from './declaration.js'
  7. import Document from './document.js'
  8. import Input from './input.js'
  9. import { Stringifier, Syntax } from './postcss.js'
  10. import Result from './result.js'
  11. import Root from './root.js'
  12. import Rule, { RuleProps } from './rule.js'
  13. import Warning, { WarningOptions } from './warning.js'
  14. declare namespace Node {
  15. export type ChildNode = AtRule.default | Comment | Declaration | Rule
  16. export type AnyNode =
  17. | AtRule.default
  18. | Comment
  19. | Declaration
  20. | Document
  21. | Root
  22. | Rule
  23. export type ChildProps =
  24. | AtRuleProps
  25. | CommentProps
  26. | DeclarationProps
  27. | RuleProps
  28. export interface Position {
  29. /**
  30. * Source line in file. In contrast to `offset` it starts from 1.
  31. */
  32. column: number
  33. /**
  34. * Source column in file.
  35. */
  36. line: number
  37. /**
  38. * Source offset in file. It starts from 0.
  39. */
  40. offset: number
  41. }
  42. export interface Range {
  43. /**
  44. * End position, exclusive.
  45. */
  46. end: Position
  47. /**
  48. * Start position, inclusive.
  49. */
  50. start: Position
  51. }
  52. /**
  53. * Source represents an interface for the {@link Node.source} property.
  54. */
  55. export interface Source {
  56. /**
  57. * The inclusive ending position for the source
  58. * code of a node.
  59. *
  60. * However, `end.offset` of a non `Root` node is the exclusive position.
  61. * See https://github.com/postcss/postcss/pull/1879 for details.
  62. *
  63. * ```js
  64. * const root = postcss.parse('a { color: black }')
  65. * const a = root.first
  66. * const color = a.first
  67. *
  68. * // The offset of `Root` node is the inclusive position
  69. * css.source.end // { line: 1, column: 19, offset: 18 }
  70. *
  71. * // The offset of non `Root` node is the exclusive position
  72. * a.source.end // { line: 1, column: 18, offset: 18 }
  73. * color.source.end // { line: 1, column: 16, offset: 16 }
  74. * ```
  75. */
  76. end?: Position
  77. /**
  78. * The source file from where a node has originated.
  79. */
  80. input: Input
  81. /**
  82. * The inclusive starting position for the source
  83. * code of a node.
  84. */
  85. start?: Position
  86. }
  87. /**
  88. * Interface represents an interface for an object received
  89. * as parameter by Node class constructor.
  90. */
  91. export interface NodeProps {
  92. source?: Source
  93. }
  94. export interface NodeErrorOptions {
  95. /**
  96. * An ending index inside a node's string that should be highlighted as
  97. * source of error.
  98. */
  99. endIndex?: number
  100. /**
  101. * An index inside a node's string that should be highlighted as source
  102. * of error.
  103. */
  104. index?: number
  105. /**
  106. * Plugin name that created this error. PostCSS will set it automatically.
  107. */
  108. plugin?: string
  109. /**
  110. * A word inside a node's string, that should be highlighted as source
  111. * of error.
  112. */
  113. word?: string
  114. }
  115. class Node extends Node_ {}
  116. export { Node as default }
  117. }
  118. /**
  119. * It represents an abstract class that handles common
  120. * methods for other CSS abstract syntax tree nodes.
  121. *
  122. * Any node that represents CSS selector or value should
  123. * not extend the `Node` class.
  124. */
  125. declare abstract class Node_ {
  126. /**
  127. * It represents parent of the current node.
  128. *
  129. * ```js
  130. * root.nodes[0].parent === root //=> true
  131. * ```
  132. */
  133. parent: Container | Document | undefined
  134. /**
  135. * It represents unnecessary whitespace and characters present
  136. * in the css source code.
  137. *
  138. * Information to generate byte-to-byte equal node string as it was
  139. * in the origin input.
  140. *
  141. * The properties of the raws object are decided by parser,
  142. * the default parser uses the following properties:
  143. *
  144. * * `before`: the space symbols before the node. It also stores `*`
  145. * and `_` symbols before the declaration (IE hack).
  146. * * `after`: the space symbols after the last child of the node
  147. * to the end of the node.
  148. * * `between`: the symbols between the property and value
  149. * for declarations, selector and `{` for rules, or last parameter
  150. * and `{` for at-rules.
  151. * * `semicolon`: contains true if the last child has
  152. * an (optional) semicolon.
  153. * * `afterName`: the space between the at-rule name and its parameters.
  154. * * `left`: the space symbols between `/*` and the comment’s text.
  155. * * `right`: the space symbols between the comment’s text
  156. * and <code>*&#47;</code>.
  157. * - `important`: the content of the important statement,
  158. * if it is not just `!important`.
  159. *
  160. * PostCSS filters out the comments inside selectors, declaration values
  161. * and at-rule parameters but it stores the origin content in raws.
  162. *
  163. * ```js
  164. * const root = postcss.parse('a {\n color:black\n}')
  165. * root.first.first.raws //=> { before: '\n ', between: ':' }
  166. * ```
  167. */
  168. raws: any
  169. /**
  170. * It represents information related to origin of a node and is required
  171. * for generating source maps.
  172. *
  173. * The nodes that are created manually using the public APIs
  174. * provided by PostCSS will have `source` undefined and
  175. * will be absent in the source map.
  176. *
  177. * For this reason, the plugin developer should consider
  178. * duplicating nodes as the duplicate node will have the
  179. * same source as the original node by default or assign
  180. * source to a node created manually.
  181. *
  182. * ```js
  183. * decl.source.input.from //=> '/home/ai/source.css'
  184. * decl.source.start //=> { line: 10, column: 2 }
  185. * decl.source.end //=> { line: 10, column: 12 }
  186. * ```
  187. *
  188. * ```js
  189. * // Incorrect method, source not specified!
  190. * const prefixed = postcss.decl({
  191. * prop: '-moz-' + decl.prop,
  192. * value: decl.value
  193. * })
  194. *
  195. * // Correct method, source is inherited when duplicating.
  196. * const prefixed = decl.clone({
  197. * prop: '-moz-' + decl.prop
  198. * })
  199. * ```
  200. *
  201. * ```js
  202. * if (atrule.name === 'add-link') {
  203. * const rule = postcss.rule({
  204. * selector: 'a',
  205. * source: atrule.source
  206. * })
  207. *
  208. * atrule.parent.insertBefore(atrule, rule)
  209. * }
  210. * ```
  211. */
  212. source?: Node.Source
  213. /**
  214. * It represents type of a node in
  215. * an abstract syntax tree.
  216. *
  217. * A type of node helps in identification of a node
  218. * and perform operation based on it's type.
  219. *
  220. * ```js
  221. * const declaration = new Declaration({
  222. * prop: 'color',
  223. * value: 'black'
  224. * })
  225. *
  226. * declaration.type //=> 'decl'
  227. * ```
  228. */
  229. type: string
  230. constructor(defaults?: object)
  231. /**
  232. * Insert new node after current node to current node’s parent.
  233. *
  234. * Just alias for `node.parent.insertAfter(node, add)`.
  235. *
  236. * ```js
  237. * decl.after('color: black')
  238. * ```
  239. *
  240. * @param newNode New node.
  241. * @return This node for methods chain.
  242. */
  243. after(
  244. newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
  245. ): this
  246. /**
  247. * It assigns properties to an existing node instance.
  248. *
  249. * ```js
  250. * decl.assign({ prop: 'word-wrap', value: 'break-word' })
  251. * ```
  252. *
  253. * @param overrides New properties to override the node.
  254. *
  255. * @return `this` for method chaining.
  256. */
  257. assign(overrides: object): this
  258. /**
  259. * Insert new node before current node to current node’s parent.
  260. *
  261. * Just alias for `node.parent.insertBefore(node, add)`.
  262. *
  263. * ```js
  264. * decl.before('content: ""')
  265. * ```
  266. *
  267. * @param newNode New node.
  268. * @return This node for methods chain.
  269. */
  270. before(
  271. newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
  272. ): this
  273. /**
  274. * Clear the code style properties for the node and its children.
  275. *
  276. * ```js
  277. * node.raws.before //=> ' '
  278. * node.cleanRaws()
  279. * node.raws.before //=> undefined
  280. * ```
  281. *
  282. * @param keepBetween Keep the `raws.between` symbols.
  283. */
  284. cleanRaws(keepBetween?: boolean): void
  285. /**
  286. * It creates clone of an existing node, which includes all the properties
  287. * and their values, that includes `raws` but not `type`.
  288. *
  289. * ```js
  290. * decl.raws.before //=> "\n "
  291. * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
  292. * cloned.raws.before //=> "\n "
  293. * cloned.toString() //=> -moz-transform: scale(0)
  294. * ```
  295. *
  296. * @param overrides New properties to override in the clone.
  297. *
  298. * @return Duplicate of the node instance.
  299. */
  300. clone(overrides?: object): this
  301. /**
  302. * Shortcut to clone the node and insert the resulting cloned node
  303. * after the current node.
  304. *
  305. * @param overrides New properties to override in the clone.
  306. * @return New node.
  307. */
  308. cloneAfter(overrides?: object): this
  309. /**
  310. * Shortcut to clone the node and insert the resulting cloned node
  311. * before the current node.
  312. *
  313. * ```js
  314. * decl.cloneBefore({ prop: '-moz-' + decl.prop })
  315. * ```
  316. *
  317. * @param overrides Mew properties to override in the clone.
  318. *
  319. * @return New node
  320. */
  321. cloneBefore(overrides?: object): this
  322. /**
  323. * It creates an instance of the class `CssSyntaxError` and parameters passed
  324. * to this method are assigned to the error instance.
  325. *
  326. * The error instance will have description for the
  327. * error, original position of the node in the
  328. * source, showing line and column number.
  329. *
  330. * If any previous map is present, it would be used
  331. * to get original position of the source.
  332. *
  333. * The Previous Map here is referred to the source map
  334. * generated by previous compilation, example: Less,
  335. * Stylus and Sass.
  336. *
  337. * This method returns the error instance instead of
  338. * throwing it.
  339. *
  340. * ```js
  341. * if (!variables[name]) {
  342. * throw decl.error(`Unknown variable ${name}`, { word: name })
  343. * // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
  344. * // color: $black
  345. * // a
  346. * // ^
  347. * // background: white
  348. * }
  349. * ```
  350. *
  351. * @param message Description for the error instance.
  352. * @param options Options for the error instance.
  353. *
  354. * @return Error instance is returned.
  355. */
  356. error(message: string, options?: Node.NodeErrorOptions): CssSyntaxError
  357. /**
  358. * Returns the next child of the node’s parent.
  359. * Returns `undefined` if the current node is the last child.
  360. *
  361. * ```js
  362. * if (comment.text === 'delete next') {
  363. * const next = comment.next()
  364. * if (next) {
  365. * next.remove()
  366. * }
  367. * }
  368. * ```
  369. *
  370. * @return Next node.
  371. */
  372. next(): Node.ChildNode | undefined
  373. /**
  374. * Get the position for a word or an index inside the node.
  375. *
  376. * @param opts Options.
  377. * @return Position.
  378. */
  379. positionBy(opts?: Pick<WarningOptions, 'index' | 'word'>): Node.Position
  380. /**
  381. * Convert string index to line/column.
  382. *
  383. * @param index The symbol number in the node’s string.
  384. * @return Symbol position in file.
  385. */
  386. positionInside(index: number): Node.Position
  387. /**
  388. * Returns the previous child of the node’s parent.
  389. * Returns `undefined` if the current node is the first child.
  390. *
  391. * ```js
  392. * const annotation = decl.prev()
  393. * if (annotation.type === 'comment') {
  394. * readAnnotation(annotation.text)
  395. * }
  396. * ```
  397. *
  398. * @return Previous node.
  399. */
  400. prev(): Node.ChildNode | undefined
  401. /**
  402. * Get the range for a word or start and end index inside the node.
  403. * The start index is inclusive; the end index is exclusive.
  404. *
  405. * @param opts Options.
  406. * @return Range.
  407. */
  408. rangeBy(
  409. opts?: Pick<WarningOptions, 'end' | 'endIndex' | 'index' | 'start' | 'word'>
  410. ): Node.Range
  411. /**
  412. * Returns a `raws` value. If the node is missing
  413. * the code style property (because the node was manually built or cloned),
  414. * PostCSS will try to autodetect the code style property by looking
  415. * at other nodes in the tree.
  416. *
  417. * ```js
  418. * const root = postcss.parse('a { background: white }')
  419. * root.nodes[0].append({ prop: 'color', value: 'black' })
  420. * root.nodes[0].nodes[1].raws.before //=> undefined
  421. * root.nodes[0].nodes[1].raw('before') //=> ' '
  422. * ```
  423. *
  424. * @param prop Name of code style property.
  425. * @param defaultType Name of default value, it can be missed
  426. * if the value is the same as prop.
  427. * @return {string} Code style value.
  428. */
  429. raw(prop: string, defaultType?: string): string
  430. /**
  431. * It removes the node from its parent and deletes its parent property.
  432. *
  433. * ```js
  434. * if (decl.prop.match(/^-webkit-/)) {
  435. * decl.remove()
  436. * }
  437. * ```
  438. *
  439. * @return `this` for method chaining.
  440. */
  441. remove(): this
  442. /**
  443. * Inserts node(s) before the current node and removes the current node.
  444. *
  445. * ```js
  446. * AtRule: {
  447. * mixin: atrule => {
  448. * atrule.replaceWith(mixinRules[atrule.params])
  449. * }
  450. * }
  451. * ```
  452. *
  453. * @param nodes Mode(s) to replace current one.
  454. * @return Current node to methods chain.
  455. */
  456. replaceWith(...nodes: NewChild[]): this
  457. /**
  458. * Finds the Root instance of the node’s tree.
  459. *
  460. * ```js
  461. * root.nodes[0].nodes[0].root() === root
  462. * ```
  463. *
  464. * @return Root parent.
  465. */
  466. root(): Root
  467. /**
  468. * Fix circular links on `JSON.stringify()`.
  469. *
  470. * @return Cleaned object.
  471. */
  472. toJSON(): object
  473. /**
  474. * It compiles the node to browser readable cascading style sheets string
  475. * depending on it's type.
  476. *
  477. * ```js
  478. * new Rule({ selector: 'a' }).toString() //=> "a {}"
  479. * ```
  480. *
  481. * @param stringifier A syntax to use in string generation.
  482. * @return CSS string of this node.
  483. */
  484. toString(stringifier?: Stringifier | Syntax): string
  485. /**
  486. * It is a wrapper for {@link Result#warn}, providing convenient
  487. * way of generating warnings.
  488. *
  489. * ```js
  490. * Declaration: {
  491. * bad: (decl, { result }) => {
  492. * decl.warn(result, 'Deprecated property: bad')
  493. * }
  494. * }
  495. * ```
  496. *
  497. * @param result The `Result` instance that will receive the warning.
  498. * @param message Description for the warning.
  499. * @param options Options for the warning.
  500. *
  501. * @return `Warning` instance is returned
  502. */
  503. warn(result: Result, message: string, options?: WarningOptions): Warning
  504. /**
  505. * If this node isn't already dirty, marks it and its ancestors as such. This
  506. * indicates to the LazyResult processor that the {@link Root} has been
  507. * modified by the current plugin and may need to be processed again by other
  508. * plugins.
  509. */
  510. protected markDirty(): void
  511. }
  512. declare class Node extends Node_ {}
  513. export = Node