Add Document.gettype

This commit is contained in:
Karl Seguin
2025-12-21 17:13:36 +08:00
parent 8fbd64955f
commit 25ad3559f7
3 changed files with 25 additions and 3 deletions

View File

@@ -50,9 +50,9 @@ pub const Opts = struct {
};
pub fn root(doc: *Node.Document, opts: RootOpts, writer: *std.Io.Writer, page: *Page) !void {
if (opts.with_base) {
if (doc.is(Node.Document.HTMLDocument)) |html_doc| {
try writer.writeAll("<!DOCTYPE html>");
if (doc.is(Node.Document.HTMLDocument)) |html_doc| {
try writer.writeAll("<!DOCTYPE html>");
if (opts.with_base) {
const parent = if (html_doc.getHead()) |head| head.asNode() else doc.asNode();
const base = try doc.createElement("base", null, page);
try base.setAttributeSafe("base", page.url, page);

View File

@@ -30,6 +30,7 @@ const Parser = @import("../parser/Parser.zig");
const collections = @import("collections.zig");
const Selector = @import("selector/Selector.zig");
const NodeFilter = @import("NodeFilter.zig");
const DocumentType = @import("DocumentType.zig");
const DOMTreeWalker = @import("DOMTreeWalker.zig");
const DOMNodeIterator = @import("DOMNodeIterator.zig");
const DOMImplementation = @import("DOMImplementation.zig");
@@ -404,6 +405,10 @@ pub fn elementsFromPoint(self: *Document, x: f64, y: f64, page: *Page) ![]const
return result.items;
}
pub fn getDocType(_: *const Document) ?*DocumentType {
return null;
}
pub fn write(self: *Document, text: []const []const u8, page: *Page) !void {
if (self._type == .xml) {
return error.InvalidStateError;
@@ -598,6 +603,7 @@ pub const JsApi = struct {
pub const write = bridge.function(Document.write, .{ .dom_exception = true });
pub const open = bridge.function(Document.open, .{ .dom_exception = true });
pub const close = bridge.function(Document.close, .{ .dom_exception = true });
pub const doctype = bridge.accessor(Document.getDocType, null, .{});
pub const defaultView = bridge.accessor(struct {
fn defaultView(_: *const Document, page: *Page) *@import("Window.zig") {

View File

@@ -24,11 +24,13 @@ const Page = @import("../Page.zig");
const Node = @import("Node.zig");
const Document = @import("Document.zig");
const Element = @import("Element.zig");
const DocumentType = @import("DocumentType.zig");
const collections = @import("collections.zig");
const HTMLDocument = @This();
_proto: *Document,
_document_type: ?*DocumentType = null,
pub fn asDocument(self: *HTMLDocument) *Document {
return self._proto;
@@ -163,6 +165,19 @@ pub fn setCookie(_: *HTMLDocument, cookie_str: []const u8, page: *Page) ![]const
return cookie_str;
}
pub fn getDocType(self: *HTMLDocument, page: *Page) !*DocumentType {
if (self._document_type) |dt| {
return dt;
}
self._document_type = try page._factory.node(DocumentType{
._proto = undefined,
._name = "html",
._public_id = "",
._system_id = "",
});
return self._document_type.?;
}
pub const JsApi = struct {
pub const bridge = js.Bridge(HTMLDocument);
@@ -194,4 +209,5 @@ pub const JsApi = struct {
pub const location = bridge.accessor(HTMLDocument.getLocation, null, .{ .cache = "location" });
pub const all = bridge.accessor(HTMLDocument.getAll, null, .{});
pub const cookie = bridge.accessor(HTMLDocument.getCookie, HTMLDocument.setCookie, .{});
pub const doctype = bridge.accessor(HTMLDocument.getDocType, null, .{});
};