From 9a1ca73217bc165eaa4240de4bffdd8db7e7f5ed Mon Sep 17 00:00:00 2001 From: slackero Date: Wed, 10 Apr 2019 13:57:59 +0300 Subject: [PATCH 1/3] Added option to preserve conditional comment --- changelog.md | 3 +++ composer.json | 2 +- readme.md | 18 +++++++++++++++++- tiny-html-minifier.php | 15 ++++++++------- 4 files changed, 29 insertions(+), 9 deletions(-) mode change 100644 => 100755 changelog.md mode change 100644 => 100755 composer.json mode change 100644 => 100755 readme.md mode change 100644 => 100755 tiny-html-minifier.php diff --git a/changelog.md b/changelog.md old mode 100644 new mode 100755 index bb90313..20475f0 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## 2.2 +- Added option to preserve conditional comments ``. + ## 2.1 - Added experimental feature support for minify ``. diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index e77c3a5..6bb543a --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/readme.md b/readme.md old mode 100644 new mode 100755 index b8573b1..644d1da --- a/readme.md +++ b/readme.md @@ -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) @@ -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, ]); ``` @@ -127,6 +128,21 @@ Spaces are collapsed. The text inside the element is still untouched. Set this v ``` +### preserve_conditional_comments + +#### Do not preserve + +All comments will be removed ``, also conditional comments ``). + + +#### Preserve + +Conditional comments will be preserved. Other comments `` will be removed. + +```html + +``` + ### collapse_json_lt ***This is an experimental feature that could break your site!*** diff --git a/tiny-html-minifier.php b/tiny-html-minifier.php old mode 100644 new mode 100755 index 0f042ea..e6db0b9 --- a/tiny-html-minifier.php +++ b/tiny-html-minifier.php @@ -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]); @@ -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); @@ -107,7 +107,8 @@ function walk(&$part) { // Remove comments function removeComments($content = '') { - return preg_replace('//', '', $content); + $regexp = empty($this->options['preserve_conditional_comments']) ? '//' : '//'; + return preg_replace($regexp, '', $content); } // Check if string contains string @@ -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']; From 80746cc0027fbc879a161249a1dd3dbcc1bd6634 Mon Sep 17 00:00:00 2001 From: slackero Date: Tue, 30 Apr 2019 09:46:01 +0300 Subject: [PATCH 2/3] Added test case for option `preserve_conditional_comments` Issue #28 --- tests/tests.html | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/tests.html b/tests/tests.html index 2b5607e..20b69a3 100644 --- a/tests/tests.html +++ b/tests/tests.html @@ -12,6 +12,13 @@ + +
From b4a00d7d9e30c54b699f1ea7a7beb9a1b770aed3 Mon Sep 17 00:00:00 2001 From: slackero Date: Wed, 1 May 2019 09:55:45 +0300 Subject: [PATCH 3/3] Added more information regarding `preserve_conditional_comments` --- readme.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 644d1da..dd103cb 100755 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ ## Details - What the minifier does -- Remove HTML comments. +- Remove HTML comments but keep conditional comments `` (optional). - Remove slash in self closing elements. ` />` becomes `>`. - Remove ` type="text/css"` and `type="text/javascript"` in `style` and `script` tags. - Minimize elements within ``. It will not keep any whitespace (except inside `script`). @@ -62,6 +62,13 @@ echo TinyMinify::html($html); +
@@ -88,7 +95,7 @@ echo TinyMinify::html($html); ### After ```html -Tiny Html Minifier