Skip to content

Fix :in-range and :out-of-range for time and datetime-local values with seconds - #323

Merged
facelessuser merged 1 commit into
facelessuser:mainfrom
spicysharkco:fix/time-seconds-range
Oct 20, 2026
Merged

facelessuser merged 1 commit into
facelessuser:mainfrom
spicysharkco:fix/time-seconds-range

Conversation

@spicysharkco

Copy link
Copy Markdown
Contributor

:in-range and :out-of-range treat a time or datetime-local value with seconds as invalid, so it is never out of range, and drop a min or max with seconds as if it were missing. The same call fails on 1.7.0, 1.9.6, 2.0.1, 2.5 and 3.0.3; 1.7 is where these selectors were added.

import soupsieve as sv
from bs4 import BeautifulSoup

soup = BeautifulSoup('<input id="a" type="time" min="09:00" max="11:00" value="12:30:15">', 'html.parser')
sv.select(':out-of-range', soup)

On the current main this returns [] and :in-range returns the input. It should return the input for :out-of-range. The HTML spec says a valid time string may continue after the minute with "A U+003A COLON character (:)" and "Two ASCII digits, representing the integer part of second, in the range 0 ≤ s ≤ 59", so 12:30:15 is valid. After the seconds it allows a fraction of "One, two, or three ASCII digits, representing the fractional part of second".

RE_TIME (L60) and RE_DATETIME (L64-L66) only accept HH:MM.

Both now take optional seconds with an optional fraction of one to three digits. Seconds of 60 or more stay invalid. The parsed tuple always ends with the seconds, so 18:45 and 18:45:00 compare equal.

The new rows are in the time and datetime-local tests of both files, with seconds in the value and in min and max, a fraction, :60, and :00. python -m pytest tests/test_level4/test_in_range.py tests/test_level4/test_out_of_range.py: 4 failed before the fix, 16 passed after. Whole suite: 424 passed, 1 skipped.

…alues with seconds

`RE_TIME` and `RE_DATETIME` only accepted `HH:MM`, but a valid time
string may also carry seconds and up to three fraction digits. A value
such as `12:30:15` was treated as invalid and so never out of range, and
a `min` or `max` with seconds was dropped as if it were missing. Accept
the optional seconds part, check it is below 60, and keep it as the last
item of the parsed tuple so `18:45` and `18:45:00` still compare equal.

Releases from 1.7, where these selectors were added, through 3.0.3
return the wrong result for a `time` input with `min="09:00"`,
`max="11:00"` and `value="12:30:15"`.
@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: docs Related to documentation. C: source Related to source code. C: tests Related to testing. labels Oct 20, 2026
@facelessuser

Copy link
Copy Markdown
Owner

Do browsers allow seconds as decimals? I will need to verify results in real browsers, but generally I think this is a good fix.

@spicysharkco

Copy link
Copy Markdown
Contributor Author

Yes, up to three digits after the seconds. I opened the same 11 inputs in Chrome 155 and Firefox 149 (Developer Edition) and read matches(':in-range') and matches(':out-of-range') on each. The two browsers agree with each other and with this branch on all 11; main differs on 7 (rows 1, 3, 5, 6, 8, 9 and 11).

# type min / max value Chrome, Firefox, this branch
1 time 09:00 / 11:00 12:30:15 out of range
2 time 09:00 / 11:00 10:30:15.5 in range
3 time 09:00 / 11:00 12:30:15.123 out of range
4 time 09:00 / 11:00 12:30:15.1234 in range
5 time 09:00:00.5 / 09:00:00.9 09:00:00.7 in range
6 time 09:00:00.5 / 09:00:00.9 09:00:00.95 out of range
7 time 09:00 / 11:00 12:30:60 in range
8 time 09:00:30 / 09:00:45 09:00 out of range
9 datetime-local 2026-10-20T09:00 / 2026-10-20T11:00 2026-10-20T12:30:15.5 out of range
10 datetime-local 2026-10-20T09:00 / 2026-10-20T11:00 2026-10-20T10:30:15.250 in range
11 datetime-local 2026-10-20T09:00:00.5 / 2026-10-20T09:00:00.9 2026-10-20T09:00:00.95 out of range

A fraction of one to three digits is read as part of the time. A fourth digit (row 4) or :60 (row 7) makes both browsers drop the value (input.value is ""), so the input is in range, which is what this branch does with a value it cannot parse. I did not try Safari.

Page used
<!doctype html>
<input id="A" type="time" min="09:00" max="11:00" value="12:30:15">
<input id="B" type="time" min="09:00" max="11:00" value="10:30:15.5">
<input id="C" type="time" min="09:00" max="11:00" value="12:30:15.123">
<input id="D" type="time" min="09:00" max="11:00" value="12:30:15.1234">
<input id="E" type="time" min="09:00:00.5" max="09:00:00.9" value="09:00:00.7">
<input id="F" type="time" min="09:00:00.5" max="09:00:00.9" value="09:00:00.95">
<input id="G" type="time" min="09:00" max="11:00" value="12:30:60">
<input id="H" type="time" min="09:00:30" max="09:00:45" value="09:00">
<input id="I" type="datetime-local" min="2026-10-20T09:00" max="2026-10-20T11:00" value="2026-10-20T12:30:15.5">
<input id="J" type="datetime-local" min="2026-10-20T09:00" max="2026-10-20T11:00" value="2026-10-20T10:30:15.250">
<input id="K" type="datetime-local" min="2026-10-20T09:00:00.5" max="2026-10-20T09:00:00.9" value="2026-10-20T09:00:00.95">
<pre id="out"></pre>
<script>
var r = [];
document.querySelectorAll('input').forEach(function (e) {
  r.push(e.id + ' in=' + e.matches(':in-range') + ' out=' + e.matches(':out-of-range') + ' value="' + e.value + '"');
});
document.getElementById('out').textContent = r.join('\n');
</script>

@facelessuser

Copy link
Copy Markdown
Owner

I've verified the cases as well.

@gir-bot lgtm

@gir-bot gir-bot added S: approved The pull request is ready to be merged. and removed S: needs-review Needs to be reviewed and/or approved. labels Oct 20, 2026
@facelessuser
facelessuser merged commit b0de0e6 into facelessuser:main Oct 20, 2026
18 checks passed
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: docs Related to documentation. C: source Related to source code. C: tests Related to testing. S: approved The pull request is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants