adamchainz/django-minify-html

minify_html strips empty attributes, which produces an error for django select widgets with blank=True

Closed this issue ยท 6 comments

Python Version

3.9

Django Version

4.0

Description

Hey Adam ๐Ÿ‘‹

Quick FYI I noticed for you (although this is probably more of an unintended feature rather than a bug).

When using minify_html on a Django template with a select widget that can accept a blank value, minify_html will strip the blank option, which means an exception is caught in the field clean method. E.g:

class MyModel(models.Model):
    struct_org_type = models.CharField(
        default="",
        blank=True,
        max_length=255,
        choices=SCHEMA_ORG_CHOICES
    )

Would normally render something like:

<select id="id_struct_org_type" name="struct_org_type">
    <option value="" selected="">---------</option>
    <option value="example_1">Example 1</option>
    <option value="example_2">Example 2</option>
</select>

However, minify_html will strip the empty value="" attribute in the first option. Without this, django won't recognise the input as valid (being null rather than a blank str) if this is the selected option. It produces:

Select a valid choice. --------- is not one of the available choices.

I'm not sure there's any way around this, other than to wrap some conditional logic around should_minify() to prevent its use on views with forms, but thought it worth pointing out.

This seems like a bug in minify-html, I've reported it upstream: #71.

Doesn't seem like there's anything we could do here, none of the configuration values look like they'd prevent this behaviour, so closing this issue.

Can you link to the report so we can stay up to date?

The link is above my message

Got it. Thanks.

If the problem can't be fixed because the upstream library has to be the one to make the fix, this library should at least warn the user in the readme and/or add a workaround in should_minify.
Leaving the bug out there for your users to encounter is cruel. Hunting this bug cost me a full day and most of my sanity.

Everything has bugs all the time, it's impractical for packages to list all the known bugs in dependencies, and it's hard hard to gauge the impact.

Leaving the bug out there for your users to encounter is cruel.

Making entitled comments on free open source projects, provided "without warranty", is cruel.

Feel free to actually try and help instead, by making a PR adding some kind of warning to the documentation.