Skip to content

Commit

Permalink
refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Conengmo committed Nov 7, 2022
1 parent 6c8d68e commit 0180bec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 2 additions & 3 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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,8 +362,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):
# escape backticks because we use those in the JS template
html = re.sub(r"(?<!\\)`", r'\`', html)
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)
6 changes: 2 additions & 4 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ def test_popup_show():

def test_popup_backticks():
m = Map()
popup = Popup('back`tick').add_to(m)
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</div>`)[0];
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 compare_rendered(rendered, expected)
assert normalize(rendered) == normalize(expected)


Expand All @@ -117,7 +116,6 @@ def test_popup_backticks_already_escaped():
""".format(popup_name=popup.get_name(),
html_name=list(popup.html._children.keys())[0],
map_name=m.get_name())
# assert compare_rendered(rendered, expected)
assert normalize(rendered) == normalize(expected)


Expand Down

0 comments on commit 0180bec

Please sign in to comment.