From 6d808d89b0a0a4c25eafaae1ab32da313688a093 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 14 May 2024 12:00:08 +0200 Subject: [PATCH] anchor: implement set_host --- src/html/elements.zig | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/html/elements.zig b/src/html/elements.zig index d9ff0ce3..cf0175f5 100644 --- a/src/html/elements.zig +++ b/src/html/elements.zig @@ -231,11 +231,32 @@ pub const HTMLAnchorElement = struct { } pub fn set_host(self: *parser.Anchor, alloc: std.mem.Allocator, v: []const u8) !void { - _ = self; - _ = alloc; - _ = v; - // TODO - return error.NotImplemented; + // search : separator + var p: ?u16 = null; + var h: []const u8 = undefined; + for (v, 0..) |c, i| { + if (c == ':') { + h = v[0..i]; + p = try std.fmt.parseInt(u16, v[i + 1 ..], 10); + break; + } + } + + var u = try url(self, alloc); + defer u.deinit(alloc); + + if (p) |pp| { + u.uri.host = h; + u.uri.port = pp; + } else { + u.uri.host = v; + u.uri.port = null; + } + + const href = try u.format(alloc); + defer alloc.free(href); + + try parser.anchorSetHref(self, href); } // TODO return a disposable string @@ -855,6 +876,16 @@ pub fn testExecFn( .{ .src = "a.origin", .ex = "https://lightpanda.io" }, + .{ .src = "a.host = 'lightpanda.io:443'", .ex = "lightpanda.io:443" }, + .{ .src = "a.host", .ex = "lightpanda.io:443" }, + .{ .src = "a.port", .ex = "443" }, + .{ .src = "a.hostname", .ex = "lightpanda.io" }, + + .{ .src = "a.host = 'lightpanda.io'", .ex = "lightpanda.io" }, + .{ .src = "a.host", .ex = "lightpanda.io" }, + .{ .src = "a.port", .ex = "" }, + .{ .src = "a.hostname", .ex = "lightpanda.io" }, + .{ .src = "a.host", .ex = "lightpanda.io" }, .{ .src = "a.hostname", .ex = "lightpanda.io" }, .{ .src = "a.hostname = 'foo.bar'", .ex = "foo.bar" },