Skip to main content

unescape_in

Function unescape_in 

Source
pub fn unescape_in<'a, S: Into<Cow<'a, str>>>(
    escaped: S,
    context: Context,
) -> Cow<'a, str>
Available on crate features unescape or unescape_fast only.
Expand description

Expand all valid entities in a given context.

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 not expanded when followed by an alphanumeric character or =.

For example:

use htmlize::{unescape_in, Context};

assert!(unescape_in("&times",   Context::General)   == "×");
assert!(unescape_in("&times",   Context::Attribute) == "×");
assert!(unescape_in("&times;X", Context::General)   == "×X");
assert!(unescape_in("&times;X", Context::Attribute) == "×X");
assert!(unescape_in("&timesX",  Context::General)   == "×X");
assert!(unescape_in("&timesX",  Context::Attribute) == "&timesX");
assert!(unescape_in("&times=",  Context::General)   == "×=");
assert!(unescape_in("&times=",  Context::Attribute) == "&times=");
assert!(unescape_in("&times#",  Context::General)   == "×#");
assert!(unescape_in("&times#",  Context::Attribute) == "×#");

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