[6a3a178] | 1 | import Declaration, { DeclarationProps } from './declaration.js'
|
---|
| 2 | import Comment, { CommentProps } from './comment.js'
|
---|
| 3 | import { Stringifier, Syntax } from './postcss.js'
|
---|
| 4 | import AtRule, { AtRuleProps } from './at-rule.js'
|
---|
| 5 | import Rule, { RuleProps } from './rule.js'
|
---|
| 6 | import { WarningOptions } from './warning.js'
|
---|
| 7 | import CssSyntaxError from './css-syntax-error.js'
|
---|
| 8 | import Result from './result.js'
|
---|
| 9 | import Input from './input.js'
|
---|
| 10 | import Root from './root.js'
|
---|
| 11 | import Document from './document.js'
|
---|
| 12 | import Container from './container.js'
|
---|
| 13 |
|
---|
| 14 | export type ChildNode = AtRule | Rule | Declaration | Comment
|
---|
| 15 |
|
---|
| 16 | export type AnyNode = AtRule | Rule | Declaration | Comment | Root | Document
|
---|
| 17 |
|
---|
| 18 | export type ChildProps =
|
---|
| 19 | | AtRuleProps
|
---|
| 20 | | RuleProps
|
---|
| 21 | | DeclarationProps
|
---|
| 22 | | CommentProps
|
---|
| 23 |
|
---|
| 24 | export interface Position {
|
---|
| 25 | /**
|
---|
| 26 | * Source offset in file. It starts from 0.
|
---|
| 27 | */
|
---|
| 28 | offset: number
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Source line in file. In contrast to `offset` it starts from 1.
|
---|
| 32 | */
|
---|
| 33 | column: number
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * Source column in file.
|
---|
| 37 | */
|
---|
| 38 | line: number
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | export interface Source {
|
---|
| 42 | /**
|
---|
| 43 | * The file source of the node.
|
---|
| 44 | */
|
---|
| 45 | input: Input
|
---|
| 46 | /**
|
---|
| 47 | * The starting position of the node’s source.
|
---|
| 48 | */
|
---|
| 49 | start?: Position
|
---|
| 50 | /**
|
---|
| 51 | * The ending position of the node's source.
|
---|
| 52 | */
|
---|
| 53 | end?: Position
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | export interface NodeProps {
|
---|
| 57 | source?: Source
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | interface NodeErrorOptions {
|
---|
| 61 | /**
|
---|
| 62 | * Plugin name that created this error. PostCSS will set it automatically.
|
---|
| 63 | */
|
---|
| 64 | plugin?: string
|
---|
| 65 | /**
|
---|
| 66 | * A word inside a node's string, that should be highlighted as source
|
---|
| 67 | * of error.
|
---|
| 68 | */
|
---|
| 69 | word?: string
|
---|
| 70 | /**
|
---|
| 71 | * An index inside a node's string that should be highlighted as source
|
---|
| 72 | * of error.
|
---|
| 73 | */
|
---|
| 74 | index?: number
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * All node classes inherit the following common methods.
|
---|
| 79 | *
|
---|
| 80 | * You should not extend this classes to create AST for selector or value
|
---|
| 81 | * parser.
|
---|
| 82 | */
|
---|
| 83 | export default abstract class Node {
|
---|
| 84 | /**
|
---|
| 85 | * tring representing the node’s type. Possible values are `root`, `atrule`,
|
---|
| 86 | * `rule`, `decl`, or `comment`.
|
---|
| 87 | *
|
---|
| 88 | * ```js
|
---|
| 89 | * new Declaration({ prop: 'color', value: 'black' }).type //=> 'decl'
|
---|
| 90 | * ```
|
---|
| 91 | */
|
---|
| 92 | type: string
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * The node’s parent node.
|
---|
| 96 | *
|
---|
| 97 | * ```js
|
---|
| 98 | * root.nodes[0].parent === root
|
---|
| 99 | * ```
|
---|
| 100 | */
|
---|
| 101 | parent: Document | Container | undefined
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * The input source of the node.
|
---|
| 105 | *
|
---|
| 106 | * The property is used in source map generation.
|
---|
| 107 | *
|
---|
| 108 | * If you create a node manually (e.g., with `postcss.decl()`),
|
---|
| 109 | * that node will not have a `source` property and will be absent
|
---|
| 110 | * from the source map. For this reason, the plugin developer should
|
---|
| 111 | * consider cloning nodes to create new ones (in which case the new node’s
|
---|
| 112 | * source will reference the original, cloned node) or setting
|
---|
| 113 | * the `source` property manually.
|
---|
| 114 | *
|
---|
| 115 | * ```js
|
---|
| 116 | * decl.source.input.from //=> '/home/ai/a.sass'
|
---|
| 117 | * decl.source.start //=> { line: 10, column: 2 }
|
---|
| 118 | * decl.source.end //=> { line: 10, column: 12 }
|
---|
| 119 | * ```
|
---|
| 120 | *
|
---|
| 121 | * ```js
|
---|
| 122 | * // Bad
|
---|
| 123 | * const prefixed = postcss.decl({
|
---|
| 124 | * prop: '-moz-' + decl.prop,
|
---|
| 125 | * value: decl.value
|
---|
| 126 | * })
|
---|
| 127 | *
|
---|
| 128 | * // Good
|
---|
| 129 | * const prefixed = decl.clone({ prop: '-moz-' + decl.prop })
|
---|
| 130 | * ```
|
---|
| 131 | *
|
---|
| 132 | * ```js
|
---|
| 133 | * if (atrule.name === 'add-link') {
|
---|
| 134 | * const rule = postcss.rule({ selector: 'a', source: atrule.source })
|
---|
| 135 | * atrule.parent.insertBefore(atrule, rule)
|
---|
| 136 | * }
|
---|
| 137 | * ```
|
---|
| 138 | */
|
---|
| 139 | source?: Source
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * Information to generate byte-to-byte equal node string as it was
|
---|
| 143 | * in the origin input.
|
---|
| 144 | *
|
---|
| 145 | * Every parser saves its own properties,
|
---|
| 146 | * but the default CSS parser uses:
|
---|
| 147 | *
|
---|
| 148 | * * `before`: the space symbols before the node. It also stores `*`
|
---|
| 149 | * and `_` symbols before the declaration (IE hack).
|
---|
| 150 | * * `after`: the space symbols after the last child of the node
|
---|
| 151 | * to the end of the node.
|
---|
| 152 | * * `between`: the symbols between the property and value
|
---|
| 153 | * for declarations, selector and `{` for rules, or last parameter
|
---|
| 154 | * and `{` for at-rules.
|
---|
| 155 | * * `semicolon`: contains true if the last child has
|
---|
| 156 | * an (optional) semicolon.
|
---|
| 157 | * * `afterName`: the space between the at-rule name and its parameters.
|
---|
| 158 | * * `left`: the space symbols between `/*` and the comment’s text.
|
---|
| 159 | * * `right`: the space symbols between the comment’s text
|
---|
| 160 | * and <code>*/</code>.
|
---|
| 161 | * * `important`: the content of the important statement,
|
---|
| 162 | * if it is not just `!important`.
|
---|
| 163 | *
|
---|
| 164 | * PostCSS cleans selectors, declaration values and at-rule parameters
|
---|
| 165 | * from comments and extra spaces, but it stores origin content in raws
|
---|
| 166 | * properties. As such, if you don’t change a declaration’s value,
|
---|
| 167 | * PostCSS will use the raw value with comments.
|
---|
| 168 | *
|
---|
| 169 | * ```js
|
---|
| 170 | * const root = postcss.parse('a {\n color:black\n}')
|
---|
| 171 | * root.first.first.raws //=> { before: '\n ', between: ':' }
|
---|
| 172 | * ```
|
---|
| 173 | */
|
---|
| 174 | raws: any
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * @param defaults Value for node properties.
|
---|
| 178 | */
|
---|
| 179 | constructor(defaults?: object)
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * Returns a `CssSyntaxError` instance containing the original position
|
---|
| 183 | * of the node in the source, showing line and column numbers and also
|
---|
| 184 | * a small excerpt to facilitate debugging.
|
---|
| 185 | *
|
---|
| 186 | * If present, an input source map will be used to get the original position
|
---|
| 187 | * of the source, even from a previous compilation step
|
---|
| 188 | * (e.g., from Sass compilation).
|
---|
| 189 | *
|
---|
| 190 | * This method produces very useful error messages.
|
---|
| 191 | *
|
---|
| 192 | * ```js
|
---|
| 193 | * if (!variables[name]) {
|
---|
| 194 | * throw decl.error(`Unknown variable ${name}`, { word: name })
|
---|
| 195 | * // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
|
---|
| 196 | * // color: $black
|
---|
| 197 | * // a
|
---|
| 198 | * // ^
|
---|
| 199 | * // background: white
|
---|
| 200 | * }
|
---|
| 201 | * ```
|
---|
| 202 | *
|
---|
| 203 | * @param message Error description.
|
---|
| 204 | * @param opts Options.
|
---|
| 205 | *
|
---|
| 206 | * @return Error object to throw it.
|
---|
| 207 | */
|
---|
| 208 | error(message: string, options?: NodeErrorOptions): CssSyntaxError
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * This method is provided as a convenience wrapper for `Result#warn`.
|
---|
| 212 | *
|
---|
| 213 | * ```js
|
---|
| 214 | * Declaration: {
|
---|
| 215 | * bad: (decl, { result }) => {
|
---|
| 216 | * decl.warn(result, 'Deprecated property bad')
|
---|
| 217 | * }
|
---|
| 218 | * }
|
---|
| 219 | * ```
|
---|
| 220 | *
|
---|
| 221 | * @param result The `Result` instance that will receive the warning.
|
---|
| 222 | * @param text Warning message.
|
---|
| 223 | * @param opts Warning Options.
|
---|
| 224 | *
|
---|
| 225 | * @return Created warning object.
|
---|
| 226 | */
|
---|
| 227 | warn(result: Result, text: string, opts?: WarningOptions): void
|
---|
| 228 |
|
---|
| 229 | /**
|
---|
| 230 | * Removes the node from its parent and cleans the parent properties
|
---|
| 231 | * from the node and its children.
|
---|
| 232 | *
|
---|
| 233 | * ```js
|
---|
| 234 | * if (decl.prop.match(/^-webkit-/)) {
|
---|
| 235 | * decl.remove()
|
---|
| 236 | * }
|
---|
| 237 | * ```
|
---|
| 238 | *
|
---|
| 239 | * @return Node to make calls chain.
|
---|
| 240 | */
|
---|
| 241 | remove(): this
|
---|
| 242 |
|
---|
| 243 | /**
|
---|
| 244 | * Returns a CSS string representing the node.
|
---|
| 245 | *
|
---|
| 246 | * ```js
|
---|
| 247 | * new Rule({ selector: 'a' }).toString() //=> "a {}"
|
---|
| 248 | * ```
|
---|
| 249 | *
|
---|
| 250 | * @param stringifier A syntax to use in string generation.
|
---|
| 251 | * @return CSS string of this node.
|
---|
| 252 | */
|
---|
| 253 | toString(stringifier?: Stringifier | Syntax): string
|
---|
| 254 |
|
---|
| 255 | /**
|
---|
| 256 | * Assigns properties to the current node.
|
---|
| 257 | *
|
---|
| 258 | * ```js
|
---|
| 259 | * decl.assign({ prop: 'word-wrap', value: 'break-word' })
|
---|
| 260 | * ```
|
---|
| 261 | *
|
---|
| 262 | * @param overrides New properties to override the node.
|
---|
| 263 | * @return Current node to methods chain.
|
---|
| 264 | */
|
---|
| 265 | assign(overrides: object): this
|
---|
| 266 |
|
---|
| 267 | /**
|
---|
| 268 | * Returns an exact clone of the node.
|
---|
| 269 | *
|
---|
| 270 | * The resulting cloned node and its (cloned) children will retain
|
---|
| 271 | * code style properties.
|
---|
| 272 | *
|
---|
| 273 | * ```js
|
---|
| 274 | * decl.raws.before //=> "\n "
|
---|
| 275 | * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
|
---|
| 276 | * cloned.raws.before //=> "\n "
|
---|
| 277 | * cloned.toString() //=> -moz-transform: scale(0)
|
---|
| 278 | * ```
|
---|
| 279 | *
|
---|
| 280 | * @param overrides New properties to override in the clone.
|
---|
| 281 | * @return Clone of the node.
|
---|
| 282 | */
|
---|
| 283 | clone(overrides?: object): this
|
---|
| 284 |
|
---|
| 285 | /**
|
---|
| 286 | * Shortcut to clone the node and insert the resulting cloned node
|
---|
| 287 | * before the current node.
|
---|
| 288 | *
|
---|
| 289 | * ```js
|
---|
| 290 | * decl.cloneBefore({ prop: '-moz-' + decl.prop })
|
---|
| 291 | * ```
|
---|
| 292 | *
|
---|
| 293 | * @param overrides Mew properties to override in the clone.
|
---|
| 294 | *
|
---|
| 295 | * @return New node
|
---|
| 296 | */
|
---|
| 297 | cloneBefore(overrides?: object): this
|
---|
| 298 |
|
---|
| 299 | /**
|
---|
| 300 | * Shortcut to clone the node and insert the resulting cloned node
|
---|
| 301 | * after the current node.
|
---|
| 302 | *
|
---|
| 303 | * @param overrides New properties to override in the clone.
|
---|
| 304 | * @return New node.
|
---|
| 305 | */
|
---|
| 306 | cloneAfter(overrides?: object): this
|
---|
| 307 |
|
---|
| 308 | /**
|
---|
| 309 | * Inserts node(s) before the current node and removes the current node.
|
---|
| 310 | *
|
---|
| 311 | * ```js
|
---|
| 312 | * AtRule: {
|
---|
| 313 | * mixin: atrule => {
|
---|
| 314 | * atrule.replaceWith(mixinRules[atrule.params])
|
---|
| 315 | * }
|
---|
| 316 | * }
|
---|
| 317 | * ```
|
---|
| 318 | *
|
---|
| 319 | * @param nodes Mode(s) to replace current one.
|
---|
| 320 | * @return Current node to methods chain.
|
---|
| 321 | */
|
---|
| 322 | replaceWith(
|
---|
| 323 | ...nodes: (ChildNode | ChildProps | ChildNode[] | ChildProps[])[]
|
---|
| 324 | ): this
|
---|
| 325 |
|
---|
| 326 | /**
|
---|
| 327 | * Returns the next child of the node’s parent.
|
---|
| 328 | * Returns `undefined` if the current node is the last child.
|
---|
| 329 | *
|
---|
| 330 | * ```js
|
---|
| 331 | * if (comment.text === 'delete next') {
|
---|
| 332 | * const next = comment.next()
|
---|
| 333 | * if (next) {
|
---|
| 334 | * next.remove()
|
---|
| 335 | * }
|
---|
| 336 | * }
|
---|
| 337 | * ```
|
---|
| 338 | *
|
---|
| 339 | * @return Next node.
|
---|
| 340 | */
|
---|
| 341 | next(): ChildNode | undefined
|
---|
| 342 |
|
---|
| 343 | /**
|
---|
| 344 | * Returns the previous child of the node’s parent.
|
---|
| 345 | * Returns `undefined` if the current node is the first child.
|
---|
| 346 | *
|
---|
| 347 | * ```js
|
---|
| 348 | * const annotation = decl.prev()
|
---|
| 349 | * if (annotation.type === 'comment') {
|
---|
| 350 | * readAnnotation(annotation.text)
|
---|
| 351 | * }
|
---|
| 352 | * ```
|
---|
| 353 | *
|
---|
| 354 | * @return Previous node.
|
---|
| 355 | */
|
---|
| 356 | prev(): ChildNode | undefined
|
---|
| 357 |
|
---|
| 358 | /**
|
---|
| 359 | * Insert new node before current node to current node’s parent.
|
---|
| 360 | *
|
---|
| 361 | * Just alias for `node.parent.insertBefore(node, add)`.
|
---|
| 362 | *
|
---|
| 363 | * ```js
|
---|
| 364 | * decl.before('content: ""')
|
---|
| 365 | * ```
|
---|
| 366 | *
|
---|
| 367 | * @param newNode New node.
|
---|
| 368 | * @return This node for methods chain.
|
---|
| 369 | */
|
---|
| 370 | before(newNode: Node | ChildProps | string | Node[]): this
|
---|
| 371 |
|
---|
| 372 | /**
|
---|
| 373 | * Insert new node after current node to current node’s parent.
|
---|
| 374 | *
|
---|
| 375 | * Just alias for `node.parent.insertAfter(node, add)`.
|
---|
| 376 | *
|
---|
| 377 | * ```js
|
---|
| 378 | * decl.after('color: black')
|
---|
| 379 | * ```
|
---|
| 380 | *
|
---|
| 381 | * @param newNode New node.
|
---|
| 382 | * @return This node for methods chain.
|
---|
| 383 | */
|
---|
| 384 | after(newNode: Node | ChildProps | string | Node[]): this
|
---|
| 385 |
|
---|
| 386 | /**
|
---|
| 387 | * Finds the Root instance of the node’s tree.
|
---|
| 388 | *
|
---|
| 389 | * ```js
|
---|
| 390 | * root.nodes[0].nodes[0].root() === root
|
---|
| 391 | * ```
|
---|
| 392 | *
|
---|
| 393 | * @return Root parent.
|
---|
| 394 | */
|
---|
| 395 | root(): Root
|
---|
| 396 |
|
---|
| 397 | /**
|
---|
| 398 | * Returns a `Node#raws` value. If the node is missing
|
---|
| 399 | * the code style property (because the node was manually built or cloned),
|
---|
| 400 | * PostCSS will try to autodetect the code style property by looking
|
---|
| 401 | * at other nodes in the tree.
|
---|
| 402 | *
|
---|
| 403 | * ```js
|
---|
| 404 | * const root = postcss.parse('a { background: white }')
|
---|
| 405 | * root.nodes[0].append({ prop: 'color', value: 'black' })
|
---|
| 406 | * root.nodes[0].nodes[1].raws.before //=> undefined
|
---|
| 407 | * root.nodes[0].nodes[1].raw('before') //=> ' '
|
---|
| 408 | * ```
|
---|
| 409 | *
|
---|
| 410 | * @param prop Name of code style property.
|
---|
| 411 | * @param defaultType Name of default value, it can be missed
|
---|
| 412 | * if the value is the same as prop.
|
---|
| 413 | * @return {string} Code style value.
|
---|
| 414 | */
|
---|
| 415 | raw(prop: string, defaultType?: string): string
|
---|
| 416 |
|
---|
| 417 | /**
|
---|
| 418 | * Clear the code style properties for the node and its children.
|
---|
| 419 | *
|
---|
| 420 | * ```js
|
---|
| 421 | * node.raws.before //=> ' '
|
---|
| 422 | * node.cleanRaws()
|
---|
| 423 | * node.raws.before //=> undefined
|
---|
| 424 | * ```
|
---|
| 425 | *
|
---|
| 426 | * @param keepBetween Keep the `raws.between` symbols.
|
---|
| 427 | */
|
---|
| 428 | cleanRaws(keepBetween?: boolean): void
|
---|
| 429 |
|
---|
| 430 | /**
|
---|
| 431 | * Fix circular links on `JSON.stringify()`.
|
---|
| 432 | *
|
---|
| 433 | * @return Cleaned object.
|
---|
| 434 | */
|
---|
| 435 | toJSON(): object
|
---|
| 436 |
|
---|
| 437 | /**
|
---|
| 438 | * Convert string index to line/column.
|
---|
| 439 | *
|
---|
| 440 | * @param index The symbol number in the node’s string.
|
---|
| 441 | * @return Symbol position in file.
|
---|
| 442 | */
|
---|
| 443 | positionInside(index: number): Position
|
---|
| 444 | }
|
---|