A CSS parser, primarily designed for minifying CSS documents.
The parser designed around a tokeniser to make the document processing more reliable than regex based minifiers, which are a bit blunt and can be problematic if they match patterns in the wrong places.
How CSSdoc Works
Under the hood, the software processes are split into a number of stages:
The input CSS is loaded into the tokeniser as a string, and a regular expression splits it up into categorised tokens.
The tokens are passed to the parser which then loops through and consumes each token through an object based Finite State Machine to create an internal object structure that represents the document. This enables irregular tokens to be ignored and the document to be parsed with more reliability.
Once parsed, the document will contain an array of child objects each representing each rule or declaration, which will in turn have their own child documents, rules, selectors, properties and values.
Minification is performed by each object on its own structure, the command passed down each level from the one above.
The compilation process reconstructs the CSS from its object representation. Each object generates itself as a string, and then requests its children generate themselves. The result is all concatenated together and either output as a string or saved to the requested location.
CSSdoc has been written with performance in mind, as its main purpose is for the minification of inline CSS. Since PHP is widely used to generate HTML that may contain inline CSS, it is expected and designed for the use case that minification will happen on the fly.
For most CSS documents the speed and memory usage will be well within acceptable boundaries, although if you put large complex documents into it with a large amount of nodes, the speed will suffer.
For the most part the object is memory efficient, most memory will be used by the tokenisation process, which uses a regular expression to split the input string into tokens. This part of the program has been optimised to minimise the amount of memory used.
You can try out CSSdoc in my apps.