From 4a94dc85caf561dfc324119f01c0c485bcc941b4 Mon Sep 17 00:00:00 2001 From: KhadeerBasha1232 Date: Thu, 19 Nov 2026 09:06:25 +0530 Subject: [PATCH 1/2] Escape ${ in popup and tooltip text Popup, Tooltip and ClickForMarker put their text inside a JavaScript template literal. escape_backticks escaped backslashes and backticks, but not "${", so text such as "price: ${price}" was evaluated as an interpolation: the map script stopped with a ReferenceError, and any expression in the text ran as code. This also happened with parse_html=True, which is meant to show the text as-is. Escape "${" as well, so the text is shown exactly as given. --- folium/utilities.py | 8 ++++++-- tests/test_map.py | 18 ++++++++++++++++++ tests/test_utilities.py | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/folium/utilities.py b/folium/utilities.py index 123054206c..f3e33bc2fd 100644 --- a/folium/utilities.py +++ b/folium/utilities.py @@ -413,8 +413,12 @@ def remove_empty(**kwargs: TypeJsonValue) -> dict[str, TypeJsonValueNoNone]: def escape_backticks(text: str) -> str: - """Escape backslashes and backticks so text can be used in a JS template.""" - return text.replace("\\", "\\\\").replace("`", "\\`") + """Escape text so it can be placed inside a JS template literal. + + Backslashes, backticks and ``${`` are escaped, so the text is kept as-is + instead of ending the literal or being evaluated as an interpolation. + """ + return text.replace("\\", "\\\\").replace("`", "\\`").replace("${", "\\${") def escape_double_quotes(text: str) -> str: diff --git a/tests/test_map.py b/tests/test_map.py index 1cebf9c2fb..5ac96ba303 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -255,6 +255,24 @@ def test_popup_backslashes(): assert normalize(rendered) == normalize(expected) +@pytest.mark.parametrize("parse_html", [False, True]) +def test_popup_template_literal_interpolation(parse_html): + m = Map() + popup = Popup("price: ${price}", parse_html=parse_html).add_to(m) + rendered = popup._template.render(this=popup, kwargs={}) + assert "price: \\${price}" in rendered + assert "price: ${price}" not in rendered + + +def test_tooltip_template_literal_interpolation(): + m = Map() + marker = Marker([0, 0]).add_to(m) + tooltip = Tooltip("price: ${price}").add_to(marker) + rendered = tooltip._template.module.script(this=tooltip, kwargs={}) + assert "price: \\${price}" in rendered + assert "price: ${price}" not in rendered + + def test_tooltip_backslashes_and_backticks(): m = Map() marker = Marker([0, 0]).add_to(m) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 48d30df78c..c619059077 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -8,6 +8,7 @@ _is_url, camelize, deep_copy, + escape_backticks, escape_double_quotes, get_obj_in_upper_tree, if_pandas_df_convert_to_numpy, @@ -271,6 +272,21 @@ def test_image_to_url_svg(tmp_path): assert image_to_url(str(path)).startswith("data:image/svg+xml;base64,") +@pytest.mark.parametrize( + "text,result", + [ + ("bla", "bla"), + ("a`b", r"a\`b"), + (r"a\b", r"a\\b"), + ("${x}", r"\${x}"), + ("$5 and {x}", "$5 and {x}"), + (r"\${x}", r"\\\${x}"), + ], +) +def test_escape_backticks(text, result): + assert escape_backticks(text) == result + + @pytest.mark.parametrize( "text,result", [ From 7e6fe38d1227133a0fe2435d1635d93a94d94224 Mon Sep 17 00:00:00 2001 From: KhadeerBasha1232 Date: Thu, 19 Nov 2026 22:33:38 +0530 Subject: [PATCH 2/2] Keep ${lat} and ${lng} working in ClickForMarker ClickForMarker documents ${lat} and ${lng} as placeholders in its popup text, so escaping "${" there turned them into literal text. Add an interpolate option to escape_backticks and use it for ClickForMarker. Co-Authored-By: Claude Opus 5.5 --- folium/features.py | 3 ++- folium/utilities.py | 9 +++++++-- tests/test_features.py | 7 +++++++ tests/test_utilities.py | 4 ++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/folium/features.py b/folium/features.py index 136f5e652b..7a94b22d1e 100644 --- a/folium/features.py +++ b/folium/features.py @@ -1852,7 +1852,8 @@ def __init__(self, popup: IFrame | Html | str | None = None): if isinstance(popup, Element): popup = popup.render() if popup: - self.popup = "`" + escape_backticks(popup) + "`" # type: ignore + # ${lat} and ${lng} are documented placeholders, so keep them. + self.popup = "`" + escape_backticks(popup, interpolate=True) + "`" # type: ignore else: self.popup = '"Latitude: " + lat + "
Longitude: " + lng ' diff --git a/folium/utilities.py b/folium/utilities.py index f3e33bc2fd..43b744cf0a 100644 --- a/folium/utilities.py +++ b/folium/utilities.py @@ -412,13 +412,18 @@ def remove_empty(**kwargs: TypeJsonValue) -> dict[str, TypeJsonValueNoNone]: return {key: value for key, value in kwargs.items() if value is not None} -def escape_backticks(text: str) -> str: +def escape_backticks(text: str, interpolate: bool = False) -> str: """Escape text so it can be placed inside a JS template literal. Backslashes, backticks and ``${`` are escaped, so the text is kept as-is instead of ending the literal or being evaluated as an interpolation. + Pass ``interpolate=True`` to leave ``${...}`` untouched, for templates + that are meant to fill in JS variables. """ - return text.replace("\\", "\\\\").replace("`", "\\`").replace("${", "\\${") + text = text.replace("\\", "\\\\").replace("`", "\\`") + if not interpolate: + text = text.replace("${", "\\${") + return text def escape_double_quotes(text: str) -> str: diff --git a/tests/test_features.py b/tests/test_features.py index 5f2ae7b37c..8da9c3af58 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -310,6 +310,13 @@ def test_geojson_from_utf8_file(tmp_path, encoding): assert GeoJson(str(path)).data == data +def test_click_for_marker_keeps_lat_lng_placeholders(): + m = Map() + click = ClickForMarker("Lat: ${lat}
Lon: ${lng}").add_to(m) + rendered = click._template.module.script(this=click, kwargs={}) + assert "`Lat: ${lat}
Lon: ${lng}`" in rendered + + # GeoJsonMarker type validation. def test_geojson_marker(): m = folium.Map([30.4, -97.5], zoom_start=10) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index c619059077..2d309a601c 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -287,6 +287,10 @@ def test_escape_backticks(text, result): assert escape_backticks(text) == result +def test_escape_backticks_interpolate(): + assert escape_backticks("a`b ${lat}", interpolate=True) == r"a\`b ${lat}" + + @pytest.mark.parametrize( "text,result", [