Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: strip and collapse whitespace in option text

Jay Hemnani86f87a9ca94b
diff --git a/src/browser/tests/element/html/option.html b/src/browser/tests/element/html/option.html
index 93bafa0b9..1318db6ff 100644
--- a/src/browser/tests/element/html/option.html
+++ b/src/browser/tests/element/html/option.html
@@ -68,6 +68,27 @@
testing.expectEqual('Text 1', $('#opt1').label)
</script>
+<form id="ws_form">
+ <select id="ws_select" name="city">
+ <option id="opt_ws1">
+ San Francisco
+ </option>
+ <option id="opt_ws2" selected>New
+ York</option>
+ </select>
+</form>
+
+<script id="text_strips_and_collapses_whitespace">
+ testing.expectEqual('San Francisco', $('#opt_ws1').text)
+ testing.expectEqual('San Francisco', $('#opt_ws1').label)
+ testing.expectEqual('San Francisco', $('#opt_ws1').value)
+ testing.expectEqual('New York', $('#opt_ws2').value)
+ testing.expectEqual('New York', new FormData($('#ws_form')).get('city'))
+
+ $('#ws_select').value = 'San Francisco'
+ testing.expectEqual(0, $('#ws_select').selectedIndex)
+</script>
+
<script id="text_set">
$('#opt1').text = 'New Text 1'
testing.expectEqual('New Text 1', $('#opt1').text)
diff --git a/src/browser/webapi/element/html/Option.zig b/src/browser/webapi/element/html/Option.zig
index fea38fd64..d43c8d68e 100644
--- a/src/browser/webapi/element/html/Option.zig
+++ b/src/browser/webapi/element/html/Option.zig
@@ -70,14 +70,12 @@ pub fn asNode(self: *Option) *Node {
}
pub fn getValue(self: *Option, frame: *Frame) []const u8 {
- // If value attribute exists, use that; otherwise use text content (stripped)
+ // If value attribute exists, use that; otherwise use the text
if (self._value) |v| {
return v;
}
- const node = self.asNode();
- const text = node.getTextContentAlloc(frame.local_arena) catch return "";
- return std.mem.trim(u8, text, &std.ascii.whitespace);
+ return self.strippedText(frame.local_arena);
}
pub fn setValue(self: *Option, value: []const u8, frame: *Frame) !void {
@@ -87,8 +85,24 @@ pub fn setValue(self: *Option, value: []const u8, frame: *Frame) !void {
}
pub fn getText(self: *const Option, frame: *Frame) []const u8 {
+ return self.strippedText(frame.call_arena);
+}
+
+// https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
+fn strippedText(self: *const Option, allocator: std.mem.Allocator) []const u8 {
const node: *Node = @constCast(self.asConstElement().asConstNode());
- return node.getTextContentAlloc(frame.call_arena) catch "";
+ const content = node.getTextContentAlloc(allocator) catch return "";
+
+ // Strip and collapse ASCII whitespace.
+ var result: std.ArrayList(u8) = std.ArrayList(u8).initCapacity(allocator, content.len) catch return "";
+ var it = std.mem.tokenizeAny(u8, content, " \t\n\r\x0C");
+ while (it.next()) |word| {
+ if (result.items.len > 0) {
+ result.appendAssumeCapacity(' ');
+ }
+ result.appendSliceAssumeCapacity(word);
+ }
+ return result.items;
}
fn setText(self: *Option, value: []const u8, frame: *Frame) !void {