CSS parser / stringifier.
$ npm install css
var css = require('css');
var obj = css.parse('body { font-size: 12px; }', options);
css.stringify(obj, options);
Accepts a CSS string and returns an AST object.
options:
css. Makes errors and sourceAccepts an AST object (as css.parse produces) and returns a CSS string.
options:
sourcecss.parse is strongly recommended when creating a source map.sourcemap: 'generator' to return the SourceMapGenerator objectfalse to disable) reads anyvar 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 thrown during parsing have the following properties:
String. The full error message with the source position.String. The error message without position.String or undefined. The value of options.source ifcss.parse. Otherwise undefined.Integer.Integer.String. The portion of code that couldn't be parsed.When parsing with the silent option, errors are listed in theparsingErrors 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.
Interactively explore the AST with http://iamdustan.com/reworkcss_ast_explorer/.
All nodes have the following properties.
Information about the position in the source string that corresponds to
the node.
Object:
Object:Number.Number.Object:Number.Number.String or undefined. The value of options.source if passed tocss.parse. Otherwise undefined.String. The full source string passed to css.parse.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.
String. The possible values are the ones listed in the Types section below.
A reference to the parent node, or null if the node has no parent.
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.)
The root node returned by css.parse.
Object:Array of nodes with the types rule, comment and any of theArray of Errors. Errors collected during parsing whensilent is true.Array of Strings. The list of selectors of the rule, splitArray of nodes with the types declaration and comment.String. The property name, trimmed from whitespace andString. The value of the property, trimmed from whitespace andA rule-level or declaration-level comment. Comments inside selectors,
properties and values etc. are lost.
String. The part between the starting /* and the ending */The @charset at-rule.
String. The part following @charset.The @custom-media at-rule.
String. The ---prefixed name.String. The part following the name.The @document at-rule.
String. The part following @document.String or undefined. The vendor prefix in @document, orundefined if there is none.Array of nodes with the types rule, comment and any of theThe @font-face at-rule.
Array of nodes with the types declaration and comment.The @host at-rule.
Array of nodes with the types rule, comment and any of theThe @import at-rule.
String. The part following @import.The @keyframes at-rule.
String. The name of the keyframes rule.String or undefined. The vendor prefix in @keyframes, orundefined if there is none.Array of nodes with the types keyframe and comment.Array of Strings. The list of “selectors” of the keyframe rule,Array of nodes with the types declaration and comment.The @media at-rule.
String. The part following @media.Array of nodes with the types rule, comment and any of theThe @namespace at-rule.
String. The part following @namespace.The @page at-rule.
Array of Strings. The list of selectors of the rule, splitArray of nodes with the types declaration and comment.The @supports at-rule.
String. The part following @supports.Array of nodes with the types rule, comment and any of theCSS:
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
}
}
}
]
}
}
MIT