@@ -1255,31 +1255,44 @@ class HTML(UserString):
12551255 <div><p>Hello</p></div>
12561256 """
12571257
1258- _html : str
1259-
12601258 def __init__ (self , html : object ) -> None :
12611259 if isinstance (html , HTML ):
12621260 html = html .as_string ()
1263- self . _html = str (html )
1261+ super (). __init__ ( str (html ) )
12641262
12651263 def __str__ (self ) -> str :
12661264 return self .as_string ()
12671265
1268- # This class is a building block for other classes, therefore it should not tagifiable!
1269- # If this method is added, HTML strings are escaped within Shiny and not kept "as is"
1266+ # DEV NOTE: 2024/09 -
1267+ # This class is a building block for other classes, therefore it should not
1268+ # tagifiable! If this method is added, HTML strings are escaped within Shiny and
1269+ # not kept "as is"
12701270 # def tagify(self) -> Tag:
12711271 # return self.as_string()
12721272
1273- # HTML() + HTML() should return HTML()
1274- # HTML() + str should return HTML()
1275- # str + HTML() should return HTML() # This is not implemented and hard to catch!
1273+ # Cases:
1274+ # * `str + str` should return str # Not HTML's responsibility!
1275+ # * `str + HTML()` should return HTML() # Handled by HTML.__radd__()
1276+ # * `HTML() + str` should return HTML()
1277+ # * `HTML() + HTML()` should return HTML()
12761278 def __add__ (self , other : object ) -> HTML :
12771279 if isinstance (other , HTML ):
12781280 # HTML strings should be concatenated without escaping
1281+ # Convert each element to strings, then concatenate them, and return HTML
1282+ # Case: `HTML() + HTML()`
12791283 return HTML (self .as_string () + other .as_string ())
12801284
12811285 # Non-HTML text added to HTML should be escaped before being added
1282- return HTML (str .__add__ (self .as_string (), html_escape (str (other ))))
1286+ # Convert each element to strings, then concatenate them, and return HTML
1287+ # Case: `HTML() + str`
1288+ return HTML (self .as_string () + html_escape (str (other )))
1289+
1290+ # Right side addition for when types are: `str + HTML()` or `unknown + HTML()`
1291+ def __radd__ (self , other : object ) -> HTML :
1292+ # Non-HTML text added to HTML should be escaped before being added
1293+ # Convert each element to strings, then concatenate them, and return HTML
1294+ # Case: `str + HTML()`
1295+ return HTML (html_escape (str (other )) + self .as_string ())
12831296
12841297 def __eq__ (self , x : object ) -> bool :
12851298 # Set `x` first so that it can dispatch to the other object's __eq__ method as we've upgraded to `str`
@@ -1292,7 +1305,8 @@ def _repr_html_(self) -> str:
12921305 return self .as_string ()
12931306
12941307 def as_string (self ) -> str :
1295- return self ._html + ""
1308+ # Returns a new string
1309+ return self .data + ""
12961310
12971311
12981312# =============================================================================
0 commit comments