Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for <input type=time> reverse range validation
  • Loading branch information
TimothyGu committed Jan 14, 2020
1 parent 94b40d3 commit e2dbad8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
66 changes: 57 additions & 9 deletions lib/jsdom/living/nodes/HTMLInputElement-impl.js
Expand Up @@ -844,6 +844,14 @@ class HTMLInputElementImpl extends HTMLElementImpl {
return this._value;
}

get _parsedValueIsNumber() {
if (this._convertStringToNumber === undefined) {
return false;
}
const parsedValue = this._parsedValue;
return typeof parsedValue === "number" && !isNaN(parsedValue);
}

// https://html.spec.whatwg.org/multipage/input.html#concept-input-step
get _allowedValueStep() {
const attr = this._getAttributeIfApplies("step");
Expand Down Expand Up @@ -951,8 +959,27 @@ class HTMLInputElementImpl extends HTMLElementImpl {
return this._hasAttributeAndApplies("required");
}

// https://html.spec.whatwg.org/multipage/input.html#has-a-periodic-domain
get _hasAPeriodicDomain() {
return this.type === "time";
}

// https://html.spec.whatwg.org/multipage/input.html#has-a-reversed-range
get _hasAReversedRange() {
return this._hasAPeriodicDomain && this._maximum < this._minimum;
}

get validity() {
if (!this._validity) {
// Constraint validation: When an element has a reversed range, and the result of applying
// the algorithm to convert a string to a number to the string given by the element's value
// is a number, and the number obtained from that algorithm is more than the maximum and less
// than the minimum, the element is simultaneously suffering from an underflow and suffering
// from an overflow.
const reversedRangeSufferingOverUnderflow = () => {
return this._parsedValueIsNumber && this._parsedValue > this._maximum && this._parsedValue < this._minimum;
};

const state = {
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-being-missing
valueMissing: () => {
Expand Down Expand Up @@ -1010,12 +1037,36 @@ class HTMLInputElementImpl extends HTMLElementImpl {
tooShort: () => false,

// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-an-overflow
// https://html.spec.whatwg.org/multipage/input.html#attr-input-max
rangeOverflow: () => this._maximum !== null && this._parsedValue > this._maximum,
rangeOverflow: () => {
// https://html.spec.whatwg.org/multipage/input.html#the-min-and-max-attributes
if (this._hasAReversedRange) {
return reversedRangeSufferingOverUnderflow();
}
// Constraint validation: When the element has a maximum and does not have a reversed
// range, and the result of applying the algorithm to convert a string to a number to the
// string given by the element's value is a number, and the number obtained from that
// algorithm is more than the maximum, the element is suffering from an overflow.
if (this._maximum !== null && this._parsedValueIsNumber && this._parsedValue > this._maximum) {
return true;
}
return false;
},

// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-an-underflow
// https://html.spec.whatwg.org/multipage/input.html#attr-input-min
rangeUnderflow: () => this._minimum !== null && this._parsedValue < this._minimum,
rangeUnderflow: () => {
// https://html.spec.whatwg.org/multipage/input.html#the-min-and-max-attributes
if (this._hasAReversedRange) {
return reversedRangeSufferingOverUnderflow();
}
// Constraint validation: When the element has a minimum and does not have a reversed
// range, and the result of applying the algorithm to convert a string to a number to the
// string given by the element's value is a number, and the number obtained from that
// algorithm is less than the minimum, the element is suffering from an underflow.
if (this._minimum !== null && this._parsedValueIsNumber && this._parsedValue < this._minimum) {
return true;
}
return false;
},

// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-pattern-mismatch
patternMismatch: () => {
Expand Down Expand Up @@ -1047,14 +1098,11 @@ class HTMLInputElementImpl extends HTMLElementImpl {
if (allowedValueStep === null) {
return false;
}
if (this._convertStringToNumber === undefined) {
return false;
}
const number = this._parsedValue;
if (typeof number !== "number" || isNaN(number)) {
if (!this._parsedValueIsNumber) {
return false;
}

const number = this._parsedValue;
const isIntegralMultiple =
new Decimal(number - this._stepBase)
.modulo(allowedValueStep)
Expand Down
2 changes: 0 additions & 2 deletions test/web-platform-tests/to-run.yaml
Expand Up @@ -557,8 +557,6 @@ formaction.html: [fail, Unknown]

DIR: html/semantics/forms/constraints

form-validation-validity-rangeOverflow.html: [fail, To be fixed]
form-validation-validity-rangeUnderflow.html: [fail, To be fixed]
form-validation-validity-valueMissing.html: [fail, 'Spec unclear (see https://github.com/whatwg/html/issues/5202)']
infinite_backtracking.html: [timeout, We cannot restrict duration of regex matching from JavaScript]

Expand Down

0 comments on commit e2dbad8

Please sign in to comment.