Lightpanda: the headless browser designed for AI and automation
webapi: give range and color inputs their default value
Jay Hemnanid96610c85c01
diff --git a/src/browser/tests/element/html/input-attrs.html b/src/browser/tests/element/html/input-attrs.htmlindex 5f7734690..67cd9145e 100644--- a/src/browser/tests/element/html/input-attrs.html+++ b/src/browser/tests/element/html/input-attrs.html@@ -364,3 +364,73 @@testing.expectEqual(null, i.getAttribute('formnovalidate'))}</script>++<form id="range_color_form">+ <input type="range" name="r1">+ <input type="range" name="r2" min="0" max="11">+ <input type="color" name="c1">+</form>++<script id="range_color_default_value">+ {+ // A range or color input without a value attribute sanitizes the empty+ // string to its default: the range midpoint, or #000000.+ const form = $('#range_color_form');+ testing.expectEqual('r1=50&r2=6&c1=%23000000', new URLSearchParams(new FormData(form)).toString());++ const r = document.createElement('input');+ r.type = 'range';+ testing.expectEqual('50', r.value);+ r.setAttribute('value', '30');+ testing.expectEqual('30', r.value);+ r.removeAttribute('value');+ testing.expectEqual('50', r.value);++ const c = document.createElement('input');+ c.type = 'color';+ testing.expectEqual('#000000', c.value);+ c.setAttribute('value', '#ABCDEF');+ testing.expectEqual('#abcdef', c.value);+ c.removeAttribute('value');+ testing.expectEqual('#000000', c.value);++ const f = document.createElement('form');+ f.innerHTML = '<input type=range>';+ f.firstChild.value = '10';+ f.reset();+ testing.expectEqual('50', f.firstChild.value);++ // The value carries over a type change and is sanitized for the new type+ const t = document.createElement('input');+ t.type = 'color';+ t.type = 'range';+ testing.expectEqual('50', t.value);+ t.type = 'text';+ testing.expectEqual('50', t.value);+ }+</script>++<script id="range_min_max_change">+ {+ // Changing min, max or step sanitizes the current value again.+ const r = document.createElement('input');+ r.type = 'range';+ r.max = '20';+ testing.expectEqual('20', r.value);++ const r2 = document.createElement('input');+ r2.type = 'range';+ r2.value = '80';+ r2.max = '50';+ testing.expectEqual('50', r2.value);+ r2.step = '20';+ testing.expectEqual('40', r2.value);++ const r3 = document.createElement('input');+ r3.setAttribute('value', '80');+ r3.type = 'range';+ r3.max = '50';+ testing.expectEqual('50', r3.value);+ testing.expectEqual('80', r3.getAttribute('value'));+ }+</script>diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zigindex 851015925..95c161032 100644--- a/src/browser/webapi/element/html/Input.zig+++ b/src/browser/webapi/element/html/Input.zig@@ -771,6 +771,8 @@ fn changeType(self: *Input, new_type: Type, frame: *Frame) !void {// Sanitize the current value according to the new typeif (self._value) |current_value| {self._value = try self.sanitizeValue(false, current_value, frame);+ } else {+ try self.syncValueFromAttribute(frame);}} else {self._value_dirty = false;@@ -802,13 +804,26 @@ pub fn reset(self: *Input, frame: *Frame) !void {// The value of a non-dirty control in value mode is its sanitized value attributefn syncValueFromAttribute(self: *Input, frame: *Frame) !void {- const default_value = self._default_value orelse {- self._value = null;- return;+ const default_value = self._default_value orelse switch (self._input_type) {+ // These sanitize the empty string to a default (midpoint, #000000)+ .range, .color => "",+ else => {+ self._value = null;+ return;+ },};self._value = try self.sanitizeValue(false, default_value, frame);}+// A range value depends on min, max and step, so it is sanitized again when they change+fn resanitizeRange(self: *Input, frame: *Frame) !void {+ if (self._input_type != .range) {+ return;+ }+ const current = self._value orelse return;+ self._value = try self.sanitizeValue(false, current, frame);+}+/// Sanitize the value according to the current input typefn sanitizeValue(self: *Input, comptime dupe: bool, value: []const u8, frame: *Frame) ![]const u8 {switch (self._input_type) {@@ -1612,10 +1627,11 @@ pub const Build = struct {}pub fn attributeChange(element: *Element, name: String, value: String, frame: *Frame) !void {- const attribute = std.meta.stringToEnum(enum { type, value, checked }, name.str()) orelse return;+ const attribute = std.meta.stringToEnum(enum { type, value, checked, min, max, step }, name.str()) orelse return;const self = element.as(Input);switch (attribute) {.type => try self.changeType(Type.fromString(value.str()), frame),+ .min, .max, .step => try self.resanitizeRange(frame),.value => {self._default_value = try frame.page_arena.dupe(u8, value.str());if (self._value_dirty == false and self._input_type.valueMode() == .value) {@@ -1637,14 +1653,15 @@ pub const Build = struct {}pub fn attributeRemove(element: *Element, name: String, frame: *Frame) !void {- const attribute = std.meta.stringToEnum(enum { type, value, checked }, name.str()) orelse return;+ const attribute = std.meta.stringToEnum(enum { type, value, checked, min, max, step }, name.str()) orelse return;const self = element.as(Input);switch (attribute) {.type => try self.changeType(.text, frame),+ .min, .max, .step => try self.resanitizeRange(frame),.value => {self._default_value = null;if (self._value_dirty == false and self._input_type.valueMode() == .value) {- self._value = null;+ try self.syncValueFromAttribute(frame);}},.checked => {