1515# limitations under the License.
1616#
1717
18+ import html
19+ import json
1820import sys
1921import typing
2022import urllib
@@ -119,28 +121,31 @@ def display_link(text: str, url: str, icon: Optional[str] = "open_in_new") -> No
119121
120122 button_id = f"view-vertex-resource-{ str (uuid4 ())} "
121123
124+ # Safe encodings
125+ safe_href = html .escape (url , quote = True )
126+ safe_text = html .escape (text )
127+ safe_icon = html .escape (icon ) if icon else ""
128+ safe_url_js = json .dumps (url )
129+
122130 # Add the markup for the CSS and link component
123- html = f"""
131+ html_out = f"""
124132 { _get_styles ()}
125- <a class="view-vertex-resource" id="{ button_id } " href="#view- { button_id } ">
126- <span class="material-icons view-vertex-icon">{ icon } </span>
127- <span>{ text } </span>
133+ <a class="view-vertex-resource" id="{ button_id } " href="{ safe_href } " target="_blank ">
134+ <span class="material-icons view-vertex-icon">{ safe_icon } </span>
135+ <span>{ safe_text } </span>
128136 </a>
129137 """
130138
131139 # Add the click handler for the link
132- html += f"""
140+ html_out += f"""
133141 <script>
134142 (function () {{
135143 const link = document.getElementById('{ button_id } ');
136144 link.addEventListener('click', (e) => {{
137145 if (window.google?.colab?.openUrl) {{
138- window.google.colab.openUrl('{ url } ');
139- }} else {{
140- window.open('{ url } ', '_blank');
146+ window.google.colab.openUrl({ safe_url_js } );
147+ e.preventDefault();
141148 }}
142- e.stopPropagation();
143- e.preventDefault();
144149 }});
145150 }})();
146151 </script>
@@ -149,7 +154,7 @@ def display_link(text: str, url: str, icon: Optional[str] = "open_in_new") -> No
149154 from IPython .display import display
150155 from IPython .display import HTML
151156
152- display (HTML (html ))
157+ display (HTML (html_out ))
153158
154159
155160def display_experiment_button (experiment : "experiment_resources.Experiment" ) -> None :
0 commit comments