create_element_external WIP

This commit is contained in:
sjorsdonkers
2025-06-07 00:19:39 +02:00
parent 446e5b2ddd
commit 3544e98871
5 changed files with 32 additions and 18 deletions

View File

@@ -29,6 +29,7 @@
const Env = @import("env.zig").Env; const Env = @import("env.zig").Env;
const parser = @import("netsurf.zig"); const parser = @import("netsurf.zig");
const CSSStyleDeclaration = @import("cssom/css_style_declaration.zig").CSSStyleDeclaration; const CSSStyleDeclaration = @import("cssom/css_style_declaration.zig").CSSStyleDeclaration;
const Page = @import("page.zig").Page;
// for HTMLScript (but probably needs to be added to more) // for HTMLScript (but probably needs to be added to more)
onload: ?Env.Function = null, onload: ?Env.Function = null,
@@ -58,6 +59,9 @@ active_element: ?*parser.Element = null,
// default (by returning selectedIndex == 0). // default (by returning selectedIndex == 0).
explicit_index_set: bool = false, explicit_index_set: bool = false,
// TODO
page: ?*Page = null,
const ReadyState = enum { const ReadyState = enum {
loading, loading,
interactive, interactive,

View File

@@ -42,7 +42,8 @@ pub const DOMImplementation = struct {
} }
pub fn _createHTMLDocument(_: *DOMImplementation, title: ?[]const u8) !*parser.DocumentHTML { pub fn _createHTMLDocument(_: *DOMImplementation, title: ?[]const u8) !*parser.DocumentHTML {
return try parser.domImplementationCreateHTMLDocument(title); const Elements = @import("../html/elements.zig");
return try parser.domImplementationCreateHTMLDocument(title, &Elements.createElement);
} }
pub fn _hasFeature(_: *DOMImplementation) bool { pub fn _hasFeature(_: *DOMImplementation) bool {

View File

@@ -627,17 +627,20 @@ pub const HTMLImageElement = struct {
}; };
}; };
pub fn createElement(doc: *parser.Document, params: *parser.dom_html_element_create_params, elem: **parser.HtmlElement) !void { pub fn createElement(doc: [*c]parser.DocumentHTML, params: [*c]parser.c.dom_html_element_create_params, elem: [*c][*c]parser.ElementHTML) callconv(.c) parser.c.dom_exception {
// Required to be set on all documents. How during dom parsing? // Required to be set on all htmldocuments. How during dom parsing?
const wrap = parser.nodeGetEmbedderData(@ptrCast(doc)).?; const wrap = parser.nodeGetEmbedderData(@ptrCast(doc)).?; // TODO this is not set yet
const state = @as(State, @alignCast(@ptrCast(wrap))); const state = @as(*State, @alignCast(@ptrCast(wrap)));
const page = state.page; const page = state.page.?;
switch (params.type) { const p: *parser.c.dom_html_element_create_params = @ptrCast(params);
.DOM_HTML_ELEMENT_TYPE_INPUT => { switch (p.type) {
parser.c.DOM_HTML_ELEMENT_TYPE_INPUT => {
elem.* = try HTMLInputElement.dom_create(params, page); elem.* = try HTMLInputElement.dom_create(params, page);
}, },
else => return parser.c.DOM_NOT_FOUND_ERR,
} }
return parser.c.DOM_NO_ERR;
} }
pub const HTMLInputElement = struct { pub const HTMLInputElement = struct {
@@ -646,29 +649,29 @@ pub const HTMLInputElement = struct {
pub const subtype = .node; pub const subtype = .node;
// VTables can be generated from the dom_ funcs // VTables can be generated from the dom_ funcs
pub const vtable: parser.dom_html_element_vtable = parser._dom_html_element_vtable; vtable: parser.c.dom_html_element_vtable = parser.c._dom_html_element_vtable, // TODO make global, instantiate value and cast to void probably
pub const protected_vtable: parser.dom_element_protected_vtable = .{ protected_vtable: parser.c.dom_element_protected_vtable = .{
.dom_node_copy = dom_node_copy, .dom_node_copy = dom_node_copy,
// .dom_node_destroy = dom_node_destroy, // Not needed in zig // .dom_node_destroy = dom_node_destroy, // Not needed in zig
.dom_initialise = dom_initialise, .dom_initialise = dom_initialise,
}; },
base: parser.ElementHTML, base: parser.ElementHTML,
// Should instead have 2 vtable fields to generate the creation function // Should instead have 2 vtable fields to generate the creation function
pub fn dom_create(params: *parser.dom_html_element_create_params, page: *Page) !*parser.HtmlElement { pub fn dom_create(params: *parser.c.dom_html_element_create_params, page: *Page) !*parser.ElementHTML {
var self = try page.arena.create(HTMLInputElement); // Put in pool? var self = try page.arena.create(HTMLInputElement); // Put in pool?
self.base.base.base.vtable = &HTMLInputElement.vtable; self.base.base.base.vtable = &HTMLInputElement.vtable;
self.base.base.vtable = &HTMLInputElement.protected_vtable; self.base.base.vtable = &HTMLInputElement.protected_vtable;
// set vtable and protected vtable // set vtable and protected vtable
self.initialise(params); self.dom_initialise(params);
return self.base; return self.base;
} }
// Initialise is separated from create such that the leaf type sets the vtable, then calls all the way up the protochain to init // Initialise is separated from create such that the leaf type sets the vtable, then calls all the way up the protochain to init
// Currently we do only leaf types tho // Currently we do only leaf types tho
pub fn dom_initialise(self: *HTMLInputElement, params: *parser.dom_html_element_create_params) !void { pub fn dom_initialise(self: *HTMLInputElement, params: *parser.c.dom_html_element_create_params) parser.c.dom_exception {
return parser._dom_html_element_initialise(params, &self.base); return parser.c._dom_html_element_initialise(params, &self.base);
} }
// This should always be the same and we should not have cleanup for new zig implementation, hopefully // This should always be the same and we should not have cleanup for new zig implementation, hopefully

View File

@@ -18,7 +18,7 @@
const std = @import("std"); const std = @import("std");
const c = @cImport({ pub const c = @cImport({
@cInclude("dom/dom.h"); @cInclude("dom/dom.h");
@cInclude("core/pi.h"); @cInclude("core/pi.h");
@cInclude("dom/bindings/hubbub/parser.h"); @cInclude("dom/bindings/hubbub/parser.h");
@@ -1989,7 +1989,9 @@ pub inline fn domImplementationCreateDocumentType(
return dt.?; return dt.?;
} }
pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8) !*DocumentHTML { pub const CreateElementFn = ?*const fn ([*c]DocumentHTML, [*c]c.dom_html_element_create_params, [*c][*c]ElementHTML) callconv(.c) c.dom_exception;
pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8, create_element: CreateElementFn) !*DocumentHTML {
const doc_html = try documentCreateDocument(title); const doc_html = try documentCreateDocument(title);
const doc = documentHTMLToDocument(doc_html); const doc = documentHTMLToDocument(doc_html);
@@ -2010,6 +2012,7 @@ pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8) !*Document
const body = try documentCreateElement(doc, "body"); const body = try documentCreateElement(doc, "body");
_ = try nodeAppendChild(elementToNode(html), elementToNode(body)); _ = try nodeAppendChild(elementToNode(html), elementToNode(body));
doc_html.create_element_external = create_element;
return doc_html; return doc_html;
} }
@@ -2085,6 +2088,9 @@ pub inline fn documentCreateDocument(title: ?[]const u8) !*DocumentHTML {
try DOMErr(err); try DOMErr(err);
const doc_html = @as(*DocumentHTML, @ptrCast(doc.?)); const doc_html = @as(*DocumentHTML, @ptrCast(doc.?));
if (title) |t| try documentHTMLSetTitle(doc_html, t); if (title) |t| try documentHTMLSetTitle(doc_html, t);
// doc_html.create_element_external =
return doc_html; return doc_html;
} }