Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: report stepMismatch for input values off the step

Jay Hemnani4909eaa7e083
diff --git a/src/browser/tests/element/html/input-validity.html b/src/browser/tests/element/html/input-validity.html
index 5fb153d72..61349cf05 100644
--- a/src/browser/tests/element/html/input-validity.html
+++ b/src/browser/tests/element/html/input-validity.html
@@ -313,3 +313,50 @@
testing.expectEqual(false, $('#date-ok').checkValidity());
}
</script>
+
+<script id="step_mismatch">
+ {
+ const input = (type, attrs) => {
+ const i = document.createElement('input');
+ i.type = type;
+ for (const [k, v] of Object.entries(attrs)) i.setAttribute(k, v);
+ return i;
+ };
+ const odd = input('number', {step: '2'});
+ odd.value = '3';
+ testing.expectEqual(true, odd.validity.stepMismatch);
+ testing.expectEqual(false, odd.validity.valid);
+ testing.expectEqual(false, odd.checkValidity());
+ odd.value = '4';
+ testing.expectEqual(false, odd.validity.stepMismatch);
+
+ const fraction = input('number', {});
+ fraction.value = '1.5';
+ testing.expectEqual(true, fraction.validity.stepMismatch);
+
+ // The step base is min, then the value attribute.
+ const fromMin = input('number', {min: '1', step: '2'});
+ fromMin.value = '3';
+ testing.expectEqual(false, fromMin.validity.stepMismatch);
+ testing.expectEqual(false, input('number', {step: '2', value: '3'}).validity.stepMismatch);
+
+ const any = input('number', {step: 'any'});
+ any.value = '1.5';
+ testing.expectEqual(false, any.validity.stepMismatch);
+ const tenths = input('number', {step: '0.1'});
+ tenths.value = '0.3';
+ testing.expectEqual(false, tenths.validity.stepMismatch);
+
+ const date = input('date', {step: '2'});
+ date.value = '2020-01-02';
+ testing.expectEqual(true, date.validity.stepMismatch);
+ const time = input('time', {});
+ time.value = '10:00:30';
+ testing.expectEqual(true, time.validity.stepMismatch);
+
+ const empty = input('number', {step: '2'});
+ testing.expectEqual(false, empty.validity.stepMismatch);
+ testing.expectEqual(false, input('text', {step: '2', value: '3'}).validity.stepMismatch);
+ }
+</script>
+
diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zig
index 14da6545f..d230a61c6 100644
--- a/src/browser/webapi/element/html/Input.zig
+++ b/src/browser/webapi/element/html/Input.zig
@@ -485,6 +485,15 @@ pub fn suffersRangeOverflow(self: *const Input) bool {
return numericRangeBreach(self, .overflow);
}
+pub fn suffersStepMismatch(self: *const Input) bool {
+ const typ = self._input_type;
+ if (!hasNumericValue(typ)) return false;
+ const step = self.allowedValueStep() orelse return false;
+ const value = valueToNumber(typ, self.getValue()) orelse return false;
+ const rungs = (value - self.stepBase()) / step;
+ return @abs(rungs - @round(rungs)) > 1e-9;
+}
+
fn numericRangeBreach(self: *const Input, comptime kind: enum { underflow, overflow }) bool {
const typ = self._input_type;
if (!hasNumericValue(typ)) return false;
diff --git a/src/browser/webapi/element/html/ValidityState.zig b/src/browser/webapi/element/html/ValidityState.zig
index 956b0d5cc..04ad713f2 100644
--- a/src/browser/webapi/element/html/ValidityState.zig
+++ b/src/browser/webapi/element/html/ValidityState.zig
@@ -86,8 +86,7 @@ fn getRangeOverflow(self: *const ValidityState) bool {
fn getStepMismatch(self: *const ValidityState) bool {
if (self.customFlags()) |flags| return flags.stepMismatch;
- // Step matching is not implemented yet (PR #2280 begins step rounding for
- // <input type=range>). Returning false keeps well-formed values valid.
+ if (self._owner.is(Input)) |input| return input.suffersStepMismatch();
return false;
}