Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions folium/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import math
import os
import re
import tempfile
import uuid
from collections.abc import Iterable, Iterator, Sequence
Expand Down Expand Up @@ -412,8 +411,8 @@ def remove_empty(**kwargs: TypeJsonValue) -> dict[str, TypeJsonValueNoNone]:


def escape_backticks(text: str) -> str:
"""Escape backticks so text can be used in a JS template."""
return re.sub(r"(?<!\\)`", r"\`", text)
"""Escape backslashes and backticks so text can be used in a JS template."""
return text.replace("\\", "\\\\").replace("`", "\\`")


def escape_double_quotes(text: str) -> str:
Expand Down
10 changes: 6 additions & 4 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,17 @@ def test_popup_backticks():
assert normalize(rendered) == normalize(expected)


def test_popup_backticks_already_escaped():
def test_popup_backslashes():
m = Map()
popup = Popup("back\\`tick").add_to(m)
# A backslash followed by a digit would become an illegal octal escape
# sequence inside the JS template literal if it is not escaped, see #1907.
popup = Popup("C:\\path\\20190221").add_to(m)
rendered = popup._template.render(this=popup, kwargs={})
expected = """
expected = r"""
var {popup_name} = L.popup({{
"maxWidth": "100%",
}});
var {html_name} = $(`<div id="{html_name}" style="width: 100.0%; height: 100.0%;">back\\`tick</div>`)[0];
var {html_name} = $(`<div id="{html_name}" style="width: 100.0%; height: 100.0%;">C:\\path\\20190221</div>`)[0];
{popup_name}.setContent({html_name});
{map_name}.bindPopup({popup_name});
""".format(
Expand Down