Lightpanda: the headless browser designed for AI and automation
webapi: scale the step tolerance with the value in stepMismatch and stepBy
Jay Hemnanie514a90b6983
diff --git a/src/browser/tests/element/html/input-validity.html b/src/browser/tests/element/html/input-validity.htmlindex 61349cf05..ea29f37b7 100644--- a/src/browser/tests/element/html/input-validity.html+++ b/src/browser/tests/element/html/input-validity.html@@ -346,6 +346,15 @@const tenths = input('number', {step: '0.1'});tenths.value = '0.3';testing.expectEqual(false, tenths.validity.stepMismatch);+ // Float error in a large value with a fractional step is not a mismatch.+ const cents = input('number', {step: '0.01'});+ cents.value = '1234567.89';+ testing.expectEqual(false, cents.validity.stepMismatch);+ testing.expectEqual(true, cents.checkValidity());+ cents.value = '1000000000.01';+ testing.expectEqual(false, cents.validity.stepMismatch);+ cents.value = '1234567.891';+ testing.expectEqual(true, cents.validity.stepMismatch);const date = input('date', {step: '2'});date.value = '2020-01-02';diff --git a/src/browser/tests/element/html/input-value-as.html b/src/browser/tests/element/html/input-value-as.htmlindex fafaa9741..b46eb763a 100644--- a/src/browser/tests/element/html/input-value-as.html+++ b/src/browser/tests/element/html/input-value-as.html@@ -132,6 +132,16 @@num.stepDown(3);testing.expectEqual('1.5', num.value);+ // A large value on a fractional step moves by one step, not a snap.+ const cents = document.createElement('input');+ cents.type = 'number';+ cents.step = '0.01';+ cents.value = '1234567.89';+ cents.stepUp();+ testing.expectEqual('1234567.9', cents.value);+ cents.stepDown(2);+ testing.expectEqual('1234567.88', cents.value);+// No min: the value attribute is the step base.const date = $('#date');date.value = '2024-03-10';diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zigindex d230a61c6..c0a413354 100644--- a/src/browser/webapi/element/html/Input.zig+++ b/src/browser/webapi/element/html/Input.zig@@ -490,8 +490,15 @@ pub fn suffersStepMismatch(self: *const Input) bool {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;+ return offStep(value, self.stepBase(), step);+}++/// Whether value is off the ladder of steps from base. The tolerance grows with the+/// magnitudes, so float error in a large value with a fractional step is not a mismatch.+fn offStep(value: f64, base: f64, step: f64) bool {+ const rungs = (value - base) / step;+ const tolerance = @max(1e-9, 4 * std.math.floatEps(f64) * (@abs(value) + @abs(base)) / step);+ return @abs(rungs - @round(rungs)) > tolerance;}fn numericRangeBreach(self: *const Input, comptime kind: enum { underflow, overflow }) bool {@@ -961,7 +968,7 @@ fn stepBy(self: *Input, n: i32, frame: *Frame) !void {const base = self.stepBase();const rungs = (before - base) / step;const steps: f64 = @floatFromInt(n);- var value = if (@abs(rungs - @round(rungs)) > 1e-9)+ var value = if (offStep(before, base, step))// Off the ladder: the snap to the next rung counts as the first step// (what browsers do; the spec text ignores n here).base + (if (n < 0) @floor(rungs) + steps + 1 else @ceil(rungs) + steps - 1) * step