Skip to content

Do not require 'static to UrlRelativeEvaluate#176

Merged
bors[bot] merged 1 commit intorust-ammonia:masterfrom
rhysd:issue-175
Dec 4, 2022
Merged

Do not require 'static to UrlRelativeEvaluate#176
bors[bot] merged 1 commit intorust-ammonia:masterfrom
rhysd:issue-175

Conversation

@rhysd
Copy link
Contributor

@rhysd rhysd commented Dec 4, 2022

Fixes #175

With this patch, UrlRelative knows the outer lifetime 'a and is bound by it.

I confirmed that the following code works.

use ammonia::*;
use std::borrow::Cow;

struct Eval<'a>(&'a str);

impl<'a> UrlRelativeEvaluate<'a> for Eval<'a> {
    fn evaluate<'u>(&self, url: &'u str) -> Option<Cow<'u, str>> {
        let mut s = self.0.to_string();
        s.push_str(url);
        Some(Cow::Owned(s))
    }
}

fn main() {
    let prefix = "/prefix/".to_string();
    let eval = Eval(&prefix);

    let mut sanitizer = Builder::default();
    sanitizer.url_relative(UrlRelative::Custom(Box::new(eval)));
    let s = "<a href=\"foo\">link</a>".to_string();
    let s = sanitizer.clean(&s).to_string();
    println!("{}", s)
}

Output:

> cargo run
    Blocking waiting for file lock on build directory
   Compiling ammonia v3.3.0 (/Users/rhysd/Develop/github.com/rust-ammonia/ammonia)
    Finished dev [unoptimized + debuginfo] target(s) in 0.79s
     Running `target/debug/ammonia`
<a href="/prefix/foo" rel="noopener noreferrer">link</a>

Comparing with the code in #175, please notice UrlRelativeEvaluate is now bound as UrlRelativeEvaluate<'a>.

@notriddle
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 4, 2022

Build succeeded:

@bors bors bot merged commit 15792e1 into rust-ammonia:master Dec 4, 2022
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

Successfully merging this pull request may close these issues.

Allow UrlRelative::Custom to hold value bound in some lifetime

2 participants