Skip to content

Commit 60e21d0

Browse files
committed
Follow up to #1868: prevent escaping of HTML inside message content strings
1 parent f2e774c commit 60e21d0

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

shiny/ui/_chat.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from weakref import WeakValueDictionary
2020

21-
from htmltools import HTML, Tag, TagAttrValue, TagChild, TagList, css
21+
from htmltools import HTML, Tag, TagAttrValue, TagChild, TagList, css, RenderedHTML
2222

2323
from .. import _utils, reactive
2424
from .._deprecated import warn_deprecated
@@ -1450,7 +1450,12 @@ def chat_ui(
14501450
if "role" in x:
14511451
role = x["role"]
14521452

1453-
ui = TagList(content).render()
1453+
# `content` is most likely a string, so avoid overhead in that case
1454+
# (it's also important that we *don't escape HTML* here).
1455+
if isinstance(content, str):
1456+
ui: RenderedHTML = {"html": content, "dependencies": []}
1457+
else:
1458+
ui = TagList(content).render()
14541459

14551460
if role == "user":
14561461
tag_name = "shiny-user-message"

shiny/ui/_markdown_stream.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from contextlib import asynccontextmanager
22
from typing import AsyncIterable, Iterable, Literal, Union
33

4-
from htmltools import TagChild, TagList, css
4+
from htmltools import RenderedHTML, TagChild, TagList, css
55

66
from .. import _utils, reactive
77
from .._deprecated import warn_deprecated
@@ -353,7 +353,13 @@ def output_markdown_stream(
353353
height
354354
The height of the UI element.
355355
"""
356-
ui = TagList(content).render()
356+
357+
# `content` is most likely a string, so avoid overhead in that case
358+
# (it's also important that we *don't escape HTML* here).
359+
if isinstance(content, str):
360+
ui: RenderedHTML = {"html": content, "dependencies": []}
361+
else:
362+
ui = TagList(content).render()
357363

358364
return Tag(
359365
"shiny-markdown-stream",

0 commit comments

Comments
 (0)