1+ """
2+ Tests for issue #2186: Add better control for height when browser window
3+ is not maximized.
4+
5+ Validates that:
6+ 1. Percentage heights/widths emit vh/vw viewport units instead of %
7+ 2. Pixel heights/widths emit min-height/min-width to prevent collapse
8+ 3. The dead `#map { position:absolute; ... }` CSS rule is removed
9+ 4. All existing size formats (int, px string, % string) still parse correctly
10+ """
11+
12+ import re
13+ import sys
14+
15+ import pytest
16+
17+ sys .path .insert (0 , "." )
18+ import folium
19+
20+
21+ def _get_map_css (m : folium .Map ) -> str :
22+ """Return the CSS block for the map's own ID selector."""
23+ html = m .get_root ().render ()
24+ match = re .search (r"(#map_[a-f0-9]+ \{.*?\})" , html , re .DOTALL )
25+ assert match , "Could not find map CSS block in rendered HTML"
26+ return match .group (1 )
27+
28+
29+ # ---------------------------------------------------------------------------
30+ # Percentage → viewport units
31+ # ---------------------------------------------------------------------------
32+
33+ class TestPercentageUsesViewportUnits :
34+ def test_height_100_percent_emits_vh (self ):
35+ """The default height='100%' should use 100vh, not 100%."""
36+ m = folium .Map (location = [0 , 0 ], height = "100%" )
37+ css = _get_map_css (m )
38+ assert "100.0vh" in css , f"Expected vh unit, got:\n { css } "
39+ assert "100.0%" not in css .split ("position" )[1 ], (
40+ "Should not emit bare % for height when using viewport units"
41+ )
42+
43+ def test_width_100_percent_emits_vw (self ):
44+ """width='100%' should use 100vw."""
45+ m = folium .Map (location = [0 , 0 ], width = "100%" )
46+ css = _get_map_css (m )
47+ assert "100.0vw" in css , f"Expected vw unit, got:\n { css } "
48+
49+ def test_partial_percentage_height (self ):
50+ """height='80%' should emit 80vh."""
51+ m = folium .Map (location = [0 , 0 ], height = "80%" , width = "60%" )
52+ css = _get_map_css (m )
53+ assert "80.0vh" in css , f"Expected 80vh, got:\n { css } "
54+ assert "60.0vw" in css , f"Expected 60vw, got:\n { css } "
55+
56+ def test_percentage_height_no_min_height (self ):
57+ """Viewport-unit heights don't need min-height (it's implicit via vh)."""
58+ m = folium .Map (location = [0 , 0 ], height = "100%" )
59+ css = _get_map_css (m )
60+ assert "min-height" not in css
61+
62+
63+ # ---------------------------------------------------------------------------
64+ # Pixel values → min-height / min-width guards
65+ # ---------------------------------------------------------------------------
66+
67+ class TestPixelValuesGetMinConstraints :
68+ def test_pixel_string_height_gets_min_height (self ):
69+ """height='1000px' should also emit min-height: 1000px."""
70+ m = folium .Map (location = [0 , 0 ], height = "1000px" )
71+ css = _get_map_css (m )
72+ assert "height: 1000.0px" in css
73+ assert "min-height: 1000.0px" in css , f"min-height missing:\n { css } "
74+
75+ def test_integer_height_gets_min_height (self ):
76+ """height=500 (integer) should emit min-height: 500px."""
77+ m = folium .Map (location = [0 , 0 ], height = 500 , width = 750 )
78+ css = _get_map_css (m )
79+ assert "min-height: 500.0px" in css , f"min-height missing:\n { css } "
80+ assert "min-width: 750.0px" in css , f"min-width missing:\n { css } "
81+
82+ def test_pixel_string_width_gets_min_width (self ):
83+ """width='400px' should emit min-width: 400px."""
84+ m = folium .Map (location = [0 , 0 ], width = "400px" )
85+ css = _get_map_css (m )
86+ assert "min-width: 400.0px" in css , f"min-width missing:\n { css } "
87+
88+
89+ # ---------------------------------------------------------------------------
90+ # Dead CSS rule removal
91+ # ---------------------------------------------------------------------------
92+
93+ class TestDeadMapRuleRemoved :
94+ def test_dead_map_id_rule_absent (self ):
95+ """The stale `#map { position:absolute; ... }` block must not appear."""
96+ m = folium .Map (location = [0 , 0 ])
97+ html = m .get_root ().render ()
98+ # The old rule targeted the literal id="map" which never matched the
99+ # hashed IDs folium actually generates.
100+ assert "<style>#map {" not in html , (
101+ "Dead `#map { position:absolute; }` CSS rule should have been removed"
102+ )
103+
104+
105+ # ---------------------------------------------------------------------------
106+ # Backwards-compatibility: all existing call signatures still work
107+ # ---------------------------------------------------------------------------
108+
109+ class TestBackwardsCompatibility :
110+ def test_default_call (self ):
111+ """folium.Map() with no size args still renders."""
112+ m = folium .Map (location = [0 , 0 ])
113+ assert m .get_root ().render ()
114+
115+ def test_integer_sizes (self ):
116+ m = folium .Map (location = [0 , 0 ], width = 750 , height = 500 )
117+ css = _get_map_css (m )
118+ assert "750.0px" in css
119+ assert "500.0px" in css
120+
121+ def test_px_string_sizes (self ):
122+ m = folium .Map (location = [0 , 0 ], width = "800px" , height = "600px" )
123+ css = _get_map_css (m )
124+ assert "800.0px" in css
125+ assert "600.0px" in css
126+
127+ def test_percent_string_sizes_parse (self ):
128+ """Percentage strings are accepted without raising."""
129+ m = folium .Map (location = [0 , 0 ], width = "90%" , height = "75%" )
130+ css = _get_map_css (m )
131+ assert "90.0vw" in css
132+ assert "75.0vh" in css
133+
134+ def test_flags_set_correctly (self ):
135+ m_pct = folium .Map (location = [0 , 0 ], height = "100%" , width = "100%" )
136+ assert m_pct ._height_is_percent is True
137+ assert m_pct ._width_is_percent is True
138+
139+ m_px = folium .Map (location = [0 , 0 ], height = 500 , width = 750 )
140+ assert m_px ._height_is_percent is False
141+ assert m_px ._width_is_percent is False
0 commit comments