Function rphtml::entity::encode_with
source · pub fn encode_with<'a>(
content: &'a [u8],
encode_type: &EncodeType,
filter_fn: impl Fn(&char, &EncodeType) -> (bool, Option<(EntityType, Cow<'static, [u8]>)>)
) -> EncodedData<'a>
Expand description
Encode the html entities in utf-8 bytes into encoded data, and specify the characters to be encoded and the encoding format through the filter_fn
method parameter.
§Examples
use htmlentity::entity::*;
use htmlentity::types::AnyhowResult;
use std::borrow::Cow;
let html = "<div class='header'></div>";
let charset = CharacterSet::SpecialChars;
let encoded_data = encode_with(&html.as_bytes(), &EncodeType::Named, |ch, encode_type|{
// Use html entity number encoding for single quotes (')
if ch == &'\''{
if let Some(char_entity) = encode_char(ch, &EncodeType::Decimal){
return (true, Some((EntityType::Decimal, Cow::from(char_entity.data()))));
}
}
return charset.filter(ch, encode_type);
});
let data_to_string = encoded_data.to_string();
assert!(data_to_string.is_ok());
assert_eq!(data_to_string?, String::from("<div class='header'></div>"));