fix: nested embeds (JS in HTML in JS)#6038
Conversation
bff20f0 to
d42faff
Compare
|
Sorry this was overlooked @thorn0 Can you add an entry to the CHANGELOG? |
|
FTR this doesn't work (not saying you should fix it) Prettier pr-6038 --parser babylonInput: const html = /* HTML */ `<script>const html = /* HTML */ \`<script></script>\`;</script>`;Output: SyntaxError: Unexpected closing tag "script". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags (1:53)
> 1 | <script>const html = /* HTML */ `<script></script>`;</script>
| ^I guess I'm wondering how far we need to go to fix this. Is this blocking you ? Do you have "JS in HTML in JS" ? |
|
I have a bit of "JS in HTML in TS". Not that it's blocking. I'd be happier if I got "JS in TS" though... As for your snippet, there is nothing to fix there. The result is expected and correct. The first Prettier pr-6038 --parser htmlInput: <script>const html = /* HTML */ `<script></script>`;</script>Output: SyntaxError: Unexpected closing tag "script". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags (1:53)
> 1 | <script>const html = /* HTML */ `<script></script>`;</script>
| ^
Prettier pr-6038 --parser babylonInput: const html = /* HTML */ `<script>const html = /* HTML */ \`<script><\\/script>\`;</script>`;Output: const html = /* HTML */ `
<script>
const html = /* HTML */ \`
<script></script>
\`;
</script>
`;As for the CHANGELOG, I thought minor bugs aren't reflected there, but okay, I'll do it. |
Thanks! My bad
Yikes, would you open a new issue? (or fix it here if you're interested)
Yeah they are.. we only leave out changes that aren't visible to users |
I'll fix it here. |
|
@thorn0 I know we already handle something similar with GraphQL |
|
Maybe more relevant edge cases to test: https://stackoverflow.com/questions/41297404/is-it-possible-to-correctly-escape-arbitrary-script-tag-contents |
|
@lydell My two takeaways from that SO question:
As for number one, since it doesn't exist, we probably aren't going to support it. :) Also this PR is specifically about JS in As for number two, I never saw unescaped HTML comments in JS cause trouble. I'd be really curious to look at such a case. Looks like unneeded overcautiousness to me. |
Currently, if JS code embedded in HTML (via
<script>) embedded in JS (via a template literal) contains template literals, the inner JS isn't formatted.(playground)
This fix has been extracted from #5993.
✨Try the playground for this PR✨