Skip to content

Commit e12d6e8

Browse files
committed
url: add origin getter
1 parent 7da440e commit e12d6e8

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/url/url.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ pub const URL = struct {
7171
alloc.free(self.rawuri);
7272
}
7373

74+
pub fn get_origin(self: *URL, alloc: std.mem.Allocator) ![]const u8 {
75+
var buf = std.ArrayList(u8).init(alloc);
76+
defer buf.deinit();
77+
78+
try self.uri.writeToStream(.{
79+
.scheme = true,
80+
.authentication = false,
81+
.authority = true,
82+
.path = false,
83+
.query = false,
84+
.fragment = false,
85+
}, buf.writer());
86+
return try buf.toOwnedSlice();
87+
}
88+
7489
// the caller must free the returned string.
7590
// TODO return a disposable string
7691
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
@@ -223,6 +238,7 @@ pub fn testExecFn(
223238
) anyerror!void {
224239
var url = [_]Case{
225240
.{ .src = "var url = new URL('https://foo.bar/path?query#fragment')", .ex = "undefined" },
241+
.{ .src = "url.origin", .ex = "https://foo.bar" },
226242
.{ .src = "url.href", .ex = "https://foo.bar/path?query#fragment" },
227243
.{ .src = "url.protocol", .ex = "https:" },
228244
.{ .src = "url.username", .ex = "" },

0 commit comments

Comments
 (0)