add add_attrs_if_missing callback

This commit is contained in:
Karl Seguin
2025-11-14 23:33:31 +08:00
parent 5ae74d6924
commit c311828217
5 changed files with 55 additions and 16 deletions

View File

@@ -44,6 +44,7 @@ pub extern "C" fn html5ever_parse_document(
pop_callback: PopCallback,
create_comment_callback: CreateCommentCallback,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
) -> () {
if html.is_null() || len == 0 {
return ();
@@ -63,6 +64,7 @@ pub extern "C" fn html5ever_parse_document(
create_element_callback: create_element_callback,
create_comment_callback: create_comment_callback,
append_doctype_to_document: append_doctype_to_document,
add_attrs_if_missing_callback: add_attrs_if_missing_callback,
};
let bytes = unsafe { std::slice::from_raw_parts(html, len) };
@@ -84,6 +86,7 @@ pub extern "C" fn html5ever_parse_fragment(
pop_callback: PopCallback,
create_comment_callback: CreateCommentCallback,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
) -> () {
if html.is_null() || len == 0 {
return ();
@@ -103,6 +106,7 @@ pub extern "C" fn html5ever_parse_fragment(
create_element_callback: create_element_callback,
create_comment_callback: create_comment_callback,
append_doctype_to_document: append_doctype_to_document,
add_attrs_if_missing_callback: add_attrs_if_missing_callback,
};
let bytes = unsafe { std::slice::from_raw_parts(html, len) };
@@ -183,6 +187,7 @@ pub extern "C" fn html5ever_streaming_parser_create(
pop_callback: PopCallback,
create_comment_callback: CreateCommentCallback,
append_doctype_to_document: AppendDoctypeToDocumentCallback,
add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
) -> *mut c_void {
let arena = Box::new(typed_arena::Arena::new());
@@ -205,6 +210,7 @@ pub extern "C" fn html5ever_streaming_parser_create(
create_element_callback: create_element_callback,
create_comment_callback: create_comment_callback,
append_doctype_to_document: append_doctype_to_document,
add_attrs_if_missing_callback: add_attrs_if_missing_callback,
};
// Create a parser which implements TendrilSink for streaming parsing

View File

@@ -55,6 +55,7 @@ pub struct Sink<'arena> {
pub create_element_callback: CreateElementCallback,
pub create_comment_callback: CreateCommentCallback,
pub append_doctype_to_document: AppendDoctypeToDocumentCallback,
pub add_attrs_if_missing_callback: AddAttrsIfMissingCallback,
}
impl<'arena> TreeSink for Sink<'arena> {
@@ -120,19 +121,6 @@ impl<'arena> TreeSink for Sink<'arena> {
let data = self.arena.alloc(ElementData::new(name.clone(), flags));
unsafe {
let mut c_attrs: Vec<CAttribute> = Vec::with_capacity(attrs.len());
for attr in attrs.iter() {
let v: &str = &attr.value;
c_attrs.push(CAttribute {
name: CQualName::create(&attr.name),
value: StringSlice {
ptr: v.as_ptr(),
len: attr.value.len(),
},
})
}
let mut attribute_iterator = CAttributeIterator { vec: attrs, pos: 0 };
return (self.create_element_callback)(
@@ -226,9 +214,15 @@ impl<'arena> TreeSink for Sink<'arena> {
}
fn add_attrs_if_missing(&self, target: &Ref, attrs: Vec<Attribute>) {
_ = target;
_ = attrs;
panic!("add_attrs_if_missing");
unsafe {
let mut attribute_iterator = CAttributeIterator { vec: attrs, pos: 0 };
(self.add_attrs_if_missing_callback)(
self.ctx,
*target,
&mut attribute_iterator as *mut _ as *mut c_void,
);
}
}
fn remove_from_parent(&self, target: &Ref) {

View File

@@ -51,6 +51,12 @@ pub type ParseErrorCallback = unsafe extern "C" fn(ctx: Ref, str: StringSlice) -
pub type PopCallback = unsafe extern "C" fn(ctx: Ref, node: Ref) -> ();
pub type AddAttrsIfMissingCallback = unsafe extern "C" fn(
ctx: Ref,
target: Ref,
attributes: *mut c_void,
) -> ();
pub type Ref = *const c_void;
#[repr(C)]