Skip to content

Can Builder::attribute_filter be made to not require static? #182

@pitdicker

Description

@pitdicker

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions