denoland / denoland/std

feat_req(html): check whether a html attribute is boolean according to the spec

Open
#5,658 0 comments 2 reactions 0 assignees View on GitHub
feedback welcome
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**

I hope the pandora box wasn't opened with #5437, but I was hoping for some utilitary function that'd be able to tell if an html attribute for a given tagname is a boolean attribute or not.

Spec refs:
- https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attribute
- https://html.spec.whatwg.org/#attributes-3

The second link is a table containing a list of tagname/attributes and allowed values which can be used as reference. It is complete enough to also implement other utilities function.

Why the focus on boolean attributes ?
- These can often be misused, because of how boolean attributes are handled in html: a falsy value is when it is omitted, while a truthy value is its presence (including empty value or its name itself)
- e.g. `disabled="false"` is actually treated as `disabled="disabled"` by most browsers, in addition to being technically incorrect
- Such a function could be used for simple linting/formatting/sanitizing/testing tools, helping when generating html templates, etc.
- It makes the scope relatively small for now if std thinks this can be in-scope for the html package

As the spec is well-defined, it wouldn't require much maintenance, while once again abstracting the complexity of the html spec for users.

**Describe the solution you'd like**

Something along:
```ts
isBooleanAttribute("VIDEO", "playsinline") // true
isBooleanAttribute("DIV", "playsinline") // false
```

**Describe alternatives you've considered**

There's [is-boolean-attribute](https://www.npmjs.com/package/is-boolean-attribute) but technically it's not compliant since it doesn't check the tagname

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.