preact-render-to-string icon indicating copy to clipboard operation
preact-render-to-string copied to clipboard

Add HTML comments support

Open dirkjonker opened this issue 2 years ago • 1 comments

Preact doesn't really support HTML comments, but it is possible to (mis)use it to generate HTML comments anyway, by creating an element with a name that starts with !-- and ends with --, making sure it does not have any attributes. For example, this is being used in Fresh (see denoland/fresh#838) to enable islands. The closing tag of such a "comment" tag however generates invalid HTML, i.e.

h("!--foo--", null, children)

will generate

<!--foo--> ... </!--foo-->

The closing tag: </!-- is invalid HTML and might make it hard for some clients to deal with. Chrome/Firefox have a long track record of magically fixing broken HTML and they turn this into:

<!--!--foo---->

If you plan to programmatically read data from comments (which is a perfectly valid thing to do), you will get some unexpected results as your data will now be prefixed with !-- and suffixed with --.

This change adds "support" for HTML comments by at least not generating invalid HTML.

I'm not 100% sure what kind of change this is, I would think a patch since it's quite the edge case and it fixes rendering invalid HTML in this edge case. But please do correct me if I'm wrong.

dirkjonker avatar Oct 14 '22 10:10 dirkjonker

🦋 Changeset detected

Latest commit: dc05abe2722840f5f271063808b6f9dec9af4214

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
preact-render-to-string Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Oct 14 '22 10:10 changeset-bot[bot]

That makes sense. I'm not using comments myself directly but Fresh uses it as a means to get islands working. Currently that doesn't give any real issues, only the w3 validator gives some warnings about garbage tags.

dirkjonker avatar Oct 26 '22 14:10 dirkjonker