implement html5ever createPI callback

This commit is contained in:
Karl Seguin
2025-12-21 16:04:59 +08:00
parent d95b19d31b
commit 32c83d166d
6 changed files with 45 additions and 5 deletions

View File

@@ -64,6 +64,7 @@ const Error = struct {
append,
create_element,
create_comment,
create_processing_instruction,
append_doctype_to_document,
add_attrs_if_missing,
get_template_content,
@@ -84,6 +85,7 @@ pub fn parse(self: *Parser, html: []const u8) void {
parseErrorCallback,
popCallback,
createCommentCallback,
createProcessingInstruction,
appendDoctypeToDocument,
addAttrsIfMissingCallback,
getTemplateContentsCallback,
@@ -104,6 +106,7 @@ pub fn parseFragment(self: *Parser, html: []const u8) void {
parseErrorCallback,
popCallback,
createCommentCallback,
createProcessingInstruction,
appendDoctypeToDocument,
addAttrsIfMissingCallback,
getTemplateContentsCallback,
@@ -141,6 +144,7 @@ pub const Streaming = struct {
parseErrorCallback,
popCallback,
createCommentCallback,
createProcessingInstruction,
appendDoctypeToDocument,
addAttrsIfMissingCallback,
getTemplateContentsCallback,
@@ -218,6 +222,24 @@ fn _createCommentCallback(self: *Parser, str: []const u8) !*anyopaque {
return pn;
}
fn createProcessingInstruction(ctx: *anyopaque, target: h5e.StringSlice, data: h5e.StringSlice) callconv(.c) ?*anyopaque {
const self: *Parser = @ptrCast(@alignCast(ctx));
return self._createProcessingInstruction(target.slice(), data.slice()) catch |err| {
self.err = .{ .err = err, .source = .create_processing_instruction };
return null;
};
}
fn _createProcessingInstruction(self: *Parser, target: []const u8, data: []const u8) !*anyopaque {
const page = self.page;
const node = try page.createProcessingInstruction(target, data);
const pn = try self.arena.create(ParsedNode);
pn.* = .{
.data = null,
.node = node,
};
return pn;
}
fn appendDoctypeToDocument(ctx: *anyopaque, name: h5e.StringSlice, public_id: h5e.StringSlice, system_id: h5e.StringSlice) callconv(.c) void {
const self: *Parser = @ptrCast(@alignCast(ctx));
self._appendDoctypeToDocument(name.slice(), public_id.slice(), system_id.slice()) catch |err| {

View File

@@ -29,6 +29,7 @@ pub extern "c" fn html5ever_parse_document(
parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,
popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,
createCommentCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) ?*anyopaque,
createProcessingInstruction: *const fn (ctx: *anyopaque, StringSlice, StringSlice) callconv(.c) ?*anyopaque,
appendDoctypeToDocument: *const fn (ctx: *anyopaque, StringSlice, StringSlice, StringSlice) callconv(.c) void,
addAttrsIfMissingCallback: *const fn (ctx: *anyopaque, target_ref: *anyopaque, AttributeIterator) callconv(.c) void,
getTemplateContentsCallback: *const fn (ctx: *anyopaque, target_ref: *anyopaque) callconv(.c) ?*anyopaque,
@@ -47,6 +48,7 @@ pub extern "c" fn html5ever_parse_fragment(
parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,
popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,
createCommentCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) ?*anyopaque,
createProcessingInstruction: *const fn (ctx: *anyopaque, StringSlice, StringSlice) callconv(.c) ?*anyopaque,
appendDoctypeToDocument: *const fn (ctx: *anyopaque, StringSlice, StringSlice, StringSlice) callconv(.c) void,
addAttrsIfMissingCallback: *const fn (ctx: *anyopaque, target_ref: *anyopaque, AttributeIterator) callconv(.c) void,
getTemplateContentsCallback: *const fn (ctx: *anyopaque, target_ref: *anyopaque) callconv(.c) ?*anyopaque,
@@ -74,6 +76,7 @@ pub extern "c" fn html5ever_streaming_parser_create(
parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,
popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,
createCommentCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) ?*anyopaque,
createProcessingInstruction: *const fn (ctx: *anyopaque, StringSlice, StringSlice) callconv(.c) ?*anyopaque,
appendDoctypeToDocument: *const fn (ctx: *anyopaque, StringSlice, StringSlice, StringSlice) callconv(.c) void,
addAttrsIfMissingCallback: *const fn (ctx: *anyopaque, target_ref: *anyopaque, AttributeIterator) callconv(.c) void,
getTemplateContentsCallback: *const fn (ctx: *anyopaque, target_ref: *anyopaque) callconv(.c) ?*anyopaque,

View File

@@ -43,6 +43,7 @@ pub extern "C" fn html5ever_parse_document(
parse_error_callback: ParseErrorCallback,
pop_callback: PopCallback,
create_comment_callback: CreateCommentCallback,
create_processing_instruction: CreateProcessingInstruction,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
get_template_contents_callback: GetTemplateContentsCallback,
@@ -66,6 +67,7 @@ pub extern "C" fn html5ever_parse_document(
parse_error_callback: parse_error_callback,
create_element_callback: create_element_callback,
create_comment_callback: create_comment_callback,
create_processing_instruction: create_processing_instruction,
append_doctype_to_document: append_doctype_to_document,
add_attrs_if_missing_callback: add_attrs_if_missing_callback,
get_template_contents_callback: get_template_contents_callback,
@@ -91,6 +93,7 @@ pub extern "C" fn html5ever_parse_fragment(
parse_error_callback: ParseErrorCallback,
pop_callback: PopCallback,
create_comment_callback: CreateCommentCallback,
create_processing_instruction: CreateProcessingInstruction,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
get_template_contents_callback: GetTemplateContentsCallback,
@@ -114,6 +117,7 @@ pub extern "C" fn html5ever_parse_fragment(
parse_error_callback: parse_error_callback,
create_element_callback: create_element_callback,
create_comment_callback: create_comment_callback,
create_processing_instruction: create_processing_instruction,
append_doctype_to_document: append_doctype_to_document,
add_attrs_if_missing_callback: add_attrs_if_missing_callback,
get_template_contents_callback: get_template_contents_callback,
@@ -199,6 +203,7 @@ pub extern "C" fn html5ever_streaming_parser_create(
parse_error_callback: ParseErrorCallback,
pop_callback: PopCallback,
create_comment_callback: CreateCommentCallback,
create_processing_instruction: CreateProcessingInstruction,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
get_template_contents_callback: GetTemplateContentsCallback,
@@ -225,6 +230,7 @@ pub extern "C" fn html5ever_streaming_parser_create(
parse_error_callback: parse_error_callback,
create_element_callback: create_element_callback,
create_comment_callback: create_comment_callback,
create_processing_instruction: create_processing_instruction,
append_doctype_to_document: append_doctype_to_document,
add_attrs_if_missing_callback: add_attrs_if_missing_callback,
get_template_contents_callback: get_template_contents_callback,

View File

@@ -54,6 +54,7 @@ pub struct Sink<'arena> {
pub parse_error_callback: ParseErrorCallback,
pub create_element_callback: CreateElementCallback,
pub create_comment_callback: CreateCommentCallback,
pub create_processing_instruction: CreateProcessingInstruction,
pub append_doctype_to_document: AppendDoctypeToDocumentCallback,
pub add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
pub get_template_contents_callback: GetTemplateContentsCallback,
@@ -144,9 +145,11 @@ impl<'arena> TreeSink for Sink<'arena> {
}
fn create_pi(&self, target: StrTendril, data: StrTendril) -> Ref {
_ = target;
_ = data;
panic!("create_pi");
let str_target = StringSlice{ ptr: target.as_ptr(), len: target.len()};
let str_data = StringSlice{ ptr: data.as_ptr(), len: data.len()};
unsafe {
return (self.create_processing_instruction)(self.ctx, str_target, str_data);
}
}
fn append(&self, parent: &Ref, child: NodeOrText<Ref>) {

View File

@@ -39,6 +39,12 @@ pub type AppendDoctypeToDocumentCallback = unsafe extern "C" fn(
system_id: StringSlice,
) -> ();
pub type CreateProcessingInstruction = unsafe extern "C" fn(
ctx: Ref,
target: StringSlice,
data: StringSlice,
) -> Ref;
pub type GetDataCallback = unsafe extern "C" fn(ctx: Ref) -> *mut c_void;
pub type AppendCallback = unsafe extern "C" fn(