Work on HTMLTemplateElement

Implement Html5ever get_template_contents and add_attrs_if_missing callbacks
This commit is contained in:
Karl Seguin
2025-11-15 22:04:39 +08:00
parent c311828217
commit 19dfea7762
15 changed files with 527 additions and 33 deletions

View File

@@ -45,6 +45,7 @@ pub extern "C" fn html5ever_parse_document(
create_comment_callback: CreateCommentCallback,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
get_template_contents_callback: GetTemplateContentsCallback,
) -> () {
if html.is_null() || len == 0 {
return ();
@@ -65,6 +66,7 @@ pub extern "C" fn html5ever_parse_document(
create_comment_callback: create_comment_callback,
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,
};
let bytes = unsafe { std::slice::from_raw_parts(html, len) };
@@ -87,6 +89,7 @@ pub extern "C" fn html5ever_parse_fragment(
create_comment_callback: CreateCommentCallback,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
get_template_contents_callback: GetTemplateContentsCallback,
) -> () {
if html.is_null() || len == 0 {
return ();
@@ -107,6 +110,7 @@ pub extern "C" fn html5ever_parse_fragment(
create_comment_callback: create_comment_callback,
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,
};
let bytes = unsafe { std::slice::from_raw_parts(html, len) };
@@ -188,6 +192,7 @@ pub extern "C" fn html5ever_streaming_parser_create(
create_comment_callback: CreateCommentCallback,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
get_template_contents_callback: GetTemplateContentsCallback,
) -> *mut c_void {
let arena = Box::new(typed_arena::Arena::new());
@@ -211,6 +216,7 @@ pub extern "C" fn html5ever_streaming_parser_create(
create_comment_callback: create_comment_callback,
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,
};
// Create a parser which implements TendrilSink for streaming parsing

View File

@@ -56,6 +56,7 @@ pub struct Sink<'arena> {
pub create_comment_callback: CreateCommentCallback,
pub append_doctype_to_document: AppendDoctypeToDocumentCallback,
pub add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
pub get_template_contents_callback: GetTemplateContentsCallback,
}
impl<'arena> TreeSink for Sink<'arena> {
@@ -101,8 +102,9 @@ impl<'arena> TreeSink for Sink<'arena> {
}
fn get_template_contents(&self, target: &Ref) -> Ref {
_ = target;
panic!("get_template_contents")
unsafe {
return (self.get_template_contents_callback)(self.ctx, *target);
}
}
fn is_mathml_annotation_xml_integration_point(&self, target: &Ref) -> bool {

View File

@@ -57,6 +57,8 @@ pub type AddAttrsIfMissingCallback = unsafe extern "C" fn(
attributes: *mut c_void,
) -> ();
pub type GetTemplateContentsCallback = unsafe extern "C" fn(ctx: Ref, target: Ref) -> Ref;
pub type Ref = *const c_void;
#[repr(C)]