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. // 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 // 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 parser = @import("../netsurf.zig");
const Walker = @import("../dom/walker.zig").WalkerChildren; const Walker = @import("../dom/walker.zig").WalkerChildren;
pub fn htmlFile(doc: *parser.Document, out: anytype) !void { // writer must be a std.io.Writer
try out.writeAll("<!DOCTYPE html>\n"); pub fn writeHTML(doc: *parser.Document, writer: anytype) !void {
try nodeFile(parser.documentToNode(doc), out); try writer.writeAll("<!DOCTYPE html>\n");
try out.writeAll("\n"); try writeNode(parser.documentToNode(doc), writer);
try writer.writeAll("\n");
} }
// out must be a std.io.Writer // writer must be a std.io.Writer
pub fn nodeFile(root: *parser.Node, out: anytype) !void { pub fn writeNode(root: *parser.Node, writer: anytype) !void {
const walker = Walker{}; const walker = Walker{};
var next: ?*parser.Node = null; var next: ?*parser.Node = null;
while (true) { while (true) {
@@ -20,8 +21,8 @@ pub fn nodeFile(root: *parser.Node, out: anytype) !void {
.element => { .element => {
// open the tag // open the tag
const tag = try parser.nodeLocalName(next.?); const tag = try parser.nodeLocalName(next.?);
try out.writeAll("<"); try writer.writeAll("<");
try out.writeAll(tag); try writer.writeAll(tag);
// write the attributes // write the attributes
const map = try parser.nodeGetAttributes(next.?); const map = try parser.nodeGetAttributes(next.?);
@@ -29,40 +30,40 @@ pub fn nodeFile(root: *parser.Node, out: anytype) !void {
var i: u32 = 0; var i: u32 = 0;
while (i < ln) { while (i < ln) {
const attr = try parser.namedNodeMapItem(map, i) orelse break; const attr = try parser.namedNodeMapItem(map, i) orelse break;
try out.writeAll(" "); try writer.writeAll(" ");
try out.writeAll(try parser.attributeGetName(attr)); try writer.writeAll(try parser.attributeGetName(attr));
try out.writeAll("=\""); try writer.writeAll("=\"");
try out.writeAll(try parser.attributeGetValue(attr) orelse ""); try writer.writeAll(try parser.attributeGetValue(attr) orelse "");
try out.writeAll("\""); try writer.writeAll("\"");
i += 1; i += 1;
} }
try out.writeAll(">"); try writer.writeAll(">");
// write the children // write the children
// TODO avoid recursion // TODO avoid recursion
try nodeFile(next.?, out); try writeNode(next.?, writer);
// close the tag // close the tag
try out.writeAll("</"); try writer.writeAll("</");
try out.writeAll(tag); try writer.writeAll(tag);
try out.writeAll(">"); try writer.writeAll(">");
}, },
.text => { .text => {
const v = try parser.nodeValue(next.?) orelse continue; const v = try parser.nodeValue(next.?) orelse continue;
try out.writeAll(v); try writer.writeAll(v);
}, },
.cdata_section => { .cdata_section => {
const v = try parser.nodeValue(next.?) orelse continue; const v = try parser.nodeValue(next.?) orelse continue;
try out.writeAll("<![CDATA["); try writer.writeAll("<![CDATA[");
try out.writeAll(v); try writer.writeAll(v);
try out.writeAll("]]>"); try writer.writeAll("]]>");
}, },
.comment => { .comment => {
const v = try parser.nodeValue(next.?) orelse continue; const v = try parser.nodeValue(next.?) orelse continue;
try out.writeAll("<!--"); try writer.writeAll("<!--");
try out.writeAll(v); try writer.writeAll(v);
try out.writeAll("-->"); try writer.writeAll("-->");
}, },
// TODO handle processing instruction dump // TODO handle processing instruction dump
.processing_instruction => continue, .processing_instruction => continue,
@@ -82,8 +83,8 @@ pub fn nodeFile(root: *parser.Node, out: anytype) !void {
} }
} }
// HTMLFileTestFn is run by run_tests.zig // writeHTMLTestFn is run by run_tests.zig
pub fn HTMLFileTestFn(out: File) !void { pub fn writeHTMLTestFn(out: File) !void {
const file = try std.fs.cwd().openFile("test.html", .{}); const file = try std.fs.cwd().openFile("test.html", .{});
defer file.close(); defer file.close();
@@ -93,5 +94,5 @@ pub fn HTMLFileTestFn(out: File) !void {
const doc = parser.documentHTMLToDocument(doc_html); 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 Variadic = jsruntime.Variadic;
const collection = @import("html_collection.zig"); 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 Node = @import("node.zig").Node;
const Walker = @import("walker.zig").WalkerDepthFirst; const Walker = @import("walker.zig").WalkerDepthFirst;
@@ -83,7 +83,7 @@ pub const Element = struct {
var buf = std.ArrayList(u8).init(alloc); var buf = std.ArrayList(u8).init(alloc);
defer buf.deinit(); defer buf.deinit();
try dumpNode(parser.elementToNode(self), buf.writer()); try writeNode(parser.elementToNode(self), buf.writer());
// TODO express the caller owned the slice. // TODO express the caller owned the slice.
// https://github.com/lightpanda-io/jsruntime-lib/issues/195 // https://github.com/lightpanda-io/jsruntime-lib/issues/195
return buf.toOwnedSlice(); return buf.toOwnedSlice();