Giter Club home page Giter Club logo

css's Introduction

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(css, [options])

Accepts a CSS string and returns an AST object.

options:

  • silent - silently fail on parse errors.
  • source - the path to the file containing css. Makes errors and source maps more helpful, by letting them know where code comes from.
  • position - record the line and column of the start and end of nodes. Required for source map generation. true by default.

For the source and position options, also see the parse tree examples below.

css.stringify(object, [options])

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

options:

  • compress - omit comments and extraneous whitespace.
  • sourcemap - return a sourcemap along with the CSS output (requires the position option of css.parse, and its source option is strongly recommended).
var ast = css.parse('body { font-size: 12px; }', { position: true, 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 will have error.position, with the following properties:

  • start - start line and column numbers.
  • end - end line and column numbers.
  • source - options.source if passed to css.parse.

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

Examples

CSS:

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

Parse tree:

{
  "type": "stylesheet",
  "stylesheet": {
    "rules": [
      {
        "type": "rule",
        "selectors": [
          "body"
        ],
        "declarations": [
          {
            "type": "declaration",
            "property": "background",
            "value": "#eee"
          },
          {
            "type": "declaration",
            "property": "color",
            "value": "#888"
          }
        ]
      }
    ]
  }
}

Parse tree with the position option enabled:

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

node.position.content is set on each node to the full source string. If you also pass in source: 'path/to/source.css' to css.parse, that will be set on node.position.source.

License

MIT

css's People

Contributors

tj avatar jonathanong avatar conradz avatar lydell avatar necolas avatar moox avatar andreypopp avatar bnjmnt4n avatar forbeslindesay avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.