Skip to content

Commit

Permalink
fix: Fix validation of hyperlink text
Browse files Browse the repository at this point in the history
- Fix: Ensure `TypeError` is raised for non-`None` falsy values of the
  *text* constructor argument.
- Fix: Raise `ValueError` when given hyperlink text is empty.
  • Loading branch information
AnonymouX47 committed May 17, 2023
1 parent 8b35012 commit f9ee7d5
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/urwidgets/hyperlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ def __init__(
) -> None:
self._uw_set_uri(uri)
super().__init__(urwid.Text((_Attr(attr), ""), "left", "ellipsis"))
self._uw_set_text(text or uri)
self._uw_set_text(uri if text is None else text)

def render(self, size: Tuple[int,], focus: bool = False) -> urwid.HyperlinkCanvas:
return HyperlinkCanvas(self._uw_uri, self._w.render(size, focus))

def _uw_set_text(self, text: str):
if not isinstance(text, str):
raise TypeError(f"Invalid type for 'text' (got: {type(text).__name__!r})")
if "\n" in text:
if not text:
raise ValueError("Hyperlink text is empty")
if "\n" in text: # Other multi-line whitespace characters are escaped by urwid
raise ValueError(f"Multi-line text (got: {text!r})")
self._w.set_text((self._w.attrib[0][0], text))

Expand Down

0 comments on commit f9ee7d5

Please sign in to comment.