Skip to content

Commit

Permalink
Refactor step handling in <input>
Browse files Browse the repository at this point in the history
stepMismatch currently has its own way of checking whether the value is
aligned, even though the _isStepAligned function was written for this
very purpose. Move the decimal.js-based modulo check to _isStepAligned,
and make stepMismatch use that function instead.

Also rename the parameter for _stepAlign so that it makes more sense.
  • Loading branch information
TimothyGu authored and domenic committed Feb 1, 2020
1 parent a4c6144 commit ac497bb
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions lib/jsdom/living/nodes/HTMLInputElement-impl.js
Expand Up @@ -144,11 +144,12 @@ class HTMLInputElementImpl extends HTMLElementImpl {
}

_isStepAligned(v) {
const relativeDistance = v - this._stepBase;
return (relativeDistance % this._allowedValueStep) === 0;
return new Decimal(v).minus(this._stepBase)
.modulo(this._allowedValueStep)
.isZero();
}

_stepAlign(v, isPositive) {
_stepAlign(v, roundUp) {
if (this._isStepAligned(v)) {
return v;
}
Expand All @@ -157,7 +158,7 @@ class HTMLInputElementImpl extends HTMLElementImpl {
const stepIntervalCount = Math.trunc(relativeDistance / this._allowedValueStep);
let candidate = stepIntervalCount * this._allowedValueStep;

if (isPositive) {
if (roundUp) {
if (v > candidate) {
candidate += this._allowedValueStep;
}
Expand Down Expand Up @@ -544,29 +545,24 @@ class HTMLInputElementImpl extends HTMLElementImpl {
const valueBeforeStepping = value;

if (!this._isStepAligned(value)) {
value = this._stepAlign(value, isUp);
value = this._stepAlign(value, /* roundUp = */ isUp);
}

let delta = n * allowedValueStep;
if (!isUp) {
delta *= -1;
}

value += delta;

if (min !== null && value < min) {
value = this._stepAlign(min, true);
value = this._stepAlign(min, /* roundUp = */ true);
}

if (max !== null && value > max) {
value = this._stepAlign(max, false);
value = this._stepAlign(max, /* roundUp = */ false);
}

if (isUp) {
if (value < valueBeforeStepping) {
return;
}
} else if (value > valueBeforeStepping) {
if (isUp ? value < valueBeforeStepping : value > valueBeforeStepping) {
return;
}

Expand Down Expand Up @@ -1102,11 +1098,7 @@ class HTMLInputElementImpl extends HTMLElementImpl {
}

const number = this._parsedValue;
const isIntegralMultiple =
new Decimal(number - this._stepBase)
.modulo(allowedValueStep)
.isZero();
return !isIntegralMultiple;
return !this._isStepAligned(number);
},

// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-type-mismatch
Expand Down

0 comments on commit ac497bb

Please sign in to comment.