Lightpanda: the headless browser designed for AI and automation
track dirty value so that we can properly sanitize on type change
Karl Seguindf0d7ab146fa
diff --git a/src/browser/tests/element/html/input-value-mode.html b/src/browser/tests/element/html/input-value-mode.htmlindex e925dc4bf..eea789ed5 100644--- a/src/browser/tests/element/html/input-value-mode.html+++ b/src/browser/tests/element/html/input-value-mode.html@@ -8,6 +8,7 @@<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">+ <input id="text2" type="text" value="a"></form><script id="default_mode_setter_writes_attribute">@@ -200,3 +201,67 @@testing.expectEqual('', input.value);}</script>++<script id="type_change_default_to_value_sanitizes">+ {+ const input = document.createElement('input');+ input.type = 'hidden';+ input.value = 'abc';+ input.type = 'number';+ testing.expectEqual('', input.value);+ testing.expectEqual('abc', input.getAttribute('value'));++ const color = document.createElement('input');+ color.type = 'checkbox';+ color.value = '#ABCDEF';+ color.type = 'color';+ testing.expectEqual('#abcdef', color.value);+ }+</script>++<script id="value_mode_dirty_flag">+ {+ // Until the value is set, it follows the value attribute.+ const input = document.createElement('input');+ input.setAttribute('value', 'a');+ testing.expectEqual('a', input.value);+ input.setAttribute('value', 'b');+ testing.expectEqual('b', input.value);+ input.removeAttribute('value');+ testing.expectEqual('', input.value);+ input.defaultValue = 'c';+ testing.expectEqual('c', input.value);++ // Setting it, even to the same value, makes it dirty.+ input.value = 'c';+ input.setAttribute('value', 'd');+ testing.expectEqual('c', input.value);+ input.removeAttribute('value');+ testing.expectEqual('c', input.value);++ // The attribute is sanitized as it is followed.+ const number = document.createElement('input');+ number.type = 'number';+ number.setAttribute('value', '12');+ testing.expectEqual('12', number.value);+ number.setAttribute('value', 'abc');+ testing.expectEqual('', number.value);++ // Clones keep the dirty flag.+ const clone = input.cloneNode();+ clone.setAttribute('value', 'e');+ testing.expectEqual('c', clone.value);+ const clean = number.cloneNode();+ clean.setAttribute('value', '7');+ testing.expectEqual('7', clean.value);+ }+</script>++<script id="value_mode_dirty_flag_parsed">+ {+ const input = $('#text2');+ testing.expectEqual('a', input.value);+ input.setAttribute('value', 'b');+ testing.expectEqual('b', input.value);+ }+</script>diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zigindex bcb514179..649eabbb3 100644--- a/src/browser/webapi/element/html/Input.zig+++ b/src/browser/webapi/element/html/Input.zig@@ -106,6 +106,7 @@ _proto_canary: if (lp.IS_DEBUG) *HtmlElement else void = undefined,_default_value: ?[]const u8 = null,_default_checked: bool = false,_value: ?[]const u8 = null,+_value_dirty: bool = false, // Once set _value no longer follows the value attribute_checked: bool = false,_checked_dirty: bool = false,// Only user edits count for tooLong/tooShort; script and attribute values don't.@@ -156,7 +157,7 @@ fn setType(self: *Input, typ: []const u8, frame: *Frame) !void {pub fn getValue(self: *const Input) []const u8 {return switch (self._input_type.valueMode()) {- .value => self._value orelse self._default_value orelse "",+ .value => self._value orelse "",.default => self._default_value orelse "",.default_on => self._default_value orelse "on",.filename => "",@@ -189,6 +190,7 @@ pub fn setValue(self: *Input, value: []const u8, frame: *Frame) !void {// 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;+ self._value_dirty = true;if (changed == false and self._value != null) {// _value itself isn't changing (not to be mixed up with setValue// being called with the same as the default value, which would need@@ -789,21 +791,33 @@ fn changeType(self: *Input, new_type: Type, frame: *Frame) !void {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;+ switch (new_mode) {+ .value => if (old_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);+ }+ } else {+ self._value_dirty = false;+ try self.syncValueFromAttribute(frame);+ },+ .default, .default_on => {+ self._value = null;+ if (old_mode == .value and old_value.len > 0) {+ try self.asElement().setAttributeSafe(comptime .wrap("value"), .wrap(old_value), frame);+ }+ },+ .filename => self._value = null,}+}- // 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);- }+// The value of a non-dirty control in value mode is its sanitized value attribute+fn syncValueFromAttribute(self: *Input, frame: *Frame) !void {+ const default_value = self._default_value orelse {+ self._value = null;+ return;+ };+ self._value = try self.sanitizeValue(false, default_value, frame);}/// Sanitize the value according to the current input type@@ -1597,10 +1611,8 @@ pub const Build = struct {// Sanitize initial value per input type (e.g. date rejects "invalid-date").// 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);+ if (self._input_type.valueMode() == .value) {+ try self.syncValueFromAttribute(frame);} else {self._value = null;}@@ -1616,7 +1628,12 @@ pub const Build = struct {const self = element.as(Input);switch (attribute) {.type => try self.changeType(Type.fromString(value.str()), frame),- .value => self._default_value = try frame.arena.dupe(u8, value.str()),+ .value => {+ self._default_value = try frame.arena.dupe(u8, value.str());+ if (self._value_dirty == false and self._input_type.valueMode() == .value) {+ try self.syncValueFromAttribute(frame);+ }+ },.checked => {self._default_checked = true;// Only update checked state if it hasn't been manually modified@@ -1636,7 +1653,12 @@ pub const Build = struct {const self = element.as(Input);switch (attribute) {.type => try self.changeType(.text, frame),- .value => self._default_value = null,+ .value => {+ self._default_value = null;+ if (self._value_dirty == false and self._input_type.valueMode() == .value) {+ self._value = null;+ }+ },.checked => {self._default_checked = false;// Only update checked state if it hasn't been manually modified@@ -1654,6 +1676,7 @@ pub const Build = struct {// Copy runtime state from source to cloneclone._value = source._value;+ clone._value_dirty = source._value_dirty;clone._checked = source._checked;clone._checked_dirty = source._checked_dirty;clone._user_edited = source._user_edited;