mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 07:31:47 +00:00
browser: use directly the document as node
This commit is contained in:
@@ -161,8 +161,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.
|
||||||
const root = try parser.documentGetDocumentElement(self.doc.?) orelse return;
|
try Dump.htmlFile(self.doc.?, out);
|
||||||
try Dump.htmlFile(root, out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// spec reference: https://html.spec.whatwg.org/#document-lifecycle
|
// spec reference: https://html.spec.whatwg.org/#document-lifecycle
|
||||||
@@ -244,11 +243,11 @@ pub const Page = struct {
|
|||||||
var sasync = std.ArrayList(*parser.Element).init(self.allocator);
|
var sasync = std.ArrayList(*parser.Element).init(self.allocator);
|
||||||
defer sasync.deinit();
|
defer sasync.deinit();
|
||||||
|
|
||||||
const root = try parser.documentGetDocumentElement(doc) orelse return; // TODO send loaded event in this case?
|
const root = parser.documentToNode(doc);
|
||||||
const walker = Walker{};
|
const walker = Walker{};
|
||||||
var next: ?*parser.Node = null;
|
var next: ?*parser.Node = null;
|
||||||
while (true) {
|
while (true) {
|
||||||
next = try walker.get_next(parser.elementToNode(root), next) orelse break;
|
next = try walker.get_next(root, next) orelse break;
|
||||||
|
|
||||||
// ignore non-elements nodes.
|
// ignore non-elements nodes.
|
||||||
if (try parser.nodeType(next.?) != .element) {
|
if (try parser.nodeType(next.?) != .element) {
|
||||||
|
|||||||
@@ -4,17 +4,17 @@ const File = std.fs.File;
|
|||||||
const parser = @import("../netsurf.zig");
|
const parser = @import("../netsurf.zig");
|
||||||
const Walker = @import("../dom/html_collection.zig").WalkerChildren;
|
const Walker = @import("../dom/html_collection.zig").WalkerChildren;
|
||||||
|
|
||||||
pub fn htmlFile(root: *parser.Element, out: File) !void {
|
pub fn htmlFile(doc: *parser.Document, out: File) !void {
|
||||||
try out.writeAll("<!DOCTYPE html>\n<html>");
|
try out.writeAll("<!DOCTYPE html>\n");
|
||||||
try nodeFile(root, out);
|
try nodeFile(parser.documentToNode(doc), out);
|
||||||
try out.writeAll("</html>\n");
|
try out.writeAll("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn nodeFile(root: *parser.Element, out: File) !void {
|
fn nodeFile(root: *parser.Node, out: File) !void {
|
||||||
const walker = Walker{};
|
const walker = Walker{};
|
||||||
var next: ?*parser.Node = null;
|
var next: ?*parser.Node = null;
|
||||||
while (true) {
|
while (true) {
|
||||||
next = try walker.get_next(parser.elementToNode(root), next) orelse break;
|
next = try walker.get_next(root, next) orelse break;
|
||||||
switch (try parser.nodeType(next.?)) {
|
switch (try parser.nodeType(next.?)) {
|
||||||
.element => {
|
.element => {
|
||||||
// open the tag
|
// open the tag
|
||||||
@@ -40,7 +40,7 @@ fn nodeFile(root: *parser.Element, out: File) !void {
|
|||||||
|
|
||||||
// write the children
|
// write the children
|
||||||
// TODO avoid recursion
|
// TODO avoid recursion
|
||||||
try nodeFile(parser.nodeToElement(next.?), out);
|
try nodeFile(next.?, out);
|
||||||
|
|
||||||
// close the tag
|
// close the tag
|
||||||
try out.writeAll("</");
|
try out.writeAll("</");
|
||||||
@@ -91,7 +91,6 @@ pub fn HTMLFileTestFn(out: File) !void {
|
|||||||
defer parser.documentHTMLClose(doc_html) catch {};
|
defer parser.documentHTMLClose(doc_html) catch {};
|
||||||
|
|
||||||
const doc = parser.documentHTMLToDocument(doc_html);
|
const doc = parser.documentHTMLToDocument(doc_html);
|
||||||
const root = try parser.documentGetDocumentElement(doc) orelse return error.DocumentNullRoot;
|
|
||||||
|
|
||||||
try htmlFile(root, out);
|
try htmlFile(doc, out);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user