Skip to content

Escape ${ in popup and tooltip text - #2356

Open
KhadeerBasha1232 wants to merge 2 commits into
python-visualization:mainfrom
KhadeerBasha1232:escape-template-literal-interpolation
Open

KhadeerBasha1232 wants to merge 2 commits into
python-visualization:mainfrom
KhadeerBasha1232:escape-template-literal-interpolation

Conversation

@KhadeerBasha1232

@KhadeerBasha1232 KhadeerBasha1232 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Escape ${ in popup and tooltip text

Popup and Tooltip place their text inside a JavaScript template literal. escape_backticks escaped backslashes and backticks, but not ${, so any ${...} in the text was evaluated as an interpolation.

import folium

m = folium.Map()
folium.Marker([0, 0], popup="Price: ${price}").add_to(m)
m.save("map.html")

Opening map.html throws ReferenceError: price is not defined, the popup is never bound, and the rest of that script block doesn't run.

An expression inside ${...} runs as code. That also happens with parse_html=True, which is meant to show the text as-is: Popup("${console.log(document.domain)}", parse_html=True) logs the page's domain when the map loads. That matters when popup text comes from data, such as a column of names or descriptions.

I checked both cases in a browser before and after this change. With the change, the popup shows the text literally (price: ${price}) and nothing runs.

Change

escape_backticks now also escapes ${ as \${, so the text is shown exactly as given. A $ that isn't followed by { is left alone.

ClickForMarker is left as it was: its docs use ${lat} and ${lng} as placeholders in the popup text, so it calls escape_backticks(..., interpolate=True), which still escapes backslashes and backticks but keeps ${...}.

Tests

  • test_escape_backticks: unit cases for backticks, backslashes and ${, including \${ and $5 and {x}.
  • test_popup_template_literal_interpolation: covers parse_html=False and parse_html=True.
  • test_tooltip_template_literal_interpolation
  • test_escape_backticks_interpolate and test_click_for_marker_keeps_lat_lng_placeholders: ${lat} / ${lng} still work in ClickForMarker.

The new tests fail without the change.

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.
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 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
pypi — 7e6fe38d Deployed Nov 19, 2026 by KhadeerBasha1232 via packages #2116
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant