Sublime Forum

Nested html`` in javascript breaks syntax highlighting

#1

i’m on build 4200. in javascript highlighting, template literals tagged with a tag called “html” get special treatment, in that their contents get highlighted as HTML.

however, this breaks horribly when an html`` literal is nested inside another:

2026-02-18-220321_555x263_scrot

(the entire rest of the file is then highlighted incorrectly)

the inner template string doesn’t even need to be tagged html`` for this to happen, any template literal inside html`` will break it.

1 Like

#2

This is a limitation per design, to avoid infinite recursive import loops with nested syntaxes.

A possible fix was probosed via PR4318 but abandoned as it significantly increases overall complexity of involved syntax definitions.

I’d say: Just don’t do that.

0 Likes

#3

I appreciate that getting arbitrarily-nesting syntax highlighting working here is complex, but is there any chance we can at least avoid completely breaking highlighting for the rest of the file? whether automatically by detecting when this happens or at least by providing some mechanism to opt out of this special behavior for html``.

I don’t always have complete control over the coding conventions used in the files I open, so “just don’t do that” is not exactly actionable. the current behavior produces obviously broken highlighting for obviously syntactically valid javascript, which seems like an odd default.

1 Like

#4

Not the way it is and needs to be implemented, and withoout completely removing syntax highlighting within tagged template strings again.

0 Likes

#5

I just ran into this too, nesting mixed html and svg tags, and sometimes encountering issues with un-nested syntax highlighting too, on build 4200:

41%402x

I’d also love if there was support for the same HTML highlighting for svg. An svg literal creates elements in the SVG namespace, and many libraries such as Observable’s Hypertext Literal and the popular Lit web component library use it.

0 Likes

#6

This issue is not directly related to nested tagged template strings, but also fixed by https://github.com/sublimehq/Packages/pull/4566.

The PR was merged and should be part of one of the next dev builds.

1 Like

#7

That’s fantastic, thank you. Do you think it’d be possible to make svg``highlight the same as html``? i think that’s all i was asking about in the second part of my question, since the only real difference between the two is what namespace the elements are created in; they both accept the exact same syntax.

0 Likes

#8
1 Like

#9

:birthday::trumpet::sparkles::pray:

Woohoo!

0 Likes