Skip to content
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
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Implement range input sanitization

Fixes #19773
  • Loading branch information
Eijebong committed Oct 15, 2018
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);

This comment has been minimized.

Copy link
@nox

nox Oct 16, 2018

Member

Would be nice to not directly go through the IDL getters to avoid a DOMString copy. AFAIK min, max and step attribute values should be stored as integers in HTMLInputElement, feel free to ping me on IRC if you need more details about that.

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
}

This comment has been minimized.

Copy link
@nox

nox Oct 16, 2018

Member

I think this can be a shorter expression:

value.min(maximum).max(minimum)

Clamping by the maximum before the minimum covers the case where the former is less than the latter.

} else {
if maximum <= minimum {

This comment has been minimized.

Copy link
@nox

nox Oct 16, 2018

Member

This should be just <.

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
};

This comment has been minimized.

Copy link
@nox

nox Oct 16, 2018

Member

Note to self: I haven't reviewed this yet.


*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.

@@ -1,35 +1,11 @@
[range.html]
type: testharness
[Converting an illegal string to the default value]
expected: FAIL

[Converting an illegal string to the default step]
expected: FAIL

[the value is set to min when a smaller value than min attribute is given]
expected: FAIL

[the value is set to max when a larger value than max attribute is given]
expected: FAIL

[default value when min and max attributes are given (= min plus half the difference between min and max)]
expected: FAIL

[default value with step control when both min and max attributes are given]
expected: FAIL

[default value when both min and max attributes are given, while min > max]
expected: FAIL

[Step scale factor behavior when min attribute has integer value but max attribute is non-integer ]
expected: FAIL

[The default scale factor is 1 even if step attribute is explicitly set to non-integer value, unless min attribute has non-integer value]
expected: FAIL

[Solving the step mismatch]
expected: FAIL

[Performing stepUp()]
expected: FAIL

@@ -42,3 +18,6 @@
[Performing stepDown() beyond the value of the min attribute]
expected: FAIL

[Skip ASCII whitespace within input]
expected: FAIL

@@ -6,36 +6,24 @@
[change state from hidden to datetime]
expected: FAIL

[change state from hidden to range]
expected: FAIL

[change state from text to email]
expected: FAIL

[change state from text to datetime]
expected: FAIL

[change state from text to range]
expected: FAIL

[change state from search to email]
expected: FAIL

[change state from search to datetime]
expected: FAIL

[change state from search to range]
expected: FAIL

[change state from tel to email]
expected: FAIL

[change state from tel to datetime]
expected: FAIL

[change state from tel to range]
expected: FAIL

[change state from url to text]
expected: FAIL

@@ -51,9 +39,6 @@
[change state from url to datetime]
expected: FAIL

[change state from url to range]
expected: FAIL

[change state from email to hidden]
expected: FAIL

@@ -78,18 +63,12 @@
[change state from email to datetime]
expected: FAIL

[change state from email to range]
expected: FAIL

[change state from password to email]
expected: FAIL

[change state from password to datetime]
expected: FAIL

[change state from password to range]
expected: FAIL

[change state from datetime to text]
expected: FAIL

@@ -117,126 +96,48 @@
[change state from date to datetime]
expected: FAIL

[change state from date to range]
expected: FAIL

[change state from date to range]
expected: FAIL

[change state from month to range]
expected: FAIL

[change state from week to datetime]
expected: FAIL

[change state from week to range]
expected: FAIL

[change state from time to datetime]
expected: FAIL

[change state from time to range]
expected: FAIL

[change state from number to range]
expected: FAIL

[change state from range to hidden]
expected: FAIL

[change state from range to checkbox]
expected: FAIL

[change state from range to radio]
expected: FAIL

[change state from range to submit]
expected: FAIL

[change state from range to image]
expected: FAIL

[change state from range to reset]
expected: FAIL

[change state from range to button]
expected: FAIL

[change state from range to email]
expected: FAIL

[change state from range to datetime]
expected: FAIL

[change state from checkbox to email]
expected: FAIL

[change state from checkbox to range]
expected: FAIL

[change state from radio to email]
expected: FAIL

[change state from radio to datetime]
expected: FAIL

[change state from radio to range]
expected: FAIL

[change state from submit to email]
expected: FAIL

[change state from submit to datetime]
expected: FAIL

[change state from submit to range]
expected: FAIL

[change state from image to email]
expected: FAIL

[change state from image to datetime]
expected: FAIL

[change state from image to range]
expected: FAIL

[change state from reset to email]
expected: FAIL

[change state from reset to datetime]
expected: FAIL

[change state from reset to range]
expected: FAIL

[change state from button to email]
expected: FAIL

[change state from button to datetime]
expected: FAIL

[change state from button to range]
expected: FAIL

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

[change state from range to text]
expected: FAIL

[change state from range to search]
expected: FAIL

[change state from range to tel]
expected: FAIL

[change state from range to url]
expected: FAIL

[change state from range to password]
expected: FAIL

[change state from color to range]
[change state from range to number]
expected: FAIL

@@ -6,12 +6,6 @@
[value IDL attribute of input type datetime with value attribute]
expected: FAIL

[value IDL attribute of input type range without value attribute]
expected: FAIL

[value IDL attribute of input type range with value attribute]
expected: FAIL

[value IDL attribute of input type email without value attribute]
expected: FAIL

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.