Skip to content

Escape ${ in popup and tooltip text - #2356

Open
KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:escape-template-literal-interpolation
Open

KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:escape-template-literal-interpolation

Conversation

@KhadeerBasha1232

Copy link
Copy Markdown
Contributor

Escape ${ in popup and tooltip text

Popup, Tooltip and ClickForMarker 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.

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

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.

This branch was previously deployed

1 inactive deployment
pypi — 4a94dc85 Deployed Nov 19, 2026 by KhadeerBasha1232 via packages #2110
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