Skip to content

Commit 9705382

Browse files
Cleanup special case escaping.
1 parent 51e1a6d commit 9705382

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

tdom/transformer.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import functools
99
import inspect
1010

11-
from markupsafe import Markup
12-
1311
from .format import format_interpolation
1412
from .parser import TemplateParser, HTMLAttributesDict
1513
from .tnodes import (
@@ -29,7 +27,8 @@
2927
RCDATA_CONTENT_ELEMENTS,
3028
)
3129
from .escaping import (
32-
escape_html_content_in_tag as default_escape_html_content_in_tag,
30+
escape_html_script as default_escape_html_script,
31+
escape_html_style as default_escape_html_style,
3332
escape_html_text as default_escape_html_text,
3433
escape_html_comment as default_escape_html_comment,
3534
)
@@ -110,7 +109,8 @@ def interpolate_comment(
110109
assert container_tag == "<!--"
111110
bf.append(
112111
render_api.escape_html_comment(
113-
resolve_text_without_recursion(template, container_tag, comment_t)
112+
resolve_text_without_recursion(template, container_tag, comment_t),
113+
allow_markup=True,
114114
)
115115
)
116116

@@ -292,12 +292,25 @@ def interpolate_raw_texts_from_template(
292292
@NOTE: This interpolator expects a Template.
293293
"""
294294
container_tag, content_t = t.cast(InterpolateRawTextsFromTemplateInfo, ip_info)
295-
bf.append(
296-
render_api.escape_html_content_in_tag(
297-
container_tag,
298-
resolve_text_without_recursion(template, container_tag, content_t),
295+
content = resolve_text_without_recursion(template, container_tag, content_t)
296+
if container_tag == "script":
297+
bf.append(
298+
render_api.escape_html_script(
299+
container_tag,
300+
content,
301+
allow_markup=True,
302+
)
299303
)
300-
)
304+
elif container_tag == "style":
305+
bf.append(
306+
render_api.escape_html_style(
307+
container_tag,
308+
content,
309+
allow_markup=True,
310+
)
311+
)
312+
else:
313+
raise NotImplementedError(f"Container tag {container_tag} is not supported.")
301314

302315

303316
type InterpolateEscapableRawTextsFromTemplateInfo = tuple[str, Template]
@@ -318,9 +331,11 @@ def interpolate_escapable_raw_texts_from_template(
318331
container_tag, content_t = t.cast(
319332
InterpolateEscapableRawTextsFromTemplateInfo, ip_info
320333
)
334+
assert container_tag == "title" or container_tag == "textarea"
321335
bf.append(
322336
render_api.escape_html_text(
323-
resolve_text_without_recursion(template, container_tag, content_t)
337+
resolve_text_without_recursion(template, container_tag, content_t),
338+
allow_markup=True,
324339
)
325340
)
326341

@@ -689,13 +704,10 @@ def resolve_text_without_recursion(
689704
value = template.interpolations[i_index].value
690705
if value is None:
691706
return None
692-
elif (
693-
type(value) is str
694-
): # type() check to avoid subclasses, probably something smarter here
707+
elif isinstance(value, str):
708+
# @DESIGN: Markup() must be used explicitly if you want __html__ supported.
695709
return value
696-
elif hasattr(value, "__html__"):
697-
return Markup(value.__html__())
698-
elif not isinstance(value, str) and isinstance(value, (Template, Iterable)):
710+
elif isinstance(value, (Template, Iterable)):
699711
raise ValueError(
700712
f"Recursive includes are not supported within {container_tag}"
701713
)
@@ -798,7 +810,9 @@ class RenderService:
798810

799811
escape_html_comment: Callable = default_escape_html_comment
800812

801-
escape_html_content_in_tag: Callable = default_escape_html_content_in_tag
813+
escape_html_script: Callable = default_escape_html_script
814+
815+
escape_html_style: Callable = default_escape_html_style
802816

803817
def get_system(self, **kwargs: object):
804818
# @DESIGN: Maybe inject more here?

0 commit comments

Comments
 (0)