Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions htmltools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ._core import TagChildArg # pyright: ignore[reportUnusedImport] # noqa: F401
from ._core import (
HTML,
JS,
HTMLDependency,
HTMLDocument,
HTMLTextDocument,
Expand Down Expand Up @@ -46,6 +47,7 @@
"svg",
"tags",
"HTML",
"JS",
"HTMLDependency",
"HTMLDocument",
"HTMLTextDocument",
Expand Down
45 changes: 31 additions & 14 deletions htmltools/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,27 +1238,19 @@ def _static_extract_serialized_html_deps(
# =============================================================================
# HTML strings
# =============================================================================
class HTML(str):
"""
Mark a string as raw HTML. This will prevent the string from being escaped when
rendered inside an HTML tag.

Examples
--------
>>> from htmltools import HTML, div
>>> div("<p>Hello</p>")
<div>&lt;p&gt;Hello&lt;/p&gt;</div>
>>> div(HTML("<p>Hello</p>"))
<div><p>Hello</p></div>

class RAW(str):
"""
Base class for raw content.
"""

def __str__(self) -> str:
return self.as_string()

# HTML() + HTML() should return HTML()
def __add__(self, other: "str| HTML") -> str:
def __add__(self, other: "str| RAW") -> str:
res = str.__add__(self, other)
return HTML(res) if isinstance(other, HTML) else res
return self.__class__(res) if isinstance(other, self.__class__) else res

def __repr__(self) -> str:
return self.as_string()
Expand All @@ -1270,6 +1262,31 @@ def as_string(self) -> str:
return self + ""


class HTML(RAW):
"""
Mark a string as raw HTML. This will prevent the string from being escaped when
rendered inside an HTML tag.

Examples
--------
>>> from htmltools import HTML, div
>>> div("<p>Hello</p>")
<div>&lt;p&gt;Hello&lt;/p&gt;</div>
>>> div(HTML("<p>Hello</p>"))
<div><p>Hello</p></div>
"""

pass


class JS(RAW):
"""
Mark a string as raw JavaScript.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Mark a string as raw JavaScript.
Mark a string as raw JavaScript. This will prevent the string from being escaped when
rendered inside an HTML tag.

Copy link
Collaborator

@jcheng5 jcheng5 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, is this not true? Now that I think about it, maybe it shouldn't be true? Or at least, JS() for the purposes of using inside tags.script() is very different than JS() for the purposes of marking certain parts of JSON-encoded options as needing to be evaluated in the client. We should not make it easy for users to mistakenly get the latter when they really just meant the former.

"""

pass


# =============================================================================
# HTML dependencies
# =============================================================================
Expand Down