Merge pull request #934 from lightpanda-io/with-base
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

add a --with_base option to fetch
This commit is contained in:
Karl Seguin
2025-08-09 07:37:32 +08:00
committed by GitHub
3 changed files with 61 additions and 5 deletions

View File

@@ -111,6 +111,9 @@ pub const Node = struct {
// --------
// Read-only attributes
pub fn get_baseURI(_: *parser.Node, page: *Page) ![]const u8 {
return page.url.raw;
}
pub fn get_firstChild(self: *parser.Node) !?Union {
const res = try parser.nodeFirstChild(self);
@@ -737,6 +740,10 @@ test "Browser.DOM.node" {
.{ "link.normalize()", "undefined" },
}, .{});
try runner.testCases(&.{
.{ "link.baseURI", "https://lightpanda.io/opensource-browser/" },
}, .{});
try runner.testCases(&.{
.{ "content.removeChild(append) !== undefined", "true" },
.{ "last_child.__proto__.constructor.name !== 'HTMLHeadingElement'", "true" },

View File

@@ -141,8 +141,13 @@ pub const Page = struct {
repeat_delay.* = 100 * std.time.ns_per_ms;
}
pub const DumpOpts = struct {
exclude_scripts: bool = false,
with_base: bool = false,
};
// dump writes the page content into the given file.
pub fn dump(self: *const Page, opts: Dump.Opts, out: std.fs.File) !void {
pub fn dump(self: *const Page, opts: DumpOpts, out: std.fs.File) !void {
if (self.raw_data) |raw_data| {
// raw_data was set if the document was not HTML, dump the data content only.
return try out.writeAll(raw_data);
@@ -150,7 +155,33 @@ pub const Page = struct {
// if the page has a pointer to a document, dumps the HTML.
const doc = parser.documentHTMLToDocument(self.window.document);
try Dump.writeHTML(doc, opts, out);
// if the base si requested, add the base's node in the document's headers.
if (opts.with_base) {
try self.addDOMTreeBase();
}
try Dump.writeHTML(doc, .{
.exclude_scripts = opts.exclude_scripts,
}, out);
}
// addDOMTreeBase modifies the page's document to add a <base> tag after
// <head>.
// If <head> is missing, the function returns silently.
fn addDOMTreeBase(self: *const Page) !void {
const doc = parser.documentHTMLToDocument(self.window.document);
std.debug.assert(doc.is_html);
// find <head> tag
const list = try parser.documentGetElementsByTagName(doc, "head");
const head = try parser.nodeListItem(list, 0) orelse return;
const base = try parser.documentCreateElement(doc, "base");
try parser.elementSetAttribute(base, "href", self.url.raw);
const Node = @import("dom/node.zig").Node;
try Node.prepend(head, &[_]Node.NodeOrText{.{ .node = parser.elementToNode(base) }});
}
pub fn fetchModuleSource(ctx: *anyopaque, src: []const u8) !?[]const u8 {

View File

@@ -134,7 +134,10 @@ fn run(alloc: Allocator) !void {
// dump
if (opts.dump) {
try page.dump(.{ .exclude_scripts = opts.noscript }, std.io.getStdOut());
try page.dump(.{
.exclude_scripts = opts.noscript,
.with_base = opts.withbase,
}, std.io.getStdOut());
}
},
else => unreachable,
@@ -213,6 +216,7 @@ const Command = struct {
dump: bool = false,
common: Common,
noscript: bool = false,
withbase: bool = false,
};
const Common = struct {
@@ -277,6 +281,7 @@ const Command = struct {
\\--dump Dumps document to stdout.
\\ Defaults to false.
\\--noscript Exclude <script> tags in dump. Defaults to false.
\\--with_base Add a <base> tag in dump. Defaults to false.
\\
++ common_options ++
\\
@@ -351,13 +356,19 @@ fn inferMode(opt: []const u8) ?App.RunMode {
return .serve;
}
if (std.mem.startsWith(u8, opt, "--") == false) {
return .fetch;
}
if (std.mem.eql(u8, opt, "--dump")) {
return .fetch;
}
if (std.mem.eql(u8, opt, "--noscript")) {
return .fetch;
}
if (std.mem.startsWith(u8, opt, "--") == false) {
if (std.mem.eql(u8, opt, "--with_base")) {
return .fetch;
}
@@ -442,7 +453,8 @@ fn parseFetchArgs(
args: *std.process.ArgIterator,
) !Command.Fetch {
var dump: bool = false;
var noscript: bool = true;
var noscript: bool = false;
var withbase: bool = false;
var url: ?[]const u8 = null;
var common: Command.Common = .{};
@@ -457,6 +469,11 @@ fn parseFetchArgs(
continue;
}
if (std.mem.eql(u8, "--with_base", opt)) {
withbase = true;
continue;
}
if (try parseCommonArg(allocator, opt, args, &common)) {
continue;
}
@@ -483,6 +500,7 @@ fn parseFetchArgs(
.dump = dump,
.common = common,
.noscript = noscript,
.withbase = withbase,
};
}