Repository navigation
implement valid DatetimeLocal input - #19602
Conversation
|
Heads up! This PR modifies the following files:
|
|
r? @KiChjang |
|
@KiChjang Could you take a look? |
| } | ||
|
|
||
| /// Set string to this `DOMString` | ||
| pub fn set_str(&mut self, string: &str) { |
There was a problem hiding this comment.
I don't really see a purpose for this?
| 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)); |
There was a problem hiding this comment.
This can just be
self.0 = format!("...");| // Step 6 | ||
| let mut second_float: f32 = 0.0; | ||
| let second = iterator.next(); | ||
| if second.is_some() { |
There was a problem hiding this comment.
if let Some(second) = iterator.next() {
second_float = second.parse::<f32>()?;
}| /// 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); |
There was a problem hiding this comment.
Isn't this just format!("{:04}", year)? Documentation
|
|
||
| if ! textinput.single_line_content().is_valid_time_string() { | ||
| if !textinput.single_line_content().is_valid_time_string() { | ||
| *textinput.single_line_content_mut() = "".into(); |
There was a problem hiding this comment.
I wonder if this is equivalent to just calling .clear() on the DOMString?
There was a problem hiding this comment.
NO. We can't do this. It cause error
There was a problem hiding this comment.
seems the function not work well.
it just return () as error
There was a problem hiding this comment.
@KiChjang I think we can open a issue try to refactor this later.
There was a problem hiding this comment.
I'd like to at least know the exact reason why it doesn't work. Can you paste the error you're seeing?
There was a problem hiding this comment.
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`.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
sounds good. let me try.
| expected: FAIL | ||
|
|
||
| [change state from datetime-local to range] | ||
| expected: FAIL |
There was a problem hiding this comment.
It just move position in accidient
|
@KiChjang done. Please check |
| 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); |
There was a problem hiding this comment.
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.
| // Step 6 | ||
| let mut second_float: f32 = 0.0; | ||
| if let Some(second) = iterator.next() { | ||
| second_float = second.parse::<f32>().unwrap(); |
There was a problem hiding this comment.
it need to unwrap to get value.
second_float = second.parse::()
the trait
bitflags::<unnamed>::convert::From<bitflags::<unnamed>::num::ParseFloatError>is not implement
There was a problem hiding this comment.
My real question is why do we not use ? here?
There was a problem hiding this comment.
because second_float = second.parse::<f32>()?; causes error
https://play.rust-lang.org/?gist=2761a960baead4f9cb0a3ffaf862006e&version=stable
There was a problem hiding this comment.
How about .map_err(|_| ())? ?
There was a problem hiding this comment.
I try. not work.
How about .parse::<f32>().unwrap_or(());
There was a problem hiding this comment.
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))
}There was a problem hiding this comment.
OK. This is good in the end!
if let Some(second) = iterator.next() {
second_float = second.parse::<f32>().map_err(|_| ())?
}
|
@KiChjang done. Please check |
KiChjang
left a comment
There was a problem hiding this comment.
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?
|
|
||
| // Step 6 | ||
| let mut second_float: f32 = 0.0; | ||
| if let Some(second) = iterator.next() { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
I'd like to at least know the exact reason why it doesn't work. Can you paste the error you're seeing?
| 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') { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
We can still simplify this further:
self.0 = if second == 0.0 {
format!(...)
} else {
format!(...)
};|
@KiChjang Could you take a look? |
| /// 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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Another idea would be to simply return if the parsing function returns Err.
|
@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() |
There was a problem hiding this comment.
Is it possible to remove the check for valid string and still produce the correct behavior?
There was a problem hiding this comment.
I don't think so. SPEC says check valid first, and if valid make it normalized.
IMO, two functions to do that is better.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
@KiChjang It's simplified. |
|
@bors-servo r+ Thanks! |
|
📌 Commit b434241 has been approved by |
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
sure. it's ok. :)
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
@KiChjang Yes, I think the left curly brace will be next line.
|
💔 Test failed - mac-rel-css2 |
|
@bors-servo retry
|
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 -->
|
☀️ 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 |
|
Cheer~ |
Upstreamed from servo/servo#19602 [ci skip]
implement valid Date time Local input
part of #19172
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is