Conversation
When the inline-script feature is enabled, esbuild escapes </script in string/template literals so the output can be embedded in an inline <script> tag. The HTML parser's script data state machine also treats <!-- and --> specially: <!-- enters the "script data escaped" state, where a later <script sequence enters the "script data double escaped" state and prevents </script> from ending the element. Escape <!-- and --> as \x3c/\x3e in string content to keep it inert, mirroring Terser's inline_script option. Tagged template literals (e.g. String.raw`...`) emit raw content verbatim, so the printer cannot escape them in place; lower such templates into __template calls first, extending the existing </script lowering to also cover <!-- and -->. Fixes evanw#4494
Author
|
Following up here. I realise escaping behaviour is a deliberate design area in esbuild, so if this isn't a change you want I'm happy to close it — mainly looking for a yes/no. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4494.
When the
inline-scriptfeature is enabled (the default), esbuild escapes</scriptin string/template literals so output can be embedded in an inline<script>tag. The HTML parser's script data state machine also treats<!--and-->specially:<!--enters the "script data escaped" state<scriptin that state enters the "script data double escaped" state, where</script>no longer ends the element-->exits the escaped stateSo a string like
"<!-- <script>"defeats the existing</scriptescaping. This PR escapes<!--as\x3c!--and-->as--\x3ein string/template content, mirroring Terser'sinline_scriptoption, which the issue references:<script>(without a slash) is left alone, matching Terser: it is only dangerous after a<!--, which is now escaped, and in the normal script-data state<scriptdoes nothing special.Tagged template literals such as
String.raw\...`emit raw content verbatim, so the printer cannot escape them in place. The existing</scriptlowering (which turns such templates into__templatecalls) is extended to also cover, letting the same escapes apply to the raw content while preserving the runtime value (the\x3c/\x3eescapes are source-level only; the string value stays, same as the existing</script` handling).Both the printer escaping and the tagged-template lowering are gated on
compat.InlineScriptbeing supported, consistent with the existing</scriptbehavior.go build ./...andgo test ./...pass;gofmtandgo vetclean on the touched files.