Home

Awesome

css Build Status

CSS parser / stringifier.

Installation

$ npm install css

Usage

var css = require('css');
var obj = css.parse('body { font-size: 12px; }', options);
css.stringify(obj, options);

API

css.parse(code, [options])

Accepts a CSS string and returns an AST object.

options:

css.stringify(object, [options])

Accepts an AST object (as css.parse produces) and returns a CSS string.

options:

Example

var ast = css.parse('body { font-size: 12px; }', { source: 'source.css' });

var css = css.stringify(ast);

var result = css.stringify(ast, { sourcemap: true });
result.code // string with CSS
result.map // source map object

Errors

Errors thrown during parsing have the following properties:

When parsing with the silent option, errors are listed in the parsingErrors property of the stylesheet node instead of being thrown.

If you create any errors in plugins such as in rework, you must set the same properties for consistency.

AST

Interactively explore the AST with http://iamdustan.com/reworkcss_ast_explorer/.

Common properties

All nodes have the following properties.

position

Information about the position in the source string that corresponds to the node.

Object:

The line and column numbers are 1-based: The first line is 1 and the first column of a line is 1 (not 0).

The position property lets you know from which source file the node comes from (if available), what that file contains, and what part of that file was parsed into the node.

type

String. The possible values are the ones listed in the Types section below.

parent

A reference to the parent node, or null if the node has no parent.

Types

The available values of node.type are listed below, as well as the available properties of each node (other than the common properties listed above.)

stylesheet

The root node returned by css.parse.

rule

declaration

comment

A rule-level or declaration-level comment. Comments inside selectors, properties and values etc. are lost.

charset

The @charset at-rule.

custom-media

The @custom-media at-rule.

document

The @document at-rule.

font-face

The @font-face at-rule.

host

The @host at-rule.

import

The @import at-rule.

keyframes

The @keyframes at-rule.

keyframe

media

The @media at-rule.

namespace

The @namespace at-rule.

page

The @page at-rule.

supports

The @supports at-rule.

Example

CSS:

body {
  background: #eee;
  color: #888;
}

Parse tree:

{
  "type": "stylesheet",
  "stylesheet": {
    "rules": [
      {
        "type": "rule",
        "selectors": [
          "body"
        ],
        "declarations": [
          {
            "type": "declaration",
            "property": "background",
            "value": "#eee",
            "position": {
              "start": {
                "line": 2,
                "column": 3
              },
              "end": {
                "line": 2,
                "column": 19
              }
            }
          },
          {
            "type": "declaration",
            "property": "color",
            "value": "#888",
            "position": {
              "start": {
                "line": 3,
                "column": 3
              },
              "end": {
                "line": 3,
                "column": 14
              }
            }
          }
        ],
        "position": {
          "start": {
            "line": 1,
            "column": 1
          },
          "end": {
            "line": 4,
            "column": 2
          }
        }
      }
    ]
  }
}

License

MIT