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

Rule proposal: prevent some DOM elements from having children or dangerouslySetInnerHTML #709

Closed
lencioni opened this issue Jul 26, 2016 · 11 comments · Fixed by singapore/lint-condo#240

Comments

@lencioni
Copy link
Collaborator

lencioni commented Jul 26, 2016

There are some HTML elements that are normally self-closing (e.g. img, br, hr). If you try to give these children, React will give you a warning like

Invariant Violation: img is a void element tag and must neither have children nor use dangerouslySetInnerHTML.

It would be nice to have a rule enforcing this.

Bad:

<img src="path/to/img.jpg">
  Children
</img>
<img src="path/to/img.jpg" dangerouslySetInnerHTML={{ __html: 'HTML' }} />
<img src="path/to/img.jpg" children="Children" />

Good:

<img src="path/to/img.jpg" />

The full list that React uses can be found:

Which comes out as:

  • area
  • base
  • br
  • col
  • embed
  • hr
  • img
  • input
  • keygen
  • link
  • menuitem
  • meta
  • param
  • source
  • track
  • wbr
@ljharb
Copy link
Member

ljharb commented Jul 26, 2016

This seems like an excellent idea, and one that's mostly enforceable with both jsx and React.createElement (it wouldn't be enforceable with spreaded props that contain a "children" key, but I think that's OK)

@lencioni
Copy link
Collaborator Author

Yeah, I think the 99% solution is great.

@lencioni
Copy link
Collaborator Author

What should this be called? void-element-tag-no-children?

@ljharb
Copy link
Member

ljharb commented Jul 26, 2016

Since it's just for DOM elements, what about self-closing-dom-elements-no-children? "void element" isn't a term I'm familiar with, but "self-closing" is.

@lencioni
Copy link
Collaborator Author

I thought about that, but I think self-closing may refer to elements that are also able to accept children but happen to be rendered without children, such as div. I'm not totally sold on the void naming either, but it is the language used by React and the HTML spec: https://www.w3.org/TR/html-markup/syntax.html#syntax-elements

@ljharb
Copy link
Member

ljharb commented Jul 26, 2016

In that case, maybe void-dom-elements-no-children? "tag" isn't appropriate if it's not just jsx, and i like including "dom" to point out that it's not for custom components.

@lencioni
Copy link
Collaborator Author

That works for me!

@lencioni lencioni self-assigned this Jan 29, 2017
lencioni added a commit that referenced this issue Jan 29, 2017
There are some HTML elements that are only self-closing (e.g. `img`,
`br`, `hr`). These are collectively known as void DOM elements. If you
try to give these children, React will give you a warning like:

> Invariant Violation: img is a void element tag and must neither have
> `children` nor use `dangerouslySetInnerHTML`.

This rule prevents this from happening.

Since this is already a warning in React, we should add it to the
recommended configuration in our next major release.

Fixes #709
@kepi0809
Copy link

why is wbr on this list??

@ljharb
Copy link
Member

ljharb commented Nov 21, 2019

@kepi0809 because <wbr /> is a self-closing element?

@kepi0809
Copy link

kepi0809 commented Nov 21, 2019

@kepi0809 because <wbr /> is a self-closing element?

@ljharb https://developer.mozilla.org/en-US/docs/Web/HTML/Element/wbr it's not?

@ljharb
Copy link
Member

ljharb commented Nov 21, 2019

@kepi0809 HTML5 permits self-closing elements to omit the closing slash; anything is a self-closing element if it can not contain children. See "Tag omission" on that page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants