GuillaumeGomez / minifier-rs

Minifier tool/lib for JS/CSS/JSON files
MIT License
86 stars 16 forks source link

Check following element before removing </li> tag (edge case) #79

Closed sgkoishi closed 2 years ago

sgkoishi commented 2 years ago

Input (modified from MDN)

<p>Apollo astronauts:</p>

<ul>
    <li>Neil Armstrong</li>
    <li>Alan Bean</li>
    <li>Peter Conrad</li>
    <li>Edgar Mitchell</li>
    <li>Alan Shepard</li>
    <script> /* something here */ </script>
</ul>

Actual:

...
    <li>Edgar Mitchell
    <li>Alan Shepard
        <script> /* child of alan now! */ </script>
...

Expected: The last </li> should be kept so that the script is still at the same level as other lis. HTML standard allows both li, script and template inside a list, and

An li element's end tag can be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element.

Found this edge case when dealing with non-standard HTML snippets with no ol or ul: the removal of </li> makes the rest part of content as child of last li 😂

GuillaumeGomez commented 2 years ago

That sounds like quite the corner case. :o

I don't plan to fix this considering how much of a corner case it is but feel free to send a PR. :)

sgkoishi commented 2 years ago

Googled several online minifiers and they all preserve the ending </li> no matter if it can be omitted - but that's kind of regression. A hacky way is to keep </li>s first but add another pipeline later after removing white space, to remove </li> inside </li><li> | </li></ul> | </li></ol> | </li></menu> - sounds weird tho?

GuillaumeGomez commented 2 years ago

That sounds very weird... HTML spec overall is quite the nightmare.

sgkoishi commented 2 years ago

True, give up. Gonna fix my malformed input data magically 😀