Skip to content

Lightpanda: the headless browser designed for AI and automation

normalize allocation in call_arena

Karl Seguin0d2d4b3cc918
diff --git a/src/browser/tests/element/html/textarea.html b/src/browser/tests/element/html/textarea.html
index 038b920a4..a0ff7280f 100644
--- a/src/browser/tests/element/html/textarea.html
+++ b/src/browser/tests/element/html/textarea.html
@@ -47,6 +47,13 @@
ta.select()
testing.expectEqual(6, ta.selectionEnd)
}
+ {
+ const ta = document.createElement('textarea')
+ ta.defaultValue = 'a\r\nb\rc'
+ testing.expectEqual('a\r\nb\rc', ta.defaultValue)
+ testing.expectEqual('a\nb\nc', ta.value)
+ testing.expectEqual(5, ta.textLength)
+ }
</script>
<script id="textLength">
diff --git a/src/browser/webapi/element/html/TextArea.zig b/src/browser/webapi/element/html/TextArea.zig
index 0e592a2f4..cf81e9ec3 100644
--- a/src/browser/webapi/element/html/TextArea.zig
+++ b/src/browser/webapi/element/html/TextArea.zig
@@ -70,7 +70,7 @@ pub fn getValue(self: *const TextArea) []const u8 {
}
pub fn setValue(self: *TextArea, raw_value: []const u8, frame: *Frame) !void {
- const value = try normalizeNewlines(raw_value, frame);
+ const value = try normalizeNewlines(raw_value, frame.call_arena);
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
@@ -92,10 +92,10 @@ pub fn setValue(self: *TextArea, raw_value: []const u8, frame: *Frame) !void {
}
// The API value has LF newlines: CRLF and lone CR become LF.
-fn normalizeNewlines(value: []const u8, frame: *Frame) ![]const u8 {
- const first = std.mem.findAny(u8, value, "\r") orelse return value;
+fn normalizeNewlines(value: []const u8, arena: std.mem.Allocator) ![]const u8 {
+ const first = std.mem.findScalar(u8, value, '\r') orelse return value;
- var result = try frame.page_arena.alloc(u8, value.len);
+ var result = try arena.alloc(u8, value.len);
@memcpy(result[0..first], value[0..first]);
var i: usize = first;
@@ -114,10 +114,6 @@ fn normalizeNewlines(value: []const u8, frame: *Frame) ![]const u8 {
return result[0..i];
}
-pub fn getTextLength(self: *const TextArea) u32 {
- return @intCast(Node.CData.utf16Len(self.getValue()));
-}
-
pub fn reset(self: *TextArea) void {
self._value = null;
self._user_edited = false;
@@ -295,8 +291,21 @@ pub const JsApi = struct {
pub const reportValidity = bridge.function(TextArea.reportValidity, .{});
pub const setCustomValidity = bridge.function(TextArea.setCustomValidity, .{});
pub const @"type" = bridge.property("textarea", .{ .template = false, .readonly = true });
- pub const value = bridge.accessor(TextArea.getValue, TextArea.setValue, .{});
- pub const textLength = bridge.accessor(TextArea.getTextLength, null, .{});
+ pub const value = bridge.accessor(_getApiValue, TextArea.setValue, .{});
+ // _value is already normalized; the default value (text content) may not be.
+ fn _getApiValue(self: *const TextArea, frame: *Frame) ![]const u8 {
+ if (self._value) |v| {
+ return v;
+ }
+ return normalizeNewlines(self.getDefaultValue(), frame.call_arena);
+ }
+
+ pub const textLength = bridge.accessor(struct {
+ fn wrap(self: *const TextArea, frame: *Frame) !u32 {
+ return @intCast(Node.CData.utf16Len(try _getApiValue(self, frame)));
+ }
+ }.wrap, null, .{});
+
pub const defaultValue = bridge.accessor(TextArea.getDefaultValue, TextArea.setDefaultValue, .{ .ce_reactions = true });
pub const disabled = reflect.boolean("disabled");
pub const name = reflect.string("name");