observablehq / feedback

Customer submitted bugs and feature requests
42 stars 3 forks source link

How to prevent the weird escaping of backquotes in HTML cells? #535

Closed galopin closed 11 months ago

galopin commented 1 year ago

Describe the bug

It seems that backquotes are automatically escaped in HTML cells ⁉️
I don't get the rationale behind this behavior…

To Reproduce

  1. Insert a new HTML cell as follows:

\`\`

<oops>``</oops>
  1. Insert another cell as follows:

``

html`<oops>\`\``
  1. Compare the output…

  2. It gets even worse when querying the text content of the above nodes:

Array(2) [ 0: "\\`\\`" 1: "``" ]

Array.from(document.querySelectorAll("oops")).map((elt) => elt.textContent)

Expected behavior

I expect the output to be the same whether I use a JavaScript or HTML cell, e.g. ``

mootari commented 1 year ago

Output comparison:

image

Appears to be limited to uninterpolated backticks in HTML cells.

mootari commented 1 year ago

I believe the following is happening here:

  1. The HTML cell compiles to
    htl.html`<i>\`\`</i>`
  2. When passed to a template tag function, JS treats the sequence \` as two separate characters.
galopin commented 1 year ago

That's it. A clumsy but temporarily effective work-around would be to use a Markdown cell to prevent this from happening… Thank you! 🍺

lionel-rowe commented 1 year ago

This is a general limitation of tagged template literals that use the raw value of the string.

The only way to interpolate a backtick into a raw tagged template literal is to pass it as an interpolated value:

`a\`b` // a`b
String.raw`a\`b` // a\`b (literal backslash)
String.raw`a${'`'}b` // a`b

It's true that library maintainers can implement their own logic to transform \` into ` within raw string segments, but that's not the default behavior.

mbostock commented 1 year ago

This is not a bug in HTL but rather in the Observable compiler. Thank you for the report; I will file an appropriate internal bug and transfer this to the feedback repository.

mbostock commented 11 months ago

Apologies for the delay here. We have a fix for this now and it should be released next week.

galopin commented 11 months ago

Excellent!

mbostock commented 11 months ago

Confirmed that this is now fixed. Thank you for the report!

Screenshot 2023-08-07 at 1 02 48 PM