| php-ext-css stand alone | MPZ stand alone | php-ext-css MPZ edition |
|---|---|---|
| No own builds |
php-ext-css is a fast PHP7 extension for the handling of CSS3 strings (see
W3C Candidate Recommendation). It supports
preprocessing, tokenizing and minifying. Furthermore, php-ext-css implements an
API that can be used to analyse, augment or correct the style sheet while it is
being processed.
The MPZ edition uses the Memory-Pool-Z
to increase the real processing speed of php-ext-css to over 35 % while reducing
the user time to over 40% and system time to over 10 %.
- Written in pure C99.
- Doesn't require any external libraries.
- Implements an intervention API.
- PHP 7.x ready.
- Well tested.
- Removes unnecessary whitespace, comments and semicolons.
- Removes invalid or empty declarations and qualified rules.
- Color name transformations (e.g.
MediumSpringGreento#00FA9A). - Hexadecimal color transformations (e.g.
#FF0000tored). - Function transformations (e.g.
rgb(255, 255, 255)to#FFF). - Minifying of numeric values (e.g.
005to5or0.1emto.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.
To build the php-ext-css MPZ edition extension run:
phpize
./configure
makeand finally install it via:
make installpublic CSS3Processor::__construct(void) : CSS3Processor- Constructs a new
CSS3Processorobject. - Throws exceptions on errors.
public CSS3Processor::setNotifier(int $type, callable $callback) : bool- Registers a callback for the given token
$typethat will be called during tokenisation. The callback gets an info array of the current token and context. - All return values from the callback are discarded.
- The parameter
$typecan be set to any of thephp-ext-cssType Constants listed below. - If a Modifier is registered for the same token type, the Notifier is allways called bevore the Modifier.
- Multiple notifier callbacks per token type are possible.
- Returns
trueon success. - Throws exceptions on errors.
public CSS3Processor::setModifier(int $type, callable $callback) : bool- Registers a callback for the given token
$typethat 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
$typecan be set to any of thephp-ext-cssType Constants listed below that are marked asmodifiable. - If a Notifier is registered for the same token type, the Notifier is allways called bevore the Modifier.
- Only one modifier callback per token type is possible. Any subsequent call to
::setModifier()replaces previously set callbacks for the same type. - Returns
trueon success. - Throws exceptions on errors.
public CSS3Processor::dump(string $css) : string- Applies preprocessing, the registered notifiers and modifiers to
$cssand returns the resulting string. - Throws exceptions on errors.
public CSS3Processor::minify(string $css [, array $vendors ]) : string- Returns the minimized result string considering the registered notifiers, modifiers
and the blacklist of vendor prefixes given in the
$vendorsarray. - Throws exceptions on errors.
`CSS3Processor::TYPE_IDENT` 1
`CSS3Processor::TYPE_FUNCTION` 2
`CSS3Processor::TYPE_AT_KEYWORD` 3
`CSS3Processor::TYPE_HASH` 4
`CSS3Processor::TYPE_STRING` 5 (modifiable)
`CSS3Processor::TYPE_BAD_STRING` 6 (modifiable)
`CSS3Processor::TYPE_URL` 7 (modifiable)
`CSS3Processor::TYPE_BAD_URL` 8 (modifiable)
`CSS3Processor::TYPE_DELIM` 9
`CSS3Processor::TYPE_NUMBER` 10
`CSS3Processor::TYPE_PERCENTAGE` 11
`CSS3Processor::TYPE_DIMENSION` 12
`CSS3Processor::TYPE_UNICODE_RANGE` 13
`CSS3Processor::TYPE_INCLUDE_MATCH` 14
`CSS3Processor::TYPE_DASH_MATCH` 15
`CSS3Processor::TYPE_PREFIX_MATCH` 16
`CSS3Processor::TYPE_SUFFIX_MATCH` 17
`CSS3Processor::TYPE_SUBSTR_MATCH` 18
`CSS3Processor::TYPE_COLUMN` 19
`CSS3Processor::TYPE_WS` 20
`CSS3Processor::TYPE_CDO` 21
`CSS3Processor::TYPE_CDC` 22
`CSS3Processor::TYPE_COLON` 23
`CSS3Processor::TYPE_SEMICOLON` 24
`CSS3Processor::TYPE_COMMA` 25
`CSS3Processor::TYPE_BR_RO` 26
`CSS3Processor::TYPE_BR_RC` 27
`CSS3Processor::TYPE_BR_SO` 28
`CSS3Processor::TYPE_BR_SC` 29
`CSS3Processor::TYPE_BR_CO` 30
`CSS3Processor::TYPE_BR_CC` 31
`CSS3Processor::TYPE_COMMENT` 32 (modifiable)
`CSS3Processor::TYPE_EOF` 33`CSS3Processor::FLAG_ID` 1
`CSS3Processor::FLAG_UNRESTRICTED` 2
`CSS3Processor::FLAG_INTEGER` 3
`CSS3Processor::FLAG_NUMBER` 4
`CSS3Processor::FLAG_STRING` 5
`CSS3Processor::FLAG_AT_URL_STRING` 6`CSS3Processor::ERR_MEMORY` 1
`CSS3Processor::ERR_BYTES_CORRUPTION` 2
`CSS3Processor::ERR_NULL_PTR` 3
`CSS3Processor::ERR_INV_PARAM` 4
`CSS3Processor::ERR_INV_VALUE` 5$css = file_get_contents('style.css');
try {
$proc = new \CSS3Processor();
$min = $proc->minify($css, ['-ms', '-moz', '-o']);
} catch (Exception $e) {
...
}See the tests for more examples.