Repository navigation
Match range inputs as always in range - #362
RavSinghChandan wants to merge 2 commits into
Conversation
facelessuser
left a comment
There was a problem hiding this comment.
I checked this against the HTML spec and against querySelectorAll in Chrome 157, Firefox 159 and Safari 27.2. The core idea is right: a range input whose value is outside its limits is clamped, so it is in range. But the PR overgeneralizes that to "always in range", which the spec doesn't support. Soupsieve follows the spec, so the following needs to change before this can be merged.
1. A range input whose maximum is less than its minimum must match :out-of-range
The range state applies two corrections. An underflowing value is set to the minimum. An overflowing value is set to the maximum only "if the maximum is not less than the minimum". So when the maximum is below the minimum, the value ends up at or above the minimum, which is above the maximum. The element is suffering from an overflow, so it must match :out-of-range and not :in-range. "Reversed range" applies only to types with a periodic domain (time), not to range.
This also applies when one limit falls back to its default (0 for the minimum, 100 for the maximum):
<input type="range" min="10" max="0" value="3"> <!-- explicit max < min -->
<input type="range" min="200"> <!-- min 200 > default max 100 -->
<input type="range" max="-5"> <!-- max -5 < default min 0 -->
<input type="range" min="invalid" max="-5"> <!-- invalid min falls back to 0 -->
<input type="range" min="10" max="0" value=""> <!-- default value is the minimum -->Firefox matches all of these as :out-of-range, as the spec requires. Chrome and Safari match them as :in-range, which is a browser bug and not something to copy.
Required changes:
- In
match_range(), forrange, don't return in-range unconditionally. Resolve the minimum and maximum, falling back to 0 and 100 when an attribute is absent or invalid. The input is out of range when the maximum is less than the minimum and in range otherwise. - Keep
[type="range"]inCSS_OUT_OF_RANGE, without the[min]/[max]requirement, sincemin="200"alone is enough to put it out of range. Letmatch_range()decide. - Test case 14 (
min="10" max="0" value="3") must expect:out-of-range. Add the cases above to both test files.
2. Range inputs barred from constraint validation must match neither pseudo-class
:in-range and :out-of-range apply only to candidates for constraint validation. The new unconditional [type="range"] branch in CSS_IN_RANGE makes soupsieve match these as :in-range, and none of the three browsers matches any of them with either pseudo-class:
<input type="range" disabled>
<input type="range" min="0" max="10" value="50" disabled>
<input type="range" min="0" max="10" value="50" readonly>
<fieldset disabled><input type="range" min="0" max="10" value="50"></fieldset>
<datalist><input type="range" min="0" max="10" value="50"></datalist>The first one is a regression: main matched it with neither pseudo-class, and this branch now matches it as :in-range.
Required changes:
- Exclude
rangeinputs that are disabled (including through a disabledfieldset),readonly, or inside adatalistfrom both patterns. - Add these cases to the tests, expecting no match for either selector.
3. Fix the description and changelog
"A range input is always :in-range and never :out-of-range" is not correct. Please reword the changelog entry to describe the actual rule: a range input's value is clamped to its minimum and maximum (0 and 100 by default), so it is out of range only when its maximum is less than its minimum.
β¦nimum, and skip barred ones
|
Thank you for checking it against the spec and three browsers. All three points are done:
The full test suite (695) and ruff pass. |
A range input's value is clamped to its minimum and maximum, 0 and 100 by default, so it matches :in-range unless its maximum is less than its minimum, in which case it matches :out-of-range, as the HTML spec requires. A range input that is barred from constraint validation (disabled, readonly or in a datalist) matches neither. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I manually merged your fix locally and pushed it, fixing outdated comments and the commit message to properly indicate what the final solution actually does. I've also performed a more in-depth audit, fixing more issues. If all three browsers (Chrome, Safari, Firefox) agree, we agree with the browsers. If the browsers disagree, we follow the spec. Thanks for the PR and for bringing these issues to my attention. |
:out-of-rangematches arangeinput whose value is outside itsminandmax, and:in-rangeskips arangeinput that has neither attribute:import bs4, soupsieve as sv
soup = bs4.BeautifulSoup('', 'html.parser')
print([e['id'] for e in sv.select(':out-of-range', soup)]) # ['a'], expected []
print([e['id'] for e in sv.select(':in-range', soup)]) # [], expected ['a', 'b']
Unlike the other input types, a
rangeinput always has a minimum and a maximum, 0 and 100 by default, and its value sanitization algorithm clamps the value to them. So it has range limitations and is never suffering from an underflow or overflow: it always matches:in-rangeand never:out-of-range. In Chrome,querySelectorAllgives that for every case in the tests.Changes:
css_parser.py:CSS_IN_RANGEmatches arangeinput withoutminormax, andCSS_OUT_OF_RANGEno longer includesrange.css_match.py:match_range()returns in range for arangeinput.rangecases oftest_in_range_range,test_out_of_range_rangeand the numeric exponent tests expected a value outsidemin/maxto be out of range, as fornumber. They now expect what Chrome gives, with new cases for a value above the default maximum,mingreater thanmax, and an upper-case type.The full test suite (695 passed) and
ruffpass. I found this by comparing soupsieve's matches with Chrome on generated and real documents.