srcset 0.2.0__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. srcset-1.0.0/CHANGELOG.md +103 -0
  2. {srcset-0.2.0 → srcset-1.0.0}/CONTRIBUTING.md +19 -19
  3. srcset-1.0.0/GUIDE.md +233 -0
  4. {srcset-0.2.0 → srcset-1.0.0}/LICENSE +21 -21
  5. {srcset-0.2.0 → srcset-1.0.0}/PKG-INFO +30 -13
  6. {srcset-0.2.0 → srcset-1.0.0}/README.md +26 -10
  7. {srcset-0.2.0 → srcset-1.0.0}/pyproject.toml +21 -5
  8. srcset-1.0.0/scripts/_build_hook.py +17 -0
  9. {srcset-0.2.0 → srcset-1.0.0}/scripts/release.ps1 +28 -28
  10. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/__init__.py +13 -2
  11. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_build.py +5 -1
  12. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_calc.py +19 -5
  13. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_cli.py +23 -5
  14. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_descriptors.py +2 -2
  15. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_math_functions.py +51 -3
  16. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_media.py +20 -2
  17. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_media_features.py +14 -3
  18. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_model.py +57 -9
  19. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_numbers.py +11 -2
  20. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_parse.py +5 -0
  21. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_select.py +10 -0
  22. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_sizes.py +12 -8
  23. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_validate.py +8 -1
  24. srcset-1.0.0/tests/test_cert_contract.py +110 -0
  25. srcset-1.0.0/tests/test_cert_errors.py +117 -0
  26. srcset-1.0.0/tests/test_cli_module.py +25 -0
  27. srcset-1.0.0/tests/test_e2e_upgrade.py +89 -0
  28. {srcset-0.2.0 → srcset-1.0.0}/tests/test_e2e_wheel.py +3 -0
  29. srcset-1.0.0/tests/test_properties.py +198 -0
  30. srcset-1.0.0/tests/test_qa_contracts.py +199 -0
  31. srcset-1.0.0/tests/test_qa_edge_contracts.py +101 -0
  32. srcset-1.0.0/tests/test_qa_grammar_tables.py +192 -0
  33. srcset-1.0.0/tests/test_qa_math_contracts.py +155 -0
  34. srcset-1.0.0/tests/test_qa_regressions.py +184 -0
  35. srcset-1.0.0/tests/test_qa_token_tables.py +124 -0
  36. srcset-1.0.0/tests/test_scaling.py +44 -0
  37. {srcset-0.2.0 → srcset-1.0.0}/tests/test_validate.py +2 -2
  38. {srcset-0.2.0 → srcset-1.0.0}/tests/upstream/MANIFEST.json +3 -3
  39. srcset-0.2.0/.gitattributes +0 -1
  40. srcset-0.2.0/.gitignore +0 -16
  41. srcset-0.2.0/CHANGELOG.md +0 -46
  42. {srcset-0.2.0 → srcset-1.0.0}/requirements-dev.txt +0 -0
  43. {srcset-0.2.0 → srcset-1.0.0}/scripts/check.ps1 +0 -0
  44. {srcset-0.2.0 → srcset-1.0.0}/scripts/fetch_upstream.py +0 -0
  45. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/__main__.py +0 -0
  46. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_css_reader.py +0 -0
  47. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_css_tokens.py +0 -0
  48. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_css_values.py +0 -0
  49. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/_scan.py +0 -0
  50. {srcset-0.2.0 → srcset-1.0.0}/src/srcset/py.typed +0 -0
  51. {srcset-0.2.0 → srcset-1.0.0}/tests/conftest.py +0 -0
  52. {srcset-0.2.0 → srcset-1.0.0}/tests/test_build.py +0 -0
  53. {srcset-0.2.0 → srcset-1.0.0}/tests/test_cli.py +0 -0
  54. {srcset-0.2.0 → srcset-1.0.0}/tests/test_contract.py +0 -0
  55. {srcset-0.2.0 → srcset-1.0.0}/tests/test_css.py +0 -0
  56. {srcset-0.2.0 → srcset-1.0.0}/tests/test_css_boundaries.py +0 -0
  57. {srcset-0.2.0 → srcset-1.0.0}/tests/test_docs.py +0 -0
  58. {srcset-0.2.0 → srcset-1.0.0}/tests/test_edges.py +0 -0
  59. {srcset-0.2.0 → srcset-1.0.0}/tests/test_media.py +0 -0
  60. {srcset-0.2.0 → srcset-1.0.0}/tests/test_model.py +0 -0
  61. {srcset-0.2.0 → srcset-1.0.0}/tests/test_parse.py +0 -0
  62. {srcset-0.2.0 → srcset-1.0.0}/tests/test_review_regressions.py +0 -0
  63. {srcset-0.2.0 → srcset-1.0.0}/tests/test_select.py +0 -0
  64. {srcset-0.2.0 → srcset-1.0.0}/tests/test_sizes.py +0 -0
  65. {srcset-0.2.0 → srcset-1.0.0}/tests/test_typing.py +0 -0
  66. {srcset-0.2.0 → srcset-1.0.0}/tests/test_wpt.py +0 -0
@@ -0,0 +1,103 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
5
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.0] - 2026-11-28
10
+
11
+ ### Stable
12
+
13
+ The public API is stable from this release and follows Semantic Versioning: `parse`,
14
+ `build`, `parse_sizes`, `source_set`, `select`, `validate`, `Candidate`, `Environment`,
15
+ `ImageSource`, `Issue` and `SrcsetError`, with the signatures, defaults, error types and
16
+ issue codes of 0.2.0. Code written for 0.2.0 runs unchanged.
17
+
18
+ ### Added
19
+
20
+ - Runnable examples in the docstring of every public name, checked by the tests.
21
+ - `GUIDE.md`: the behaviour in detail, edge cases, errors and the upgrade from 0.2.0.
22
+ - Tests: pinned 0.2.0 signatures, seeded property tests and input scaling checks, and an
23
+ installed-wheel test that upgrades the published 0.2.0 in place.
24
+
25
+ ### Changed
26
+
27
+ - Widths and heights longer than 100,000 significant digits are out of range: `parse`
28
+ drops the candidate, `validate` reports `invalid-descriptor`, and `build` and
29
+ `Candidate` raise `SrcsetError`. Converting such values cost more than linear time, so a
30
+ hostile attribute could stall the caller. Shorter values stay exact.
31
+ - Public classes report `srcset` as their module, so tracebacks and pickles name
32
+ `srcset.Candidate` rather than a private module. Pickles from 0.2.0 still load.
33
+ - Development status is Production/Stable. The source distribution lists its contents
34
+ explicitly and leaves out repository-only files.
35
+
36
+ ### Fixed
37
+
38
+ - The repr of a `Candidate` with a width or height of more than 4,300 digits no longer
39
+ raises `ValueError`, and `build` reports a wrong item with `TypeError` whatever its size;
40
+ the message now names the item's type instead of repeating its value.
41
+ - `auto_width` too large for a float raises `SrcsetError`, as `Environment` does, instead
42
+ of `OverflowError`.
43
+ - `validate` reports a supported media feature with a value of the wrong type, such as
44
+ `(min-width: 600)` or `(orientation: sideways)`, and a `min-`/`max-` prefix on a
45
+ feature that takes none, as `invalid-sizes`. Media Queries 4 makes these unknown, and
46
+ `parse_sizes` already never matched them.
47
+ - Container query units (`cqw`, `cqh`, `cqi`, `cqb`, `cqmin`, `cqmax`) resolve to the
48
+ small viewport units instead of making the entry invalid, as in browsers.
49
+ - Command line usage errors cut the quoted argument to 80 characters instead of
50
+ repeating it in full.
51
+ - `Candidate` and `Environment` store plain `int` and `float` values, so a subclass
52
+ with its own comparisons or conversions cannot slip a negative width through or raise
53
+ its own exception.
54
+ - A NaN argument to any math function makes the size invalid, in any position. Before,
55
+ `min`, `max`, `clamp` and `hypot` only noticed it first, and `sign()` and `pow()` could
56
+ turn it into a number.
57
+ - Infinities pass through nested math functions, as CSS Values 4 says: only an infinite or
58
+ NaN result at the top level makes the size invalid. `round()`, `mod()` and `rem()` with
59
+ an infinite or zero step, `pow()` and `log()` of zero, `pow()` and `exp()` overflows,
60
+ and negative zero in sums follow the specification.
61
+ - `round()` with a negative step rounds in the direction its strategy names; the sign of
62
+ the step no longer flips `up` and `down`.
63
+ - `srcset select --json` writes `null` for an infinite density instead of `Infinity`,
64
+ which is not JSON.
65
+
66
+ ## [0.2.0] - 2026-10-22
67
+
68
+ ### Added
69
+
70
+ - `parse_sizes` reads a sizes attribute with the WHATWG algorithm on top of a CSS tokenizer:
71
+ absolute, font and viewport lengths, the CSS math functions, and media
72
+ conditions with `not`, `and`, `or`, range syntax and three-valued logic. `auto` resolves
73
+ to a width the caller passes when the value starts with it.
74
+ - `source_set` and `select` build and normalize the source set (with the `src` fallback)
75
+ and pick the image a browser fetches for an `Environment` (viewport, pixel ratio, font
76
+ size).
77
+ - `validate` lists authoring errors with stable codes.
78
+ - The `srcset` command (`parse`, `select`, `check`) and `python -m srcset`.
79
+ - Tests against the web-platform-tests srcset and sizes cases, fetched locally by
80
+ `scripts/fetch_upstream.py`, plus executed README examples, a typing check and an
81
+ installed-wheel test.
82
+
83
+ ### Changed
84
+
85
+ - `Candidate` is a frozen, validated value with `url`, `width`, `density` and `height`;
86
+ its `descriptor` property gives the text. It replaces the `(url, descriptor)` tuple.
87
+ - `build` also accepts `Candidate` values, normalizes descriptor text and refuses a URL
88
+ that starts with a comma.
89
+
90
+ ### Fixed
91
+
92
+ - Numbers follow the HTML microsyntaxes: `1.x` is no longer accepted, `0x` and `-0x` are
93
+ kept, and only ASCII digits count.
94
+ - `100w 50h` is a valid candidate: the `h` descriptor is accepted next to a width.
95
+ - Widths of any length parse exactly, past the interpreter's digit limit.
96
+
97
+ ## [0.1.0]
98
+
99
+ - `parse` reads a srcset attribute into a list of `Candidate` values, following the
100
+ WHATWG splitting algorithm so URLs that contain commas stay intact.
101
+ - `build` serialises candidates back into a srcset value and rejects anything that
102
+ cannot round-trip (empty URLs, whitespace in a URL, a trailing comma, bad descriptors).
103
+ - `Candidate` named tuple and `SrcsetError` are part of the public surface.
@@ -1,19 +1,19 @@
1
- # Contributing
2
-
3
- Thanks for taking the time. This project keeps a small, predictable shape.
4
-
5
- ## Setup
6
-
7
- ```
8
- pip install -r requirements-dev.txt
9
- ```
10
-
11
- ## Before you open a pull request
12
-
13
- - Keep public API in `src/srcset/__init__.py`; internals live in `_`-prefixed modules.
14
- - Add a test for the behaviour you change. Tests inject time, disk and network; they
15
- never touch the real thing.
16
- - Run `scripts/check.ps1` (format, lint, types, tests) and make sure it passes.
17
- - One logical change per commit, imperative subject line.
18
-
19
- Open an issue first if the change is large or shifts the public API.
1
+ # Contributing
2
+
3
+ Thanks for taking the time. This project keeps a small, predictable shape.
4
+
5
+ ## Setup
6
+
7
+ ```
8
+ pip install -r requirements-dev.txt
9
+ ```
10
+
11
+ ## Before you open a pull request
12
+
13
+ - Keep public API in `src/srcset/__init__.py`; internals live in `_`-prefixed modules.
14
+ - Add a test for the behaviour you change. Tests inject time, disk and network; they
15
+ never touch the real thing.
16
+ - Run `scripts/check.ps1` (format, lint, types, tests) and make sure it passes.
17
+ - One logical change per commit, imperative subject line.
18
+
19
+ Open an issue first if the change is large or shifts the public API.
srcset-1.0.0/GUIDE.md ADDED
@@ -0,0 +1,233 @@
1
+ # srcset guide
2
+
3
+ The README shows the common calls. This guide covers the behaviour in detail: what each
4
+ function accepts, what it refuses, and where the library makes a choice the standard
5
+ leaves open. Every example below runs in the test suite and prints what its comments say.
6
+
7
+ ## Parsing a srcset value
8
+
9
+ `parse(value)` follows the WHATWG "parse a srcset attribute" algorithm. A URL is a run of
10
+ characters up to ASCII whitespace, so a comma inside it, as in a data URI, never splits
11
+ the list; commas at the end of a URL are separators and are removed. Leading, repeated
12
+ and trailing commas are skipped. A candidate with no descriptor keeps `None` in all three
13
+ fields and stands for `1x`.
14
+
15
+ ```python
16
+ from srcset import parse
17
+
18
+ print(parse("data:image/png;base64,iVBO,AAA 2x, b.png"))
19
+ # [Candidate(url='data:image/png;base64,iVBO,AAA', width=None, density=2.0, height=None), Candidate(url='b.png', width=None, density=None, height=None)]
20
+ print([c.url for c in parse(" ,a.png 1x,,b.png 2x,")])
21
+ # ['a.png', 'b.png']
22
+ ```
23
+
24
+ Descriptors are read as browsers read them, and a candidate with any invalid descriptor
25
+ is dropped without an error:
26
+
27
+ - A width is one or more ASCII digits followed by a lower-case `w`, above zero.
28
+ - A density is an HTML floating-point number followed by a lower-case `x`: `.5x`, `1e1x`
29
+ and `-0x` are valid, `1.x`, `+1x` and `1X` are not. A negative result drops the
30
+ candidate; `0x` and `-0x` are kept as density `0.0`.
31
+ - The `h` descriptor is accepted only next to a width. It is reserved for future use, so
32
+ `validate` reports it.
33
+ - Two descriptors of the same kind, or a width together with a density, drop the
34
+ candidate. So does a parenthesised descriptor, which the standard reserves.
35
+
36
+ ```python
37
+ from srcset import parse
38
+
39
+ print([c.url for c in parse("a.png 1x 2x, b.png 0x, c.png -0x, d.png 1e1x, e.png 1X")])
40
+ # ['b.png', 'c.png', 'd.png']
41
+ print([c.url for c in parse("a.png 100w 50h, b.png 50h, c.png (x 1x)")])
42
+ # ['a.png']
43
+ ```
44
+
45
+ Widths and heights are exact integers of any length up to 100,000 significant digits.
46
+ Converting longer digit strings would cost more than linear time, so past that ceiling a
47
+ width or height counts as out of range and its candidate is dropped, much as browsers
48
+ drop a value that overflows their integers. `parse` never raises for a malformed value;
49
+ it raises `TypeError` only when the value is not a `str`.
50
+
51
+ ## Building a srcset value
52
+
53
+ `build(candidates)` accepts `Candidate` values and `(url, descriptor)` pairs, and writes
54
+ `url descriptor` items joined by `", "`. Descriptor text in pairs is normalized. The
55
+ output always parses back to the same candidates, so `build` refuses anything that would
56
+ not: an empty URL, a URL with ASCII whitespace, a URL that starts or ends with a comma,
57
+ and descriptors a browser would drop. Those raise `SrcsetError`; an item that is neither
58
+ a `Candidate` nor a pair of strings raises `TypeError`, and so does a bare string.
59
+
60
+ ```python
61
+ from srcset import SrcsetError, build
62
+
63
+ print(build([("a.png", " 320w "), ("b.png", "1.50x")]))
64
+ # a.png 320w, b.png 1.5x
65
+ for bad in [("a.png", "100w 2x"), ("a b.png", "1x")]:
66
+ try:
67
+ build([bad])
68
+ except SrcsetError as error:
69
+ print(error)
70
+ # invalid descriptor: '100w 2x'
71
+ # candidate URL must not contain ASCII whitespace: 'a b.png'
72
+ ```
73
+
74
+ `Candidate` validates itself: `width` and `height` must be `int` values above zero (not
75
+ `bool`), `density` a finite number not below zero, `height` needs `width`, and `density`
76
+ excludes both. Its `descriptor` property gives the text, `""` when there is none.
77
+
78
+ ## Resolving sizes
79
+
80
+ `parse_sizes(value, environment, *, auto_width=None)` returns the layout width in CSS
81
+ pixels. Entries are tried in order and the first one with a valid size and a true media
82
+ condition wins; when none applies the result is `environment.width`, which is `100vw`.
83
+
84
+ `Environment(width, height=None, density=1.0, font_size=16.0)` describes the viewport in
85
+ CSS pixels, the device pixel ratio, and the root font size used by `em` and `rem`. All
86
+ values must be finite; `width` and `height` may be zero, `density` and `font_size` must be
87
+ above zero. When `height` is `None`, conditions and units that need it are unknown.
88
+
89
+ ```python
90
+ from srcset import Environment, parse_sizes
91
+
92
+ laptop = Environment(width=1024, height=768, density=2)
93
+ print(parse_sizes("(min-width: 1024px) 50vw, 100vw", laptop))
94
+ # 512.0
95
+ print(parse_sizes("(width >= 600px) and (orientation: landscape) 400px, 100vw", laptop))
96
+ # 400.0
97
+ print(parse_sizes("min(100vw, 600px)", laptop), parse_sizes("calc(100vw - 2rem)", laptop))
98
+ # 600.0 992.0
99
+ print(parse_sizes("(prefers-color-scheme: dark) 10px, 20px", laptop))
100
+ # 20.0
101
+ print(parse_sizes("-10px, 30px", laptop), parse_sizes("calc(10px - 50px)", laptop))
102
+ # 30.0 0.0
103
+ print(parse_sizes("(min-height: 100px) 5px, 7px", Environment(width=1024)))
104
+ # 7.0
105
+ ```
106
+
107
+ Media features other than `width`, `height`, `aspect-ratio`, `orientation` and
108
+ `resolution` evaluate to unknown, so their entry is skipped. So does a supported feature
109
+ with a value of the wrong type, such as `(min-width: 600)` without a unit, or a math
110
+ function as a `resolution` value. A negative literal length is invalid, while a negative
111
+ math result is clamped to zero, as CSS does for lengths that cannot be negative. A result
112
+ that is infinite or not a number makes the entry invalid; inside a calculation an infinity
113
+ is kept, so `calc(1px / calc(infinity) + 5px)` is a valid 5 pixels. Container units such
114
+ as `cqw` fall back to the small viewport units, because no container applies to `sizes`.
115
+ The trigonometric functions and angle units are not supported; an entry using them is
116
+ skipped.
117
+
118
+ `auto` stands for `auto_width` only when the value is `auto` or starts with `auto,` (ASCII
119
+ case-insensitive) and `auto_width` is given; it must then be a finite number not below
120
+ zero. Otherwise `auto` is skipped and the next entry applies, and `auto_width` is not
121
+ looked at. Work grows linearly with the length of the value; the CSS reader costs a few
122
+ microseconds per character, so a megabyte of `sizes` takes seconds.
123
+
124
+ ```python
125
+ from srcset import Environment, parse_sizes
126
+
127
+ laptop = Environment(width=1024, height=768, density=2)
128
+ print(parse_sizes("auto, 50vw", laptop), parse_sizes("auto, 50vw", laptop, auto_width=320))
129
+ # 512.0 320.0
130
+ ```
131
+
132
+ ### Known limitations
133
+
134
+ The CSS math functions follow CSS Values 4, including infinities, NaN and signed zeros,
135
+ for the cases the tests pin. Unusual combinations of these special values inside nested
136
+ math functions are not exhaustively checked against the specification; they have no
137
+ practical use in a `sizes` attribute. Trigonometric functions and angle units are not
138
+ supported, and font-relative units other than `em` and `rem` use fixed ratios.
139
+
140
+ ## Source sets and selection
141
+
142
+ `source_set(srcset, environment, *, sizes="", src="", auto_width=None)` gives every
143
+ candidate as an `ImageSource(url, density)`. A width candidate gets `width / size`, where
144
+ the size comes from `parse_sizes`; a bare candidate is `1x`. `src` joins as `1x` when no
145
+ candidate has a `1x` descriptor and none has a width. Later entries with a density that
146
+ is already present are removed, which is why `src` loses to a bare candidate.
147
+
148
+ ```python
149
+ from srcset import Environment, source_set
150
+
151
+ laptop = Environment(width=1024, height=768, density=2)
152
+ print(source_set("a.png 1x, b.png 1x, c.png", laptop))
153
+ # [ImageSource(url='a.png', density=1.0)]
154
+ print(source_set("a.png 2x", laptop, src="f.png"))
155
+ # [ImageSource(url='a.png', density=2.0), ImageSource(url='f.png', density=1.0)]
156
+ print(source_set("a.png 400w", laptop, src="f.png"))
157
+ # [ImageSource(url='a.png', density=0.390625)]
158
+ ```
159
+
160
+ `select` takes the same arguments and returns one `ImageSource`, or `None` when the
161
+ source set is empty. The standard lets browsers choose freely; `select` returns the
162
+ lowest density that is at least `environment.density`, and the highest density when none
163
+ reaches it. A width candidate with a size of zero gets an infinite density.
164
+
165
+ ```python
166
+ from srcset import Environment, select
167
+
168
+ laptop = Environment(width=1024, height=768, density=2)
169
+ print(select("a.png 1x, b.png 1.5x", laptop))
170
+ # ImageSource(url='b.png', density=1.5)
171
+ print(select("a.png 480w, b.png 1080w", laptop, sizes="50vw"))
172
+ # ImageSource(url='b.png', density=2.109375)
173
+ print(select("", laptop))
174
+ # None
175
+ ```
176
+
177
+ ## Validation
178
+
179
+ `validate(srcset, sizes=None)` reports what the standard asks authors not to write, as
180
+ `Issue(code, message)` values. `sizes=None` means the attribute is absent, so width
181
+ descriptors are reported as `missing-sizes`. The codes are stable: `empty`,
182
+ `extra-comma`, `invalid-descriptor`, `duplicate-width`, `duplicate-density`,
183
+ `zero-density`, `height-descriptor`, `mixed-descriptors`, `sizes-without-width`,
184
+ `missing-sizes` and `invalid-sizes`. Messages are for people and may be reworded; quoted
185
+ input in them is cut to 80 characters.
186
+
187
+ `invalid-sizes` covers entries that do not parse, a supported media feature with a value
188
+ of the wrong type, an infinite value or a `min-`/`max-` prefix it does not take (a
189
+ degenerate ratio such as `1/0` is accepted), and sizes that are infinite
190
+ or not a number when evaluated for a 1000 by 1000 pixel viewport (a value that divides
191
+ by a viewport-dependent zero is only caught at that size). As browsers do, a math result
192
+ below zero is accepted and clamped rather than reported.
193
+
194
+ ```python
195
+ from srcset import validate
196
+
197
+ for issue in validate("a.png 400w, b.png 2x,"):
198
+ print(issue.code)
199
+ # extra-comma
200
+ # mixed-descriptors
201
+ # missing-sizes
202
+ print(validate("a.png 400w", sizes="(min-width: 600px) 50vw"))
203
+ # [Issue(code='invalid-sizes', message='sizes entry 1: the last entry takes no condition')]
204
+ ```
205
+
206
+ ## Errors
207
+
208
+ - `TypeError`: an argument of the wrong type, such as `bytes` for a value, a `bool`
209
+ width, or a non-`Environment` environment.
210
+ - `SrcsetError`, a subclass of `ValueError`: a value of the right type that is out of
211
+ range, or a candidate that `build` cannot write so that it reads back the same.
212
+
213
+ `parse`, `parse_sizes`, `source_set`, `select` and `validate` never raise for malformed
214
+ attribute text; they behave as browsers do with it.
215
+
216
+ ## Command line
217
+
218
+ `srcset parse VALUE` prints the candidates as JSON; a width or height of more than 400
219
+ digits is written as a string of digits, so every JSON reader can take it. `srcset select
220
+ VALUE` prints the URL a browser would fetch, with `--sizes`, `--src`, `--width` (1280 by
221
+ default), `--height`, `--dpr` and `--json` (the density is `null` when it is infinite); it
222
+ exits 1 when there is no candidate and 2 for an invalid environment. `srcset check VALUE
223
+ [--sizes SIZES]` prints one `code: message` line per issue and exits 1 when there is any.
224
+ `python -m srcset` is the same program. Usage errors exit 2 with a short message;
225
+ an argument quoted in it is cut to 80 characters.
226
+
227
+ ## Upgrade from 0.2.0
228
+
229
+ Code written for 0.2.0 runs unchanged: the names, signatures, defaults, error types and
230
+ issue codes are the same. The one behaviour change concerns widths and heights longer
231
+ than 100,000 significant digits, which 0.2.0 converted at a cost that grows faster than
232
+ the input. They are now out of range: `parse` drops the candidate, `validate` reports
233
+ `invalid-descriptor`, and `build` and `Candidate` raise `SrcsetError`.
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Javier Lianes
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Javier Lianes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: srcset
3
- Version: 0.2.0
3
+ Version: 1.0.0
4
4
  Summary: Parse, build, check and resolve the HTML srcset and sizes attributes like a browser
5
5
  Project-URL: Homepage, https://lianes8studio.com
6
- Project-URL: Repository, https://github.com/JaviLianes8/srcset
6
+ Project-URL: Source, https://github.com/JaviLianes8/srcset
7
7
  Project-URL: Issues, https://github.com/JaviLianes8/srcset/issues
8
8
  Project-URL: Changelog, https://github.com/JaviLianes8/srcset/blob/main/CHANGELOG.md
9
9
  Project-URL: LinkedIn, https://www.linkedin.com/in/jlianes/
@@ -12,7 +12,7 @@ Author-email: Javier Lianes <javier@lianes8studio.com>
12
12
  License-Expression: MIT
13
13
  License-File: LICENSE
14
14
  Keywords: html,image,media-queries,parser,picture,responsive-images,sizes,srcset,whatwg
15
- Classifier: Development Status :: 4 - Beta
15
+ Classifier: Development Status :: 5 - Production/Stable
16
16
  Classifier: Environment :: Console
17
17
  Classifier: Intended Audience :: Developers
18
18
  Classifier: Operating System :: OS Independent
@@ -31,6 +31,7 @@ Provides-Extra: dev
31
31
  Requires-Dist: build; extra == 'dev'
32
32
  Requires-Dist: mypy; extra == 'dev'
33
33
  Requires-Dist: pytest; extra == 'dev'
34
+ Requires-Dist: pytest-timeout; extra == 'dev'
34
35
  Requires-Dist: ruff; extra == 'dev'
35
36
  Requires-Dist: twine; extra == 'dev'
36
37
  Description-Content-Type: text/markdown
@@ -48,7 +49,10 @@ and a device pixel ratio.
48
49
  pip install srcset
49
50
  ```
50
51
 
51
- Python 3.10 or newer, no dependencies.
52
+ Python 3.10 or newer, no dependencies. The API is stable from 1.0.0 and follows Semantic
53
+ Versioning. The [guide](https://github.com/JaviLianes8/srcset/blob/main/GUIDE.md) covers
54
+ the behaviour in detail; changes are in the
55
+ [changelog](https://github.com/JaviLianes8/srcset/blob/main/CHANGELOG.md).
52
56
 
53
57
  ## Usage
54
58
 
@@ -132,11 +136,17 @@ duplicate-density: 2 candidates share the density 1
132
136
  density is a valid HTML floating-point number followed by `x` (`.5x` and `1e1x` are fine,
133
137
  `1.x`, `+1x` and `1X` are not), with only ASCII digits. `0x` is kept, as browsers keep it.
134
138
  The `h` descriptor is accepted only next to a width. Two descriptors of one kind, or a
135
- width with a density, drop the candidate.
139
+ width with a density, drop the candidate. Widths and heights are exact up to 100,000
140
+ significant digits; longer ones are out of range.
136
141
  - **Sizes.** Entries are read with a CSS tokenizer (comments, escapes, unclosed blocks).
137
- Lengths cover the absolute units, `em`, `rem`, and the viewport units with their `s`, `l`
138
- and `d` forms. Math functions: `calc`, `min`, `max`, `clamp`, `abs`, `sign`, `round`,
139
- `mod`, `rem`, `hypot`, `sqrt`, `pow`, `exp` and `log`, with the constants `e` and `pi`.
142
+ Lengths cover the absolute units (`px`, `cm`, `mm`, `q`, `in`, `pt`, `pc`), the font
143
+ units `em`, `ex`, `ch`, `cap`, `ic` and `lh` with their root forms (`rem`, `rlh` and so
144
+ on), the viewport units `vw`, `vh`, `vi`, `vb`, `vmin` and `vmax` with their `s`, `l`
145
+ and `d` forms, and the container units `cqw`, `cqh`, `cqi`, `cqb`, `cqmin` and `cqmax`,
146
+ which fall back to the small viewport units as no container applies to `sizes`. Math
147
+ functions: `calc`, `min`, `max`, `clamp`, `abs`, `sign`, `round`, `mod`, `rem`, `hypot`,
148
+ `sqrt`, `pow`, `exp` and `log`, with the constants `e` and `pi`. The trigonometric
149
+ functions and angle units are not supported: an entry that uses them is skipped.
140
150
  A negative literal is invalid and a negative math result becomes 0. A result that is
141
151
  infinite or not a number (division by zero, for example) makes the entry invalid, which
142
152
  is what the web-platform-tests expect from browsers. `ex`, `ch`, `cap`, `ic` and `lh` use
@@ -144,10 +154,12 @@ duplicate-density: 2 candidates share the density 1
144
154
  more than 32 deep make their entry invalid.
145
155
  - **Media conditions.** `not`, `and`, `or`, nesting, the `min-` and `max-` forms and range
146
156
  syntax such as `(400px <= width < 800px)`, for `width`, `height`, `aspect-ratio`,
147
- `orientation` and `resolution` (including `infinite`). Other features, and media types such as `screen`, never
148
- match: the first are unknown under the three-valued logic of Media Queries 4, the second
149
- are not allowed in `sizes`. A height-based condition is unknown while
150
- `Environment.height` is `None`.
157
+ `orientation` and `resolution` (including `infinite`). Other features, and media types
158
+ such as `screen`, never match: the first are unknown under the three-valued logic of
159
+ Media Queries 4, the second are not allowed in `sizes`. A supported feature with a value
160
+ of the wrong type, such as `(min-width: 600)`, is unknown too, and `validate` reports it;
161
+ a math function as a `resolution` value is unknown but valid. A height-based condition
162
+ is unknown while `Environment.height` is `None`.
151
163
  - **`auto`.** When the value is `auto` or starts with `auto,` (any ASCII case), `auto`
152
164
  stands for `auto_width` if one is given: an image that allows auto-sizes, rendered at
153
165
  that width. Otherwise it is skipped and the next entry applies.
@@ -168,6 +180,11 @@ media conditions), and the density arithmetic that ties the two together. This l
168
180
  implements the WHATWG algorithms for all three steps, typed and without dependencies, and
169
181
  checks itself against the web-platform-tests cases that browsers run.
170
182
 
183
+ Python HTML parsers, `html.parser` in the standard library included, hand the attribute
184
+ over as one string. On npm, `parse-srcset` (1.0.2) and `srcset` (5.0.3) parse and write
185
+ the `srcset` value; their documentation does not cover `sizes` or choosing an image
186
+ (checked on 2026-11-28).
187
+
171
188
  ## Standards
172
189
 
173
190
  - WHATWG HTML: parsing a srcset attribute, parsing a sizes attribute, creating and
@@ -189,7 +206,7 @@ records where they come from, and their tests skip until the script has fetched
189
206
 
190
207
  ## License
191
208
 
192
- MIT. See [LICENSE](LICENSE).
209
+ MIT. See [LICENSE](https://github.com/JaviLianes8/srcset/blob/main/LICENSE).
193
210
 
194
211
  ## Links
195
212
 
@@ -11,7 +11,10 @@ and a device pixel ratio.
11
11
  pip install srcset
12
12
  ```
13
13
 
14
- Python 3.10 or newer, no dependencies.
14
+ Python 3.10 or newer, no dependencies. The API is stable from 1.0.0 and follows Semantic
15
+ Versioning. The [guide](https://github.com/JaviLianes8/srcset/blob/main/GUIDE.md) covers
16
+ the behaviour in detail; changes are in the
17
+ [changelog](https://github.com/JaviLianes8/srcset/blob/main/CHANGELOG.md).
15
18
 
16
19
  ## Usage
17
20
 
@@ -95,11 +98,17 @@ duplicate-density: 2 candidates share the density 1
95
98
  density is a valid HTML floating-point number followed by `x` (`.5x` and `1e1x` are fine,
96
99
  `1.x`, `+1x` and `1X` are not), with only ASCII digits. `0x` is kept, as browsers keep it.
97
100
  The `h` descriptor is accepted only next to a width. Two descriptors of one kind, or a
98
- width with a density, drop the candidate.
101
+ width with a density, drop the candidate. Widths and heights are exact up to 100,000
102
+ significant digits; longer ones are out of range.
99
103
  - **Sizes.** Entries are read with a CSS tokenizer (comments, escapes, unclosed blocks).
100
- Lengths cover the absolute units, `em`, `rem`, and the viewport units with their `s`, `l`
101
- and `d` forms. Math functions: `calc`, `min`, `max`, `clamp`, `abs`, `sign`, `round`,
102
- `mod`, `rem`, `hypot`, `sqrt`, `pow`, `exp` and `log`, with the constants `e` and `pi`.
104
+ Lengths cover the absolute units (`px`, `cm`, `mm`, `q`, `in`, `pt`, `pc`), the font
105
+ units `em`, `ex`, `ch`, `cap`, `ic` and `lh` with their root forms (`rem`, `rlh` and so
106
+ on), the viewport units `vw`, `vh`, `vi`, `vb`, `vmin` and `vmax` with their `s`, `l`
107
+ and `d` forms, and the container units `cqw`, `cqh`, `cqi`, `cqb`, `cqmin` and `cqmax`,
108
+ which fall back to the small viewport units as no container applies to `sizes`. Math
109
+ functions: `calc`, `min`, `max`, `clamp`, `abs`, `sign`, `round`, `mod`, `rem`, `hypot`,
110
+ `sqrt`, `pow`, `exp` and `log`, with the constants `e` and `pi`. The trigonometric
111
+ functions and angle units are not supported: an entry that uses them is skipped.
103
112
  A negative literal is invalid and a negative math result becomes 0. A result that is
104
113
  infinite or not a number (division by zero, for example) makes the entry invalid, which
105
114
  is what the web-platform-tests expect from browsers. `ex`, `ch`, `cap`, `ic` and `lh` use
@@ -107,10 +116,12 @@ duplicate-density: 2 candidates share the density 1
107
116
  more than 32 deep make their entry invalid.
108
117
  - **Media conditions.** `not`, `and`, `or`, nesting, the `min-` and `max-` forms and range
109
118
  syntax such as `(400px <= width < 800px)`, for `width`, `height`, `aspect-ratio`,
110
- `orientation` and `resolution` (including `infinite`). Other features, and media types such as `screen`, never
111
- match: the first are unknown under the three-valued logic of Media Queries 4, the second
112
- are not allowed in `sizes`. A height-based condition is unknown while
113
- `Environment.height` is `None`.
119
+ `orientation` and `resolution` (including `infinite`). Other features, and media types
120
+ such as `screen`, never match: the first are unknown under the three-valued logic of
121
+ Media Queries 4, the second are not allowed in `sizes`. A supported feature with a value
122
+ of the wrong type, such as `(min-width: 600)`, is unknown too, and `validate` reports it;
123
+ a math function as a `resolution` value is unknown but valid. A height-based condition
124
+ is unknown while `Environment.height` is `None`.
114
125
  - **`auto`.** When the value is `auto` or starts with `auto,` (any ASCII case), `auto`
115
126
  stands for `auto_width` if one is given: an image that allows auto-sizes, rendered at
116
127
  that width. Otherwise it is skipped and the next entry applies.
@@ -131,6 +142,11 @@ media conditions), and the density arithmetic that ties the two together. This l
131
142
  implements the WHATWG algorithms for all three steps, typed and without dependencies, and
132
143
  checks itself against the web-platform-tests cases that browsers run.
133
144
 
145
+ Python HTML parsers, `html.parser` in the standard library included, hand the attribute
146
+ over as one string. On npm, `parse-srcset` (1.0.2) and `srcset` (5.0.3) parse and write
147
+ the `srcset` value; their documentation does not cover `sizes` or choosing an image
148
+ (checked on 2026-11-28).
149
+
134
150
  ## Standards
135
151
 
136
152
  - WHATWG HTML: parsing a srcset attribute, parsing a sizes attribute, creating and
@@ -152,7 +168,7 @@ records where they come from, and their tests skip until the script has fetched
152
168
 
153
169
  ## License
154
170
 
155
- MIT. See [LICENSE](LICENSE).
171
+ MIT. See [LICENSE](https://github.com/JaviLianes8/srcset/blob/main/LICENSE).
156
172
 
157
173
  ## Links
158
174
 
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "srcset"
7
- version = "0.2.0"
7
+ version = "1.0.0"
8
8
  description = "Parse, build, check and resolve the HTML srcset and sizes attributes like a browser"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -16,7 +16,7 @@ keywords = [
16
16
  "image", "parser",
17
17
  ]
18
18
  classifiers = [
19
- "Development Status :: 4 - Beta",
19
+ "Development Status :: 5 - Production/Stable",
20
20
  "Environment :: Console",
21
21
  "Intended Audience :: Developers",
22
22
  "Operating System :: OS Independent",
@@ -34,14 +34,14 @@ classifiers = [
34
34
  dependencies = []
35
35
 
36
36
  [project.optional-dependencies]
37
- dev = ["ruff", "mypy", "pytest", "build", "twine"]
37
+ dev = ["ruff", "mypy", "pytest", "pytest-timeout", "build", "twine"]
38
38
 
39
39
  [project.scripts]
40
40
  srcset = "srcset._cli:main"
41
41
 
42
42
  [project.urls]
43
43
  Homepage = "https://lianes8studio.com"
44
- Repository = "https://github.com/JaviLianes8/srcset"
44
+ Source = "https://github.com/JaviLianes8/srcset"
45
45
  Issues = "https://github.com/JaviLianes8/srcset/issues"
46
46
  Changelog = "https://github.com/JaviLianes8/srcset/blob/main/CHANGELOG.md"
47
47
  LinkedIn = "https://www.linkedin.com/in/jlianes/"
@@ -50,8 +50,23 @@ Funding = "https://buymeacoffee.com/jlianesglrs"
50
50
  [tool.hatch.build.targets.wheel]
51
51
  packages = ["src/srcset"]
52
52
 
53
+ [tool.hatch.build.hooks.custom]
54
+ path = "scripts/_build_hook.py"
55
+
53
56
  [tool.hatch.build.targets.sdist]
54
- exclude = ["IMPROVEMENTS-round*.md", "tests/upstream/*.html", ".github"]
57
+ ignore-vcs = true
58
+ include = ["/src", "/tests", "/scripts", "/*.md", "/requirements-dev.txt", "/pyproject.toml", "/LICENSE"]
59
+ # Third-party test files are fetched locally and never shipped; only their manifest is.
60
+ exclude = [
61
+ ".github",
62
+ "IMPROVEMENTS-round*.md",
63
+ "CERT-*.md",
64
+ ".gitignore",
65
+ ".gitattributes",
66
+ "tests/upstream/*.html",
67
+ "**/__pycache__",
68
+ "**/*.pyc",
69
+ ]
55
70
 
56
71
  [tool.ruff]
57
72
  line-length = 100
@@ -68,3 +83,4 @@ strict = true
68
83
  testpaths = ["tests"]
69
84
  pythonpath = ["src"]
70
85
  addopts = "-q"
86
+ timeout = 300