|
11 | 11 | import tempfile |
12 | 12 | import urllib.parse |
13 | 13 | import webbrowser |
| 14 | +from collections import UserString |
14 | 15 | from copy import copy, deepcopy |
15 | 16 | from pathlib import Path |
16 | 17 | from typing import ( |
@@ -1240,7 +1241,7 @@ def _static_extract_serialized_html_deps( |
1240 | 1241 | # ============================================================================= |
1241 | 1242 |
|
1242 | 1243 |
|
1243 | | -class HTML: |
| 1244 | +class HTML(UserString): |
1244 | 1245 | """ |
1245 | 1246 | Mark a string as raw HTML. This will prevent the string from being escaped when |
1246 | 1247 | rendered inside an HTML tag. |
@@ -1271,12 +1272,14 @@ def __str__(self) -> str: |
1271 | 1272 |
|
1272 | 1273 | # HTML() + HTML() should return HTML() |
1273 | 1274 | # 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: |
1276 | 1277 | if isinstance(other, HTML): |
| 1278 | + # HTML strings should be concatenated without escaping |
1277 | 1279 | return HTML(self.as_string() + other.as_string()) |
| 1280 | + |
1278 | 1281 | # 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)))) |
1280 | 1283 |
|
1281 | 1284 | def __eq__(self, x: object) -> bool: |
1282 | 1285 | # Set `x` first so that it can dispatch to the other object's __eq__ method as we've upgraded to `str` |
|
0 commit comments