From 9fbebdf505b0c492d8f1605b709da2c72b4392af Mon Sep 17 00:00:00 2001 From: nk-batman21 Date: Mon, 26 Oct 2026 23:45:54 +0700 Subject: [PATCH] Escape backslashes and backticks in Tooltip text Tooltip text is rendered inside a JS template literal, like Popup text. A backslash or backtick in it made the page script invalid and the map did not render. Use escape_backticks for Tooltip as #2240 did for Popup. --- folium/map.py | 2 +- tests/test_map.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/folium/map.py b/folium/map.py index 62bdf50ad8..daf7ee84cb 100644 --- a/folium/map.py +++ b/folium/map.py @@ -728,7 +728,7 @@ def __init__( super().__init__() self._name = "Tooltip" - self.text = str(text) + self.text = escape_backticks(str(text)) kwargs.update({"sticky": sticky}) self.options = remove_empty(**kwargs) diff --git a/tests/test_map.py b/tests/test_map.py index ad8d20c098..594c42ce0e 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -10,7 +10,7 @@ import pytest from folium import GeoJson, Map, TileLayer -from folium.map import Class, CustomPane, Icon, LayerControl, Marker, Popup +from folium.map import Class, CustomPane, Icon, LayerControl, Marker, Popup, Tooltip from folium.utilities import JsCode, normalize tmpl = """ @@ -255,6 +255,24 @@ def test_popup_backslashes(): assert normalize(rendered) == normalize(expected) +def test_tooltip_backslashes_and_backticks(): + m = Map() + marker = Marker([0, 0]).add_to(m) + tooltip = Tooltip("C:\\path\\20190221 `tick`").add_to(marker) + rendered = tooltip._template.module.script(this=tooltip, kwargs={}) + expected = rf""" + {marker.get_name()}.bindTooltip( + `
+ C:\\path\\20190221 \`tick\` +
`, + {{ + "sticky": true, + }} + ); + """ + assert normalize(rendered) == normalize(expected) + + def test_icon_valid_marker_colors(): assert len(Icon.color_options) == 19 with warnings.catch_warnings():