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
Implement range input sanitization #21952
Closed
+92
−149
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
Implement range input sanitization
Fixes #19773
- Loading branch information
commit e790002e6607baf9ef4f0015489acfb45275da82
| @@ -1193,7 +1193,47 @@ impl HTMLInputElement { | ||
| }, | ||
| // https://html.spec.whatwg.org/multipage/#range-state-(type=range):value-sanitization-algorithm | ||
| InputType::Range => { | ||
| value.set_best_representation_of_the_floating_point_number(); | ||
| let minimum = self.Min().parse().unwrap_or(0f64); | ||
|
||
| let maximum = self.Max().parse().unwrap_or(100f64); | ||
|
|
||
| let new_v = if let Ok(value) = value.trim().parse::<f64>() { | ||
| if value < minimum || maximum < minimum { | ||
| minimum | ||
| } else if value > maximum { | ||
| maximum | ||
| } else { | ||
| value | ||
| } | ||
nox
Member
|
||
| } else { | ||
| if maximum <= minimum { | ||
|
||
| minimum | ||
| } else { | ||
| minimum + (maximum - minimum) / 2f64 | ||
| } | ||
| }; | ||
|
|
||
| let step = self.Step().parse::<f64>().unwrap_or(1f64); | ||
| let delta = (new_v - minimum) - step * ((new_v - minimum) / step).floor(); | ||
| let new_v = if delta != 0f64 { | ||
| let step_below = new_v - delta; | ||
| let step_above = new_v - delta + step; | ||
| let half_step = step / 2f64; | ||
| let step_above_is_closest = (step_above - new_v) <= half_step; | ||
| let step_above_in_range = step_above >= minimum && step_above <= maximum; | ||
| let step_below_in_range = step_below >= minimum && step_below <= maximum; | ||
|
|
||
| if (step_above_is_closest || !step_below_in_range) && step_above_in_range { | ||
| step_above | ||
| } else if (!step_above_is_closest || !step_above_in_range) && step_below_in_range { | ||
| step_below | ||
| } else { | ||
| new_v | ||
| } | ||
| } else { | ||
| new_v | ||
| }; | ||
|
||
|
|
||
| *value = DOMString::from_string(new_v.to_string()); | ||
| }, | ||
| _ => (), | ||
| } | ||
| @@ -1344,6 +1384,12 @@ impl VirtualMethods for HTMLInputElement { | ||
| self.textinput.borrow_mut().set_content(value); | ||
| self.update_placeholder_shown_state(); | ||
| }, | ||
| &local_name!("max") | &local_name!("min") | &local_name!("step") => { | ||
| let mut textinput = self.textinput.borrow_mut(); | ||
| let mut value = textinput.single_line_content().clone(); | ||
| self.sanitize_value(&mut value); | ||
| textinput.set_content(value); | ||
| }, | ||
| &local_name!("name") if self.input_type() == InputType::Radio => { | ||
| self.radio_group_updated( | ||
| mutation.new_value(attr).as_ref().map(|name| name.as_atom()), | ||
This file was deleted.
Oops, something went wrong.
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Would be nice to not directly go through the IDL getters to avoid a
DOMStringcopy. AFAIKmin,maxandstepattribute values should be stored as integers inHTMLInputElement, feel free to ping me on IRC if you need more details about that.