Skip to content

Commit e761c7e

Browse files
authored
Merge pull request #1115 from lightpanda-io/nikneym/url-changes
Small URL & Location changes
2 parents 27c9e18 + b8d4e3a commit e761c7e

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

src/browser/html/elements.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub const HTMLAnchorElement = struct {
281281
// TODO return a disposable string
282282
pub fn get_protocol(self: *parser.Anchor, page: *Page) ![]const u8 {
283283
var u = try url(self, page);
284-
return u.get_protocol(page);
284+
return u.get_protocol();
285285
}
286286

287287
pub fn set_protocol(self: *parser.Anchor, v: []const u8, page: *Page) !void {

src/browser/html/location.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ pub const Location = struct {
4141
return page.navigateFromWebAPI(href, .{ .reason = .script });
4242
}
4343

44-
pub fn get_protocol(self: *Location, page: *Page) ![]const u8 {
45-
return self.url.get_protocol(page);
44+
pub fn get_protocol(self: *Location) []const u8 {
45+
return self.url.get_protocol();
4646
}
4747

4848
pub fn get_host(self: *Location, page: *Page) ![]const u8 {

src/browser/url/url.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,11 @@ pub const URL = struct {
164164
return aw.written();
165165
}
166166

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

171174
pub fn get_username(self: *URL) []const u8 {

src/tests/url/url.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@
7676
testing.expectEqual("", sk.hostname);
7777
testing.expectEqual("sveltekit-internal://", sk.href);
7878
</script>
79+
80+
<script id=invalidUrl>
81+
let u = new URL("://foo.bar/path?query#fragment");
82+
testing.expectEqual(":", u.protocol);
83+
</script>

0 commit comments

Comments
 (0)