Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape popup backticks #1642

Merged
merged 3 commits into from
Nov 8, 2022
Merged
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
4 changes: 2 additions & 2 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
Classes for drawing maps.

"""

import warnings
from collections import OrderedDict

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

from folium.utilities import camelize, parse_options, validate_location
from folium.utilities import camelize, parse_options, validate_location, escape_backticks

from jinja2 import Template

Expand Down Expand Up @@ -362,6 +361,7 @@ def __init__(self, html=None, parse_html=False, max_width='100%',
if isinstance(html, Element):
self.html.add_child(html)
elif isinstance(html, str):
html = escape_backticks(html)
self.html.add_child(Html(html, script=script))

self.show = show
Expand Down
6 changes: 6 additions & 0 deletions folium/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import math
import os
import re
import struct
import tempfile
import uuid
Expand Down Expand Up @@ -474,3 +475,8 @@ def parse_options(**kwargs):
return {camelize(key): value
for key, value in kwargs.items()
if value is not None}


def escape_backticks(text):
"""Escape backticks so text can be used in a JS template."""
return re.sub(r"(?<!\\)`", r'\`', text)
30 changes: 30 additions & 0 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ def test_popup_show():
assert normalize(rendered) == normalize(expected)


def test_popup_backticks():
m = Map()
popup = Popup('back`tick`tick').add_to(m)
rendered = popup._template.render(this=popup, kwargs={})
expected = """
var {popup_name} = L.popup({{"maxWidth": "100%"}});
var {html_name} = $(`<div id="{html_name}" style="width: 100.0%; height: 100.0%;">back\\`tick\\`tick</div>`)[0];
{popup_name}.setContent({html_name});
{map_name}.bindPopup({popup_name});
""".format(popup_name=popup.get_name(),
html_name=list(popup.html._children.keys())[0],
map_name=m.get_name())
assert normalize(rendered) == normalize(expected)


def test_popup_backticks_already_escaped():
m = Map()
popup = Popup('back\\`tick').add_to(m)
rendered = popup._template.render(this=popup, kwargs={})
expected = """
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];
{popup_name}.setContent({html_name});
{map_name}.bindPopup({popup_name});
""".format(popup_name=popup.get_name(),
html_name=list(popup.html._children.keys())[0],
map_name=m.get_name())
assert normalize(rendered) == normalize(expected)


def test_icon_valid_marker_colors():
assert len(Icon.color_options) == 19
with pytest.warns(None) as record:
Expand Down