Skip to content

Commit

Permalink
[+]: fix logic of detecting next sibling dom node
Browse files Browse the repository at this point in the history
-> see isse #50
  • Loading branch information
voku committed Apr 6, 2020
1 parent 030b2bf commit 67d39fb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/voku/helper/HtmlMin.php
Expand Up @@ -874,11 +874,7 @@ private function domNodeClosingTagOptional(\DOMNode $node): bool
)
&&
(
(
$nextSibling === null
&&
$tag_name === 'dd'
)
$nextSibling === null
||
(
$nextSibling instanceof \DOMElement
Expand Down Expand Up @@ -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
)
)
||
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/HtmlMinTest.php
Expand Up @@ -811,6 +811,26 @@ public function testKeepPTagIfNeeded()
static::assertSame($expected, $result);
}

public function testKeepPTagIfNeeded2()
{
$html = '
<div>
<p>
<span>First Paragraph</span>
</p>
Loose Text
<p>Another Paragraph</p>
</div>
';

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

$expected = '<div><p><span>First Paragraph</span> </p> Loose Text <p>Another Paragraph </div>';

static::assertSame($expected, $result);
}

public function testVueJsExample()
{
// init
Expand Down

0 comments on commit 67d39fb

Please sign in to comment.