Skip to content

Commit

Permalink
BUG: url regex in style_render does not pass colon and other valid (#…
Browse files Browse the repository at this point in the history
…46457)

* BUG: url regex in `style_render` does not pass colon and other valid

URLs containing some valid characters such as colon in port numbers get
cut off when html-formatting. As a workaround, expanded the regex to
match a wider variety of URLs.

* Add whatsnew entry for #46389 fix

* Update whatsnew entry for fix #46389

Co-authored-by: Simon Hawkins <simonjayhawkins@gmail.com>

Co-authored-by: Simon Hawkins <simonjayhawkins@gmail.com>
  • Loading branch information
kianelbo and simonjayhawkins committed Mar 24, 2022
1 parent c3ab0c9 commit f99ec8b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.2.rst
Expand Up @@ -31,7 +31,7 @@ Bug fixes
~~~~~~~~~
- Fix some cases for subclasses that define their ``_constructor`` properties as general callables (:issue:`46018`)
- Fixed "longtable" formatting in :meth:`.Styler.to_latex` when ``column_format`` is given in extended format (:issue:`46037`)
-
- Fixed incorrect rendering in :meth:`.Styler.format` with ``hyperlinks="html"`` when the url contains a colon or other special characters (:issue:`46389`)

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style_render.py
Expand Up @@ -1589,7 +1589,7 @@ def _render_href(x, format):
href = r"\href{{{0}}}{{{0}}}"
else:
raise ValueError("``hyperlinks`` format can only be 'html' or 'latex'")
pat = r"(https?:\/\/|ftp:\/\/|www.)[\w/\-?=%.]+\.[\w/\-&?=%.]+"
pat = r"((http|ftp)s?:\/\/|www.)[\w/\-?=%.:@]+\.[\w/\-&?=%.,':;~!@#$*()\[\]]+"
return re.sub(pat, lambda m: href.format(m.group(0)), x)
return x

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/formats/style/test_html.py
Expand Up @@ -778,8 +778,20 @@ def test_hiding_index_columns_multiindex_trimming():
("no scheme, no top-level: www.web", False, "www.web"),
("https scheme: https://www.web.com", True, "https://www.web.com"),
("ftp scheme: ftp://www.web", True, "ftp://www.web"),
("ftps scheme: ftps://www.web", True, "ftps://www.web"),
("subdirectories: www.web.com/directory", True, "www.web.com/directory"),
("Multiple domains: www.1.2.3.4", True, "www.1.2.3.4"),
("with port: http://web.com:80", True, "http://web.com:80"),
(
"full net_loc scheme: http://user:pass@web.com",
True,
"http://user:pass@web.com",
),
(
"with valid special chars: http://web.com/,.':;~!@#$*()[]",
True,
"http://web.com/,.':;~!@#$*()[]",
),
],
)
def test_rendered_links(type, text, exp, found):
Expand Down

0 comments on commit f99ec8b

Please sign in to comment.