@@ -271,8 +271,18 @@ pub const HTMLAnchorElement = struct {
271271 return try parser .nodeSetTextContent (parser .anchorToNode (self ), v );
272272 }
273273
274- inline fn url (self : * parser.Anchor , page : * Page ) ! URL {
275- return URL .constructor (.{ .element = @alignCast (@ptrCast (self )) }, null , page ); // TODO inject base url
274+ fn url (self : * parser.Anchor , page : * Page ) ! URL {
275+ // Although the URL.constructor union accepts an .{.element = X}, we
276+ // can't use this here because the behavior is different.
277+ // URL.constructor(document.createElement('a')
278+ // should fail (a.href isn't a valid URL)
279+ // But
280+ // document.createElement('a').host
281+ // should not fail, it should return an empty string
282+ if (try parser .elementGetAttribute (@alignCast (@ptrCast (self )), "href" )) | href | {
283+ return URL .constructor (.{ .string = href }, null , page ); // TODO inject base url
284+ }
285+ return .empty ;
276286 }
277287
278288 // TODO return a disposable string
@@ -1559,6 +1569,8 @@ test "Browser.HTML.Element" {
15591569
15601570 try runner .testCases (&.{
15611571 .{ "let a = document.createElement('a');" , null },
1572+ .{ "a.href" , "" },
1573+ .{ "a.host" , "" },
15621574 .{ "a.href = 'about'" , null },
15631575 .{ "a.href" , "https://lightpanda.io/opensource-browser/about" },
15641576 }, .{});
0 commit comments