Function htmlize::unescape_in

source ·
pub fn unescape_in<'a, S: Into<Cow<'a, str>>>(
    escaped: S,
    context: Context
) -> Cow<'a, str>
Expand description

Expand all valid entities in a given context (requires unescape or unescape_fast 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_in("&times",   Context::General)   == "×");
check!(unescape_in("&times",   Context::Attribute) == "×");
check!(unescape_in("&times;X", Context::General)   == "×X");
check!(unescape_in("&times;X", Context::Attribute) == "×X");
check!(unescape_in("&timesX",  Context::General)   == "×X");
check!(unescape_in("&timesX",  Context::Attribute) == "&timesX");
check!(unescape_in("&times=",  Context::General)   == "×=");
check!(unescape_in("&times=",  Context::Attribute) == "&times=");
check!(unescape_in("&times#",  Context::General)   == "×#");
check!(unescape_in("&times#",  Context::Attribute) == "×#");

To work with bytes ([u8]) instead of strings, see unescape_bytes_in().