Skip to content

Commit c947a5e

Browse files
codingjoetruongvan
andcommitted
Fix #90 -- Container is smaller then smallest breakpoint
An error occured, if the container was smaller then all breakpoints. This should not happen during regular usage, and will trigger a user warning now. However, since the API allows any container size to be passed, this can be use the cause an internal server error and should be handled gracefully. Co-authored-by: truongvan <[email protected]>
1 parent 099ead2 commit c947a5e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pictures/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import math
44
import random
55
import sys
6+
import warnings
67
from fractions import Fraction
78
from functools import lru_cache
89
from urllib.parse import unquote
@@ -39,7 +40,13 @@ def _media_query(*, container_width: int = None, **breakpoints: {str: int}):
3940
yield f"(min-width: {prev_width}px) and (max-width: {width - 1}px) {math.floor(prev_ratio * 100)}vw"
4041
prev_width = width
4142
prev_ratio = ratio
42-
yield f"{math.floor(prev_ratio * container_width)}px" if container_width else f"{math.floor(prev_ratio * 100)}vw"
43+
if prev_ratio:
44+
yield f"{math.floor(prev_ratio * container_width)}px" if container_width else f"{math.floor(prev_ratio * 100)}vw"
45+
else:
46+
warnings.warn(
47+
"Your container is smaller than all your breakpoints.", UserWarning
48+
)
49+
yield f"{container_width}px" if container_width else "100vw"
4350

4451

4552
def sizes(*, cols=12, container_width: int = None, **breakpoints: {str: int}) -> str:

tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def test_mixed__container(self):
8282
" 600px"
8383
)
8484

85+
def test_container__smaller_than_breakpoint(self):
86+
with pytest.warns() as records:
87+
assert (
88+
utils.sizes(container_width=500)
89+
== "(min-width: 0px) and (max-width: 499px) 100vw, 500px"
90+
)
91+
assert str(records[0].message) == (
92+
"Your container is smaller than all your breakpoints."
93+
)
94+
8595

8696
class TestSourceSet:
8797
def test_default(self):

0 commit comments

Comments
 (0)