Skip to content
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

Add option to preserve conditional comments #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
3 changes: 3 additions & 0 deletions changelog.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.2
- Added option to preserve conditional comments `<!--[if lt IE 10]>IE only<![endif]-->`.

## 2.1

- Added experimental feature support for minify `<script type="json-ld"></script>`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jenstornell/tiny-html-minifier",
"description": "Minify HTML in PHP with just a single class",
"version": "2.1",
"version": "2.2",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
18 changes: 17 additions & 1 deletion readme.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tiny Html Minifier

![Version 2.0](https://img.shields.io/badge/version-2.0-blue.svg) ![MIT license](https://img.shields.io/badge/license-MIT-green.svg) [![Donate](https://img.shields.io/badge/give-donation-yellow.svg)](https://www.paypal.me/DevoneraAB)
![Version 2.2](https://img.shields.io/badge/version-2.2-blue.svg) ![MIT license](https://img.shields.io/badge/license-MIT-green.svg) [![Donate](https://img.shields.io/badge/give-donation-yellow.svg)](https://www.paypal.me/DevoneraAB)

[Changelog](changelog.md)

Expand Down Expand Up @@ -106,6 +106,7 @@ require 'tiny-html-minifier.php';
echo TinyMinify::html($html, $options = [
'collapse_whitespace' => false,
'collapse_json_lt' => false, // WARNING - EXPERIMENTAL FEATURE
'preserve_conditional_comments' => false,
]);
```

Expand All @@ -127,6 +128,21 @@ Spaces are collapsed. The text inside the element is still untouched. Set this v
<ul><li><a href="#">My link</a></li><li><a href="#">My link</a></li></ul>
```

### preserve_conditional_comments

#### Do not preserve

All comments will be removed `<!-- Whatever -->`, also conditional comments `<!--[if lt IE 10]>...<![endif]-->`).


#### Preserve

Conditional comments will be preserved. Other comments `<!-- Whatever -->` will be removed.

```html
<!--[if lt IE 10]>IE only<![endif]-->
```

### collapse_json_lt

***This is an experimental feature that could break your site!***
Expand Down
15 changes: 8 additions & 7 deletions tiny-html-minifier.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ function __construct($options) {
// Run minifier
function minify($html) {
$html = $this->removeComments($html);

$rest = $html;

while(!empty($rest)) :

$parts = explode('<', $rest, 2);

$this->walk($parts[0]);
Expand All @@ -73,7 +73,7 @@ function walk(&$part) {

$tag_parts = explode('>', $part);
$tag_content = $tag_parts[0];

if(!empty($tag_content)) {
$name = $this->findName($tag_content);
$element = $this->toElement($tag_content, $part, $name);
Expand Down Expand Up @@ -107,7 +107,8 @@ function walk(&$part) {

// Remove comments
function removeComments($content = '') {
return preg_replace('/<!--(.|\s)*?-->/', '', $content);
$regexp = empty($this->options['preserve_conditional_comments']) ? '/<!--(.|\s)*?-->/' : '/<!--((?![\[]{1,1}).|\s)*?-->/';
return preg_replace($regexp, '', $content);
}

// Check if string contains string
Expand Down Expand Up @@ -209,13 +210,13 @@ function buildHtml() {
foreach($this->build as $build) {

if(!empty($this->options['collapse_whitespace'])) {

if(strlen(trim($build['content'])) == 0)
continue;

elseif($build['type'] != 'content' && !in_array($build['name'], $this->elements['inline']))
trim($build['content']);

}

$this->output .= $build['content'];
Expand Down