Skip to content

Commit 28a87c2

Browse files
committed
url: first draft
1 parent e2cd983 commit 28a87c2

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

src/apiweb.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const HTML = @import("html/html.zig");
77
const Events = @import("events/event.zig");
88
const XHR = @import("xhr/xhr.zig");
99
const Storage = @import("storage/storage.zig");
10+
const URL = @import("url/url.zig");
1011

1112
pub const HTMLDocument = @import("html/document.zig").HTMLDocument;
1213

@@ -18,4 +19,5 @@ pub const Interfaces = generate.Tuple(.{
1819
HTML.Interfaces,
1920
XHR.Interfaces,
2021
Storage.Interfaces,
22+
URL.Interfaces,
2123
});

src/run_tests.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const apiweb = @import("apiweb.zig");
1010
const Window = @import("html/window.zig").Window;
1111
const xhr = @import("xhr/xhr.zig");
1212
const storage = @import("storage/storage.zig");
13+
const url = @import("url/url.zig");
1314

1415
const documentTestExecFn = @import("dom/document.zig").testExecFn;
1516
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
@@ -30,6 +31,7 @@ const EventTestExecFn = @import("events/event.zig").testExecFn;
3031
const XHRTestExecFn = xhr.testExecFn;
3132
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
3233
const StorageTestExecFn = storage.testExecFn;
34+
const URLTestExecFn = url.testExecFn;
3335

3436
pub const Types = jsruntime.reflect(apiweb.Interfaces);
3537

@@ -95,6 +97,7 @@ fn testsAllExecFn(
9597
ProgressEventTestExecFn,
9698
ProcessingInstructionTestExecFn,
9799
StorageTestExecFn,
100+
URLTestExecFn,
98101
};
99102

100103
inline for (testFns) |testFn| {

src/url/url.zig

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

Comments
 (0)