Skip to content

MPZ edition #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 58 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
0280328
Fixes for the MS-IE and MS-Edge browsers
Dec 13, 2017
48c8b79
Fix incorrectly transforming of `0%` to `0`
Dec 13, 2017
d36a160
Add memory checks via valgrind
alex-schneider May 27, 2018
89e2beb
Merge branch 'fix-incorrectly-transforming-of-0-percent-to-0'
alex-schneider May 27, 2018
30b8231
Merge branch 'ms-ie-and-edge-fixes'
alex-schneider May 27, 2018
be5e3ba
Fix invalid memory read warning/error
alex-schneider May 27, 2018
9b12190
Redesign the README.md
alex-schneider May 27, 2018
2bd377f
Redesign the README.md
alex-schneider May 27, 2018
cf47b03
Redesign the README.md
alex-schneider May 27, 2018
f584c6d
Redesign the README.md
alex-schneider May 27, 2018
263707f
Redesign the README.md
alex-schneider May 27, 2018
bcfa92b
Travis builds with PHP 7.2, too
alex-schneider May 27, 2018
c81f0de
Add memory checks via valgrind
alex-schneider May 27, 2018
1d7eb40
Memory checks via valgrind without 'nightly', because unofficial
alex-schneider May 27, 2018
d354af1
Build in the MPZ, resort struct members to prevent/reduce memory alig…
alex-schneider May 27, 2018
e10d05a
WS
alex-schneider May 27, 2018
61d5afa
Build in the MPZ directly
alex-schneider May 27, 2018
cb4f22d
Implementation of setNotifier() method
alex-schneider May 28, 2018
ec57450
No semicolon after definement
alex-schneider May 28, 2018
195744d
Neu exception constant + WS
alex-schneider May 28, 2018
70870f8
Texts
alex-schneider May 28, 2018
016b850
WS
alex-schneider May 28, 2018
e802471
Texts
alex-schneider May 28, 2018
8b22db4
Eliminate an 'always TRUE' condition
alex-schneider May 28, 2018
fb2db01
Eliminate an 'always TRUE' condition
alex-schneider May 28, 2018
1ca14fe
Add compiler flag to see more warnings and errors if any
alex-schneider May 28, 2018
2cf1d2f
Overall using of MPZ
alex-schneider May 28, 2018
d505601
Update version
alex-schneider May 28, 2018
b945150
Eliminate compiler warnings 'comparison is always false due to limite…
alex-schneider May 28, 2018
b9f473f
README.md
alex-schneider May 28, 2018
ec2bfec
Update README.md
alex-schneider May 28, 2018
3d97426
Sync MPZ
alex-schneider May 28, 2018
1c8ce7e
Disable memory errors detection in the MPZ
alex-schneider May 28, 2018
0bef16f
Update README.md
alex-schneider May 28, 2018
1463491
README.md
alex-schneider May 28, 2018
e016853
extcss3 => php-ext-css
alex-schneider May 28, 2018
fa1b0e4
Update README.md
alex-schneider May 28, 2018
33cc99a
Update README.md
alex-schneider May 28, 2018
d91478e
WS
alex-schneider May 29, 2018
4667e9e
Set theme jekyll-theme-cayman
alex-schneider May 30, 2018
4655559
Update README.md
alex-schneider May 30, 2018
d1d61ec
Set theme jekyll-theme-cayman
alex-schneider May 30, 2018
7dd6f71
Update README.md
alex-schneider May 30, 2018
436c776
Increasing performance
alex-schneider Jun 7, 2018
d62ccbc
Compiler specifical inlining
alex-schneider Jun 7, 2018
899ee35
Sync README.md
alex-schneider Jun 7, 2018
68a931d
Moing installation description into the README.md
alex-schneider Jun 7, 2018
ba9e119
Update README.md
alex-schneider Jun 9, 2018
823a141
Update README.md
alex-schneider Jun 9, 2018
274c8bd
Update intern.c
alex-schneider Jun 9, 2018
3ad06e0
Micro optimizations
alex-schneider Jun 12, 2018
9b8f967
More micro optimizations
alex-schneider Jun 13, 2018
7c8aaa6
Update README.md
alex-schneider Jun 13, 2018
64e70a1
Sync current state of MPZ
alex-schneider Jul 25, 2018
c2d7daf
Add PHP-7.3 checks
alex-schneider Jan 27, 2019
86b53ca
Remove PHP-7.3 build
alex-schneider Jan 27, 2019
3d36177
Add PHP 7.3 support to the README
alex-schneider Jan 29, 2019
32d5d38
PHP 7.x ready
alex-schneider Aug 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ language: php
php:
- 7.0
- 7.1
- nightly
- 7.2

before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq valgrind

script:
- phpize
Expand All @@ -12,4 +16,4 @@ script:
- export NO_INTERACTION=1
- export REPORT_EXIT_STATUS=1
- export TEST_PHP_EXECUTABLE=`which php`
- php -n run-tests.php -n -d extension_dir=./modules/ -d extension=extcss3.so --show-diff
- php -n run-tests.php -m -n -d extension_dir=./modules/ -d extension=extcss3.so --show-diff
7 changes: 0 additions & 7 deletions INSTALL

This file was deleted.

234 changes: 140 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
[![Software license][ico-license]](LICENSE)
[![Build status][ico-travis]][link-travis]
| php-ext-css stand alone | MPZ stand alone | php-ext-css MPZ edition |
| :---: | :---: | :---: |
| [![Software license][ico-license]](https://github.com/sevenval/php-ext-css/blob/master/LICENSE) | [![Software license][ico-license-mpz]](https://github.com/alex-schneider/mpz/blob/master/LICENSE) | [![Software license][ico-license-mpz]](mpz/LICENSE) |
| [![Build status][ico-travis]][link-travis] | No own builds | [![Build status][ico-travis-mpz]][link-travis-mpz] |

# `extcss3`
# php-ext-css MPZ edition

`extcss3` is a fast PHP7 extension for the handling of CSS3 strings
(see [W3C Candidate Recommendation](https://www.w3.org/TR/css-syntax-3/)).
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.
`php-ext-css` is a fast PHP7 extension for the handling of CSS3 strings (see
[W3C Candidate Recommendation](https://www.w3.org/TR/css-syntax-3/)). 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](https://alex-schneider.github.io/mpz/)
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 %.

## Library Features

* Written in pure C99.
* Doesn't require any external libraries.
* Implements an intervention API.
* PHP 7 and PHP 7.1 ready.

* PHP 7.x ready.
* Well tested.

## Minifying Features

Expand All @@ -28,136 +33,177 @@ correct the style sheet while it is being processed.
* Minifying of numeric values (e.g. `005` to `5` or `0.1em` to `.1em`).
* Optional: Removal of vendor-prefixed declarations.


## Current Limitations

* 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.
* 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.

## Installation

## PHP Class (`CSS3Processor`)
To build the `php-ext-css` MPZ edition extension run:

```bash
phpize
./configure
make
```

and finally install it via:

```bash
make install
```

## PHP Class (`CSS3Processor`)

### CSS3Processor::__construct()

```php
public CSS3Processor::__construct(void) : CSS3Processor
```

* Constructs a new `CSS3Processor` object.
* Throws exceptions on errors.

### CSS3Processor::setNotifier()

```php
public CSS3Processor::setNotifier(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.
* All return values from the callback are discarded.
* The parameter `$type` can be set to any of the `php-ext-css` Type 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 `true` on success.
* Throws exceptions on errors.

### CSS3Processor::setModifier()

```php
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 the `extcss3` Type Constants
listed below that are marked as `modifiable`.
* Only one modifier callback per token type is possible. Any subsequent call
to `::setModifier()` replaces previously set callbacks for the same type.
* 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 the `php-ext-css` Type Constants listed
below that are marked as `modifiable`.
* 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 `true` on success.
* Throws exceptions on errors.

### CSS3Processor::dump()

```
```php
public CSS3Processor::dump(string $css) : string
```

* Applies preprocessing and the registered modifiers to `$css`
and returns the resulting string.
* Applies preprocessing, the registered notifiers and modifiers to `$css` and returns
the resulting string.
* Throws exceptions on errors.

### CSS3Processor::minify()

```
```php
public CSS3Processor::minify(string $css [, array $vendors ]) : string
```

* Returns the minimized result string considering the registered modifiers
* Returns the minimized result string considering the registered notifiers, modifiers
and the blacklist of vendor prefixes given in the `$vendors` array.
* Throws exceptions on errors.

## PHP Class Constants

### Type Constants

```php
`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
```

### PHP Class Constants


#### Type Constants

* `TYPE_IDENT` 1
* `TYPE_FUNCTION` 2
* `TYPE_AT_KEYWORD` 3
* `TYPE_HASH` 4
* `TYPE_STRING` 5 (modifiable)
* `TYPE_BAD_STRING` 6 (modifiable)
* `TYPE_URL` 7 (modifiable)
* `TYPE_BAD_URL` 8 (modifiable)
* `TYPE_DELIM` 9
* `TYPE_NUMBER` 10
* `TYPE_PERCENTAGE` 11
* `TYPE_DIMENSION` 12
* `TYPE_UNICODE_RANGE` 13
* `TYPE_INCLUDE_MATCH` 14
* `TYPE_DASH_MATCH` 15
* `TYPE_PREFIX_MATCH` 16
* `TYPE_SUFFIX_MATCH` 17
* `TYPE_SUBSTR_MATCH` 18
* `TYPE_COLUMN` 19
* `TYPE_WS` 20
* `TYPE_CDO` 21
* `TYPE_CDC` 22
* `TYPE_COLON` 23
* `TYPE_SEMICOLON` 24
* `TYPE_COMMA` 25
* `TYPE_BR_RO` 26
* `TYPE_BR_RC` 27
* `TYPE_BR_SO` 28
* `TYPE_BR_SC` 29
* `TYPE_BR_CO` 30
* `TYPE_BR_CC` 31
* `TYPE_COMMENT` 32 (modifiable)
* `TYPE_EOF` 33


#### Flag Constants

* `FLAG_ID` 1
* `FLAG_UNRESTRICTED` 2
* `FLAG_INTEGER` 3
* `FLAG_NUMBER` 4
* `FLAG_STRING` 5
* `FLAG_AT_URL_STRING` 6


#### Exception Code Constants

* `ERR_MEMORY` 1
* `ERR_BYTES_CORRUPTION` 2
* `ERR_NULL_PTR` 3
* `ERR_INV_PARAM` 4


### Examples
### Flag Constants

```php
`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
```

### Exception Code Constants

```php
`CSS3Processor::ERR_MEMORY` 1
`CSS3Processor::ERR_BYTES_CORRUPTION` 2
`CSS3Processor::ERR_NULL_PTR` 3
`CSS3Processor::ERR_INV_PARAM` 4
`CSS3Processor::ERR_INV_VALUE` 5
```

## Example

```php
$css = file_get_contents('style.css');

try {
$proc = new \CSS3Processor();
$proc = new \CSS3Processor();

$min = $proc->minify($css, ['-ms', '-moz', '-o']);
$min = $proc->minify($css, ['-ms', '-moz', '-o']);
} catch (Exception $e) {
...
...
}
```

More examples coming soon...
See the tests for more examples.

[ico-license]: https://img.shields.io/github/license/mashape/apistatus.svg
[ico-travis]: https://travis-ci.org/sevenval/php-ext-css.svg?branch=master
[link-travis]: https://travis-ci.org/sevenval/php-ext-css
[ico-license-mpz]: https://img.shields.io/github/license/mashape/apistatus.svg
[ico-travis-mpz]: https://api.travis-ci.org/alex-schneider/php-ext-css.svg?branch=master
[link-travis-mpz]: https://travis-ci.org/alex-schneider/php-ext-css
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
29 changes: 15 additions & 14 deletions config.m4
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@

PHP_ARG_WITH(extcss3, whether to enable the extcss3 extension,
[--with-extcss3 Enable extcss3 support])
PHP_ARG_WITH(extcss3, whether to enable the extcss3 MPZ eidition extension,
[--with-extcss3 Enable extcss3 MPZ edition support])

if test "$PHP_EXTCSS3" != "no"; then
PHP_SUBST(EXTCSS3_SHARED_LIBADD)
PHP_NEW_EXTENSION(extcss3,
extcss3/intern.c \
extcss3/utils.c \
extcss3/dumper/dumper.c \
extcss3/minifier/minifier.c \
extcss3/minifier/tree.c \
extcss3/minifier/types/numeric.c \
extcss3/minifier/types/hash.c \
extcss3/minifier/types/function.c \
extcss3/tokenizer/preprocessor.c \
extcss3/tokenizer/context.c \
extcss3/tokenizer/tokenizer.c \
mpz/src/mpz_alloc.c \
extcss3/intern.c \
extcss3/utils.c \
extcss3/dumper/dumper.c \
extcss3/minifier/minifier.c \
extcss3/minifier/tree.c \
extcss3/minifier/types/numeric.c \
extcss3/minifier/types/hash.c \
extcss3/minifier/types/function.c \
extcss3/tokenizer/preprocessor.c \
extcss3/tokenizer/context.c \
extcss3/tokenizer/tokenizer.c \
php_extcss3.c,
$ext_shared, , "-Wall")
$ext_shared, , "-Wall -Wextra")
fi
5 changes: 2 additions & 3 deletions extcss3/dumper/dumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ char *extcss3_dump_rules(extcss3_intern *intern, extcss3_rule *rule, unsigned in
if ((intern == NULL) || (intern->copy.str == NULL) || (rule == NULL)) {
return _extcss3_set_error_code(error, EXTCSS3_ERR_NULL_PTR);
} else if ((result = curr = (char *)calloc(intern->copy.len + intern->modifier.user_strlen_diff + 1, sizeof(char))) == NULL) {
/* Use calloc(), because the user has to free the result. */
return _extcss3_set_error_code(error, EXTCSS3_ERR_MEMORY);
}

Expand All @@ -46,6 +47,7 @@ char *extcss3_dump_tokens(extcss3_intern *intern, unsigned int *error)
} else if ((token = intern->base_token) == NULL) {
return _extcss3_set_error_code(error, EXTCSS3_ERR_NULL_PTR);
} else if ((result = pos = (char *)calloc(intern->copy.len + intern->modifier.user_strlen_diff + 1, sizeof(char))) == NULL) {
/* Use calloc(), because the user has to free the result. */
return _extcss3_set_error_code(error, EXTCSS3_ERR_MEMORY);
}

Expand Down Expand Up @@ -82,9 +84,6 @@ char *extcss3_dump_tokens(extcss3_intern *intern, unsigned int *error)

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

extcss3_release_tokens_list(intern->base_token);
intern->base_token = NULL;

return result;
}

Expand Down
Loading