|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const jsruntime = @import("jsruntime"); |
| 4 | +const Case = jsruntime.test_utils.Case; |
| 5 | +const checkCases = jsruntime.test_utils.checkCases; |
| 6 | +const generate = @import("../generate.zig"); |
| 7 | + |
| 8 | +pub const Interfaces = generate.Tuple(.{ |
| 9 | + URL, |
| 10 | + URLSearchParams, |
| 11 | +}); |
| 12 | + |
| 13 | +// https://url.spec.whatwg.org/#url |
| 14 | +// |
| 15 | +// TODO we could avoid many of these getter string allocation in two differents |
| 16 | +// way: |
| 17 | +// |
| 18 | +// 1. We can eventually get the slice of scheme *with* the following char in |
| 19 | +// the underlying string. But I don't know if it's possible and how to do that. |
| 20 | +// I mean, if the rawuri contains `https://foo.bar`, uri.scheme is a slice |
| 21 | +// containing only `https`. I want `https:` so, in theory, I don't need to |
| 22 | +// allocate data, I should be able to retrieve the scheme + the following `:` |
| 23 | +// from rawuri. |
| 24 | +// |
| 25 | +// 2. The other way would bu to copy the `std.Uri` code to ahve a dedicated |
| 26 | +// parser including the characters we want for the web API. |
| 27 | +pub const URL = struct { |
| 28 | + rawuri: []const u8, |
| 29 | + uri: std.Uri, |
| 30 | + |
| 31 | + pub const mem_guarantied = true; |
| 32 | + |
| 33 | + pub fn constructor(alloc: std.mem.Allocator, url: []const u8, base: ?[]const u8) !URL { |
| 34 | + const raw = try std.mem.concat(alloc, u8, &[_][]const u8{ url, base orelse "" }); |
| 35 | + errdefer alloc.free(raw); |
| 36 | + |
| 37 | + const uri = std.Uri.parse(raw) catch { |
| 38 | + return error.TypeError; |
| 39 | + }; |
| 40 | + |
| 41 | + return .{ |
| 42 | + .rawuri = raw, |
| 43 | + .uri = uri, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + pub fn deinit(self: *URL, alloc: std.mem.Allocator) void { |
| 48 | + alloc.free(self.rawuri); |
| 49 | + } |
| 50 | + |
| 51 | + // the caller must free the returned string. |
| 52 | + // TODO return a disposable string |
| 53 | + // https://github.com/lightpanda-io/jsruntime-lib/issues/195 |
| 54 | + pub fn get_href(self: URL, alloc: std.mem.Allocator) ![]const u8 { |
| 55 | + var buf = std.ArrayList(u8).init(alloc); |
| 56 | + defer buf.deinit(); |
| 57 | + |
| 58 | + try self.uri.writeToStream(.{ |
| 59 | + .scheme = true, |
| 60 | + .authentication = true, |
| 61 | + .authority = true, |
| 62 | + .path = true, |
| 63 | + .query = true, |
| 64 | + .fragment = true, |
| 65 | + }, buf.writer()); |
| 66 | + return try buf.toOwnedSlice(); |
| 67 | + } |
| 68 | + |
| 69 | + // the caller must free the returned string. |
| 70 | + // TODO return a disposable string |
| 71 | + // https://github.com/lightpanda-io/jsruntime-lib/issues/195 |
| 72 | + pub fn get_protocol(self: *URL, alloc: std.mem.Allocator) ![]const u8 { |
| 73 | + return try std.mem.concat(alloc, u8, &[_][]const u8{ self.uri.scheme, ":" }); |
| 74 | + } |
| 75 | + |
| 76 | + pub fn get_username(self: *URL) []const u8 { |
| 77 | + return self.uri.user orelse ""; |
| 78 | + } |
| 79 | + |
| 80 | + pub fn get_password(self: *URL) []const u8 { |
| 81 | + return self.uri.password orelse ""; |
| 82 | + } |
| 83 | + |
| 84 | + pub fn get_host(self: *URL) []const u8 { |
| 85 | + return self.uri.host orelse ""; |
| 86 | + } |
| 87 | + |
| 88 | + pub fn get_hostname(self: *URL) []const u8 { |
| 89 | + return self.uri.host orelse ""; |
| 90 | + } |
| 91 | + |
| 92 | + // the caller must free the returned string. |
| 93 | + // TODO return a disposable string |
| 94 | + // https://github.com/lightpanda-io/jsruntime-lib/issues/195 |
| 95 | + pub fn get_port(self: *URL, alloc: std.mem.Allocator) ![]const u8 { |
| 96 | + if (self.uri.port == null) return try alloc.dupe(u8, ""); |
| 97 | + |
| 98 | + var buf = std.ArrayList(u8).init(alloc); |
| 99 | + defer buf.deinit(); |
| 100 | + |
| 101 | + try std.fmt.formatInt(self.uri.port.?, 10, .lower, .{}, buf.writer()); |
| 102 | + return try buf.toOwnedSlice(); |
| 103 | + } |
| 104 | + |
| 105 | + pub fn get_pathname(self: *URL) []const u8 { |
| 106 | + if (self.uri.path.len == 0) return "/"; |
| 107 | + return self.uri.path; |
| 108 | + } |
| 109 | + |
| 110 | + // the caller must free the returned string. |
| 111 | + // TODO return a disposable string |
| 112 | + // https://github.com/lightpanda-io/jsruntime-lib/issues/195 |
| 113 | + pub fn get_search(self: *URL, alloc: std.mem.Allocator) ![]const u8 { |
| 114 | + if (self.uri.query == null) return try alloc.dupe(u8, ""); |
| 115 | + |
| 116 | + return try std.mem.concat(alloc, u8, &[_][]const u8{ "?", self.uri.query.? }); |
| 117 | + } |
| 118 | + |
| 119 | + // the caller must free the returned string. |
| 120 | + // TODO return a disposable string |
| 121 | + // https://github.com/lightpanda-io/jsruntime-lib/issues/195 |
| 122 | + pub fn get_hash(self: *URL, alloc: std.mem.Allocator) ![]const u8 { |
| 123 | + if (self.uri.fragment == null) return try alloc.dupe(u8, ""); |
| 124 | + |
| 125 | + return try std.mem.concat(alloc, u8, &[_][]const u8{ "#", self.uri.fragment.? }); |
| 126 | + } |
| 127 | + |
| 128 | + pub fn _toJSON(self: *URL, alloc: std.mem.Allocator) ![]const u8 { |
| 129 | + return try self.get_href(alloc); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +// https://url.spec.whatwg.org/#interface-urlsearchparams |
| 134 | +pub const URLSearchParams = struct { |
| 135 | + pub const mem_guarantied = true; |
| 136 | +}; |
| 137 | + |
| 138 | +// Tests |
| 139 | +// ----- |
| 140 | + |
| 141 | +pub fn testExecFn( |
| 142 | + _: std.mem.Allocator, |
| 143 | + js_env: *jsruntime.Env, |
| 144 | +) anyerror!void { |
| 145 | + var url = [_]Case{ |
| 146 | + .{ .src = "var url = new URL('https://foo.bar/path?query#fragment')", .ex = "undefined" }, |
| 147 | + .{ .src = "url.href", .ex = "https://foo.bar/path?query#fragment" }, |
| 148 | + .{ .src = "url.protocol", .ex = "https:" }, |
| 149 | + .{ .src = "url.username", .ex = "" }, |
| 150 | + .{ .src = "url.password", .ex = "" }, |
| 151 | + .{ .src = "url.host", .ex = "foo.bar" }, |
| 152 | + .{ .src = "url.hostname", .ex = "foo.bar" }, |
| 153 | + .{ .src = "url.port", .ex = "" }, |
| 154 | + .{ .src = "url.pathname", .ex = "/path" }, |
| 155 | + .{ .src = "url.search", .ex = "?query" }, |
| 156 | + .{ .src = "url.hash", .ex = "#fragment" }, |
| 157 | + }; |
| 158 | + try checkCases(js_env, &url); |
| 159 | +} |
0 commit comments