Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/fed_dev/deps.ts>postcss.Node

A Bundler with the web in mind.
Latest
class postcss.Node
Re-export
Abstract
import { postcss } from "https://deno.land/x/fed_dev@0.9.0/deps.ts";
const { Node } = postcss;

All node classes inherit the following common methods.

You should not extend this classes to create AST for selector or value parser.

Constructors

new
Node(defaults?: object)

Properties

parent: Document | Container | undefined

The node’s parent node.

root.nodes[0].parent === root
raws: any

Information to generate byte-to-byte equal node string as it was in the origin input.

Every parser saves its own properties, but the default CSS parser uses:

  • before: the space symbols before the node. It also stores * and _ symbols before the declaration (IE hack).
  • after: the space symbols after the last child of the node to the end of the node.
  • between: the symbols between the property and value for declarations, selector and { for rules, or last parameter and { for at-rules.
  • semicolon: contains true if the last child has an (optional) semicolon.
  • afterName: the space between the at-rule name and its parameters.
  • left: the space symbols between /* and the comment’s text.
  • right: the space symbols between the comment’s text and */.
  • important: the content of the important statement, if it is not just !important.

PostCSS cleans selectors, declaration values and at-rule parameters from comments and extra spaces, but it stores origin content in raws properties. As such, if you don’t change a declaration’s value, PostCSS will use the raw value with comments.

const root = postcss.parse('a {\n  color:black\n}')
root.first.first.raws //=> { before: '\n  ', between: ':' }
optional
source: Source

The input source of the node.

The property is used in source map generation.

If you create a node manually (e.g., with postcss.decl()), that node will not have a source property and will be absent from the source map. For this reason, the plugin developer should consider cloning nodes to create new ones (in which case the new node’s source will reference the original, cloned node) or setting the source property manually.

decl.source.input.from //=> '/home/ai/a.sass'
decl.source.start      //=> { line: 10, column: 2 }
decl.source.end        //=> { line: 10, column: 12 }
// Bad
const prefixed = postcss.decl({
  prop: '-moz-' + decl.prop,
  value: decl.value
})

// Good
const prefixed = decl.clone({ prop: '-moz-' + decl.prop })
if (atrule.name === 'add-link') {
  const rule = postcss.rule({ selector: 'a', source: atrule.source })
  atrule.parent.insertBefore(atrule, rule)
}
type: string

tring representing the node’s type. Possible values are root, atrule, rule, decl, or comment.

new Declaration({ prop: 'color', value: 'black' }).type //=> 'decl'

Methods

after(newNode:
| Node
| string
| Node[]
): this

Insert new node after current node to current node’s parent.

Just alias for node.parent.insertAfter(node, add).

decl.after('color: black')
assign(overrides: object): this

Assigns properties to the current node.

decl.assign({ prop: 'word-wrap', value: 'break-word' })
before(newNode:
| Node
| string
| Node[]
): this

Insert new node before current node to current node’s parent.

Just alias for node.parent.insertBefore(node, add).

decl.before('content: ""')
cleanRaws(keepBetween?: boolean): void

Clear the code style properties for the node and its children.

node.raws.before  //=> ' '
node.cleanRaws()
node.raws.before  //=> undefined
clone(overrides?: object): this

Returns an exact clone of the node.

The resulting cloned node and its (cloned) children will retain code style properties.

decl.raws.before    //=> "\n  "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before  //=> "\n  "
cloned.toString()   //=> -moz-transform: scale(0)
cloneAfter(overrides?: object): this

Shortcut to clone the node and insert the resulting cloned node after the current node.

cloneBefore(overrides?: object): this

Shortcut to clone the node and insert the resulting cloned node before the current node.

decl.cloneBefore({ prop: '-moz-' + decl.prop })
error(message: string, options?: NodeErrorOptions): CssSyntaxError

Returns a CssSyntaxError instance containing the original position of the node in the source, showing line and column numbers and also a small excerpt to facilitate debugging.

If present, an input source map will be used to get the original position of the source, even from a previous compilation step (e.g., from Sass compilation).

This method produces very useful error messages.

if (!variables[name]) {
  throw decl.error(`Unknown variable ${name}`, { word: name })
  // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
  //   color: $black
  // a
  //          ^
  //   background: white
}
next(): ChildNode | undefined

Returns the next child of the node’s parent. Returns undefined if the current node is the last child.

if (comment.text === 'delete next') {
  const next = comment.next()
  if (next) {
    next.remove()
  }
}
positionBy(opts?: Pick<WarningOptions, "word" | "index">): Position

Get the position for a word or an index inside the node.

positionInside(index: number): Position

Convert string index to line/column.

prev(): ChildNode | undefined

Returns the previous child of the node’s parent. Returns undefined if the current node is the first child.

const annotation = decl.prev()
if (annotation.type === 'comment') {
  readAnnotation(annotation.text)
}
rangeBy(opts?: Pick<WarningOptions, "word" | "index" | "endIndex">): Range

Get the range for a word or start and end index inside the node. The start index is inclusive; the end index is exclusive.

raw(prop: string, defaultType?: string): string

Returns a Node#raws value. If the node is missing the code style property (because the node was manually built or cloned), PostCSS will try to autodetect the code style property by looking at other nodes in the tree.

const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before   //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
remove(): this

Removes the node from its parent and cleans the parent properties from the node and its children.

if (decl.prop.match(/^-webkit-/)) {
  decl.remove()
}
replaceWith(...nodes: ()[]): this

Inserts node(s) before the current node and removes the current node.

AtRule: {
  mixin: atrule => {
    atrule.replaceWith(mixinRules[atrule.params])
  }
}

Finds the Root instance of the node’s tree.

root.nodes[0].nodes[0].root() === root
toJSON(): object

Fix circular links on JSON.stringify().

toString(stringifier?: Stringifier | Syntax): string

Returns a CSS string representing the node.

new Rule({ selector: 'a' }).toString() //=> "a {}"
warn(
result: Result,
text: string,
): Warning

This method is provided as a convenience wrapper for Result#warn.

  Declaration: {
    bad: (decl, { result }) => {
      decl.warn(result, 'Deprecated property bad')
    }
  }