Repository navigation
implement range input sanitization - #19761
Conversation
|
Heads up! This PR modifies the following files:
|
| /// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number | ||
| pub fn best_representation_of_the_float_number(&mut self) { | ||
| let input = &self.0.clone(); | ||
| match input.parse::<f64>() { |
There was a problem hiding this comment.
Can we re-use the logic in is_valid_float_number_string, or refactor so the logic isn't duplicated?
| year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) | ||
| } | ||
|
|
||
|
|
|
|
||
| /// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number | ||
| pub fn best_representation_of_the_float_number(&mut self) { | ||
| let input = &self.0.clone(); |
There was a problem hiding this comment.
I think you can do same way as is_valid_float_number_string.
Just passing &self.0 into parse_float_number then no need to clone here.
There was a problem hiding this comment.
is_valid_float_number_string don't need to change self.
However, here we change it. Need clone it to avoid conflict.
There was a problem hiding this comment.
oh, maybe I'm not familiar with borrow checker enough.
If you passing self.0 with reference, will the variable be moved?
| } | ||
|
|
||
| /// https://html.spec.whatwg.org/multipage/#best-representation-of-the-number-as-a-floating-point-number | ||
| pub fn best_representation_of_the_float_number(&mut self) { |
There was a problem hiding this comment.
IMO, maybe naming the function with a set prefix might be better to let other developers to know this function will set value.
| self.0 = val; | ||
| }, | ||
| _ => {} | ||
| } |
There was a problem hiding this comment.
Maybe changing this match to if let might be better; then you don't need to use the wildcard matching here.
There was a problem hiding this comment.
Could you paste code let me know?
There was a problem hiding this comment.
I think we might be able to do this like
if let Ok(parsed_value) = parse_float_number(input) {
self.0 = parsed_value;
}| val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+") | ||
| ) => { | ||
| // FIXME(tigercosmos): need consider `min`, `max`, `step`, when they are implemented | ||
| Ok(format!("{}", val.round())) |
There was a problem hiding this comment.
Is there a reason why this can't be Ok(val.round().to_string())?
| input.parse::<f64>().ok().map_or(false, |val| { | ||
| !(val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+")) | ||
| }) | ||
| pub fn is_valid_float_number_string(&self) -> bool { |
There was a problem hiding this comment.
I find the terminology here very odd. We usually call them "floats" or "floating point numbers", but never "float number".
| @@ -306,11 +306,15 @@ impl DOMString { | |||
| /// A valid number is the same as what rust considers to be valid, | |||
| /// except for +1., NaN, and Infinity. | |||
There was a problem hiding this comment.
This comment should be pasted above parse_float_number.
| Ok(val) if !( | ||
| val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+") | ||
| ) => { | ||
| // FIXME(tigercosmos): need consider `min`, `max`, `step`, when they are implemented |
There was a problem hiding this comment.
Please do file an issue about this.
| // TODO: Implement more value sanitization algorithms for different types of inputs | ||
| InputType::Range => { | ||
| let mut textinput = self.textinput.borrow_mut(); | ||
| textinput.single_line_content_mut() |
There was a problem hiding this comment.
I believe we don't need to create the textinput variable here:
self.textinput
.borrow_mut()
.single_line_content_mut()
.set_best_representation_of_the_float_number();| year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) | ||
| } | ||
|
|
||
| // https://html.spec.whatwg.org/multipage/#rules-for-parsing-floating-point-number-values |
There was a problem hiding this comment.
Maybe this one could be a documentation comment which should be start with ///.
| match input.parse::<f64>() { | ||
| Ok(val) if !( | ||
| // A valid number is the same as what rust considers to be valid, | ||
| // except for +1., NaN, and Infinity. |
There was a problem hiding this comment.
This does not account for trimming ASCII whitespace, I am also not sure this properly accounts for steps 15-17 of the spec.
| } | ||
|
|
||
| /// https://html.spec.whatwg.org/multipage/#rules-for-parsing-floating-point-number-values | ||
| fn parse_floating_point_number(input: &str) -> Result<String, ()> { |
There was a problem hiding this comment.
Why does this method return a String?
There was a problem hiding this comment.
do you mean return f64?
I think string is better, so we don't parse again.
There was a problem hiding this comment.
Actually, @nox has a point here, there's no reason why this function needs to return a String, and it doesn't make sense either since the name of this function starts with parse. I would expect it to return a Result containing a floating number instead.
| expected: FAIL | ||
|
|
||
| [range input value equals 2] | ||
| [range input value set to an integer] |
There was a problem hiding this comment.
Originally pass because of accident, in other words, it is lucky.
Origin code doesn't do anything, but we now add logic.
There was a problem hiding this comment.
Can you elaborate on what this accident is about? Is it a case of where two negatives makes a positive?
There was a problem hiding this comment.
there is no function to check the value originally, so the output is still as same as input, which made it passed. But the new code do some checking, and without step inside, the checking is not completed. That's why here is a test become to failed.
range input value equals 2 test:
the divisor is 1 as default, so 4.6 will turn to 5
but when step is 0.1, 4.6 is still 4.6, and 4.67 will turn to be 4.7
| self.textinput | ||
| .borrow_mut() | ||
| .single_line_content_mut() | ||
| .set_best_representation_of_the_floating_point_number(); |
There was a problem hiding this comment.
Please add a spec link for this.
| }) | ||
| pub fn is_valid_floating_point_number_string(&self) -> bool { | ||
| // for the case that `parse_floating_point_number` cannot handle | ||
| if *&self.0.contains(" ") { |
There was a problem hiding this comment.
Why is this creating a reference and then immediately dereferenced? Also, this check does not look like it belongs here, but rather in parse_floating_point_number. Even so, step 6 of the parsing algorithm explicitly states to skip ASCII whitespace in front. Why is this check necessary?
There was a problem hiding this comment.
valid floating number cannot include whitespace.
skipping the front whitespace is for parsing, which is for making the input to best presentation.
the two purpose are different. One can have whitespace and the one cannot. That's why the code here do this.
There was a problem hiding this comment.
This defies intuition, since this would mean an invalid floating point number string can successfully be parsed. I'm going to have to clarify the spec language with WHATWG.
Regardless of what the result may be, you still need to address the first question of my last comment.
There was a problem hiding this comment.
Okay, so WHATWG replied that it is indeed OK to successfully parse an invalid string. Ignore that last comment and please address the first question in the first comment.
|
@bors-servo r+ Thanks! |
|
📌 Commit b29230b has been approved by |
implement range input sanitization <!-- Please describe your changes on the following line: --> implement range input sanitation. Since there is no `min`, `max`, `step` implementation currently, this should be continued in the future. r? KiChjang --- <!-- 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 #19172 (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/19761) <!-- 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 |
Upstreamed from servo/servo#19761 [ci skip]
implement range input sanitation.
Since there is no
min,max,stepimplementation currently, this should be continued in the future.r? KiChjang
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is