-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: use viewport units for percentage map dimensions (fixes #2186) #2229
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
Open
Kakarot35
wants to merge
6
commits into
python-visualization:main
Choose a base branch
from
Kakarot35:fix/height-viewport-units-2186
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−18
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ce3bd5
fix: use viewport units for percentage map dimensions (fixes #2186)
Kakarot35 62b2402
fix: address pre-commit linting errors
Kakarot35 dc4bddf
fix: remove unused imports in test file
Kakarot35 84897fb
fix: sort imports alphabetically
Kakarot35 dbc607f
fix: sort and group imports correctly
Kakarot35 fbaa932
fix: apply black formatting
Kakarot35 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| """ | ||
| Tests for issue #2186: Add better control for height when browser window | ||
| is not maximized. | ||
|
|
||
| Validates that: | ||
| 1. Percentage heights/widths emit vh/vw viewport units instead of % | ||
| 2. Pixel heights/widths emit min-height/min-width to prevent collapse | ||
| 3. The dead `#map { position:absolute; ... }` CSS rule is removed | ||
| 4. All existing size formats (int, px string, % string) still parse correctly | ||
|
|
||
| """ | ||
|
|
||
| import re | ||
|
|
||
| import folium | ||
|
|
||
|
|
||
| def _get_map_css(m: folium.Map) -> str: | ||
| """Return the CSS block for the map's own ID selector.""" | ||
| html = m.get_root().render() | ||
| match = re.search(r"(#map_[a-f0-9]+ \{.*?\})", html, re.DOTALL) | ||
| assert match, "Could not find map CSS block in rendered HTML" | ||
| return match.group(1) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Percentage → viewport units | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestPercentageUsesViewportUnits: | ||
| def test_height_100_percent_emits_vh(self): | ||
| """The default height='100%' should use 100vh, not 100%.""" | ||
| m = folium.Map(location=[0, 0], height="100%") | ||
| css = _get_map_css(m) | ||
| assert "100.0vh" in css, f"Expected vh unit, got:\n{css}" | ||
| assert ( | ||
| "100.0%" not in css.split("position")[1] | ||
| ), "Should not emit bare % for height when using viewport units" | ||
|
|
||
| def test_width_100_percent_emits_vw(self): | ||
| """width='100%' should use 100vw.""" | ||
| m = folium.Map(location=[0, 0], width="100%") | ||
| css = _get_map_css(m) | ||
| assert "100.0vw" in css, f"Expected vw unit, got:\n{css}" | ||
|
|
||
| def test_partial_percentage_height(self): | ||
| """height='80%' should emit 80vh.""" | ||
| m = folium.Map(location=[0, 0], height="80%", width="60%") | ||
| css = _get_map_css(m) | ||
| assert "80.0vh" in css, f"Expected 80vh, got:\n{css}" | ||
| assert "60.0vw" in css, f"Expected 60vw, got:\n{css}" | ||
|
|
||
| def test_percentage_height_no_min_height(self): | ||
| """Viewport-unit heights don't need min-height (it's implicit via vh).""" | ||
| m = folium.Map(location=[0, 0], height="100%") | ||
| css = _get_map_css(m) | ||
| assert "min-height" not in css | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Pixel values → min-height / min-width guards | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestPixelValuesGetMinConstraints: | ||
| def test_pixel_string_height_gets_min_height(self): | ||
| """height='1000px' should also emit min-height: 1000px.""" | ||
| m = folium.Map(location=[0, 0], height="1000px") | ||
| css = _get_map_css(m) | ||
| assert "height: 1000.0px" in css | ||
| assert "min-height: 1000.0px" in css, f"min-height missing:\n{css}" | ||
|
|
||
| def test_integer_height_gets_min_height(self): | ||
| """height=500 (integer) should emit min-height: 500px.""" | ||
| m = folium.Map(location=[0, 0], height=500, width=750) | ||
| css = _get_map_css(m) | ||
| assert "min-height: 500.0px" in css, f"min-height missing:\n{css}" | ||
| assert "min-width: 750.0px" in css, f"min-width missing:\n{css}" | ||
|
|
||
| def test_pixel_string_width_gets_min_width(self): | ||
| """width='400px' should emit min-width: 400px.""" | ||
| m = folium.Map(location=[0, 0], width="400px") | ||
| css = _get_map_css(m) | ||
| assert "min-width: 400.0px" in css, f"min-width missing:\n{css}" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Dead CSS rule removal | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestDeadMapRuleRemoved: | ||
| def test_dead_map_id_rule_absent(self): | ||
| """The stale `#map { position:absolute; ... }` block must not appear.""" | ||
| m = folium.Map(location=[0, 0]) | ||
| html = m.get_root().render() | ||
| # The old rule targeted the literal id="map" which never matched the | ||
| # hashed IDs folium actually generates. | ||
| assert ( | ||
| "<style>#map {" not in html | ||
| ), "Dead `#map { position:absolute; }` CSS rule should have been removed" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Backwards-compatibility: all existing call signatures still work | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestBackwardsCompatibility: | ||
| def test_default_call(self): | ||
| """folium.Map() with no size args still renders.""" | ||
| m = folium.Map(location=[0, 0]) | ||
| assert m.get_root().render() | ||
|
|
||
| def test_integer_sizes(self): | ||
| m = folium.Map(location=[0, 0], width=750, height=500) | ||
| css = _get_map_css(m) | ||
| assert "750.0px" in css | ||
| assert "500.0px" in css | ||
|
|
||
| def test_px_string_sizes(self): | ||
| m = folium.Map(location=[0, 0], width="800px", height="600px") | ||
| css = _get_map_css(m) | ||
| assert "800.0px" in css | ||
| assert "600.0px" in css | ||
|
|
||
| def test_percent_string_sizes_parse(self): | ||
| """Percentage strings are accepted without raising.""" | ||
| m = folium.Map(location=[0, 0], width="90%", height="75%") | ||
| css = _get_map_css(m) | ||
| assert "90.0vw" in css | ||
| assert "75.0vh" in css | ||
|
|
||
| def test_flags_set_correctly(self): | ||
| m_pct = folium.Map(location=[0, 0], height="100%", width="100%") | ||
| assert m_pct._height_is_percent is True | ||
| assert m_pct._width_is_percent is True | ||
|
|
||
| m_px = folium.Map(location=[0, 0], height=500, width=750) | ||
| assert m_px._height_is_percent is False | ||
| assert m_px._width_is_percent is False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why these changes are necessary? Did you branch from an older version of the code? This was changed not too long ago. Using
WebDriverWaitensures we wait only until the appropriate conditions are met, instead of waiting a fixed amount of time.