Merge pull request #843 from lightpanda-io/empty_anchor_fix
Some checks failed
e2e-test / zig build release (push) Has been cancelled
zig-test / zig build dev (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
zig-test / browser fetch (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled

An empty anchor should return empty strings for its getters
This commit is contained in:
Karl Seguin
2025-07-04 23:43:10 +08:00
committed by GitHub
2 changed files with 19 additions and 2 deletions

View File

@@ -271,8 +271,18 @@ pub const HTMLAnchorElement = struct {
return try parser.nodeSetTextContent(parser.anchorToNode(self), v); return try parser.nodeSetTextContent(parser.anchorToNode(self), v);
} }
inline fn url(self: *parser.Anchor, page: *Page) !URL { fn url(self: *parser.Anchor, page: *Page) !URL {
return URL.constructor(.{ .element = @alignCast(@ptrCast(self)) }, null, page); // TODO inject base url // Although the URL.constructor union accepts an .{.element = X}, we
// can't use this here because the behavior is different.
// URL.constructor(document.createElement('a')
// should fail (a.href isn't a valid URL)
// But
// document.createElement('a').host
// should not fail, it should return an empty string
if (try parser.elementGetAttribute(@alignCast(@ptrCast(self)), "href")) |href| {
return URL.constructor(.{ .string = href }, null, page); // TODO inject base url
}
return .empty;
} }
// TODO return a disposable string // TODO return a disposable string
@@ -1559,6 +1569,8 @@ test "Browser.HTML.Element" {
try runner.testCases(&.{ try runner.testCases(&.{
.{ "let a = document.createElement('a');", null }, .{ "let a = document.createElement('a');", null },
.{ "a.href", "" },
.{ "a.host", "" },
.{ "a.href = 'about'", null }, .{ "a.href = 'about'", null },
.{ "a.href", "https://lightpanda.io/opensource-browser/about" }, .{ "a.href", "https://lightpanda.io/opensource-browser/about" },
}, .{}); }, .{});

View File

@@ -54,6 +54,11 @@ pub const URL = struct {
uri: std.Uri, uri: std.Uri,
search_params: URLSearchParams, search_params: URLSearchParams,
pub const empty = URL{
.uri = .{ .scheme = "" },
.search_params = .{},
};
const URLArg = union(enum) { const URLArg = union(enum) {
url: *URL, url: *URL,
element: *parser.ElementHTML, element: *parser.ElementHTML,