Skip to content

Commit 980fd50

Browse files
author
Christoph Singer
committed
keep leading and trailing space in inline elements
1 parent 49ccc93 commit 980fd50

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

PrettyMin.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,18 @@ protected function removeWhitespace() {
257257
$node->nodeValue = str_replace(' ', ' ', $node->nodeValue);
258258
}
259259

260-
if (!($node->previousSibling && in_array($node->previousSibling->nodeName, $this->options['keep_whitespace_around']))) {
261-
$node->nodeValue = ltrim($node->nodeValue);
262-
}
260+
if (!in_array($node->parentNode->nodeName, $this->options['keep_whitespace_around'])) {
261+
if (!($node->previousSibling && in_array($node->previousSibling->nodeName,
262+
$this->options['keep_whitespace_around']))
263+
) {
264+
$node->nodeValue = ltrim($node->nodeValue);
265+
}
263266

264-
if (!($node->nextSibling && in_array($node->nextSibling->nodeName, $this->options['keep_whitespace_around']))) {
265-
$node->nodeValue = rtrim($node->nodeValue);
267+
if (!($node->nextSibling && in_array($node->nextSibling->nodeName,
268+
$this->options['keep_whitespace_around']))
269+
) {
270+
$node->nodeValue = rtrim($node->nodeValue);
271+
}
266272
}
267273

268274
if((strlen($node->nodeValue) == 0)) {

Tests/PrettyMinTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function () {
3737
<div class="" style=""><p>This is <b>bold</b>
3838
Text.
3939
And some more text, still in the same paragraph.
40+
<strong>Inline tag </strong>whith whitespace at the end but not after.
4041
</p><p>This is another paragraph with a <a href="">link</a>.
4142
</p>
4243
</div>
@@ -55,7 +56,7 @@ public function testMinify()
5556

5657
$expected = <<<HTML
5758
<!DOCTYPE html>
58-
<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>
59+
<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>
5960
6061
HTML;
6162

@@ -93,7 +94,7 @@ function () {
9394
<h1>Test</h1>
9495
<div class="keep">
9596
<div class="" style="">
96-
<p>This is <b>bold</b> Text. And some more text, still in the same paragraph.</p>
97+
<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>
9798
<p>This is another paragraph with a <a href="">link</a>.</p>
9899
</div>
99100
</div>

0 commit comments

Comments
 (0)