Skip to content

Match range inputs as always in range - #362

Closed
RavSinghChandan wants to merge 2 commits into
facelessuser:mainfrom
RavSinghChandan:range-input-clamped
Closed

RavSinghChandan wants to merge 2 commits into
facelessuser:mainfrom
RavSinghChandan:range-input-clamped

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

:out-of-range matches a range input whose value is outside its min and max, and :in-range skips a range input 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 range input 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-range and never :out-of-range. In Chrome, querySelectorAll gives that for every case in the tests.

Changes:

  • css_parser.py: CSS_IN_RANGE matches a range input without min or max, and CSS_OUT_OF_RANGE no longer includes range.
  • css_match.py: match_range() returns in range for a range input.
  • Tests: the range cases of test_in_range_range, test_out_of_range_range and the numeric exponent tests expected a value outside min/max to be out of range, as for number. They now expect what Chrome gives, with new cases for a value above the default maximum, min greater than max, and an upper-case type.
  • Changelog entry under 4.1.1.

The full test suite (695 passed) and ruff pass. I found this by comparing soupsieve's matches with Chrome on generated and real documents.

@gir-bot gir-bot added S: needs-review Needs to be reviewed and/or approved. C: css-level-4 CSS level 4 selectors. C: css-matching Related to CSS matching. C: css-parsing Related to CSS parsing. C: docs Related to documentation. C: source Related to source code. C: tests Related to testing. labels Nov 13, 2026

@facelessuser facelessuser left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(), for range, 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"] in CSS_OUT_OF_RANGE, without the [min]/[max] requirement, since min="200" alone is enough to put it out of range. Let match_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 range inputs that are disabled (including through a disabled fieldset), readonly, or inside a datalist from 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.

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Thank you for checking it against the spec and three browsers. All three points are done:

  1. For range, match_range() now resolves the minimum and maximum, falling back to 0 and 100 when absent or invalid, and the input is out of range only when the maximum is less than the minimum. [type="range"] is back in CSS_OUT_OF_RANGE without the [min]/[max] requirement. Case 14 now expects :out-of-range, and your four other cases are added to both test files.
  2. Range inputs that are disabled (including through a disabled fieldset, but not in its first legend), readonly, or inside a datalist are excluded from both patterns. Your five cases are in both tests, matching neither.
  3. The changelog now says: a range input's value is clamped to its minimum and maximum, 0 and 100 by default, so it matches :out-of-range only when its maximum is less than its minimum, and :in-range otherwise; one that is disabled, readonly or in a datalist matches neither.

The full test suite (695) and ruff pass.

facelessuser pushed a commit that referenced this pull request Nov 13, 2026
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>
@facelessuser

Copy link
Copy Markdown
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C: css-level-4 CSS level 4 selectors. C: css-matching Related to CSS matching. C: css-parsing Related to CSS parsing. C: docs Related to documentation. C: source Related to source code. C: tests Related to testing. S: needs-review Needs to be reviewed and/or approved.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants