Git Product home page Git Product logo

css-parse's Introduction

css-parse Build Status

JavaScript CSS parser for nodejs and the browser.

Installation

$ npm install css-parse

Usage

var parse = require('css-parse');

// CSS input string
var css = "body { \n background-color: #fff;\n }";

var output_obj = parse(css);

// Position and Filename parameters
var output_obj_pos = parse(css, { position: true, filename: 'file.css' });

// Print parsed object as CSS string
console.log(JSON.stringify(output_obj, null, 2));

API

var ast = parse(css, [options])

options:

  • filename - recommended for debugging
  • position - true by default.

Errors

Errors will have err.position where position is:

  • start - start line and column numbers
  • end - end line and column numbers
  • filename - filename if passed to options
  • source - source CSS string

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

Example

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 .position 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
          }
        }
      }
    ]
  }
}

If you also pass in filename: 'path/to/original.css', that will be set on node.position.filename.

Performance

Parsed 15,000 lines of CSS (2mb) in 40ms on my macbook air.

Related

css-stringify css-value

License

MIT

css-parse's People

Contributors

andreypopp avatar brettstimmerman avatar dennis-johnson-dev avatar elegantcoder avatar fedyk avatar fgnass avatar forbeslindesay avatar jo avatar jonathanong avatar krofdrakula avatar lydell avatar paulyoung avatar philipwalton avatar rhiokim avatar rstacruz avatar tj avatar tschaub avatar wnr avatar wu-wu avatar

Watchers

 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.