CSS Preprocessor is an extension to the CSS language that adds features not otherwise available in CSS to make it easier to quickly write advanced CSS documents.
The main features of CSS Preprocessor are:
There are other projects availables that handle CSS in various ways albeit similar to what CSS Preprocessor offers. We will attempt to add as many of the options available in those projects to the CSS Preprocessor.
If you know of other projects we could list here, let us know.
The syntax supported by the CSS Preprocessor language follows the standard CSS 3 syntax with just a few exceptions. Files are expected to be named with extension .scss, although the compiler does not enforce the extension in any way.
All input files must be UTF-8. Since most CSS files are written in ASCII, this is not likely going to be a problem.
The one main exception to the CSS 3 syntax is the setting of a variable at the top level (i.e. a global variable). Setting a variable looks like declaring a field:
In CSS 3, this is not allowed at the top level, which expects lists of selectors followed by a block or @-rules.
Another exception is the support of nested fields. These look like qualified rules by default, but selectors can have a ':' only if followed by an identifier, so a colon followed by a '{' is clearly not a qualified rule. Note that to further ensure the validity of the rule, we also enforce a ';' at the end of the construct. With all of that we can safely change the behavior and support the nested fields as SASS does (except for the ';' at the end of the {}-block):
Other exceptions are mainly in the lexer which support additional tokens: the variable syntax ($<name>), the reference character (&), and the placeholder extension (%<identifier>).
However, anything that is not supported generates an error and no output is generated. This allows you to write scripts and makefiles that make sure that your output is always valid CSS before you publish it.
At this time, the CSS Preprocessor does not handle identifiers correctly. It will force them all to lowercase, meaning that the case is not valid for documents such as XML that are not case insensitive like HTML. We only expect our preprocessor to be used for CSS that is itself used with HTML.
There are many features from SASS that are not yet implemented in csspp but are eventually mentioned in this documentation. We do intend to add support for most of the features present in SASS, possibly with a few tweaks, but SASS is already pretty advanced so it will take us a little bit of time to reach completion of the csspp project in that realm.
That being said, csspp is already quite functional, especially for a first level validation run and for compressing your existing CSS files as much as CSS makes it possible (removing unnecessary spaces, shortening colors, etc.)
You may use the CSS Preprocessor command line tool to compile your SCSS files. It is very similar to using a compiler:
The command line tool supports many options. By default the output is written to standard output. The tool exits with 1 on errors and 0 on warnings or no messages.
If you are writing a C++ application, you may directly include the library. With cmake, you may use the FindCSSPP macros as in:
Then look at the API documentation for details on how to use the csspp objects. You may check out the src/csspp.cpp file as an example of use of the CSS Preprocessor library.
In general, you want to open a file, give it to a lexer object. Create a parser and parse the input. With the resulting node tree, create a compiler and compile the parser tree. The compiled tree can then be output using an assembler object.
The compiler automatically runs all the validation steps currently supported by csspp.
The CSS Preprocessor supports standard C (and thus CSS) and C++ comments:
C++ comments that span on multiple lines are viewed as one comment.
All comments are removed from the output except those that include the special "\@preserve" keyword. This is useful to include comments such as copyrights.
Variable expension is provided for comments with the @preserve keyword. The variables have to be written between curly brackets as in:
To be SASS compatible, we will also remove a preceeding '#' character:
Note that C++ like comments are saved as normal C comments in the final output. In other words, we make C++ comments CSS compatible in the end.
The CSS Preprocess compiler adds a pletoria of @-commands to support various useful capabilities of the compiler.
The compiler will also validate the syntax for all the @-commands it knows about.
The same selectors as CSS 3 are supported by the CSS Preprocessor. All the lists of selectors get compiled to make sure they are valid CSS code.
At some point, the compiler will also verify each identifier, class name, attribute name, attribute value, and identifier will be checked (if you want to, it won't be mandatory.)
Also like SASS, we support the %<name> selector. This allows for creating rules that do not automatically get inserted in the output. This allows for the definition of various CSS libraries with rules that get used only when referenced.
Detailed information about the supported selectors and extensions is found in CSS Preprocessor Reference – Selectors.
The CSS Preprocessor adds support for C-like expressions. The syntax is described in CSS Preprocessor Reference – Expressions.
Expressions are accepted in various places:
1) In fields declarations:
2) between certain @-keyword and there block or semicolon (;):
Documentation of CSS Preprocessor.
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.