Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple on_end_tag callbacks on Element. #177

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/rewritable_units/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Element<'r, 't> {
start_tag: &'r mut StartTag<'t>,
end_tag_mutations: Option<Mutations>,
modified_end_tag_name: Option<Bytes<'static>>,
end_tag_handler: Option<EndTagHandler<'static>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter that we're getting rid of the Option here? What about Vec<Option<EndTagHandler<'static>>>?

Copy link
Member Author

@orium orium Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Option<_> here was just to make a container of 0 or 1 elements. Using a Vec<_> we generalize to any number of elements.

end_tag_handlers: Vec<EndTagHandler<'static>>,
can_have_content: bool,
should_remove_content: bool,
encoding: &'static Encoding,
Expand All @@ -60,7 +60,7 @@ impl<'r, 't> Element<'r, 't> {
start_tag,
end_tag_mutations: None,
modified_end_tag_name: None,
end_tag_handler: None,
end_tag_handlers: Vec::new(),
can_have_content,
should_remove_content: false,
encoding,
Expand Down Expand Up @@ -489,7 +489,8 @@ impl<'r, 't> Element<'r, 't> {

/// Sets a handler to run when the end tag is reached.
///
/// Subsequent calls to the method on the same element replace the previous handler.
/// Consecutive calls to this method will register multiple handlers. The handlers will
/// run in the order of registration.
///
/// # Example
///
Expand Down Expand Up @@ -538,7 +539,7 @@ impl<'r, 't> Element<'r, 't> {
handler: impl FnOnce(&mut EndTag) -> HandlerResult + 'static,
) -> Result<(), EndTagError> {
if self.can_have_content {
self.end_tag_handler = Some(Box::new(handler));
self.end_tag_handlers.push(Box::new(handler));
Ok(())
} else {
Err(EndTagError::NoEndTag)
Expand All @@ -548,11 +549,11 @@ impl<'r, 't> Element<'r, 't> {
pub(crate) fn into_end_tag_handler(self) -> Option<EndTagHandler<'static>> {
let end_tag_mutations = self.end_tag_mutations;
let modified_end_tag_name = self.modified_end_tag_name;
let end_tag_handler = self.end_tag_handler;
let end_tag_handlers = self.end_tag_handlers;

if end_tag_mutations.is_some()
|| modified_end_tag_name.is_some()
|| end_tag_handler.is_some()
|| !end_tag_handlers.is_empty()
{
Some(Box::new(move |end_tag: &mut EndTag| {
if let Some(name) = modified_end_tag_name {
Expand All @@ -563,11 +564,11 @@ impl<'r, 't> Element<'r, 't> {
end_tag.mutations = mutations;
}

if let Some(handler) = end_tag_handler {
handler(end_tag)
} else {
Ok(())
for handler in end_tag_handlers.into_iter() {
handler(end_tag)?;
}

Ok(())
}))
} else {
None
Expand Down