-
Notifications
You must be signed in to change notification settings - Fork 49
Open
Description
Similar to #176 I would like to write a callback for Builder::attribute_filter
which doesn't have a 'static
lifetime.
But I just can't figure out if this is possible. This is my current attempt: pitdicker@7c69882
And this is used to test the functionality (for fixing absolute URLs with spaces or other syntax errors):
struct CleanUrl<'a>(Option<&'a ammonia::Url>);
impl<'a> AttributeFilter<'a> for CleanUrl<'a>
{
fn filter<'val>(&self, element: &str, attribute: &str, value: &'val str) -> Option<Cow<'val, str>> {
// Fix invalid links image urls
match (element, attribute) {
("a", "href") => {
eprintln!("href: {}", value);
Some(match self.0 {
Some(base) => base.join(value),
None => ammonia::Url::parse(value),
}.map(|u| Cow::Owned(u.into())).unwrap_or(value.into()))
}
("img", "src") => {
eprintln!("src: {}", value);
Some(match self.0 {
Some(base) => base.join(value),
None => ammonia::Url::parse(value),
}.map(|u| Cow::Owned(u.into())).unwrap_or(value.into()))
}
_ => Some(value.into())
}
}
}
Metadata
Metadata
Assignees
Labels
No labels