The Simple API for CSS
The SAC api is event driven.
That is, during the parsing,
the parser notifies the application whenever it
encounters some CSS constructions.
For example, the parser notifies the application at the begining and at the end of a stylesheet, at the beginning and at the end of a ruleset, when it encounters properties etc ...
But how is the notification done basically ?
Before the parsing, the application registers a set of callback
functions into the parser. We call this set of callback functions
a "handler". To notify the application, the parser calls certain
callback functions of the handler whenever it encounters certain
CSS language constructions.
For example, the callback function named "start_document" is called by the parser at the beginning of the CSS document and the callback function named "end_document" is called by the parser at the end of the CSS document.
An asynchronous programming model
If the application does not register any handler callback,
the parser just quietly parses the CSS document
and nothing happens from an application standpoint.
On the other hand, if the application wants some actions to be taken during the parsing, it has to register callback functions that perform the task wanted. The art of using the SAC api resides in knowing how to handle such an asynchronous programming model.
Some code examples
-
sac-example-1.c is a small program that parses a CSS document and prints "Hey !" when it encounters the begining of a CSS ruleset and also "Woohoo !" when it encounters the end of a CSS ruleset.
Please, read it, try to compile it (some hints about how to compile it are given in the comments at the beginning of the file) and launch it. -
sac-example-2.c is a bigger program that parses a CSS document and prints the selector and the properties of each CSS ruleset it encounters. It also computes some very basic statistics such as the number of property per ruleset and the total number of rulesets found in the CSS document.
This example shows how the application can store a customary parsing context in the sac handler. This context is obviously accessible to the application during the parsing.
Please, read it, try to compile it (some hints about how to compile it are given in the comments at the beginning of the file) and launch it.
