add HTMLElementLink get/set href

This commit is contained in:
Karl Seguin
2025-07-09 13:27:25 +08:00
parent bdc49a65aa
commit 7f732c94da
2 changed files with 30 additions and 0 deletions

View File

@@ -895,6 +895,15 @@ pub const HTMLLinkElement = struct {
pub fn constructor(page: *Page, js_this: Env.JsThis) !*parser.Element {
return constructHtmlElement(page, js_this);
}
pub fn get_href(self: *parser.Link) ![]const u8 {
return try parser.linkGetHref(self);
}
pub fn set_href(self: *parser.Link, href: []const u8, page: *const Page) !void {
const full = try urlStitch(page.call_arena, href, page.url.raw, .{});
return try parser.linkSetHref(self, full);
}
};
pub const HTMLMapElement = struct {
@@ -1581,6 +1590,12 @@ test "Browser.HTML.Element" {
.{ "document.createElement('a').focus()", null },
.{ "document.activeElement === focused", "true" },
}, .{});
try runner.testCases(&.{
.{ "let l2 = document.createElement('link');", null },
.{ "l2.href = 'https://lightpanda.io/'", null },
.{ "l2.href", "https://lightpanda.io/" },
}, .{});
}
test "Browser.HTML.Element.DataSet" {

View File

@@ -1830,6 +1830,21 @@ pub fn anchorSetRel(a: *Anchor, rel: []const u8) !void {
try DOMErr(err);
}
// HTMLLinkElement
pub fn linkGetHref(link: *Link) ![]const u8 {
var res: ?*String = undefined;
const err = c.dom_html_link_element_get_href(link, &res);
try DOMErr(err);
if (res == null) return "";
return strToData(res.?);
}
pub fn linkSetHref(link: *Link, href: []const u8) !void {
const err = c.dom_html_link_element_set_href(link, try strFromData(href));
try DOMErr(err);
}
// ElementsHTML
pub const MediaElement = struct { base: *c.dom_html_element };