diff --git a/folium/plugins/search.py b/folium/plugins/search.py index e82876b875..55f3f7bcca 100644 --- a/folium/plugins/search.py +++ b/folium/plugins/search.py @@ -47,11 +47,11 @@ class Search(JSCSSMixin, MacroElement): var {{this.layer.get_name()}}searchControl = new L.Control.Search({ layer: {{this.layer.get_name()}}, {% if this.search_label %} - propertyName: '{{this.search_label}}', + propertyName: {{ this.search_label|tojson }}, {% endif %} collapsed: {{this.collapsed|tojson|safe}}, - textPlaceholder: '{{this.placeholder}}', - position:'{{this.position}}', + textPlaceholder: {{ this.placeholder|tojson }}, + position: {{ this.position|tojson }}, {% if this.geom_type == 'Point' %} initial: false, {% if this.search_zoom %} diff --git a/tests/plugins/test_search.py b/tests/plugins/test_search.py index 51542f8823..892c0411a2 100644 --- a/tests/plugins/test_search.py +++ b/tests/plugins/test_search.py @@ -24,3 +24,25 @@ def test_search_opens_popup_bound_to_geojson_group(): ) in rendered ) + + +def test_search_quotes_strings(): + m = folium.Map([0, 0], tiles=None) + layer = folium.GeoJson( + { + "type": "Feature", + "properties": {"state's name": "Example"}, + "geometry": {"type": "Point", "coordinates": [0, 0]}, + }, + ).add_to(m) + search = Search( + layer, + search_label="state's name", + placeholder="Search for a state's name", + ).add_to(m) + rendered = normalize(search._template.module.script(search)) + + # tojson writes the apostrophe as \u0027, a valid JS string escape. + assert r'propertyName: "state\u0027s name",' in rendered + assert r'textPlaceholder: "Search for a state\u0027s name",' in rendered + assert 'position: "topleft",' in rendered