Lightpanda: the headless browser designed for AI and automation
webapi: reflect input.value to the attribute in default modes
Navid EMADb2dca654e520
diff --git a/src/browser/tests/element/html/input-value-mode.html b/src/browser/tests/element/html/input-value-mode.htmlnew file mode 100644index 000000000..e925dc4bf--- /dev/null+++ b/src/browser/tests/element/html/input-value-mode.html@@ -0,0 +1,202 @@+<!DOCTYPE html>+<script src="../../testing.js"></script>++<!-- https://html.spec.whatwg.org/multipage/input.html#dom-input-value -->++<form id="parsed">+ <input id="hidden1" type="hidden" name="h" value="a">+ <input id="check1" type="checkbox" name="c" value="a">+ <input id="text1" type="text" name="t" value="initial">+ <input id="file1" type="file" name="f" value="a">+</form>++<script id="default_mode_setter_writes_attribute">+ {+ // A hidden input's value IS its value content attribute: setting it must+ // be visible to getAttribute, serialization and attribute selectors.+ const input = $('#hidden1');+ testing.expectEqual('a', input.value);++ input.value = 'b';+ testing.expectEqual('b', input.value);+ testing.expectEqual('b', input.getAttribute('value'));+ testing.expectEqual('b', input.defaultValue);+ testing.expectTrue(input.outerHTML.includes('value="b"'));+ testing.expectEqual(input, document.querySelector("input[type=hidden][value='b']"));+ testing.expectEqual(null, document.querySelector("input[type=hidden][value='a']"));+ }+</script>++<script id="default_mode_types">+ for (const type of ['hidden', 'submit', 'reset', 'button', 'image']) {+ const input = document.createElement('input');+ input.type = type;+ document.body.appendChild(input);++ // No attribute: the empty string, and the setter creates the attribute.+ testing.expectEqual('', input.value);+ input.value = 'x';+ testing.expectEqual('x', input.getAttribute('value'));+ testing.expectEqual('x', input.value);+ testing.expectEqual(input, document.querySelector(`input[type=${type}][value=x]`));++ // The getter follows the attribute, even after the setter ran.+ input.setAttribute('value', 'y');+ testing.expectEqual('y', input.value);+ input.removeAttribute('value');+ testing.expectEqual('', input.value);++ input.value = null;+ testing.expectEqual('', input.getAttribute('value'));+ input.remove();+ }+</script>++<script id="default_on_mode_types">+ for (const type of ['checkbox', 'radio']) {+ const input = document.createElement('input');+ input.type = type;+ document.body.appendChild(input);++ // No attribute: "on".+ testing.expectEqual('on', input.value);+ testing.expectEqual(null, input.getAttribute('value'));++ input.value = 'x';+ testing.expectEqual('x', input.getAttribute('value'));+ testing.expectEqual('x', input.value);+ testing.expectEqual(input, document.querySelector(`input[type=${type}][value=x]`));++ input.setAttribute('value', 'y');+ testing.expectEqual('y', input.value);++ // An empty attribute is a value, not a missing one.+ input.value = '';+ testing.expectEqual('', input.getAttribute('value'));+ testing.expectEqual('', input.value);++ input.removeAttribute('value');+ testing.expectEqual('on', input.value);+ input.remove();+ }+</script>++<script id="default_on_mode_parsed">+ {+ const input = $('#check1');+ testing.expectEqual('a', input.value);+ input.value = 'b';+ testing.expectEqual('b', input.getAttribute('value'));+ testing.expectEqual(input, document.querySelector("#parsed input[type=checkbox][value='b']"));+ }+</script>++<script id="value_mode_keeps_attribute">+ {+ const input = $('#text1');+ input.value = 'changed';+ testing.expectEqual('changed', input.value);+ testing.expectEqual('initial', input.getAttribute('value'));+ testing.expectEqual('initial', input.defaultValue);+ testing.expectEqual(input, document.querySelector("#parsed input[value='initial']"));+ testing.expectEqual(null, document.querySelector("#parsed input[value='changed']"));+ }+</script>++<script id="filename_mode_unchanged">+ {+ const input = $('#file1');+ testing.expectEqual('', input.value);+ input.value = '';+ testing.expectEqual('', input.value);+ testing.expectError('InvalidStateError', () => { input.value = 'b'; });+ testing.expectEqual('a', input.getAttribute('value'));+ }+</script>++<script id="form_data_uses_attribute">+ {+ const data = new FormData($('#parsed'));+ testing.expectEqual('b', data.get('h'));+ testing.expectEqual('changed', data.get('t'));+ }+</script>++<script id="type_change_value_to_default">+ {+ // value -> default: a non-empty current value is written to the attribute.+ const input = document.createElement('input');+ input.setAttribute('value', 'orig');+ input.value = 'typed';+ input.type = 'hidden';+ testing.expectEqual('typed', input.getAttribute('value'));+ testing.expectEqual('typed', input.value);++ // ...but an empty one leaves the attribute alone.+ const empty = document.createElement('input');+ empty.setAttribute('value', 'orig');+ empty.value = '';+ empty.type = 'checkbox';+ testing.expectEqual('orig', empty.getAttribute('value'));+ testing.expectEqual('orig', empty.value);+ }+</script>++<script id="type_change_default_to_value">+ {+ // default -> value: the value comes from the attribute and is no longer+ // dirty, so later attribute edits show through.+ const input = document.createElement('input');+ input.type = 'hidden';+ input.value = 'a';+ input.type = 'text';+ testing.expectEqual('a', input.value);+ input.setAttribute('value', 'b');+ testing.expectEqual('b', input.value);++ input.value = 'typed';+ input.setAttribute('value', 'c');+ testing.expectEqual('typed', input.value);+ testing.expectEqual('c', input.getAttribute('value'));++ // Same through removing the type attribute (back to text).+ const check = document.createElement('input');+ check.type = 'checkbox';+ check.value = 'x';+ check.removeAttribute('type');+ testing.expectEqual('x', check.value);++ // No attribute: the empty string, not a stale value.+ const radio = document.createElement('input');+ radio.type = 'radio';+ radio.type = 'text';+ testing.expectEqual('', radio.value);+ }+</script>++<script id="type_change_default_to_default">+ {+ const input = document.createElement('input');+ input.type = 'checkbox';+ testing.expectEqual('on', input.value);+ input.type = 'hidden';+ testing.expectEqual('', input.value);+ testing.expectEqual(null, input.getAttribute('value'));+ input.value = 'x';+ input.type = 'radio';+ testing.expectEqual('x', input.value);+ }+</script>++<script id="type_change_to_filename">+ {+ const input = document.createElement('input');+ input.value = 'typed';+ input.type = 'file';+ testing.expectEqual('', input.value);+ testing.expectEqual(null, input.getAttribute('value'));+ // Back to text: value restarts from the (absent) attribute.+ input.type = 'text';+ testing.expectEqual('', input.value);+ }+</script>diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zigindex 418848f5b..bcb514179 100644--- a/src/browser/webapi/element/html/Input.zig+++ b/src/browser/webapi/element/html/Input.zig@@ -80,6 +80,26 @@ pub const Type = enum {pub fn toString(self: Type) []const u8 {return @tagName(self);}++ // https://html.spec.whatwg.org/multipage/input.html#dom-input-value+ pub fn valueMode(self: Type) ValueMode {+ return switch (self) {+ .hidden, .submit, .image, .reset, .button => .default,+ .checkbox, .radio => .default_on,+ .file => .filename,+ else => .value,+ };+ }+};++pub const ValueMode = enum {+ // The IDL value is the current value (_value).+ value,+ // The IDL value is the value content attribute, or "".+ default,+ // The IDL value is the value content attribute, or "on".+ default_on,+ filename,};_proto_canary: if (lp.IS_DEBUG) *HtmlElement else void = undefined,@@ -135,10 +155,11 @@ fn setType(self: *Input, typ: []const u8, frame: *Frame) !void {}pub fn getValue(self: *const Input) []const u8 {- if (self._input_type == .file) return "";- return self._value orelse self._default_value orelse switch (self._input_type) {- .checkbox, .radio => "on",- else => "",+ return switch (self._input_type.valueMode()) {+ .value => self._value orelse self._default_value orelse "",+ .default => self._default_value orelse "",+ .default_on => self._default_value orelse "on",+ .filename => "",};}@@ -151,12 +172,21 @@ pub fn getRedactedValue(self: *const Input) []const u8 {}pub fn setValue(self: *Input, value: []const u8, frame: *Frame) !void {- // File inputs: setting to empty string is a no-op, anything else throws- if (self._input_type == .file) {- if (value.len == 0) return;- return error.InvalidStateError;+ switch (self._input_type.valueMode()) {+ .value => {},+ // The value _is_ the content attribute (no sanitization for these types)+ .default, .default_on => {+ self._value = null;+ self._user_edited = false;+ return self.asElement().setAttributeSafe(comptime .wrap("value"), .wrap(value), frame);+ },+ // File inputs: setting to empty string is a no-op, anything else throws+ .filename => {+ if (value.len == 0) return;+ return error.InvalidStateError;+ },}- // This should _not_ call setAttribute. It updates the current state only+ // In value mode, this should _not_ call setAttribute. It updates the current state onlyconst sanitized = try self.sanitizeValue(false, value, frame);const changed = std.mem.eql(u8, self.getValue(), sanitized) == false;if (changed == false and self._value != null) {@@ -751,6 +781,31 @@ fn setFormNoValidate(self: *Input, value: bool, frame: *Frame) !void {}}+// https://html.spec.whatwg.org/multipage/input.html#input-type-change+fn changeType(self: *Input, new_type: Type, frame: *Frame) !void {+ const old_mode = self._input_type.valueMode();+ const new_mode = new_type.valueMode();+ // The current value, read with the old type+ const old_value = self.getValue();+ self._input_type = new_type;++ if (old_mode == .value and new_mode == .value) {+ // Sanitize the current value according to the new type+ if (self._value) |current_value| {+ self._value = try self.sanitizeValue(false, current_value, frame);+ }+ return;+ }++ // Outside the value mode the value is the content attribute, and coming+ // back to it the value restarts from that attribute (no longer dirty).+ self._value = null;++ if (old_mode == .value and (new_mode == .default or new_mode == .default_on) and old_value.len > 0) {+ try self.asElement().setAttributeSafe(comptime .wrap("value"), .wrap(old_value), 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) {@@ -1541,7 +1596,10 @@ pub const Build = struct {.text;// Sanitize initial value per input type (e.g. date rejects "invalid-date").- if (self._default_value) |dv| {+ // Outside the value mode, the value is read from the attribute.+ if (self._input_type.valueMode() != .value) {+ self._value = null;+ } else if (self._default_value) |dv| {self._value = try self.sanitizeValue(false, dv, frame);} else {self._value = null;@@ -1557,17 +1615,7 @@ pub const Build = struct {const attribute = std.meta.stringToEnum(enum { type, value, checked }, name.str()) orelse return;const self = element.as(Input);switch (attribute) {- .type => {- self._input_type = Type.fromString(value.str());- // Sanitize the current value according to the new type- if (self._value) |current_value| {- self._value = try self.sanitizeValue(false, current_value, frame);- // Apply default value for checkbox/radio if value is now empty- if (self._value.?.len == 0 and (self._input_type == .checkbox or self._input_type == .radio)) {- self._value = "on";- }- }- },+ .type => try self.changeType(Type.fromString(value.str()), frame),.value => self._default_value = try frame.arena.dupe(u8, value.str()),.checked => {self._default_checked = true;@@ -1583,11 +1631,11 @@ pub const Build = struct {}}- pub fn attributeRemove(element: *Element, name: String, _: *Frame) !void {+ pub fn attributeRemove(element: *Element, name: String, frame: *Frame) !void {const attribute = std.meta.stringToEnum(enum { type, value, checked }, name.str()) orelse return;const self = element.as(Input);switch (attribute) {- .type => self._input_type = .text,+ .type => try self.changeType(.text, frame),.value => self._default_value = null,.checked => {self._default_checked = false;@@ -1626,6 +1674,7 @@ test "WebApi: HTML.Input" {try testing.htmlRunner("element/html/input-validity.html", .{});try testing.htmlRunner("element/html/input_file.html", .{});try testing.htmlRunner("element/html/input-value-as.html", .{});+ try testing.htmlRunner("element/html/input-value-mode.html", .{});}test "isValidFloatingPoint" {