Skip to content

text annotations with with hrefs containing encoded characters such as '#' break links #4084

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

Open
ned2 opened this issue Jul 28, 2019 · 13 comments
Labels
bug something broken P2 considered for next cycle

Comments

@ned2
Copy link

ned2 commented Jul 28, 2019

The solution that was adopted in #2471 to enable support for encoded hrefs seems to break for characters like # and :. This seems to be because JavaScript's encodeURI and decodeURI do not encode/decode these characters. Perhaps more nuanced used of encodeURIComponent and decodeURIComponent needs to be used?

Here's a minimal working example in Python, which is trying to link to a Twitter query for #food lang:en, in which the # and : characters need to be encoded:

import plotly.io as pio
fig = {"data": [{"type": "bar", "x": ['<a href="http://twitter.com/search? q=%23food%20lang%3Aen">query</a>'], "y": [1]}]}
pio.show(fig)

The generated link ends up being scrambled because the hash and colon escape sequences are not decoded, but are then encoded again, resulting in the following Twitter search: %23food lang%3Aen

@etpinard
Copy link
Contributor

Thanks very much for reporting!

@etpinard etpinard added the bug something broken label Jul 29, 2019
@etpinard
Copy link
Contributor

related: #3449

@etpinard
Copy link
Contributor

etpinard commented Jul 29, 2019

@RichardNeill
Copy link

Currently, it seems to also struggle with text of the form
<a href='#item3'>third</a>
because, when clicking on the link, we actually get the effect of:
<a href='thispage.html#item3'>third</a>

In other words, rather than simply scrolling down to the matching , the effect is to completely reload the page. Thanks for your help.

@etpinard
Copy link
Contributor

Some interesting thoughts in #2239 (comment)

@vpicouet
Copy link

Hello!
is there a way to bypass this issue for now?

@acjh
Copy link

acjh commented Jan 20, 2022

You can hijack plotly.io._html._window_plotly_config to override encodeURI.

pio._html._window_plotly_config += """
<script>
_encodeURI = encodeURI;
encodeURI = uri => _encodeURI(uri)
    .replace('%2523', '%23')  // #
    .replace('%253A', '%3A'); // :
</script>
"""

@vpicouet
Copy link

vpicouet commented Jan 20, 2022

Thanks for the hack!
Do you have a bigger example? I do not manage to have it working:
I integrated it in a minimalist example:

import plotly.graph_objects as go
import plotly.io as pio 
from  plotly.io import _html
from plotly.offline import init_notebook_mode, iplot,  download_plotlyjs 

_html._window_plotly_config += """
<script>
_encodeURI = encodeURI;
encodeURI = uri => _encodeURI(uri)
    .replace('%252b', '%2b')  // #
</script>
"""


text = "<a href='http://simbad.u-strasbg.fr/simbad/sim-id?Ident=SDSS+J000102.74%2b012941.9'>Link</a>"
print(text)
data=go.Scatter(x=[1],y=[1],mode='markers+text',
                hovertext = text,
                text=[text])
fig=go.Figure([data])
iplot(fig)

You can see that the link I prink works: http://simbad.u-strasbg.fr/simbad/sim-id?Ident=SDSS+J000102.74%2b012941.9
While the one return my plotly changes the %2b into %252b and then the links fails:
http://simbad.u-strasbg.fr/simbad/sim-id?Ident=SDSS+J000102.74%252b012941.9
Basically I do not see any change from the pio._html._window_plotly_config change

Thanks for you help!

@acjh
Copy link

acjh commented Jan 20, 2022

Your example works fine for me. Which version of plotly are you on?

@vpicouet
Copy link

5.5.0.
You mean that if you click on the plotly image link you get a result form the SQL query with a galaxy image and properties or it returns the website with the error: "this identifier has an incorrect format for catalog"
My example can be directly found on binder (last cell)
https://mybinder.org/v2/gh/vpicouet/notebooks/main?labpath=Plotly.ipynb

@acjh
Copy link

acjh commented Jan 21, 2022

Yes, it should work in your terminal too.

For JupyterLab (on Binder), you can run:

from IPython.display import display, Javascript

display(Javascript("""
window._encodeURI = window._encodeURI || encodeURI;
encodeURI = uri => _encodeURI(uri)
    .replace('%2523', '%23')  // #
    .replace('%253A', '%3A')  // :
    .replace('%252b', '%2b'); // +
"""))

@vpicouet
Copy link

Thanks a lot for your help!!!!
It works perfectly !

@caleblf
Copy link

caleblf commented Mar 26, 2024

I've run into this issue in a JS application where the URIs I'd like to link to may contain escaped characters, including %2F. Replacing window.encodeURI as suggested above breaks other libraries that rely on the normal behavior.

My ideal solution would be to add a config parameter to disable Plotly's broken URI sanitization for a single plot. Then I can do my own sanitization elsewhere.

@gvwilson gvwilson self-assigned this Jul 8, 2024
@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added the P2 considered for next cycle label Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

7 participants