Skip to content

Commit

Permalink
[+]: remove content before "<!doctype.*>", otherwise DOMDocument cann…
Browse files Browse the repository at this point in the history
…ot handle the input

-> fix for issue #55
  • Loading branch information
voku committed Aug 11, 2020
1 parent fa7e00e commit 4f70058
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Changelog 4.4.8 (2020-08-11)

- remove content before "<!doctype.*>", otherwise DOMDocument cannot handle the input
- use a new version of "voku/simple_html_dom" (4.7.22)

# Changelog 4.4.7 (2020-08-11)

- use a new version of "voku/simple_html_dom" (4.7.21)
Expand Down
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.7.21",
"voku/simple_html_dom": "~4.7.23",
"ext-dom": "*"
},
"require-dev": {
Expand Down
14 changes: 13 additions & 1 deletion src/voku/helper/HtmlMin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1570,11 +1570,23 @@ private function minifyHtmlDom($html, $multiDecodeNewHtmlEntity): string
$dom->getDocument()->preserveWhiteSpace = false; // remove redundant white space
$dom->getDocument()->formatOutput = false; // do not formats output with indentation

// Remove content before <!DOCTYPE.*> because otherwise the DOMDocument can not handle the input.
if (\stripos($html, '<!DOCTYPE') !== false) {
/** @noinspection NestedPositiveIfStatementsInspection */
if (
\preg_match('/(^.*?)<!(?:DOCTYPE)(?: [^>]*)?>/sui', $html, $matches_before_doctype)
&&
\trim($matches_before_doctype[1])
) {
$html = \str_replace($matches_before_doctype[1], '', $html);
}
}

// load dom
/** @noinspection UnusedFunctionResultInspection */
$dom->loadHtml($html);

$this->withDocType = (\stripos(\ltrim($html), '<!DOCTYPE') === 0);
$this->withDocType = (\stripos($html, '<!DOCTYPE') === 0);

$doctypeStr = $this->getDoctype($dom->getDocument());

Expand Down
24 changes: 24 additions & 0 deletions tests/HtmlMinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,30 @@ public function testBrokenHtmlExample()
static::assertSame($expected, $htmlMin->minify($html));
}

public function testContentBeforeDoctypeExample()
{
// init
$htmlMin = new HtmlMin();
$htmlMin->useKeepBrokenHtml(true);

$html = '<!-- === BEGIN TOP === --><!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!-->
<html prefix="og: http://ogp.me/ns#" lang="ru">
<!--<![endif]-->
<head>
<!-- Title -->
<title>test</title>
</head>
<body>lall</body></html>
';

$expected = '<!DOCTYPE html><!--[if IE 8]> <html lang="en" class="ie8"> <![endif]--><!--[if IE 9]> <html lang="en" class="ie9"> <![endif]--><!--[if !IE]><!--><html prefix="og: http://ogp.me/ns#" lang=ru> <!--<![endif]--> <head><title>test</title> <body>lall';

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

public function testDoNotCompressTag()
{
$minifier = new HtmlMin();
Expand Down

0 comments on commit 4f70058

Please sign in to comment.