Skip to content

Commit 262b59a

Browse files
committed
Fixed bug in tag::minify() where the <html> tag was not removed when 'close' is set to true.
Updated minification settings in `config.php` to make IE comments removed by default now that IE11 is dead. Added a safe settings option that disables options that can be unsafe. Updated tests. Updated dependencies.
1 parent 279b9b8 commit 262b59a

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php": ">=7.3",
1616
"hexydec/tokenise": "0.4.2",
1717
"hexydec/cssdoc": "0.5.2",
18-
"hexydec/jslite": "0.5.3"
18+
"hexydec/jslite": "0.5.4"
1919
},
2020
"autoload": {
2121
"classmap": ["src/"]

src/config.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function __construct(array $config = []) {
109109
'whitespace' => true, // strip whitespace from text nodes
110110
'comments' => [
111111
'remove' => true, // remove comments
112-
'ie' => true // preserve IE comments
112+
'ie' => false // preserve IE comments
113113
],
114114
'urls' => [ // update internal URL's to be shorter
115115
'scheme' => true, // remove the scheme from URLs that have the same scheme as the current document
@@ -146,6 +146,7 @@ public function __construct(array $config = []) {
146146
'singleton' => true, // minify singleton element by removing slash
147147
'quotes' => true, // sets the output option 'quotestyle' to 'minimal'
148148
'close' => true, // don't write close tags where possible
149+
'safe' => false, // sets the minification presets to CSS safe options
149150
'email' => false, // sets the minification presets to email safe options
150151
'style' => [], // specify CSS minifier options
151152
'script' => [], // specify CSS javascript options

src/htmldoc.php

+9
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,15 @@ public function minify(array $minify = []) : void {
597597
$this->config['output']['quotestyle'] = 'minimal';
598598
}
599599

600+
// set safe options
601+
if ($minify['safe']) {
602+
$minify['urls'] = false;
603+
if ($minify['attributes'] !== false) {
604+
$minify['attributes']['empty'] = false;
605+
$minify['attributes']['default'] = false;
606+
}
607+
}
608+
600609
// email minification
601610
if ($minify['email']) {
602611
if ($minify['comments'] !== false) {

src/tokens/tag.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@ public function minify(array $minify) : void {
483483
}
484484

485485
// work out whether to omit the closing tag
486-
if ($minify['close'] && \in_array($tag, $config['elements']['closeoptional']) && !\in_array($this->parent->tagName, $config['elements']['inline'], true)) {
487-
$tag = null;
486+
if ($minify['close'] && \in_array($tag, $config['elements']['closeoptional']) && ($this->parent->tagName === null || !\in_array($this->parent->tagName, $config['elements']['inline'], true))) {
488487
$children = $this->parent->toArray();
489488
$next = false;
490489
foreach ($children AS $item) {
@@ -513,7 +512,7 @@ public function minify(array $minify) : void {
513512
}
514513

515514
// if last tag, remove closing tag
516-
if ($next) {
515+
if (!$children || $next) {
517516
$this->close = false;
518517
}
519518
}

tests/minifyHtmldocTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ public function testCanStripComments() {
9898
'urls' => false, // update internal URL's to be shorter
9999
'attributes' => false, // remove values from boolean attributes);
100100
'quotes' => false, // minify attribute quotes
101-
'close' => false // don't write close tags where possible
101+
'close' => false, // don't write close tags where possible
102+
'comments' => [
103+
'remove' => true,
104+
'ie' => true
105+
],
106+
'singleton' => false
102107
));
103108
$minified = file_get_contents(__DIR__.'/templates/comments-minified-ie.html');
104109
$this->assertEquals($minified, $doc->html(), 'Can strip comments but leave IE comments intact');

tests/templates/svg-minified.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><title>Hexydec uGraph</title><body><svg width="100%" height="200"><defs><linearGradient id="c0" x1="0" x2="0" y1="0" y2="1"><stop stop-color="blue" offset="0%" stop-opacity="0.2"/><stop stop-color="blue" offset="100%" stop-opacity="0"/></linearGradient><linearGradient id="c1" x1="0" x2="0" y1="0" y2="1"><stop stop-color="red" offset="0%" stop-opacity="0.2"/><stop stop-color="red" offset="100%" stop-opacity="0"/></linearGradient><clipPath id="box"><rect x="0" y="0" width="100" height="100"/></clipPath></defs><svg width="100%" height="20px" viewBox="0 0 236 20" preserveAspectRatio="xMidYMid meet"><rect x="2" y="2" width="16" height="16" stroke="blue" stroke-width="2" fill="url(#c0)"/><text x="30" y="0" dominant-baseline="hanging">Views</text><rect x="112" y="2" width="16" height="16" stroke="red" stroke-width="2" fill="url(#c1)"/><text x="140" y="0" dominant-baseline="hanging">bounce</text></svg><foreignObject width="100%" height="100%" xmlns="http://www.w3.org/1999/xhtml"><div style="height:100%;box-sizing:border-box;padding:30px 0px 8px 92px"><svg x="0" y="0" width="100%" height="100%" style="overflow:visible"><g stroke="#DDD" stroke-width="1" vector-effect="non-scaling-stroke"><line x1="0" y1="0%" x2="100%" y2="0%"/><line x1="0" y1="12.5%" x2="100%" y2="12.5%"/><line x1="0" y1="25%" x2="100%" y2="25%"/><line x1="0" y1="37.5%" x2="100%" y2="37.5%"/><line x1="0" y1="50%" x2="100%" y2="50%"/><line x1="0" y1="62.5%" x2="100%" y2="62.5%"/><line x1="0" y1="75%" x2="100%" y2="75%"/><line x1="0" y1="87.5%" x2="100%" y2="87.5%"/><line x1="0" y1="100%" x2="100%" y2="100%"/></g><g stroke="#333" stroke-width="1" vector-effect="non-scaling-stroke"><line x1="-5" y1="0%" x2="0" y2="0%"/><line x1="-5" y1="12.5%" x2="0" y2="12.5%"/><line x1="-5" y1="25%" x2="0" y2="25%"/><line x1="-5" y1="37.5%" x2="0" y2="37.5%"/><line x1="-5" y1="50%" x2="0" y2="50%"/><line x1="-5" y1="62.5%" x2="0" y2="62.5%"/><line x1="-5" y1="75%" x2="0" y2="75%"/><line x1="-5" y1="87.5%" x2="0" y2="87.5%"/><line x1="-5" y1="100%" x2="0" y2="100%"/></g><g text-anchor="end" dominant-baseline="middle"><text x="-10" y="100%">20</text><text x="-10" y="87.5%">30</text><text x="-10" y="75%">40</text><text x="-10" y="62.5%">50</text><text x="-10" y="50%">60</text><text x="-10" y="37.5%">70</text><text x="-10" y="25%">80</text><text x="-10" y="12.5%">90</text><text x="-10" y="0%">100</text></g><text x="-68" y="50%" style="writing-mode:vertical-lr;text-anchor:middle" font-size="24">Test</text><svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none"><path d="M-10,61.3L0,63.8L2,35L4.1,7.5L6.1,55L8.2,30L10.2,51.3L12.2,66.3L14.3,52.5L16.3,18.8L18.4,32.5L20.4,95L22.4,58.8L24.5,66.3L26.5,86.3L28.6,50L30.6,86.3L32.7,13.8L34.7,25L36.7,35L38.8,46.3L40.8,75L42.9,60L44.9,82.5L46.9,15L49,55L51,22.5L53.1,67.5L55.1,23.8L57.1,67.5L59.2,11.3L61.2,90L63.3,80L65.3,91.3L67.3,46.3L69.4,55L71.4,28.8L73.5,31.3L75.5,25L77.6,43.8L79.6,95L81.6,10L83.7,50L85.7,17.5L87.8,16.3L89.8,90L91.8,78.8L93.9,67.5L95.9,65L98,82.5L100,12.5h10V110H-10Z" stroke="blue" stroke-width="2" fill="url(#c0)" clip-path="url(#box)" vector-effect="non-scaling-stroke"/><path d="M-10,31.3L0,93.8L2,73.8L4.1,76.3L6.1,96.3L8.2,75L10.2,68.8L12.2,65L14.3,75L16.3,87.5L18.4,82.5L20.4,85L22.4,93.8L24.5,96.3L26.5,86.3L28.6,86.3L30.6,100L32.7,95L34.7,85L36.7,65L38.8,100L40.8,82.5L42.9,96.3L44.9,91.3L46.9,85L49,63.8L51,85L53.1,73.8L55.1,72.5L57.1,86.3L59.2,85L61.2,100L63.3,81.3L65.3,67.5L67.3,76.3L69.4,71.3L71.4,80L73.5,70L75.5,88.8L77.6,83.8L79.6,66.3L81.6,70L83.7,90L85.7,85L87.8,70L89.8,88.8L91.8,98.8L93.9,75L95.9,77.5L98,66.3L100,92.5h10V110H-10Z" stroke="red" stroke-width="2" fill="url(#c1)" clip-path="url(#box)" vector-effect="non-scaling-stroke"/></svg></svg></div></foreignObject></svg></html>
1+
<!DOCTYPE html><html><head><title>Hexydec uGraph</title><body><svg width="100%" height="200"><defs><linearGradient id="c0" x1="0" x2="0" y1="0" y2="1"><stop stop-color="blue" offset="0%" stop-opacity="0.2"/><stop stop-color="blue" offset="100%" stop-opacity="0"/></linearGradient><linearGradient id="c1" x1="0" x2="0" y1="0" y2="1"><stop stop-color="red" offset="0%" stop-opacity="0.2"/><stop stop-color="red" offset="100%" stop-opacity="0"/></linearGradient><clipPath id="box"><rect x="0" y="0" width="100" height="100"/></clipPath></defs><svg width="100%" height="20px" viewBox="0 0 236 20" preserveAspectRatio="xMidYMid meet"><rect x="2" y="2" width="16" height="16" stroke="blue" stroke-width="2" fill="url(#c0)"/><text x="30" y="0" dominant-baseline="hanging">Views</text><rect x="112" y="2" width="16" height="16" stroke="red" stroke-width="2" fill="url(#c1)"/><text x="140" y="0" dominant-baseline="hanging">bounce</text></svg><foreignObject width="100%" height="100%" xmlns="http://www.w3.org/1999/xhtml"><div style="height:100%;box-sizing:border-box;padding:30px 0px 8px 92px"><svg x="0" y="0" width="100%" height="100%" style="overflow:visible"><g stroke="#DDD" stroke-width="1" vector-effect="non-scaling-stroke"><line x1="0" y1="0%" x2="100%" y2="0%"/><line x1="0" y1="12.5%" x2="100%" y2="12.5%"/><line x1="0" y1="25%" x2="100%" y2="25%"/><line x1="0" y1="37.5%" x2="100%" y2="37.5%"/><line x1="0" y1="50%" x2="100%" y2="50%"/><line x1="0" y1="62.5%" x2="100%" y2="62.5%"/><line x1="0" y1="75%" x2="100%" y2="75%"/><line x1="0" y1="87.5%" x2="100%" y2="87.5%"/><line x1="0" y1="100%" x2="100%" y2="100%"/></g><g stroke="#333" stroke-width="1" vector-effect="non-scaling-stroke"><line x1="-5" y1="0%" x2="0" y2="0%"/><line x1="-5" y1="12.5%" x2="0" y2="12.5%"/><line x1="-5" y1="25%" x2="0" y2="25%"/><line x1="-5" y1="37.5%" x2="0" y2="37.5%"/><line x1="-5" y1="50%" x2="0" y2="50%"/><line x1="-5" y1="62.5%" x2="0" y2="62.5%"/><line x1="-5" y1="75%" x2="0" y2="75%"/><line x1="-5" y1="87.5%" x2="0" y2="87.5%"/><line x1="-5" y1="100%" x2="0" y2="100%"/></g><g text-anchor="end" dominant-baseline="middle"><text x="-10" y="100%">20</text><text x="-10" y="87.5%">30</text><text x="-10" y="75%">40</text><text x="-10" y="62.5%">50</text><text x="-10" y="50%">60</text><text x="-10" y="37.5%">70</text><text x="-10" y="25%">80</text><text x="-10" y="12.5%">90</text><text x="-10" y="0%">100</text></g><text x="-68" y="50%" style="writing-mode:vertical-lr;text-anchor:middle" font-size="24">Test</text><svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none"><path d="M-10,61.3L0,63.8L2,35L4.1,7.5L6.1,55L8.2,30L10.2,51.3L12.2,66.3L14.3,52.5L16.3,18.8L18.4,32.5L20.4,95L22.4,58.8L24.5,66.3L26.5,86.3L28.6,50L30.6,86.3L32.7,13.8L34.7,25L36.7,35L38.8,46.3L40.8,75L42.9,60L44.9,82.5L46.9,15L49,55L51,22.5L53.1,67.5L55.1,23.8L57.1,67.5L59.2,11.3L61.2,90L63.3,80L65.3,91.3L67.3,46.3L69.4,55L71.4,28.8L73.5,31.3L75.5,25L77.6,43.8L79.6,95L81.6,10L83.7,50L85.7,17.5L87.8,16.3L89.8,90L91.8,78.8L93.9,67.5L95.9,65L98,82.5L100,12.5h10V110H-10Z" stroke="blue" stroke-width="2" fill="url(#c0)" clip-path="url(#box)" vector-effect="non-scaling-stroke"/><path d="M-10,31.3L0,93.8L2,73.8L4.1,76.3L6.1,96.3L8.2,75L10.2,68.8L12.2,65L14.3,75L16.3,87.5L18.4,82.5L20.4,85L22.4,93.8L24.5,96.3L26.5,86.3L28.6,86.3L30.6,100L32.7,95L34.7,85L36.7,65L38.8,100L40.8,82.5L42.9,96.3L44.9,91.3L46.9,85L49,63.8L51,85L53.1,73.8L55.1,72.5L57.1,86.3L59.2,85L61.2,100L63.3,81.3L65.3,67.5L67.3,76.3L69.4,71.3L71.4,80L73.5,70L75.5,88.8L77.6,83.8L79.6,66.3L81.6,70L83.7,90L85.7,85L87.8,70L89.8,88.8L91.8,98.8L93.9,75L95.9,77.5L98,66.3L100,92.5h10V110H-10Z" stroke="red" stroke-width="2" fill="url(#c1)" clip-path="url(#box)" vector-effect="non-scaling-stroke"/></svg></svg></div></foreignObject></svg>

0 commit comments

Comments
 (0)