html-minifier icon indicating copy to clipboard operation
html-minifier copied to clipboard

Advanced optimisation does not respect required inline white spaces

Open graphidev opened this issue 8 years ago • 3 comments

Hi,

Your minifier is one of the most awesome I could find ! Thanks a lot for this job ! However, I had a little trouble using advanced optimisation option.

For that kind of html :

<p>Here is some <strong>bold</strong> text</p>

It will output :

<p>Here is some<strong>bold</strong>text</p>

I also made a test with more inline tags

<p>
    And here is some <strong>bold</strong> with <b>b</b> tag.
    Hey, what about <em>emphasis</em> and <i>italic</i> ?
    And also <u>underline</u> string ?!
</p>

Which gave me this :

<p>And here is some<strong>bold</strong> with <b>b</b> tag.
Hey, what about <em>emphasis</em> and <i>italic</i> ?
And also <u>underline</u>string ?!</p>

Considering that the standard optimisation level works fine, I fixed it using this code I'm not really proud of :

$minifier = new \zz\Html\HTMLMinify($html, array(
    'doctype'               => \zz\Html\HTMLMinify::DOCTYPE_HTML5,
    'optimizationLevel'     => \zz\Html\HTMLMinify::OPTIMIZATION_ADVANCED,
));
$minified = $minifier->process();
$lines = preg_split('/(\r\n|\n)/', $minified);

$minified = '';
$forceNewLine = false;
foreach($lines as $line) {

    // For now an then add new line characters after this one
    if(mb_strpos($line, '<pre>') !== false)
        $forceNewLine = true;

    // Since here, stop adding new line characters
    if(mb_strpos($line, '</pre>') !== false)
        $forceNewLine = false;

    $minified .= $line;

    if($forceNewLine)
        $minified .= "\r\n";

}

$html = $minified;

I would prefer using the package dedicated option, is there a way to fix it in your code ?

graphidev avatar Mar 26 '16 18:03 graphidev

This issue also impact for me.

idhamperdameian avatar Jun 27 '16 03:06 idhamperdameian

Same here. Any fixes available? Otherwise it's useless...

ghost avatar Feb 13 '17 14:02 ghost

I have forked the project and fixed main issues at https://github.com/raivisdejus/html-minifier If you see any new issues, please let me know

raivisdejus avatar Jun 29 '18 05:06 raivisdejus