CSS Parser
CSS 2.1 compliant parser generated by jison. Currently, our grammar rules is based on the Appendix G.
Quick Start
Install the npm css-js from the npm repo.
npm install css-js
Server-side Use (node.js)
Parse the full CSS.
/* create the css parser */
var CSSParser = require("css-js");
var config = {};
config.ver = "2.1"; /* CSS 2.1 */
parser = new CSSParser(config);
var cssString = "...";
var ast = parser.parse(cssString);
The returned AST structure
{
{ charset: '...' },
{ imports: [ { import: '...', mediaqueries: [ '...', '...' ] } ] },
{ rulesets: [ { selector: '...', declaration: [ { key: '...', value: '...' }, { key: '...', value: '...' } ] } ] },
{ medias: { mediaqueries: [ '...', '...' ], rulesets: [ { "selector": "...", "declaration": [ { key: "...", value: "..." } ] } ] },
{ pages: { pseudo_class: "...", declaration: [ { key: "...", value: "..." } ] } },
}
Parse the CSS string in style attribute value of HTML5 page.
/* create the css parser */
var CSSParser = require("css-js");
var config = {};
config.ver = "2.1"; /* CSS 2.1 */
parser = new CSSParser(config);
var cssString = "...";
var result = parser.parseCssString(cssString);
The returned result array
[ { key: '...', value: '...' }, { key: '...', value: '...' } ]
Development
Build
grunt
How to test
grunt test
Future works
We will add the CSS 3.0 grammar in near future.
License
This software is free to use under the BSD license.
See the LICENSE file for license text and copyright information.