extcss3
is a fast PHP7 extension for the handling of CSS3 strings
(see W3C Candidate Recommendation).
It supports preprocessing, tokenizing and minifying. Furthermore,
extcss3
implements an API that can be used to analyse, augment or
correct the style sheet while it is being processed.
- Written in pure C99.
- Doesn't require any external libraries.
- Implements an intervention API.
- PHP 7 and PHP 7.1 ready.
- Removes unnecessary whitespace, comments and semicolons.
- Removes invalid or empty declarations and qualified rules.
- Color name transformations (e.g.
MediumSpringGreen
to#00FA9A
). - Hexadecimal color transformations (e.g.
#FF0000
tored
). - Function transformations (e.g.
rgb(255, 255, 255)
to#FFF
). - Minifying of numeric values (e.g.
005
to5
or0.1em
to.1em
). - Optional: Removal of vendor-prefixed declarations.
- The CSS string must be UTF-8 encoded.
- String and URL tokens are returned to the registered callbacks with quotes ("xxx" or 'xxx') as given in the original CSS string.
- The code compiles and runs on Linux systems. Other platforms have not been tested.
public CSS3Processor::__construct(void) : CSS3Processor
- Constructs a new
CSS3Processor
object. - Throws exceptions on errors.
public CSS3Processor::setModifier(int $type, callable $callback) : bool
- Registers a callback for the given token
$type
that will be called during tokenisation. The callback gets an info array of the current token and context. - The callback should return a string with a (new) value that replaces the given token in the result string. If the return value is not a string, the original value is left unmodified.
- The parameter
$type
can be set to any of theextcss3
Type Constants listed below that are marked asmodifiable
. - Only one modifier callback per token type is possible. Any subsequent call
to
::setModifier()
replaces previously set callbacks for the same type. - Returns
true
on success. - Throws exceptions on errors.
public CSS3Processor::dump(string $css) : string
- Applies preprocessing and the registered modifiers to
$css
and returns the resulting string. - Throws exceptions on errors.
public CSS3Processor::minify(string $css [, array $vendors ]) : string
- Returns the minimized result string considering the registered modifiers
and the blacklist of vendor prefixes given in the
$vendors
array. - Throws exceptions on errors.
TYPE_IDENT
1TYPE_FUNCTION
2TYPE_AT_KEYWORD
3TYPE_HASH
4TYPE_STRING
5 (modifiable)TYPE_BAD_STRING
6 (modifiable)TYPE_URL
7 (modifiable)TYPE_BAD_URL
8 (modifiable)TYPE_DELIM
9TYPE_NUMBER
10TYPE_PERCENTAGE
11TYPE_DIMENSION
12TYPE_UNICODE_RANGE
13TYPE_INCLUDE_MATCH
14TYPE_DASH_MATCH
15TYPE_PREFIX_MATCH
16TYPE_SUFFIX_MATCH
17TYPE_SUBSTR_MATCH
18TYPE_COLUMN
19TYPE_WS
20TYPE_CDO
21TYPE_CDC
22TYPE_COLON
23TYPE_SEMICOLON
24TYPE_COMMA
25TYPE_BR_RO
26TYPE_BR_RC
27TYPE_BR_SO
28TYPE_BR_SC
29TYPE_BR_CO
30TYPE_BR_CC
31TYPE_COMMENT
32 (modifiable)TYPE_EOF
33
FLAG_ID
1FLAG_UNRESTRICTED
2FLAG_INTEGER
3FLAG_NUMBER
4FLAG_STRING
5FLAG_AT_URL_STRING
6
ERR_MEMORY
1ERR_BYTES_CORRUPTION
2ERR_NULL_PTR
3ERR_INV_PARAM
4
$css = file_get_contents('style.css');
try {
$proc = new \CSS3Processor();
$min = $proc->minify($css, ['-ms', '-moz', '-o']);
} catch (Exception $e) {
...
}
More examples coming soon...