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

Enable attributes without values #165

Open
baloe opened this issue Aug 10, 2022 · 4 comments
Open

Enable attributes without values #165

baloe opened this issue Aug 10, 2022 · 4 comments

Comments

@baloe
Copy link

baloe commented Aug 10, 2022

It seems acceptable to also have attributes without values, e.g. the controls attribute for the video tag:
https://www.w3schools.com/html/html5_video.asp

<video controls> ...

Is this possible with dominate? If so I am unable to figure out how.
If not, how about adding a rule that attribute=None is rendered as an attribute without a value?
Right now attribute=None seems to be equivalent to attribute=False which simply prevents the attribute from being rendered at all.

@golightlyb
Copy link
Contributor

Note that

<video controls>

is equivalent to

<video controls="controls">

@Knio
Copy link
Owner

Knio commented Aug 20, 2022

Other users had requested that attribute=None means it should be interpreted the same as not present at all (same as deleted). See previous issues.

As above, attribute=True results in rendering as attribute="attribute", and the HTML spec enforces that to mean the same thing a present with no value.

@Knio Knio closed this as completed Aug 20, 2022
@bfricka
Copy link

bfricka commented Jan 3, 2025

@Knio

Boolean attributes represent their "true" state when present in an element, regardless of whether they have a value. The HTML Living Standard specifies:

If a boolean attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

This means both <option selected> and <option selected="selected"> are valid.

The minimized form (<option selected>) is valid and concise. It is the shortest way to express a boolean attribute while preserving its meaning. Browsers interpret both forms equivalently. In my experience (12+ years) most developers and tools favor the minimized form for its simplicity. The selected="selected" style is more verbose and sometimes perceived as redundant.

Adjusting dominate to support the minimized form, though not strictly necessary, could make its output more idiomatic.

You could support a global setting that affects the default rendering behavior (dominate.util.concise_attrs = True). You could have an option in rendering html.render(consise_attrs=True). You could also make it the default and have an inverse modifier for those who like verbose attrs.

Overall, it's not a big deal, but it's also an easy thing to fix, and doing so would align with how most HTML is written and rendered.

@Knio
Copy link
Owner

Knio commented Jan 7, 2025

I was previously against this because I thought it was not actually part of the spec, also it seemed like the OP was just trying to get it to work at all, and not a bug about particular styles.

In any case, since https://html.spec.whatwg.org/multipage/syntax.html#attributes-2 and https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes explicitly allow this, I'm happy to change the behavior

@Knio Knio reopened this Jan 7, 2025
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