tdewolff / minify

Go minifiers for web formats
https://go.tacodewolff.nl/minify
MIT License
3.75k stars 221 forks source link

html minifier incorrectly strips type attribute from button elements #771

Closed LixenWraith closed 1 day ago

LixenWraith commented 5 days ago

Input HTML: <button type="submit" class="button">Send Message</button>

Current Output: <button class=button>Send Message</button>

Expected Output: <button type=submit class=button>Send Message</button>

Causing below to fail: querySelector('button[type="submit"]')

LixenWraith commented 4 days ago

I have ways around it, but would be nice to have it support this best practice approach. Alternative approaches of using attributes such as id, different "submit-button" class, and data attribute work as workaround.

tdewolff commented 2 days ago

I understand that this is is causing confusion, but I'm not entirely sure about the solution. The default value of <button type is "submit", so the HTML minifier is correct in removing it. Instead, I would argue that in JS you want to select all buttons of the submit type, for which a correct selector would be querySelector('button[type="submit"], button:not([type])')

LixenWraith commented 1 day ago

Thanks for the response. I implemented with data attribute that in the end saved few more bytes. I'll keep this behavior in mind in the future use. It is a valid minification though may conflict with some use cases. As long as it remains consistent in the future updates, there's no issue and it's an efficient approach.