Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime-local value sanitization removes leading zeroes for seconds #25493

Closed
teapotd opened this issue Jan 11, 2020 · 2 comments
Closed

datetime-local value sanitization removes leading zeroes for seconds #25493

teapotd opened this issue Jan 11, 2020 · 2 comments
Assignees
Labels

Comments

@teapotd
Copy link
Contributor

@teapotd teapotd commented Jan 11, 2020

<input type="datetime-local" value="2000-01-01T12:30:00.5">
<input type="datetime-local" value="2000-01-01T12:30:01">
<script>
    let result = '';
    for (let elem of document.querySelectorAll("input")) {
        result += elem.valueAsNumber + ' ' + elem.value + '\n';
    }
    alert(result);
</script>

In the above example the leading zeroes for seconds are removed during sanitization, but they are required in valid datetime-local strings. This makes valueAsNumber fail, for example.

https://html.spec.whatwg.org/multipage/#valid-normalised-local-date-and-time-string
https://html.spec.whatwg.org/multipage/#valid-time-string

pub fn convert_valid_normalized_local_date_and_time_string(&mut self) -> Result<(), ()> {
let ((year, month, day), (hour, minute, second)) =
self.parse_local_date_and_time_string()?;
if second == 0.0 {
self.0 = format!(
"{:04}-{:02}-{:02}T{:02}:{:02}",
year, month, day, hour, minute
);
} else {
self.0 = format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{}",
year, month, day, hour, minute, second
);
}
Ok(())
}

@jdm jdm added the A-content/dom label Jan 11, 2020
@pshaughn
Copy link
Member

@pshaughn pshaughn commented Jan 11, 2020

Yes, the last field there should also have {:02}, sorry I didn't catch this when I was otherwise tweaking the time string code.

@pshaughn pshaughn self-assigned this Jan 11, 2020
@pshaughn
Copy link
Member

@pshaughn pshaughn commented Jan 11, 2020

{:02} is still wrong when there are millis; I've written a test to flex the various cases and I think I can pass them all by adding a branch for 0.0<second<10.0 that adds one leading zero

@teapotd teapotd mentioned this issue Jan 13, 2020
4 of 4 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.