Skip to content

Lightpanda: the headless browser designed for AI and automation

Merge pull request #2787 from lightpanda-io/frame_breakup

Karl Seguine9bbf95cf587
diff --cc src/browser/Frame.zig
index 86d624e55,43c9a4345..ae4761b9b
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@@ -62,12 -61,8 +61,9 @@@ const SubmitEvent = @import("webapi/eve
const popover = @import("webapi/element/popover.zig");
const slotting = @import("webapi/element/slotting.zig");
const NavigationKind = @import("webapi/navigation/root.zig").NavigationKind;
- const KeyboardEvent = @import("webapi/event/KeyboardEvent.zig");
- const MouseEvent = @import("webapi/event/MouseEvent.zig");
- const WheelEvent = @import("webapi/event/WheelEvent.zig");
const HttpClient = @import("HttpClient.zig");
+const sys_url = @import("../sys/url.zig");
const timestamp = @import("../datetime.zig").timestamp;
const milliTimestamp = @import("../datetime.zig").milliTimestamp;
@@@ -4810,67 -2987,7 +3185,36 @@@ pub fn submitForm(self: *Frame, submitt
return self.scheduleNavigationWithArena(arena, action, opts, .{ .form = target_frame });
}
- // insertText is a shortcut to insert text into the active element.
- pub fn insertText(self: *Frame, v: []const u8) !void {
- const html_element = self.document._active_element orelse return;
-
- if (html_element.is(Element.Html.Input)) |input| {
- const input_type = input._input_type;
- if (input_type == .radio or input_type == .checkbox) {
- return;
- }
-
- return input.innerInsert(v, self);
- }
-
- if (html_element.is(Element.Html.TextArea)) |textarea| {
- return textarea.innerInsert(v, self);
- }
- }
-
- fn asUint(comptime string: anytype) std.meta.Int(
- .unsigned,
- @bitSizeOf(@TypeOf(string.*)) - 8, // (- 8) to exclude sentinel 0
- ) {
- const byteLength = @sizeOf(@TypeOf(string.*)) - 1;
- const expectedType = *const [byteLength:0]u8;
- if (@TypeOf(string) != expectedType) {
- @compileError("expected : " ++ @typeName(expectedType) ++ ", got: " ++ @typeName(@TypeOf(string)));
- }
-
- return @bitCast(@as(*const [byteLength]u8, string).*);
- }
-
const testing = @import("../testing.zig");
+
+fn dispositionHeader(value: []const u8) HttpClient.Header {
+ return .{ .name = "content-disposition", .value = value };
+}
+
+test "Frame: dispositionFilename" {
+ try testing.expectEqualSlices(u8, "report.csv", dispositionFilename(dispositionHeader("attachment; filename=\"report.csv\"")).?);
+ try testing.expectEqualSlices(u8, "report.csv", dispositionFilename(dispositionHeader("attachment; filename=report.csv")).?);
+ try testing.expectEqualSlices(u8, "r e.csv", dispositionFilename(dispositionHeader("attachment; filename=\"r e.csv\"")).?);
+ // RFC 5987 extended form is preferred over the plain filename when present
+ // (the value is taken verbatim after the charset'lang' prefix).
+ try testing.expectEqualSlices(u8, "extended.txt", dispositionFilename(dispositionHeader("attachment; filename=\"fallback.txt\"; filename*=UTF-8''extended.txt")).?);
+ try testing.expect(dispositionFilename(dispositionHeader("attachment")) == null);
+ // Path components are stripped to guard against traversal.
+ try testing.expectEqualSlices(u8, "evil.sh", dispositionFilename(dispositionHeader("attachment; filename=\"../../evil.sh\"")).?);
+ try testing.expectEqualSlices(u8, "evil.sh", dispositionFilename(dispositionHeader("attachment; filename=\"..\\..\\evil.sh\"")).?);
+ try testing.expect(dispositionFilename(dispositionHeader("attachment; filename=\"..\"")) == null);
+}
+
+test "Frame: urlBasename" {
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+
+ try testing.expectEqualSlices(u8, "report.csv", (try urlBasename(a, "http://x.com/a/b/report.csv")).?);
+ try testing.expectEqualSlices(u8, "report.csv", (try urlBasename(a, "http://x.com/report.csv?v=1#x")).?);
+ try testing.expect((try urlBasename(a, "http://x.com/")) == null);
+}
+
test "WebApi: Frame" {
const filter: testing.LogFilter = .init(&.{.http});
defer filter.deinit();