Skip to content

Escape quotes and 8-bit CSI in HTML.format and ANSI.format - #2148

Merged
jonathanslenders merged 1 commit into
prompt-toolkit:mainfrom
neeraj1235476:formatted-text-escape-helpers
Oct 20, 2026
Merged

jonathanslenders merged 1 commit into
prompt-toolkit:mainfrom
neeraj1235476:formatted-text-escape-helpers

Conversation

@neeraj1235476

Copy link
Copy Markdown
Contributor

The escape helpers behind HTML.format/% and ANSI.format/% miss characters that their own parsers treat as markup, so an interpolated value can still change the output:

  • html_escape leaves ' alone: HTML("<style fg='{}'>x</style>").format("red' bg='blue") sets a background, and a plain apostrophe in a single-quoted attribute raises ExpatError
  • ansi_escape leaves \x9b alone, which the ANSI parser accepts as CSI: ANSI("{}").format("\x9b31mred") recolors the text
  • it also leaves \001/\002 alone, which wraps the value in a [ZeroWidthEscape] fragment that the renderer writes to the terminal unprocessed
    Both helpers now neutralize those characters the same way they already handle " and \x1b. Existing interpolation tests pass unchanged, since expat decodes &#x27; back to '.

@jonathanslenders
jonathanslenders merged commit 6c7fbc1 into prompt-toolkit:main Oct 20, 2026
@jonathanslenders

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants