From 67d39fb4b095a60696288af1b70a690f3f1c90d6 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Mon, 6 Apr 2020 22:38:54 +0200 Subject: [PATCH] [+]: fix logic of detecting next sibling dom node -> see isse #50 --- src/voku/helper/HtmlMin.php | 63 ++++++++++++++++++------------------- tests/HtmlMinTest.php | 20 ++++++++++++ 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/voku/helper/HtmlMin.php b/src/voku/helper/HtmlMin.php index 7e010e3..488efdd 100644 --- a/src/voku/helper/HtmlMin.php +++ b/src/voku/helper/HtmlMin.php @@ -874,11 +874,7 @@ private function domNodeClosingTagOptional(\DOMNode $node): bool ) && ( - ( - $nextSibling === null - && - $tag_name === 'dd' - ) + $nextSibling === null || ( $nextSibling instanceof \DOMElement @@ -917,32 +913,18 @@ private function domNodeClosingTagOptional(\DOMNode $node): bool ( $nextSibling === null && - ( - $node->parentNode !== null - && - ( - $node->parentNode->lastChild !== null - && - ( - $node->parentNode->lastChild === $node - || - \trim($node->parentNode->lastChild->textContent) === '' - ) - ) - && - !\in_array( - $node->parentNode->nodeName, - [ - 'a', - 'audio', - 'del', - 'ins', - 'map', - 'noscript', - 'video', - ], - true - ) + !\in_array( + $node->parentNode->nodeName, + [ + 'a', + 'audio', + 'del', + 'ins', + 'map', + 'noscript', + 'video', + ], + true ) ) || @@ -1479,8 +1461,23 @@ protected function getNextSiblingOfTypeDOMElement(\DOMNode $node) { do { /** @var \DOMNode|null $node - false-positive error from phpstan */ - $node = $node->nextSibling; - } while (!($node === null || $node instanceof \DOMElement)); + $nodeTmp = $node->nextSibling; + + if ($nodeTmp instanceof \DOMText) { + if ( + \trim($nodeTmp->textContent) !== '' + && + \strpos($nodeTmp->textContent, '<') === false + ) { + $node = $nodeTmp; + } else { + $node = $nodeTmp ? $nodeTmp->nextSibling : null; + } + } else { + $node = $nodeTmp; + } + + } while (!($node === null || $node instanceof \DOMElement || $node instanceof \DOMText)); return $node; } diff --git a/tests/HtmlMinTest.php b/tests/HtmlMinTest.php index b361bf3..e7e156b 100644 --- a/tests/HtmlMinTest.php +++ b/tests/HtmlMinTest.php @@ -811,6 +811,26 @@ public function testKeepPTagIfNeeded() static::assertSame($expected, $result); } + public function testKeepPTagIfNeeded2() + { + $html = ' +
+

+ First Paragraph +

+ Loose Text +

Another Paragraph

+
+ '; + + $htmlMin = new voku\helper\HtmlMin(); + $result = $htmlMin->minify($html); + + $expected = '

First Paragraph

Loose Text

Another Paragraph

'; + + static::assertSame($expected, $result); + } + public function testVueJsExample() { // init