Skip to content

Commit

Permalink
[+]: fixed issue #28
Browse files Browse the repository at this point in the history
-> via update from "simple_html_dom"
  • Loading branch information
voku committed Feb 28, 2019
1 parent 9cd326d commit 3ea0d35
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php": ">=7.0.0",
"voku/simple_html_dom": "~4.3",
"voku/simple_html_dom": "^4.3.1",
"ext-dom": "*"
},
"require-dev": {
Expand Down
23 changes: 12 additions & 11 deletions src/voku/helper/HtmlMin.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,10 @@ private function domNodeAttributesToString(\DOMNode $node): string

// http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#attributes-0
$omit_quotes = $this->doRemoveOmittedQuotes
&&
$attribute->value !== ''
&&
\preg_match('/["\'=<>` \t\r\n\f]+/', $attribute->value) === 0;
&&
$attribute->value !== ''
&&
\preg_match('/["\'=<>` \t\r\n\f]+/', $attribute->value) === 0;

if (
$this->doOptimizeAttributes
Expand Down Expand Up @@ -1354,6 +1354,7 @@ private function removeWhitespaceAroundTags(SimpleHtmlDom $element)
if (isset(self::$trimWhitespaceFromTags[$element->tag])) {
$node = $element->getNode();

/** @var \DOMNode[] $candidates */
$candidates = [];
if ($node->childNodes->length > 0) {
$candidates[] = $node->firstChild;
Expand All @@ -1367,7 +1368,7 @@ private function removeWhitespaceAroundTags(SimpleHtmlDom $element)
continue;
}

if ($candidate->nodeType === 3) {
if ($candidate->nodeType === \XML_TEXT_NODE) {
$candidate->nodeValue = \preg_replace(self::$regExSpace, ' ', $candidate->nodeValue);
}
}
Expand Down Expand Up @@ -1414,11 +1415,11 @@ public function setDomainsToRemoveHttpPrefixFromAttributes($domainsToRemoveHttpP
*/
private function sumUpWhitespace(HtmlDomParser $dom): HtmlDomParser
{
$textnodes = $dom->find('//text()');
foreach ($textnodes as $textnodeWrapper) {
/* @var $textnode \DOMNode */
$textnode = $textnodeWrapper->getNode();
$xp = $textnode->getNodePath();
$text_nodes = $dom->find('//text()');
foreach ($text_nodes as $text_node_wrapper) {
/* @var $text_node \DOMNode */
$text_node = $text_node_wrapper->getNode();
$xp = $text_node->getNodePath();

$doSkip = false;
foreach (self::$skipTagsForRemoveWhitespace as $pattern) {
Expand All @@ -1432,7 +1433,7 @@ private function sumUpWhitespace(HtmlDomParser $dom): HtmlDomParser
continue;
}

$textnode->nodeValue = \preg_replace(self::$regExSpace, ' ', $textnode->nodeValue);
$text_node->nodeValue = \preg_replace(self::$regExSpace, ' ', $text_node->nodeValue);
}

$dom->getDocument()->normalizeDocument();
Expand Down
19 changes: 19 additions & 0 deletions tests/HtmlMinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,25 @@ class="img-responsive"
static::assertSame($expected, $htmlMin->minify($html));
}

public function testKeepWhitespaceInPreTags()
{
$html = '<pre>
foo
bar
zoo
</pre>';

$expected = '<pre>
foo
bar
zoo
</pre>';

$htmlMin = new voku\helper\HtmlMin();

static::assertSame($expected, $htmlMin->minify($html));
}

public function testVueJsExample()
{
// init
Expand Down

0 comments on commit 3ea0d35

Please sign in to comment.