Skip to content

Commit f99ec8b

Browse files
BUG: url regex in style_render does not pass colon and other valid (#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 <[email protected]> Co-authored-by: Simon Hawkins <[email protected]>
1 parent c3ab0c9 commit f99ec8b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v1.4.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Bug fixes
3131
~~~~~~~~~
3232
- Fix some cases for subclasses that define their ``_constructor`` properties as general callables (:issue:`46018`)
3333
- Fixed "longtable" formatting in :meth:`.Styler.to_latex` when ``column_format`` is given in extended format (:issue:`46037`)
34-
-
34+
- Fixed incorrect rendering in :meth:`.Styler.format` with ``hyperlinks="html"`` when the url contains a colon or other special characters (:issue:`46389`)
3535

3636
.. ---------------------------------------------------------------------------
3737

pandas/io/formats/style_render.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ def _render_href(x, format):
15891589
href = r"\href{{{0}}}{{{0}}}"
15901590
else:
15911591
raise ValueError("``hyperlinks`` format can only be 'html' or 'latex'")
1592-
pat = r"(https?:\/\/|ftp:\/\/|www.)[\w/\-?=%.]+\.[\w/\-&?=%.]+"
1592+
pat = r"((http|ftp)s?:\/\/|www.)[\w/\-?=%.:@]+\.[\w/\-&?=%.,':;~!@#$*()\[\]]+"
15931593
return re.sub(pat, lambda m: href.format(m.group(0)), x)
15941594
return x
15951595

pandas/tests/io/formats/style/test_html.py

+12
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,20 @@ def test_hiding_index_columns_multiindex_trimming():
778778
("no scheme, no top-level: www.web", False, "www.web"),
779779
("https scheme: https://www.web.com", True, "https://www.web.com"),
780780
("ftp scheme: ftp://www.web", True, "ftp://www.web"),
781+
("ftps scheme: ftps://www.web", True, "ftps://www.web"),
781782
("subdirectories: www.web.com/directory", True, "www.web.com/directory"),
782783
("Multiple domains: www.1.2.3.4", True, "www.1.2.3.4"),
784+
("with port: http://web.com:80", True, "http://web.com:80"),
785+
(
786+
"full net_loc scheme: http://user:[email protected]",
787+
True,
788+
"http://user:[email protected]",
789+
),
790+
(
791+
"with valid special chars: http://web.com/,.':;~!@#$*()[]",
792+
True,
793+
"http://web.com/,.':;~!@#$*()[]",
794+
),
783795
],
784796
)
785797
def test_rendered_links(type, text, exp, found):

0 commit comments

Comments
 (0)