Function quick_xml::escape::unescape_with

source ·
pub fn unescape_with<'input, 'entity, F>(
    raw: &'input str,
    resolve_entity: F,
) -> Result<Cow<'input, str>, EscapeError>
where F: FnMut(&str) -> Option<&'entity str>,
Expand description

Unescape an &str and replaces all xml escaped characters (&...;) into their corresponding value, using a resolver function for custom entities.

If feature escape-html is enabled, then recognizes all HTML5 escapes.

Predefined entities will be resolved after trying to resolve with resolve_entity, which allows you to override default behavior which required in some XML dialects.

Character references (&#hh;) cannot be overridden, they are resolved before calling resolve_entity.

Note, that entities will not be resolved recursively. In order to satisfy the XML requirements you should unescape nested entities by yourself.

§Example

use quick_xml::escape::resolve_xml_entity;
let override_named_entities = |entity: &str| match entity {
    // Override standard entities
    "lt" => Some("FOO"),
    "gt" => Some("BAR"),
    // Resolve custom entities
    "baz" => Some("&lt;"),
    // Delegate other entities to the default implementation
    _ => resolve_xml_entity(entity),
};

assert_eq!(
    unescape_with("&amp;&lt;test&gt;&baz;", override_named_entities).unwrap(),
    "&FOOtestBAR&lt;"
);