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

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

Open
pitdicker opened this issue Mar 23, 2023 · 0 comments
Open

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

pitdicker opened this issue Mar 23, 2023 · 0 comments

Comments

@pitdicker
Copy link
Contributor

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())
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant