Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub const HTMLAnchorElement = struct {
// TODO return a disposable string
pub fn get_protocol(self: *parser.Anchor, page: *Page) ![]const u8 {
var u = try url(self, page);
return u.get_protocol(page);
return u.get_protocol();
}

pub fn set_protocol(self: *parser.Anchor, v: []const u8, page: *Page) !void {
Expand Down
4 changes: 2 additions & 2 deletions src/browser/html/location.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub const Location = struct {
return page.navigateFromWebAPI(href, .{ .reason = .script });
}

pub fn get_protocol(self: *Location, page: *Page) ![]const u8 {
return self.url.get_protocol(page);
pub fn get_protocol(self: *Location) []const u8 {
return self.url.get_protocol();
}

pub fn get_host(self: *Location, page: *Page) ![]const u8 {
Expand Down
7 changes: 5 additions & 2 deletions src/browser/url/url.zig
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ pub const URL = struct {
return aw.written();
}

pub fn get_protocol(self: *URL, page: *Page) ![]const u8 {
return try std.mem.concat(page.arena, u8, &[_][]const u8{ self.uri.scheme, ":" });
pub fn get_protocol(self: *const URL) []const u8 {
// std.Uri keeps a pointer to "https", "http" (scheme part) so we know
// its followed by ':'.
const scheme = self.uri.scheme;
return scheme.ptr[0 .. scheme.len + 1];
}

pub fn get_username(self: *URL) []const u8 {
Expand Down
5 changes: 5 additions & 0 deletions src/tests/url/url.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@
testing.expectEqual("", sk.hostname);
testing.expectEqual("sveltekit-internal://", sk.href);
</script>

<script id=invalidUrl>
let u = new URL("://foo.bar/path?query#fragment");
testing.expectEqual(":", u.protocol);
</script>