Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minification mangling html with conditional comments #102

Closed
collinsauve opened this issue Nov 28, 2019 · 5 comments
Closed

Minification mangling html with conditional comments #102

collinsauve opened this issue Nov 28, 2019 · 5 comments

Comments

@collinsauve
Copy link

Looks like in some cases when a the opening and closing tags are in different conditional comments, this library can rewrite the html structure.

This issue was originally discovered in a far more complex html document, but I have been able to create a minimally reproducible test here:

Given this source html:

<html>
<body>

<div><table><tr><td>

    <!--[if mso]>
    <div>
        test1
    <![endif]-->
    test2
    <!--[if mso]>
    </div>
    <![endif]-->

    <!--[if mso]>
    <div>
        test1
    <![endif]-->
    test2
    <!--[if mso]>
    </div>
    <![endif]-->
</td></tr></table></div>

</body>
</html>

Running

var settings = new HtmlMinificationSettings
{
    RemoveOptionalEndTags = false,
};
var htmlMinifier = new HtmlMinifier(settings);
var minified = htmlMinifier.Minify(html2);

Results in:

<html><body><div><table><tr><td><!--[if mso]><div>test1 <![endif]--> test2 <!--[if mso]></td></tr></table></div><![endif]--> <!--[if mso]><div>test1 <![endif]--> test2 <!--[if mso]></div><![endif]--></body></html>

Notice the </td></tr></table></div> closing tags have been moved inside one of the conditionals. The source html is a valid structure with or without the conditionals, but is not a valid structure after running minifier.

@Taritsyn
Copy link
Owner

Hello, Collin!

A rather complicated structure of HTML document is used here: start and end tags are placed in different conditional comments. Such code can break many HTML minifiers based on parsers (for example, this code is not correctly processed by the Juriy Zaytsev's HTML Minifier, but it causes other type of error).

This error will be very difficult to fix. At the moment, I recommend two ways to solve this problem:

  1. Simplify a HTML code.
  2. Wrap the fragment of problem code in a ignoring comment tag.

@collinsauve
Copy link
Author

Hi Andrey.

Thanks for the tip about the ignoring comment tag - I will use that as a work-around until I can find a way to simplify

@jonyeezs
Copy link

We use a large amount of conditional comments to manage outlook and IE. The workaround of ignoring comment is not scalable.

I believe there's a bug somewhere if the engine is parsing tags outside of comments into comments.

@uhlirradek
Copy link

+1 to this, same problem as @jonyeezs have - anyone going to fix this?

@Taritsyn
Copy link
Owner

Hello!

This error has been fixed in version 2.13.6.

@Taritsyn Taritsyn closed this as completed Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants