Skip to content

Commit

Permalink
Changed the HTML minify to attempt to preserve some spaces between el…
Browse files Browse the repository at this point in the history
…ements, e.g. buttons, links.
  • Loading branch information
jamielsharief committed Sep 13, 2020
1 parent 0292ad9 commit 52478ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.1.0] - 2020-09-21

### Changed

- Changed the HTML minify to attempt to preserve some spaces between elements, e.g. buttons, links.

## [1.0.2] - 2020-09-20

### Fixed
Expand Down
9 changes: 6 additions & 3 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ public static function minify(string $html): ?string
continue;
}
/**
* Really want to strip new lines in between tags, however certain elements require
* a space to be displayed properly
* Really want to strip new lines in between tags, however new lines between inline
* tags are treated as space. If the node value has any text then remove new lines & tabs,
* else replace with a space for inline elements.
* @see https://www.w3.org/TR/REC-html40/struct/text.html#h-9.1
*/
$node->nodeValue = str_replace(["\r\n", "\n", "\r", "\t"], '', $node->nodeValue);
$replace = preg_match('/\S/', $node->nodeValue) ? '' : ' ';
$node->nodeValue = str_replace(["\r\n", "\n", "\r", "\t"], $replace, $node->nodeValue);
$node->nodeValue = preg_replace('/(\s)+/s', '\\1', $node->nodeValue);

# Check parent and one level up for e.g pre + code Not sure of other examples
Expand Down
17 changes: 8 additions & 9 deletions tests/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ public function testMinify()
</div>
EOF;

$expected = <<< EOF
<html><body><h1>Heading #1</h1><h2>Heading #2</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae lobortis diam. Nam porta magna nec porttitor bibendum. Vestibulum tristique lorem in urna hendrerit, et commodo velit suscipit. Sed imperdiet tincidunt condimentum. Aliquam erat volutpat. Cras rhoncus mauris at enim ultrices, sed consequat lectus aliquam. Nullam venenatis porta quam, sit amet pulvinar felis porttitor ut. Morbi vel vestibulum mi. Vestibulum id erat tortor. Integer ac semper elit.</p><p>Use <a href="https://www.google.com">Google</a> to do some searches.</p><ul><li> List #1 </li><li>List #1</li></ul><div><img src="https://www.google.com/img/logo.png"></div><blockquote>Life is what happens when you're busy making other plans.</blockquote><div class="foo">Lorem <strong>ipsum</strong> <em>dolor</em> sit amet, <span>consectetur adipiscing</span> elit.</div><pre>
<code>
Csv::load('somefile.csv');
Csv::toArray(myvar);
</code>
</pre><div class="some buttons"><button type="button" class="btn btn-primary">Primary</button><button type="button" class="btn btn-secondary">Secondary</button><button type="button" class="btn btn-success">Success</button><button type="button" class="btn btn-danger">Danger</button></div></body></html>
EOF;
echo html::minify($html);
$expected = <<< EOT
<html><body><h1>Heading #1</h1><h2>Heading #2</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae lobortis diam. Nam porta magna nec porttitor bibendum. Vestibulum tristique lorem in urna hendrerit, et commodo velit suscipit. Sed imperdiet tincidunt condimentum. Aliquam erat volutpat. Cras rhoncus mauris at enim ultrices, sed consequat lectus aliquam. Nullam venenatis porta quam, sit amet pulvinar felis porttitor ut. Morbi vel vestibulum mi. Vestibulum id erat tortor. Integer ac semper elit.</p><p>Use <a href="https://www.google.com">Google</a> to do some searches.</p><ul><li> List #1 </li><li>List #1</li></ul><div><img src="https://www.google.com/img/logo.png"></div><blockquote>Life is what happens when you're busy making other plans.</blockquote><div class="foo">Lorem <strong>ipsum</strong> <em>dolor</em> sit amet, <span>consectetur adipiscing</span> elit.</div><pre>
<code>
Csv::load('somefile.csv');
Csv::toArray(myvar);
</code>
</pre><div class="some buttons"> <button type="button" class="btn btn-primary">Primary</button> <button type="button" class="btn btn-secondary">Secondary</button> <button type="button" class="btn btn-success">Success</button> <button type="button" class="btn btn-danger">Danger</button> </div></body></html>
EOT;
$this->assertEquals($expected, html::minify($html));
}

Expand Down

0 comments on commit 52478ef

Please sign in to comment.