Skip to content

Commit 30b295f

Browse files
authored
Merge branch 'matthiasmullie:master' into master
2 parents 65b7ebf + c00fb02 commit 30b295f

File tree

13 files changed

+975
-66
lines changed

13 files changed

+975
-66
lines changed

.github/workflows/test.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: test-suite
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
11+
build:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- name: Test PHP 8.1 & submit codecov
19+
run: make test PHP=8.1 UP=1 DOWN=0 && bash <(curl -s https://codecov.io/bash) && make down PHP=8.1
20+
21+
- name: Test PHP 8.0
22+
run: make test PHP=8.0
23+
24+
- name: Test PHP 7.4
25+
run: make test PHP=7.4
26+
27+
- name: Test PHP 7.3
28+
run: make test PHP=7.3
29+
30+
- name: Test PHP 7.2
31+
run: make test PHP=7.2
32+
33+
- name: Test PHP 7.1
34+
run: make test PHP=7.1
35+
36+
- name: Test PHP 5.6
37+
run: make test PHP=5.6

.travis.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN apt-get update
88
RUN apt-get install -y zip unzip libzip-dev git
99
RUN docker-php-ext-install zip
1010
RUN docker-php-ext-install pcntl
11+
RUN pecl install xdebug || pecl install xdebug-2.7.2 || pecl install -f xdebug-2.5.5 && docker-php-ext-enable xdebug
1112
RUN curl -sS https://getcomposer.org/installer | php
1213
RUN mv composer.phar /usr/local/bin/composer
1314
RUN composer install

README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# Minify
22

3-
[![Build status](https://api.travis-ci.org/matthiasmullie/minify.svg?branch=master)](https://travis-ci.org/matthiasmullie/minify)
4-
[![Code coverage](http://img.shields.io/codecov/c/github/matthiasmullie/minify.svg)](https://codecov.io/github/matthiasmullie/minify)
5-
[![Code quality](http://img.shields.io/scrutinizer/g/matthiasmullie/minify.svg)](https://scrutinizer-ci.com/g/matthiasmullie/minify)
6-
[![Latest version](http://img.shields.io/packagist/v/matthiasmullie/minify.svg)](https://packagist.org/packages/matthiasmullie/minify)
7-
[![Downloads total](http://img.shields.io/packagist/dt/matthiasmullie/minify.svg)](https://packagist.org/packages/matthiasmullie/minify)
8-
[![License](http://img.shields.io/packagist/l/matthiasmullie/minify.svg)](https://github.com/matthiasmullie/minify/blob/master/LICENSE)
3+
[![Build status](https://img.shields.io/github/workflow/status/matthiasmullie/minify/test-suite?style=flat-square)](https://github.com/matthiasmullie/minify/actions/workflows/test.yml)
4+
[![Code coverage](http://img.shields.io/codecov/c/github/matthiasmullie/minify?style=flat-square)](https://codecov.io/github/matthiasmullie/minify)
5+
[![Code quality](http://img.shields.io/scrutinizer/g/matthiasmullie/minify?style=flat-square)](https://scrutinizer-ci.com/g/matthiasmullie/minify)
6+
[![Latest version](http://img.shields.io/packagist/v/matthiasmullie/minify?style=flat-square)](https://packagist.org/packages/matthiasmullie/minify)
7+
[![Downloads total](http://img.shields.io/packagist/dt/matthiasmullie/minify?style=flat-square)](https://packagist.org/packages/matthiasmullie/minify)
8+
[![License](http://img.shields.io/packagist/l/matthiasmullie/minify?style=flat-square)](https://github.com/matthiasmullie/minify/blob/master/LICENSE)
99

1010

11-
**[Donate/Support: ![Support](https://www.mullie.eu/public/donate.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=73CVYMBETDAP4)**
12-
1311
Removes whitespace, strips comments, combines files (incl. `@import` statements and small assets in CSS files), and optimizes/shortens a few common programming patterns, such as:
1412

1513
**JavaScript**
@@ -147,8 +145,3 @@ Although it's recommended to use Composer, you can actually [include these files
147145
## License
148146

149147
Minify is [MIT](http://opensource.org/licenses/MIT) licensed.
150-
151-
152-
## Challenges
153-
154-
If you're interested in learning some of the harder technical challenges I've encountered building this, you probably want to take a look at [what I wrote about it](http://www.mullie.eu/dont-build-your-own-minifier/) on my blog.

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ services:
88
- ./src:/var/www/src
99
- ./data:/var/www/data
1010
- ./tests:/var/www/tests
11+
- ./build:/var/www/build
1112
- ./phpunit.xml.dist:/var/www/phpunit.xml.dist
13+
'8.1':
14+
extends: php
15+
build:
16+
args:
17+
version: 8.1-cli
1218
'8.0':
1319
extends: php
1420
build:

makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
PHP ?= '7.2'
1+
PHP ?= '8.1'
22
UP ?= 1
33
DOWN ?= 1
4+
TEST ?=
45

56
docs:
67
wget http://apigen.org/apigen.phar
@@ -19,5 +20,5 @@ down:
1920

2021
test:
2122
[ $(UP) -eq 1 ] && make up || true
22-
$(eval cmd='docker-compose run $(PHP) vendor/bin/phpunit')
23+
$(eval cmd='docker-compose run $(PHP) env XDEBUG_MODE=coverage vendor/bin/phpunit $(TEST)')
2324
eval $(cmd); status=$$?; [ $(DOWN) -eq 1 ] && make down; exit $$status

phpunit.xml.dist

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit colors="true" bootstrap="tests/bootstrap.php">
3-
<testsuites>
4-
<testsuite name="Minify test suite">
5-
<directory suffix="Test.php">tests/css</directory>
6-
<directory suffix="Test.php">tests/js</directory>
7-
</testsuite>
8-
</testsuites>
9-
<logging>
10-
<log type="coverage-clover" target="build/logs/clover.xml"/>
11-
</logging>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src</directory>
6+
</include>
7+
<report>
8+
<clover outputFile="build/logs/clover.xml"/>
9+
</report>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="Minify test suite">
13+
<directory suffix="Test.php">tests/css</directory>
14+
<directory suffix="Test.php">tests/js</directory>
15+
</testsuite>
16+
</testsuites>
17+
<logging/>
1218
</phpunit>

src/CSS.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ protected function extractCustomProperties()
841841
// PHP only supports $this inside anonymous functions since 5.4
842842
$minifier = $this;
843843
$this->registerPattern(
844-
'/(?<=^|[;}])(--[^:;{}"\'\s]+)\s*:([^;{}]+)/m',
844+
'/(?<=^|[;}])\s*(--[^:;{}"\'\s]+)\s*:([^;{}]+)/m',
845845
function ($match) use ($minifier) {
846846
$placeholder = '--custom-'. count($minifier->extracted) . ':0';
847847
$minifier->extracted[$placeholder] = $match[1] .':'. trim($match[2]);

src/JS.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,24 +199,24 @@ protected function stripComments()
199199
$minifier = $this;
200200
$callback = function ($match) use ($minifier) {
201201
if (
202-
substr($match[1], 0, 1) === '!' ||
203-
strpos($match[1], '@license') !== false ||
204-
strpos($match[1], '@preserve') !== false
202+
substr($match[2], 0, 1) === '!' ||
203+
strpos($match[2], '@license') !== false ||
204+
strpos($match[2], '@preserve') !== false
205205
) {
206206
// preserve multi-line comments that start with /*!
207207
// or contain @license or @preserve annotations
208208
$count = count($minifier->extracted);
209209
$placeholder = '/*'.$count.'*/';
210210
$minifier->extracted[$placeholder] = $match[0];
211211

212-
return $placeholder;
212+
return $match[1] . $placeholder . $match[3];
213213
}
214214

215-
return '';
215+
return $match[1] . $match[3];
216216
};
217217

218218
// multi-line comments
219-
$this->registerPattern('/\n?\/\*(.*?)\*\/\n?/s', $callback);
219+
$this->registerPattern('/(\n?)\/\*(.*?)\*\/(\n?)/s', $callback);
220220

221221
// single-line comments
222222
$this->registerPattern('/\/\/.*$/m', '');
@@ -415,9 +415,26 @@ protected function stripWhitespace($content)
415415
* to be the for-loop's body... Same goes for while loops.
416416
* I'm going to double that semicolon (if any) so after the next line,
417417
* which strips semicolons here & there, we're still left with this one.
418+
* Note the special recursive construct in the three inner parts of the for:
419+
* (\{([^\{\}]*(?-2))*[^\{\}]*\})? - it is intended to match inline
420+
* functions bodies, e.g.: i<arr.map(function(e){return e}).length.
421+
* Also note that the construct is applied only once and multiplied
422+
* for each part of the for, otherwise it risks a catastrophic backtracking.
423+
* The limitation is that it will not allow closures in more than one
424+
* of the three parts for a specific for() case.
425+
* REGEX throwing catastrophic backtracking: $content = preg_replace('/(for\([^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*;[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*;[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*\));(\}|$)/s', '\\1;;\\8', $content);
418426
*/
419-
$content = preg_replace('/(for\([^;\{]*;[^;\{]*;[^;\{]*\));(\}|$)/s', '\\1;;\\2', $content);
427+
$content = preg_replace('/(for\((?:[^;\{]*|[^;\{]*function[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*);[^;\{]*;[^;\{]*\));(\}|$)/s', '\\1;;\\4', $content);
428+
$content = preg_replace('/(for\([^;\{]*;(?:[^;\{]*|[^;\{]*function[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*);[^;\{]*\));(\}|$)/s', '\\1;;\\4', $content);
429+
$content = preg_replace('/(for\([^;\{]*;[^;\{]*;(?:[^;\{]*|[^;\{]*function[^;\{]*(\{([^\{\}]*(?-2))*[^\{\}]*\})?[^;\{]*)\));(\}|$)/s', '\\1;;\\4', $content);
430+
420431
$content = preg_replace('/(for\([^;\{]+\s+in\s+[^;\{]+\));(\}|$)/s', '\\1;;\\2', $content);
432+
433+
/*
434+
* Do the same for the if's that don't have a body but are followed by ;}
435+
*/
436+
$content = preg_replace('/(\bif\s*\([^{;]*\));\}/s', '\\1;;}', $content);
437+
421438
/*
422439
* Below will also keep `;` after a `do{}while();` along with `while();`
423440
* While these could be stripped after do-while, detecting this

tests/css/CSSTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,17 @@ public function dataProvider()
832832
':root{--some-var:0px}.some-class{margin-left:calc(20px + var(--some-var))}',
833833
);
834834

835+
// https://github.com/matthiasmullie/minify/issues/378
836+
$tests[] = array(
837+
':root {
838+
--some-var: 0px;
839+
}
840+
p {
841+
margin-left: calc(20px + var(--some-var));
842+
}',
843+
':root{--some-var:0px}p{margin-left:calc(20px + var(--some-var))}'
844+
);
845+
835846
return $tests;
836847
}
837848

0 commit comments

Comments
 (0)