dump: rename HTML dump funcs

This commit is contained in:
Pierre Tachoire
2024-02-28 14:39:22 +01:00
parent c0e2377e16
commit 810bd11a5b
3 changed files with 32 additions and 31 deletions

View File

@@ -151,7 +151,7 @@ pub const Page = struct {
}
// if the page has a pointer to a document, dumps the HTML.
try Dump.htmlFile(self.doc.?, out);
try Dump.writeHTML(self.doc.?, out);
}
// spec reference: https://html.spec.whatwg.org/#document-lifecycle

View File

@@ -4,14 +4,15 @@ const File = std.fs.File;
const parser = @import("../netsurf.zig");
const Walker = @import("../dom/walker.zig").WalkerChildren;
pub fn htmlFile(doc: *parser.Document, out: anytype) !void {
try out.writeAll("<!DOCTYPE html>\n");
try nodeFile(parser.documentToNode(doc), out);
try out.writeAll("\n");
// writer must be a std.io.Writer
pub fn writeHTML(doc: *parser.Document, writer: anytype) !void {
try writer.writeAll("<!DOCTYPE html>\n");
try writeNode(parser.documentToNode(doc), writer);
try writer.writeAll("\n");
}
// out must be a std.io.Writer
pub fn nodeFile(root: *parser.Node, out: anytype) !void {
// writer must be a std.io.Writer
pub fn writeNode(root: *parser.Node, writer: anytype) !void {
const walker = Walker{};
var next: ?*parser.Node = null;
while (true) {
@@ -20,8 +21,8 @@ pub fn nodeFile(root: *parser.Node, out: anytype) !void {
.element => {
// open the tag
const tag = try parser.nodeLocalName(next.?);
try out.writeAll("<");
try out.writeAll(tag);
try writer.writeAll("<");
try writer.writeAll(tag);
// write the attributes
const map = try parser.nodeGetAttributes(next.?);
@@ -29,40 +30,40 @@ pub fn nodeFile(root: *parser.Node, out: anytype) !void {
var i: u32 = 0;
while (i < ln) {
const attr = try parser.namedNodeMapItem(map, i) orelse break;
try out.writeAll(" ");
try out.writeAll(try parser.attributeGetName(attr));
try out.writeAll("=\"");
try out.writeAll(try parser.attributeGetValue(attr) orelse "");
try out.writeAll("\"");
try writer.writeAll(" ");
try writer.writeAll(try parser.attributeGetName(attr));
try writer.writeAll("=\"");
try writer.writeAll(try parser.attributeGetValue(attr) orelse "");
try writer.writeAll("\"");
i += 1;
}
try out.writeAll(">");
try writer.writeAll(">");
// write the children
// TODO avoid recursion
try nodeFile(next.?, out);
try writeNode(next.?, writer);
// close the tag
try out.writeAll("</");
try out.writeAll(tag);
try out.writeAll(">");
try writer.writeAll("</");
try writer.writeAll(tag);
try writer.writeAll(">");
},
.text => {
const v = try parser.nodeValue(next.?) orelse continue;
try out.writeAll(v);
try writer.writeAll(v);
},
.cdata_section => {
const v = try parser.nodeValue(next.?) orelse continue;
try out.writeAll("<![CDATA[");
try out.writeAll(v);
try out.writeAll("]]>");
try writer.writeAll("<![CDATA[");
try writer.writeAll(v);
try writer.writeAll("]]>");
},
.comment => {
const v = try parser.nodeValue(next.?) orelse continue;
try out.writeAll("<!--");
try out.writeAll(v);
try out.writeAll("-->");
try writer.writeAll("<!--");
try writer.writeAll(v);
try writer.writeAll("-->");
},
// TODO handle processing instruction dump
.processing_instruction => continue,
@@ -82,8 +83,8 @@ pub fn nodeFile(root: *parser.Node, out: anytype) !void {
}
}
// HTMLFileTestFn is run by run_tests.zig
pub fn HTMLFileTestFn(out: File) !void {
// writeHTMLTestFn is run by run_tests.zig
pub fn writeHTMLTestFn(out: File) !void {
const file = try std.fs.cwd().openFile("test.html", .{});
defer file.close();
@@ -93,5 +94,5 @@ pub fn HTMLFileTestFn(out: File) !void {
const doc = parser.documentHTMLToDocument(doc_html);
try htmlFile(doc, out);
try writeHTML(doc, out);
}

View File

@@ -8,7 +8,7 @@ const checkCases = jsruntime.test_utils.checkCases;
const Variadic = jsruntime.Variadic;
const collection = @import("html_collection.zig");
const dumpNode = @import("../browser/dump.zig").nodeFile;
const writeNode = @import("../browser/dump.zig").writeNode;
const Node = @import("node.zig").Node;
const Walker = @import("walker.zig").WalkerDepthFirst;
@@ -83,7 +83,7 @@ pub const Element = struct {
var buf = std.ArrayList(u8).init(alloc);
defer buf.deinit();
try dumpNode(parser.elementToNode(self), buf.writer());
try writeNode(parser.elementToNode(self), buf.writer());
// TODO express the caller owned the slice.
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
return buf.toOwnedSlice();