Function htmlize::unescape_bytes_in

source ·
pub fn unescape_bytes_in<'a, S: Into<Cow<'a, [u8]>>>(
    escaped: S,
    context: Context
) -> Cow<'a, [u8]>
Expand description

Expand all valid entities in a given context (requires unescape feature).

context may be:

  • Context::General: use the rules for text outside of an attribute. This is usually what you want.
  • Context::Attribute: use the rules for attribute values.

This uses the algorithm described in the WHATWG spec. In attributes, named entities without trailing semicolons are treated differently. They not expanded if they are followed by an alphanumeric character or or =.

For example:

use htmlize::*;
use assert2::check;

check!(unescape_bytes_in(&b"&times"[..],   Context::General)   == "×".as_bytes());
check!(unescape_bytes_in(&b"&times"[..],   Context::Attribute) == "×".as_bytes());
check!(unescape_bytes_in(&b"&times;X"[..], Context::General)   == "×X".as_bytes());
check!(unescape_bytes_in(&b"&times;X"[..], Context::Attribute) == "×X".as_bytes());
check!(unescape_bytes_in(&b"&timesX"[..],  Context::General)   == "×X".as_bytes());
check!(unescape_bytes_in(&b"&timesX"[..],  Context::Attribute) == "&timesX".as_bytes());
check!(unescape_bytes_in(&b"&times="[..],  Context::General)   == "×=".as_bytes());
check!(unescape_bytes_in(&b"&times="[..],  Context::Attribute) == "&times=".as_bytes());
check!(unescape_bytes_in(&b"&times#"[..],  Context::General)   == "×#".as_bytes());
check!(unescape_bytes_in(&b"&times#"[..],  Context::Attribute) == "×#".as_bytes());

To work with String instead of bytes, see unescape_in().