Skip to content

Commit c905e41

Browse files
authored
Merge pull request #1642 from Conengmo/escape-popup-backticks
Escape popup backticks
2 parents c32b6f9 + 20c80a8 commit c905e41

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

folium/map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
Classes for drawing maps.
33
44
"""
5-
65
import warnings
76
from collections import OrderedDict
87

98
from branca.element import Element, Figure, Html, MacroElement
109

11-
from folium.utilities import camelize, parse_options, validate_location
10+
from folium.utilities import camelize, parse_options, validate_location, escape_backticks
1211

1312
from jinja2 import Template
1413

@@ -362,6 +361,7 @@ def __init__(self, html=None, parse_html=False, max_width='100%',
362361
if isinstance(html, Element):
363362
self.html.add_child(html)
364363
elif isinstance(html, str):
364+
html = escape_backticks(html)
365365
self.html.add_child(Html(html, script=script))
366366

367367
self.show = show

folium/utilities.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import math
77
import os
8+
import re
89
import struct
910
import tempfile
1011
import uuid
@@ -476,3 +477,8 @@ def parse_options(**kwargs):
476477
return {camelize(key): value
477478
for key, value in kwargs.items()
478479
if value is not None}
480+
481+
482+
def escape_backticks(text):
483+
"""Escape backticks so text can be used in a JS template."""
484+
return re.sub(r"(?<!\\)`", r'\`', text)

tests/test_map.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,36 @@ def test_popup_show():
8989
assert normalize(rendered) == normalize(expected)
9090

9191

92+
def test_popup_backticks():
93+
m = Map()
94+
popup = Popup('back`tick`tick').add_to(m)
95+
rendered = popup._template.render(this=popup, kwargs={})
96+
expected = """
97+
var {popup_name} = L.popup({{"maxWidth": "100%"}});
98+
var {html_name} = $(`<div id="{html_name}" style="width: 100.0%; height: 100.0%;">back\\`tick\\`tick</div>`)[0];
99+
{popup_name}.setContent({html_name});
100+
{map_name}.bindPopup({popup_name});
101+
""".format(popup_name=popup.get_name(),
102+
html_name=list(popup.html._children.keys())[0],
103+
map_name=m.get_name())
104+
assert normalize(rendered) == normalize(expected)
105+
106+
107+
def test_popup_backticks_already_escaped():
108+
m = Map()
109+
popup = Popup('back\\`tick').add_to(m)
110+
rendered = popup._template.render(this=popup, kwargs={})
111+
expected = """
112+
var {popup_name} = L.popup({{"maxWidth": "100%"}});
113+
var {html_name} = $(`<div id="{html_name}" style="width: 100.0%; height: 100.0%;">back\\`tick</div>`)[0];
114+
{popup_name}.setContent({html_name});
115+
{map_name}.bindPopup({popup_name});
116+
""".format(popup_name=popup.get_name(),
117+
html_name=list(popup.html._children.keys())[0],
118+
map_name=m.get_name())
119+
assert normalize(rendered) == normalize(expected)
120+
121+
92122
def test_icon_valid_marker_colors():
93123
assert len(Icon.color_options) == 19
94124
with pytest.warns(None) as record:

0 commit comments

Comments
 (0)