Skip to content

Commit

Permalink
keep leading and trailing space in inline elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Singer committed Dec 10, 2015
1 parent 49ccc93 commit 980fd50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 11 additions & 5 deletions PrettyMin.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,18 @@ protected function removeWhitespace() {
$node->nodeValue = str_replace(' ', ' ', $node->nodeValue);
}

if (!($node->previousSibling && in_array($node->previousSibling->nodeName, $this->options['keep_whitespace_around']))) {
$node->nodeValue = ltrim($node->nodeValue);
}
if (!in_array($node->parentNode->nodeName, $this->options['keep_whitespace_around'])) {
if (!($node->previousSibling && in_array($node->previousSibling->nodeName,
$this->options['keep_whitespace_around']))
) {
$node->nodeValue = ltrim($node->nodeValue);
}

if (!($node->nextSibling && in_array($node->nextSibling->nodeName, $this->options['keep_whitespace_around']))) {
$node->nodeValue = rtrim($node->nodeValue);
if (!($node->nextSibling && in_array($node->nextSibling->nodeName,
$this->options['keep_whitespace_around']))
) {
$node->nodeValue = rtrim($node->nodeValue);
}
}

if((strlen($node->nodeValue) == 0)) {
Expand Down
5 changes: 3 additions & 2 deletions Tests/PrettyMinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function () {
<div class="" style=""><p>This is <b>bold</b>
Text.
And some more text, still in the same paragraph.
<strong>Inline tag </strong>whith whitespace at the end but not after.
</p><p>This is another paragraph with a <a href="">link</a>.
</p>
</div>
Expand All @@ -55,7 +56,7 @@ public function testMinify()

$expected = <<<HTML
<!DOCTYPE html>
<html><head><title>Test</title><script>$(document).ready(function(){if(this&&that&&a>b){doSomething();}});</script><style>body>div{border-top:1px solid green}</style></head><body><h1>Test</h1><div class="keep"><div><p>This is <b>bold</b> Text. And some more text, still in the same paragraph.</p><p>This is another paragraph with a <a href="">link</a>.</p></div></div><form><input type="text" name="a"><input type="text" name="b"></form></body></html>
<html><head><title>Test</title><script>$(document).ready(function(){if(this&&that&&a>b){doSomething();}});</script><style>body>div{border-top:1px solid green}</style></head><body><h1>Test</h1><div class="keep"><div><p>This is <b>bold</b> Text. And some more text, still in the same paragraph. <strong>Inline tag </strong>whith whitespace at the end but not after.</p><p>This is another paragraph with a <a href="">link</a>.</p></div></div><form><input type="text" name="a"><input type="text" name="b"></form></body></html>
HTML;

Expand Down Expand Up @@ -93,7 +94,7 @@ function () {
<h1>Test</h1>
<div class="keep">
<div class="" style="">
<p>This is <b>bold</b> Text. And some more text, still in the same paragraph.</p>
<p>This is <b>bold</b> Text. And some more text, still in the same paragraph. <strong>Inline tag </strong>whith whitespace at the end but not after.</p>
<p>This is another paragraph with a <a href="">link</a>.</p>
</div>
</div>
Expand Down

0 comments on commit 980fd50

Please sign in to comment.