Skip to content

Commit

Permalink
Allow None or str for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Sep 8, 2021
1 parent 87c7757 commit 6038c5f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions htmltools/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package_version = version.parse
Version = version.Version

AttrType = Union[str, None]

class tag_list():
'''
Create a list (i.e., fragment) of HTML content
Expand Down Expand Up @@ -152,7 +154,7 @@ class tag(tag_list):
>>> print(tag("MyJSXComponent"))
'''

def __init__(self, _name: str, *arguments: Any, children: Optional[Any] = None, **kwargs: str) -> None:
def __init__(self, _name: str, *arguments: Any, children: Optional[Any] = None, **kwargs: AttrType) -> None:
super().__init__(*arguments, children)
self.name: str = _name
self._attrs: List[Dict[str, str]] = []
Expand All @@ -170,17 +172,17 @@ def flag_as_jsx(x: Any):
# http://dev.w3.org/html5/spec/single-page.html#void-elements
self._is_void = getattr(self, "_is_jsx", False) or _name in ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]

def __call__(self, *args: Any, **kwargs: str) -> 'tag':
def __call__(self, *args: Any, **kwargs: AttrType) -> 'tag':
self.append(*args, **kwargs)
return self

def append(self, *args: Any, **kwargs: str) -> None:
def append(self, *args: Any, **kwargs: AttrType) -> None:
if args:
super().append(*args)
if kwargs:
self._attrs.append({encode_attr(k): v for k, v in kwargs.items()})

def prepend(self, *args: Any, **kwargs: str) -> None:
def prepend(self, *args: Any, **kwargs: AttrType) -> None:
if args:
super().prepend(*args)
if kwargs:
Expand Down Expand Up @@ -250,7 +252,7 @@ def __repr__(self) -> str:
# --------------------------------------------------------

def tag_factory_(_name: str) -> Callable[[Any], 'tag']:
def __init__(self: tag, *args: Any, children: Optional[Any] = None, **kwargs: str) -> None:
def __init__(self: tag, *args: Any, children: Optional[Any] = None, **kwargs: AttrType) -> None:
tag.__init__(self, _name, *args, children = children, **kwargs)
return __init__

Expand Down Expand Up @@ -288,7 +290,7 @@ class html_document(tag):
---------
>>> print(html_document(h1("Hello"), tags.meta(name="description", content="test"), lang = "en"))
'''
def __init__(self, body: tag_list, head: Optional[tag_list]=None, **kwargs: str):
def __init__(self, body: tag_list, head: Optional[tag_list]=None, **kwargs: AttrType):
super().__init__("html", **kwargs)
head = head.children if isinstance(head, tag) and head.name == "head" else head
body = body.children if isinstance(body, tag) and body.name == "body" else body
Expand Down

0 comments on commit 6038c5f

Please sign in to comment.