Skip to content

Commit 6d808d8

Browse files
committed
anchor: implement set_host
1 parent 6b42b5a commit 6d808d8

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/html/elements.zig

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,32 @@ pub const HTMLAnchorElement = struct {
231231
}
232232

233233
pub fn set_host(self: *parser.Anchor, alloc: std.mem.Allocator, v: []const u8) !void {
234-
_ = self;
235-
_ = alloc;
236-
_ = v;
237-
// TODO
238-
return error.NotImplemented;
234+
// search : separator
235+
var p: ?u16 = null;
236+
var h: []const u8 = undefined;
237+
for (v, 0..) |c, i| {
238+
if (c == ':') {
239+
h = v[0..i];
240+
p = try std.fmt.parseInt(u16, v[i + 1 ..], 10);
241+
break;
242+
}
243+
}
244+
245+
var u = try url(self, alloc);
246+
defer u.deinit(alloc);
247+
248+
if (p) |pp| {
249+
u.uri.host = h;
250+
u.uri.port = pp;
251+
} else {
252+
u.uri.host = v;
253+
u.uri.port = null;
254+
}
255+
256+
const href = try u.format(alloc);
257+
defer alloc.free(href);
258+
259+
try parser.anchorSetHref(self, href);
239260
}
240261

241262
// TODO return a disposable string
@@ -855,6 +876,16 @@ pub fn testExecFn(
855876

856877
.{ .src = "a.origin", .ex = "https://lightpanda.io" },
857878

879+
.{ .src = "a.host = 'lightpanda.io:443'", .ex = "lightpanda.io:443" },
880+
.{ .src = "a.host", .ex = "lightpanda.io:443" },
881+
.{ .src = "a.port", .ex = "443" },
882+
.{ .src = "a.hostname", .ex = "lightpanda.io" },
883+
884+
.{ .src = "a.host = 'lightpanda.io'", .ex = "lightpanda.io" },
885+
.{ .src = "a.host", .ex = "lightpanda.io" },
886+
.{ .src = "a.port", .ex = "" },
887+
.{ .src = "a.hostname", .ex = "lightpanda.io" },
888+
858889
.{ .src = "a.host", .ex = "lightpanda.io" },
859890
.{ .src = "a.hostname", .ex = "lightpanda.io" },
860891
.{ .src = "a.hostname = 'foo.bar'", .ex = "foo.bar" },

0 commit comments

Comments
 (0)