JSCSSP is a CSS parser written in cross-browser JavaScript. It parses a string containing CSS styles and outputs a CSS Object Model (warning: not the CSS Object Model). It can preserve some comments, at-rules and style rules that your browser does not recognize and trashes, and even whitespaces if you absolutely need to preserve indentation and blank lines.

NEW JSCSSP now has support for CSS Media Queries

JSCSSP is NOT based on regular expressions for parsing, making it robust and easily extensible.

It's very simple to use and manipulate, and can be used to write a CSS validator, filters or even CSS extensions for a given browser. Here is the whole code for the interactive demo below:

var source = document.getElementById("source");
var ss = source.value;

var parser = new CSSParser();
var sheet = parser.parse(ss, false, true);
var serialization = document.getElementById("serialization");
if (sheet)
  serialization.innerHTML = sheet.cssText();
else
  serialization.innerHTML = "";
Interactive demo:

You can edit the stylesheet in the textarea just below. Press on the "Parse and Reserialize" button to see how JSSCSSP handles it. Warning: JSCSSP is still a work in progress so please don't be too surprised if you hit a bug...

JSCSSP is called to parse the stylesheet and here's how JSCSSP reserializes it: