set page base_url during HTML parsing

This commit is contained in:
Pierre Tachoire
2025-12-22 18:12:44 +01:00
parent 6dbd008724
commit a597d31505
3 changed files with 33 additions and 0 deletions

View File

@@ -1430,6 +1430,24 @@ pub fn createElement(self: *Page, ns_: ?[]const u8, name: []const u8, attribute_
attribute_iterator,
.{ ._proto = undefined },
),
asUint("base") => {
const n = try self.createHtmlElementT(
Element.Html.Generic,
namespace,
attribute_iterator,
.{ ._proto = undefined, ._tag_name = String.init(undefined, "base", .{}) catch unreachable, ._tag = .base },
);
// If page's base url is not already set, fill it with the base
// tag.
if (self.base_url == null) {
if (n.as(Element).getAttributeSafe("href")) |href| {
self.base_url = try URL.resolve(self.arena, self.url, href, .{});
}
}
return n;
},
else => {},
},
5 => switch (@as(u40, @bitCast(name[0..5].*))) {

View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<base href="https://example.com/">
<a href="foo" id="foo">foo</a>
<script id=baseURI>
testing.expectEqual("http://127.0.0.1:9582/src/browser/tests/node/base_uri.html", document.URL);
testing.expectEqual("https://example.com/", document.baseURI);
const link = $('#foo');
testing.expectEqual("https://example.com/foo", link.href);
</script>

View File

@@ -1178,6 +1178,7 @@ pub const Tag = enum {
body,
br,
button,
base,
canvas,
circle,
custom,