-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(HTML)!: HTML
class no longer inherits from str
#86
Conversation
If a non-HTML value is added to an HTML value, the non-HTML value is escaped Fixes #15
…ngs of the tagified values
Co-Authored-By: Winston Chang <winston@posit.co>
@@ -87,7 +97,7 @@ class MetadataNode: | |||
|
|||
TagT = TypeVar("TagT", bound="Tag") | |||
|
|||
TagAttrValue = Union[str, float, bool, None] | |||
TagAttrValue = Union[str, float, bool, "HTML", None] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given parity for other large union objects (TagNode
and TagChild
), I do not believe we should make a is_tag_attr_value()
method as consolidate_attrs()
should internally handle it.
For now, we will skip making this helper method.
@runtime_checkable | ||
class Tagifiable(Protocol): | ||
""" | ||
Objects with `tagify()` methods are considered `Tagifiable`. Note that an object | ||
returns a `TagList`, the children of the `TagList` must also be tagified. | ||
""" | ||
|
||
def tagify(self) -> "TagList | Tag | MetadataNode | str": ... | ||
def tagify(self) -> "TagList | Tag | MetadataNode | str | HTML": ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML()
support!
@@ -87,7 +97,7 @@ class MetadataNode: | |||
|
|||
TagT = TypeVar("TagT", bound="Tag") | |||
|
|||
TagAttrValue = Union[str, float, bool, None] | |||
TagAttrValue = Union[str, float, bool, "HTML", None] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML()
support!
@@ -394,7 +486,7 @@ def _repr_html_(self) -> str: | |||
# ============================================================================= | |||
# TagAttrDict class | |||
# ============================================================================= | |||
class TagAttrDict(Dict[str, str]): | |||
class TagAttrDict(Dict[str, "str | HTML"]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML()
support!
@@ -453,15 +544,17 @@ def _normalize_attr_name(x: str) -> str: | |||
return x.replace("_", "-") | |||
|
|||
@staticmethod | |||
def _normalize_attr_value(x: TagAttrValue) -> Optional[str]: | |||
def _normalize_attr_value(x: TagAttrValue) -> str | HTML | None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML()
support!
@@ -689,7 +782,7 @@ def has_class(self, class_: str) -> bool: | |||
else: | |||
return False | |||
|
|||
def add_style(self: TagT, style: str, *, prepend: bool = False) -> TagT: | |||
def add_style(self: TagT, style: str | HTML, *, prepend: bool = False) -> TagT: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML()
support!
@@ -908,8 +1001,8 @@ def wrap_displayhook_handler( | |||
def handler_wrapper(value: object) -> None: | |||
if isinstance(value, (Tag, TagList, Tagifiable)): | |||
handler(value) | |||
elif hasattr(value, "_repr_html_"): | |||
handler(HTML(value._repr_html_())) # pyright: ignore | |||
elif isinstance(value, ReprHtml): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use isinstance()
rather than look for the key, which isinstance performs
) -> tuple[TagAttrs, list[TagChildT]]: ... | ||
|
||
|
||
def consolidate_attrs( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new helper method (ported from py-shiny)
""" | ||
|
||
|
||
def is_tag_node(x: object) -> TypeIs[TagNode]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New helper method
return isinstance(x, (Tagifiable, MetadataNode, ReprHtml, str, HTML)) | ||
|
||
|
||
def is_tag_child(x: object) -> TypeIs[TagChild]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New helper method
@@ -1724,7 +1904,7 @@ def _tagchilds_to_tagnodes(x: Iterable[TagChild]) -> list[TagNode]: | |||
for i, item in enumerate(result): | |||
if isinstance(item, (int, float)): | |||
result[i] = str(item) | |||
elif not isinstance(item, (Tagifiable, Tag, MetadataNode, ReprHtml, str)): | |||
elif not is_tag_node(item): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using new helper method
Co-authored-by: Winston Chang <winston@posit.co>
Breaking changes
HTML
no longer inherits fromstr
. It now inherits fromcollections.UserString
. This was done to avoid confusion betweenstr
andHTML
objects. (feat(HTML)!:HTML
class no longer inherits fromstr
#86)Tag
andTagList
's method.get_html_string()
now both returnstr
instead ofHTML
. (feat(HTML)!:HTML
class no longer inherits fromstr
#86)Strings added to
HTML
objects, now returnHTML
objects. E.g.HTML_value + str_value
andstr_value_ + HTML_value
both returnHTML
objects. To maintain astr
result, callstr()
on yourHTML
objects before adding them to strings. (feat(HTML)!:HTML
class no longer inherits fromstr
#86)New features
Exported
ReprHtml
protocol class. If an object has a_repr_html_
method, then it is of instanceReprHtml
. (feat(HTML)!:HTML
class no longer inherits fromstr
#86)Exported
is_tag_node()
andis_tag_child()
functions that utilizetyping.TypeIs
to narrowTagNode
andTagChild
type variables, respectively. (feat(HTML)!:HTML
class no longer inherits fromstr
#86)Exported
consolidate_attrs(*args, **kwargs)
function. This function will combine theTagAttrs
(supplied in*args
) withTagAttrValues
(supplied in**kwargs
) into a singleTagAttrs
object. In addition, it will also return all*args
that are not dictionary as a list of unalteredTagChild
objects. (feat(HTML)!:HTML
class no longer inherits fromstr
#86)The
Tag
method.add_style(style=)
added support forHTML
objects in addition tostr
values. (feat(HTML)!:HTML
class no longer inherits fromstr
#86)Benefits:
str
type and anHTML
type.Related py-shiny PRs in prep for this PR:
htmltools.HTML
no longer inheriting fromstr
py-shiny#1690