Skip to content

Commit 4e164b1

Browse files
committed
Use UserString as base class for HTML
1 parent 6cde017 commit 4e164b1

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

htmltools/_core.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import tempfile
1212
import urllib.parse
1313
import webbrowser
14+
from collections import UserString
1415
from copy import copy, deepcopy
1516
from pathlib import Path
1617
from typing import (
@@ -1240,7 +1241,7 @@ def _static_extract_serialized_html_deps(
12401241
# =============================================================================
12411242

12421243

1243-
class HTML:
1244+
class HTML(UserString):
12441245
"""
12451246
Mark a string as raw HTML. This will prevent the string from being escaped when
12461247
rendered inside an HTML tag.
@@ -1271,12 +1272,14 @@ def __str__(self) -> str:
12711272

12721273
# HTML() + HTML() should return HTML()
12731274
# HTML() + str should return HTML()
1274-
# str + HTML() should return HTML() # THis is not implemented and hard to catch!
1275-
def __add__(self, other: str | HTML) -> HTML:
1275+
# str + HTML() should return HTML() # This is not implemented and hard to catch!
1276+
def __add__(self, other: object) -> HTML:
12761277
if isinstance(other, HTML):
1278+
# HTML strings should be concatenated without escaping
12771279
return HTML(self.as_string() + other.as_string())
1280+
12781281
# Non-HTML text added to HTML should be escaped before being added
1279-
return HTML(str.__add__(self.as_string(), html_escape(other)))
1282+
return HTML(str.__add__(self.as_string(), html_escape(str(other))))
12801283

12811284
def __eq__(self, x: object) -> bool:
12821285
# Set `x` first so that it can dispatch to the other object's __eq__ method as we've upgraded to `str`

0 commit comments

Comments
 (0)