Skip to content

Implement value sanitization for Time type input #19603

Closed

Description

@tigercosmos

has been implemented:

  • is_valid_time_string
  • parse a time string
  • parse a time component

need to fix first before #19587(Date-time type)
part of #19172

Activity

  1. tigercosmos commented on Dec 19, 2017

    @tigercosmos
    ContributorAuthor

    @highfive: assign me

  2. highfive commented on Dec 19, 2017

    @highfive

    Hey @tigercosmos! Thanks for your interest in working on this issue. It's now assigned to you!

  3. added
    C-assigned
    on Dec 19, 2017
  4. tigercosmos commented on Dec 19, 2017

    @tigercosmos
    ContributorAuthor

    @KiChjang There's a dilemma. Currently is_valid_time_string is not suitable to transfer to parse a time string and parse a time component, because it deal with the string by chars. But parsing need to return (HH, MM, SS) = (u32, u32, u32).

    I would use split(":") and change string to u32 in parsing functions.
    However, Reinventing the wheel seems not a good idea.

    The best relationship might be:
    parse_time_component -> parse_time_string -> is_valid_time_string
    Logic should be reused.

    cc. @SWW13

    /// Validates this `DOMString` is a time string according to
    /// <https://html.spec.whatwg.org/multipage/#valid-time-string>.
    pub fn is_valid_time_string(&self) -> bool {
    enum State {
    HourHigh,
    HourLow09,
    HourLow03,
    MinuteColon,
    MinuteHigh,
    MinuteLow,
    SecondColon,
    SecondHigh,
    SecondLow,
    MilliStop,
    MilliHigh,
    MilliMiddle,
    MilliLow,
    Done,
    Error,
    }
    let next_state = |valid: bool, next: State| -> State { if valid { next } else { State::Error } };
    let state = self.chars().fold(State::HourHigh, |state, c| {
    match state {
    // Step 1 "HH"
    State::HourHigh => {
    match c {
    '0' | '1' => State::HourLow09,
    '2' => State::HourLow03,
    _ => State::Error,
    }
    },
    State::HourLow09 => next_state(c.is_digit(10), State::MinuteColon),
    State::HourLow03 => next_state(c.is_digit(4), State::MinuteColon),
    // Step 2 ":"
    State::MinuteColon => next_state(c == ':', State::MinuteHigh),
    // Step 3 "mm"
    State::MinuteHigh => next_state(c.is_digit(6), State::MinuteLow),
    State::MinuteLow => next_state(c.is_digit(10), State::SecondColon),
    // Step 4.1 ":"
    State::SecondColon => next_state(c == ':', State::SecondHigh),
    // Step 4.2 "ss"
    State::SecondHigh => next_state(c.is_digit(6), State::SecondLow),
    State::SecondLow => next_state(c.is_digit(10), State::MilliStop),
    // Step 4.3.1 "."
    State::MilliStop => next_state(c == '.', State::MilliHigh),
    // Step 4.3.2 "SSS"
    State::MilliHigh => next_state(c.is_digit(6), State::MilliMiddle),
    State::MilliMiddle => next_state(c.is_digit(10), State::MilliLow),
    State::MilliLow => next_state(c.is_digit(10), State::Done),
    _ => State::Error,
    }
    });
    match state {
    State::Done |
    // Step 4 (optional)
    State::SecondColon |
    // Step 4.3 (optional)
    State::MilliStop |
    // Step 4.3.2 (only 1 digit required)
    State::MilliMiddle | State::MilliLow => true,
    _ => false
    }
    }

  5. KiChjang commented on Dec 19, 2017

    @KiChjang
    Contributor

    I don't see much value in trying to reimplement is_valid_time_string solely in terms of parse_time_component and parse_time_string. That function today is well-optimized for its only use case, which is to validate whether the string itself is a valid time string. In fact, I think this implementation is much more spec-compliant since it iterates over the characters, instead of splitting them on the : codepoint.

  6. tigercosmos commented on Dec 19, 2017

    @tigercosmos
    ContributorAuthor

    @KiChjang I would implement parse function because date time input will use it. I cannot use valid function, because it is bind to Dom.

  7. tigercosmos commented on Dec 19, 2017

    @tigercosmos
    ContributorAuthor

    @KiChjang So do you think I just implement parse function aside with valid one is fine?

  8. KiChjang commented on Dec 19, 2017

    @KiChjang
    Contributor

    Yes.

  9. SWW13 commented on Dec 19, 2017

    @SWW13
    Contributor

    @tigercosmos You could possibly reuse parts of the state machine and combine the Low, Middle and High states for parsing.

  10. added 2 commits that reference this issue on Jan 7, 2018
    d30bf72
    7f36b59
  11. changed the title [-]implement Time type inputs[/-] [+]Implement value sanitization for Time type input[/+] on Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions