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

JSX parser should support XML-style comments #21601

Closed
crizzis opened this issue Jun 2, 2021 · 5 comments
Closed

JSX parser should support XML-style comments #21601

crizzis opened this issue Jun 2, 2021 · 5 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@crizzis
Copy link

crizzis commented Jun 2, 2021

It would be a nice feature if the JSX parser could recognize XML-style comments (<!-- -->), either by ignoring them completely or by passing the comments through to the output HTML.

Right now, using XML-style comments results in compilation errors.

@eps1lon eps1lon added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jun 2, 2021
@Jarred-Sumner
Copy link
Contributor

Fun fact: HTML-style comments are valid ECMAScript comments when the host is a web browser (excluding modules)

From the spec: https://tc39.es/ecma262/#sec-html-like-comments (you might have to search "HTML" for the section to appear)

@kevduc
Copy link

kevduc commented Jun 4, 2021

I'm currently looking for a way to add HTML comments to my page (for attributions and other comments potentially useful for whoever reads the HTML source) but as @crizzis said, it's not supported by the JSX parser.

I've found some workarounds here and there, like Stack Overflow: How to render a HTML comment in React?, but these solutions have a lot of boilerplate just for an HTML comment, and I don't think they are easily readable/understandable by someone looking at the code.
So far the most straight forward/concise way of having an HTML comment I've found is with this package: react-jsx-html-comments

TLDR: I think it would be useful to have HTML comments supported in JSX out of the box to avoid the hassle of finding a workaround/package just to write a comment.

@JuniorTour
Copy link

JuniorTour commented Nov 29, 2021

I need this feature too.

Current workaround from stackoverflow has many disadvantages.

We have to:

  1. Use DOM operation (Not work for SSR).
  2. Use dangerouslySetInnerHTML and get <div><!-- comment --></div>, NOT <!-- comment --> without <div>

@kartonrad
Copy link

The thing is, that things like Internet Explorer Comment conditionals can't end within a tag like this <div><!-- comment --></div> without causing errors.
Sometimes this is just neccesary to do, espacially in frameworks (e.g next.js) that force you to do EVERYTHING in jsx

@gaearon
Copy link
Collaborator

gaearon commented Mar 30, 2022

We're not actively changing JSX at the moment. But if you'd like to propose a change, https://github.com/facebook/jsx would be the right place.

@gaearon gaearon closed this as completed Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

7 participants