Lightpanda: the headless browser designed for AI and automation
avoid value dupe if unchanged
Karl Seguin27a7c3e516b6
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.htmlindex a8c961b3e..493afa689 100644--- a/src/browser/tests/element/html/input.html+++ b/src/browser/tests/element/html/input.html@@ -557,6 +557,31 @@testing.expectEqual(2, input.selectionStart);testing.expectEqual(2, input.selectionEnd);+ // Re-assigning the same value leaves the selection alone+ // (WPT textfieldselection/selection-after-content-change.html).+ input.value = 'hello';+ input.setSelectionRange(1, 3, 'backward');+ input.value = 'hello';+ testing.expectEqual(1, input.selectionStart);+ testing.expectEqual(3, input.selectionEnd);+ testing.expectEqual('backward', input.selectionDirection);++ // ...including when the new value only differs before sanitization.+ input.value = 'hel\nlo';+ testing.expectEqual(1, input.selectionStart);+ testing.expectEqual(3, input.selectionEnd);++ // Assigning a value equal to the default is not a change for the caret,+ // but it still sets the dirty flag: later attribute edits stay hidden.+ const dflt = document.createElement('input');+ dflt.type = 'text';+ dflt.defaultValue = 'foo';+ dflt.value = 'foo';+ testing.expectEqual(0, dflt.selectionStart);+ testing.expectEqual(0, dflt.selectionEnd);+ dflt.defaultValue = 'bar';+ testing.expectEqual('foo', dflt.value);+// Types with no text entry cursor keep reporting null rather than a number.const check = document.createElement('input');check.type = 'checkbox';diff --git a/src/browser/tests/element/html/textarea.html b/src/browser/tests/element/html/textarea.htmlindex bff1c0980..9bb61d94f 100644--- a/src/browser/tests/element/html/textarea.html+++ b/src/browser/tests/element/html/textarea.html@@ -323,5 +323,23 @@testing.expectEqual(5, textarea.selectionStart);testing.expectEqual(5, textarea.selectionEnd);testing.expectEqual('none', textarea.selectionDirection);++ // Re-assigning the same value leaves the selection alone+ // (WPT textfieldselection/selection-after-content-change.html).+ textarea.setSelectionRange(1, 3, 'backward');+ textarea.value = 'hello';+ testing.expectEqual(1, textarea.selectionStart);+ testing.expectEqual(3, textarea.selectionEnd);+ testing.expectEqual('backward', textarea.selectionDirection);++ // Assigning a value equal to the default is not a change for the caret,+ // but it still sets the dirty flag: later default edits stay hidden.+ const dflt = document.createElement('textarea');+ dflt.defaultValue = 'foo';+ dflt.value = 'foo';+ testing.expectEqual(0, dflt.selectionStart);+ testing.expectEqual(0, dflt.selectionEnd);+ dflt.defaultValue = 'bar';+ testing.expectEqual('foo', dflt.value);}</script>diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zigindex eb1f5c4e7..6362ce460 100644--- a/src/browser/webapi/element/html/Input.zig+++ b/src/browser/webapi/element/html/Input.zig@@ -157,14 +157,20 @@ pub fn setValue(self: *Input, value: []const u8, frame: *Frame) !void {return error.InvalidStateError;}// This should _not_ call setAttribute. It updates the current state only- const sanitized = try self.sanitizeValue(true, value, frame);- self._value = sanitized;+ const sanitized = try self.sanitizeValue(false, value, frame);+ const changed = std.mem.eql(u8, self.getValue(), sanitized) == false;+ 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+ // to dupe)+ self._user_edited = false;+ return;+ }+ self._value = try frame.dupeString(sanitized);self._user_edited = false;- // "move the text entry cursor position to the end of the text control,- // unselecting any selected text and resetting the selection direction to- // 'none'" -- https://html.spec.whatwg.org/#dom-input-value- if (self.selectionAvailable()) {+ // move the text entry cursor position to the end of the text control+ if (changed and self.selectionAvailable()) {self._selection_start = @intCast(sanitized.len);self._selection_end = @intCast(sanitized.len);self._selection_direction = .none;diff --git a/src/browser/webapi/element/html/TextArea.zig b/src/browser/webapi/element/html/TextArea.zigindex b2bf0f829..fc482551f 100644--- a/src/browser/webapi/element/html/TextArea.zig+++ b/src/browser/webapi/element/html/TextArea.zig@@ -79,16 +79,24 @@ pub fn getValue(self: *const TextArea) []const u8 {}pub fn setValue(self: *TextArea, value: []const u8, frame: *Frame) !void {+ const changed = std.mem.eql(u8, self.getValue(), value) == false;+ 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+ // to dupe)+ self._user_edited = false;+ return;+ }const owned = try frame.arena.dupe(u8, value);self._value = owned;self._user_edited = false;- // "move the text entry cursor position to the end of the text control,- // unselecting any selected text and resetting the selection direction to- // 'none'" -- https://html.spec.whatwg.org/#dom-textarea-value- self._selection_start = @intCast(owned.len);- self._selection_end = @intCast(owned.len);- self._selection_direction = .none;+ // move the text entry cursor position to the end of the text control+ if (changed) {+ self._selection_start = @intCast(owned.len);+ self._selection_end = @intCast(owned.len);+ self._selection_direction = .none;+ }}pub fn setUserValue(self: *TextArea, value: []const u8, frame: *Frame) !void {