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
2 changes: 1 addition & 1 deletion folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ class GeoJson(Layer):
{%- endif %}

{%- if not this.style %}
{{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;});
{{this.get_name()}}.setStyle(function(feature) {return (feature.properties || {}).style;});
{%- endif %}

{% endmacro %}
Expand Down
41 changes: 41 additions & 0 deletions tests/selenium/test_geojson_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,44 @@ def test_geojson(driver):
assert control_label.text == "geojson"
control_input = control_label.find_element(By.CSS_SELECTOR, value="input")
assert control_input.get_attribute("checked") is None


def test_geojson_null_properties(driver):
"""A feature with "properties": null must not break the map.

RFC 7946 allows null properties. The default style callback read
feature.properties.style, which threw a TypeError and stopped the rest
of the map script, so nothing after it was drawn.
"""
square = [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]]
data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": None,
"geometry": {"type": "Polygon", "coordinates": square},
},
{
"type": "Feature",
"properties": {"style": {"color": "red"}},
"geometry": {
"type": "Polygon",
"coordinates": [[[x + 20, y] for x, y in square[0]]],
},
},
],
}
m = folium.Map((5, 15), zoom_start=4)
folium.GeoJson(data).add_to(m)
folium.Marker((5, 15)).add_to(m)

html = m.get_root().render()
with temp_html_filepath(html) as filepath:
driver.get_file(filepath)
assert driver.wait_until(".folium-map")
driver.verify_js_logs()
paths = driver.find_elements(By.CSS_SELECTOR, "path.leaflet-interactive")
assert len(paths) == 2
assert paths[1].get_attribute("stroke") == "red"
assert driver.find_elements(By.CSS_SELECTOR, ".leaflet-marker-icon")
26 changes: 26 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,32 @@ def test_geojson_empty_features_with_styling():
m.get_root().render()


def test_geojson_null_properties_default_style():
"""Without a style_function, features with null properties must not crash.

RFC 7946 allows "properties": null. The default style callback used to
read feature.properties.style directly, which throws in the browser.
"""
m = Map()
data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": None,
"geometry": {"type": "Point", "coordinates": [0, 0]},
}
],
}
geojson = GeoJson(data).add_to(m)
rendered = m.get_root().render()
expected = (
f"{geojson.get_name()}.setStyle(function(feature) "
"{return (feature.properties || {}).style;});"
)
assert expected in rendered


def test_geojson_event_handler():
"""Test that event handlers are properly generated"""
m = Map()
Expand Down
Loading