Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

fix(demos): Sanitize slider input values #2018

Merged
merged 15 commits into from
Jan 24, 2018
Merged

Conversation

acdvorak
Copy link
Contributor

@acdvorak acdvorak commented Jan 20, 2018

The demo page now checks that the user's input values are finite numbers before assigning them to any of the component's setter properties. This prevents an error from being thrown when the user deletes all the text in one of the inputs (which is a fairly common thing to do before entering a new number).

In addition, to make it clearer that the slider supports negative numbers for min and max, the demo page now allows those input values to go down to -100 (previously they were bounded at 0).

Foundation's `min`, `max`, and `step` setters now accept string values, and parse them with `parseFloat()`.

If a value is negative or non-finite (e.g., `NaN`, `Infinity`), a descriptive error is thrown.

Without these checks, a random and seemingly unrelated error is thrown instead, which makes it harder for clients to debug (especially if they're using minified JS).

The demo page now checks that the user's input value is finite before assigning it to any of the aforementioned setters. This prevents an error from being thrown when the user deletes all the text in one of the inputs (which is a fairly common thing to do before entering a new number).
@codecov-io
Copy link

codecov-io commented Jan 20, 2018

Codecov Report

Merging #2018 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2018   +/-   ##
=======================================
  Coverage   99.43%   99.43%           
=======================================
  Files          84       84           
  Lines        3720     3720           
  Branches      485      485           
=======================================
  Hits         3699     3699           
  Misses         21       21

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6773b17...6e69e61. Read the comment docs.

Copy link
Contributor

@kfranqueiro kfranqueiro left a comment

Choose a reason for hiding this comment

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

While I can understand the changes to the demo page, I don't understand why we're widening the type of public APIs to accept strings when their inputs are numeric by nature.

If we do indeed widen the APIs, we should probably add tests that pass strings, and update the README.

@acdvorak acdvorak changed the title fix(slider): Sanitize numeric values; better error messages fix(demos): Sanitize slider input values Jan 23, 2018
@acdvorak
Copy link
Contributor Author

Removed string parsing from the foundation.

The reason I added it is that many standard DOM APIs automatically coerce string values to numbers (e.g., scrollTop = '50'), which is useful in UIs where component parameters often depend on user input. But since there's no particular need for it, I'll remove it and just keep the demo changes.

discreteWMarkerSlider.max = parseFloat(max.value);
customDiscreteWMarkerSlider.max = parseFloat(max.value);
maxInput.addEventListener('input', function() {
var minValue = parseFloat(maxInput.value);
Copy link
Contributor

Choose a reason for hiding this comment

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

maxValue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@acdvorak acdvorak merged commit f3d4ca7 into master Jan 24, 2018
@acdvorak acdvorak deleted the fix/slider/number-sanitizing branch January 24, 2018 20:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants