Skip to content

implement valid DatetimeLocal input - #19602

Merged
bors-servo merged 1 commit into
servo:masterfrom
tigercosmos:r1
Jan 7, 2018
Merged

bors-servo merged 1 commit into
servo:masterfrom
tigercosmos:r1

Conversation

@tigercosmos

@tigercosmos tigercosmos commented Dec 19, 2017 •

Copy link
Copy Markdown
Contributor

implement valid Date time Local input

part of #19172


  • There are tests for these changes OR
  • These changes do not require tests because _____

This change is Reviewable

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/htmlinputelement.rs, components/script/dom/bindings/str.rs
  • @fitzgen: components/script/dom/htmlinputelement.rs, components/script/dom/bindings/str.rs
  • @KiChjang: components/script/dom/htmlinputelement.rs, components/script/dom/bindings/str.rs

@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify script code, but no tests are modified. Please consider adding a test!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Dec 19, 2017
@tigercosmos

Copy link
Copy Markdown
Contributor Author

r? @KiChjang

@highfive highfive assigned KiChjang and unassigned Manishearth Dec 19, 2017
@tigercosmos tigercosmos changed the title [WIP] implement valid DatetimeLocal input implement valid DatetimeLocal input Dec 19, 2017
@tigercosmos

Copy link
Copy Markdown
Contributor Author

@KiChjang Could you take a look?

Comment thread components/script/dom/bindings/str.rs Outdated
}

/// Set string to this `DOMString`
pub fn set_str(&mut self, string: &str) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't really see a purpose for this?

Comment thread components/script/dom/bindings/str.rs Outdated
let hour_str = format_zero(hour, 2);
let minute_str = format_zero(minute, 2);
if second == 0.0 {
self.set_str(&*format!("{}-{}-{}T{}:{}", year_str, month_str, day_str, hour_str, minute_str));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can just be

self.0 = format!("...");

Comment thread components/script/dom/bindings/str.rs Outdated
// Step 6
let mut second_float: f32 = 0.0;
let second = iterator.next();
if second.is_some() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if let Some(second) = iterator.next() {
   second_float = second.parse::<f32>()?;
}

Comment thread components/script/dom/bindings/str.rs Outdated
/// https://html.spec.whatwg.org/multipage/#valid-normalised-local-date-and-time-string
pub fn convert_valid_normalized_local_date_and_time_string(&mut self) {
let ((year, month, day), (hour, minute, second)) = parse_local_date_and_time_string(&*self.0).unwrap();
let year_str = format_zero(year, 4);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't this just format!("{:04}", year)? Documentation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

cool! thanks


if ! textinput.single_line_content().is_valid_time_string() {
if !textinput.single_line_content().is_valid_time_string() {
*textinput.single_line_content_mut() = "".into();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if this is equivalent to just calling .clear() on the DOMString?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

let me try

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

NO. We can't do this. It cause error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

seems the function not work well.
it just return () as error

@tigercosmos tigercosmos Dec 25, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@KiChjang I think we can open a issue try to refactor this later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd like to at least know the exact reason why it doesn't work. Can you paste the error you're seeing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

error[E0614]: type `()` cannot be dereferenced
    --> components/script/dom/htmlinputelement.rs:1038:21
     |
1038 |                     *textinput.single_line_content_mut().clear();
     |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `script`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, it means you cannot dereference the tuple type which is the ().
If you just use textinput.single_line_content_mut().clear();, will it work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sounds good. let me try.

expected: FAIL

[change state from datetime-local to range]
expected: FAIL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this failing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It just move position in accidient

@KiChjang KiChjang added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Dec 24, 2017
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Dec 25, 2017
@tigercosmos

Copy link
Copy Markdown
Contributor Author

@KiChjang done. Please check

Comment thread components/script/dom/bindings/str.rs Outdated
let hour_str = format!("{:02}", hour);
let minute_str = format!("{:02}", minute);
if second == 0.0 {
self.0 = format!("{}-{}-{}T{}:{}", year_str, month_str, day_str, hour_str, minute_str);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If that's the case, we actually don't need the *_str intermediate bindings. We can just format them all at once in this string.

Comment thread components/script/dom/bindings/str.rs Outdated
// Step 6
let mut second_float: f32 = 0.0;
if let Some(second) = iterator.next() {
second_float = second.parse::<f32>().unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why unwrap here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it need to unwrap to get value.
second_float = second.parse::()

the trait bitflags::<unnamed>::convert::From<bitflags::<unnamed>::num::ParseFloatError> is not implement

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My real question is why do we not use ? here?

@tigercosmos tigercosmos Dec 25, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about .map_err(|_| ())? ?

@tigercosmos tigercosmos Dec 25, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I try. not work.
How about .parse::<f32>().unwrap_or(());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about something like this?

fn parse_time_component(value: &str) -> Result<(u32, u32, f32), ()> {
    // Step 1
    let mut iterator = value.split(':');
    
    let hour_int = match iterator.next() {
        Some(hour) => {
            if hour.len() != 2 {
                return Err(());
            }

            hour.parse::<u32>().map_err(|_| ())?
        },
        None => return Err(())
    };

    // Step 2
    if hour_int > 23 {
        return Err(());
    }

    // Step 3, 4
    let minute_int = match iterator.next() {
        Some(minute) => {
            if minute.len() != 2 {
                return Err(());
            }

            minute.parse::<u32>().map_err(|_| ())?
        },
        None => return Err(())
    };

    // Step 5
    if minute_int > 59 {
        return Err(());
    }

    // Step 6
    let second_float = match iterator.next() {
        Some(second) => second.parse::<f32>().map_err(|_| ())?,
        None => return Err(())
    };

    Ok((hour_int, minute_int, second_float))
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK. This is good in the end!

if let Some(second) = iterator.next() {
  second_float = second.parse::<f32>().map_err(|_| ())?
}

@tigercosmos

Copy link
Copy Markdown
Contributor Author

@KiChjang done. Please check

@KiChjang KiChjang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Almost there! One thing that I've been wondering is whether the current code is able to handle leading whitespaces. Does the spec mention anything related to this?

Comment thread components/script/dom/bindings/str.rs Outdated

// Step 6
let mut second_float: f32 = 0.0;
if let Some(second) = iterator.next() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let second_float = match iterator.next() {
    Some(second) => second.parse::<f32>().map_err(|_| ())?,
    None => 0
};


if ! textinput.single_line_content().is_valid_time_string() {
if !textinput.single_line_content().is_valid_time_string() {
*textinput.single_line_content_mut() = "".into();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd like to at least know the exact reason why it doesn't work. Can you paste the error you're seeing?

Comment thread components/script/dom/bindings/str.rs Outdated
fn parse_local_date_and_time_string(value: &str) -> Result<((u32, u32, u32), (u32, u32, f32)), ()> {
// Step 1, 2, 4
let mut iterator;
if value.contains('T') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let mut iterator = if value.contains('T') {
    value.split('T')
} else {
    value.split(' ')
};

/// https://html.spec.whatwg.org/multipage/#valid-normalised-local-date-and-time-string
pub fn convert_valid_normalized_local_date_and_time_string(&mut self) {
let ((year, month, day), (hour, minute, second)) = parse_local_date_and_time_string(&*self.0).unwrap();
if second == 0.0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can still simplify this further:

self.0 = if second == 0.0 {
    format!(...)
} else {
    format!(...)
};

@tigercosmos

Copy link
Copy Markdown
Contributor Author

@KiChjang All fixed, also including clear().
The spec doesn't mention leading whitespace is valid or not.

@tigercosmos

Copy link
Copy Markdown
Contributor Author

@KiChjang Could you take a look?

@KiChjang KiChjang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry for the delay!

Comment thread components/script/dom/bindings/str.rs Outdated
/// where date and time are both valid, and the time string must be as short as possible
/// https://html.spec.whatwg.org/multipage/#valid-normalised-local-date-and-time-string
pub fn convert_valid_normalized_local_date_and_time_string(&mut self) {
let ((year, month, day), (hour, minute, second)) = parse_local_date_and_time_string(&*self.0).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a way to remove this unwrap call? Perhaps by making this function not mutate instead, and make it return a Result? As it stands now, this function is left to the programmer to "be smart enough" to validate the string BEFORE calling this function, throwing away our compile-time guarantees.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another idea would be to simply return if the parsing function returns Err.

@KiChjang KiChjang added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Jan 6, 2018
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Jan 6, 2018
@tigercosmos

Copy link
Copy Markdown
Contributor Author

@KiChjang please check

InputType::DatetimeLocal => {
let mut textinput = self.textinput.borrow_mut();
if textinput.single_line_content().is_valid_local_date_and_time_string() {
if textinput.single_line_content_mut()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it possible to remove the check for valid string and still produce the correct behavior?

@tigercosmos tigercosmos Jan 7, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. SPEC says check valid first, and if valid make it normalized.
IMO, two functions to do that is better.

@KiChjang KiChjang Jan 7, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The point here is, since the parsing is already doing the validation anyway, if the parse function is returning an error, then it means it is not a valid string. Is this hypothesis correct?

@tigercosmos tigercosmos Jan 7, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's true. Both is_valid_local_date_and_time_string and convert_valid_normalized_local_date_and_time_string call parse_local_date_and_time_string. I don't combine them because I am afraid that each unit function would be used in other place, and with this point, combining them is not a good choice.

However, if we would not use these functions in other place, switching to valid_and_normalize_local_date_and_time_string, which check valid first and then normalize it, is good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not mean combining them, I meant whether it was possible to just use convert_valid_normalized_local_date_and_time_string instead of calling both is_valid_local_date_and_time_string and convert_valid_normalized_local_date_and_time_string. If so, then we should just use convert_valid_normalized_local_date_and_time_string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's possible.

@tigercosmos

Copy link
Copy Markdown
Contributor Author

@KiChjang It's simplified.

@KiChjang

KiChjang commented Jan 7, 2018

Copy link
Copy Markdown
Contributor

@bors-servo r+

Thanks!

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit b434241 has been approved by KiChjang

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Jan 7, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit b434241 with merge d30bf72...

bors-servo pushed a commit that referenced this pull request Jan 7, 2018
implement valid DatetimeLocal input

<!-- Please describe your changes on the following line: -->
implement valid Date time Local input

part of #19172

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #19587 fix #19603(github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19602)
<!-- Reviewable:end -->
let mut textinput = self.textinput.borrow_mut();
if textinput.single_line_content_mut()
.convert_valid_normalized_local_date_and_time_string().is_err() {
textinput.single_line_content_mut().clear();

@CYBAI CYBAI Jan 7, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit:

Can it be indented like following?

if textinput.single_line_content_mut()
            .convert_valid_normalized_local_date_and_time_string()
            .is_err() {

Oh, @KiChjang has approved. I think this is not a big issue :P

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure. it's ok. :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@CYBAI I'm actually not sure what the proper rustfmt for this is, I suspect it is this instead?

if textinput.single_line_content_mut()
    .convert_valid_normalized_local_date_and_time_string()
    .is_err()
{
    // body goes here
}

but since we don't run rustfmt yet, I don't really care too much as long as ./mach test-tidy says it's ok.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - mac-rel-css2

@highfive highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Jan 7, 2018
@tigercosmos

Copy link
Copy Markdown
Contributor Author

@bors-servo retry

  • gecko-media

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit b434241 with merge 7f36b59...

bors-servo pushed a commit that referenced this pull request Jan 7, 2018
implement valid DatetimeLocal input

<!-- Please describe your changes on the following line: -->
implement valid Date time Local input

part of #19172

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #19587 fix #19603(github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19602)
<!-- Reviewable:end -->
@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-tests-failed The changes caused existing tests to fail. labels Jan 7, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev
Approved by: KiChjang
Pushing 7f36b59 to master...

@bors-servo
bors-servo merged commit b434241 into servo:master Jan 7, 2018
@tigercosmos

Copy link
Copy Markdown
Contributor Author

Cheer~

@tigercosmos
tigercosmos deleted the r1 branch January 7, 2018 12:25
nox pushed a commit to web-platform-tests/wpt that referenced this pull request Jan 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement value sanitization for Time type input Implement value sanitization for Date-time type inputs

6 participants