Function rphtml::entity::encode_filter[][src]

pub fn encode_filter<F, C>(
    content: &str,
    filter_fn: F,
    encode_type: EncodeType,
    exclude_fn: Option<C>
) -> String where
    C: Fn(char) -> bool,
    F: Fn(char) -> bool

Encode by filter functions. Use the filte_fn to choose the character need to encode. Use the exclude_fn to exclude characters you don’t want to use named.

Examples

use htmlentity::entity::*;

let html = "<div class='header'></div>";
let html_encoded = encode_filter(html, |ch|{
  // special characters but not '<'
  ch != '<' && EntitySet::SpecialChars.contains(&ch)
}, EncodeType::Named, NOOP);
assert_eq!(html_encoded, "<div class=&apos;header&apos;&gt;</div&gt;");

// special characters, but exclude the single quote "'" use named.
let html = "<div class='header'></div>";
let html_encoded = encode_filter(html, |ch|{
  EntitySet::SpecialChars.contains(&ch)
}, EncodeType::NamedOrDecimal, Some(|ch| ch == '\''));
assert_eq!(html_encoded, "&lt;div class=&#39;header&#39;&gt;&lt;/div&gt;");