2 releases

0.1.1 May 10, 2021
0.1.0 Aug 16, 2019

#1400 in Parser implementations

Download history 603/week @ 2024-03-17 659/week @ 2024-03-24 1778/week @ 2024-03-31 775/week @ 2024-04-07 712/week @ 2024-04-14 811/week @ 2024-04-21 1207/week @ 2024-04-28 1100/week @ 2024-05-05 1445/week @ 2024-05-12 1145/week @ 2024-05-19 787/week @ 2024-05-26 869/week @ 2024-06-02 452/week @ 2024-06-09 685/week @ 2024-06-16 426/week @ 2024-06-23 401/week @ 2024-06-30

2,007 downloads per month
Used in 14 crates (11 directly)

Apache-2.0 / MIT / MPL-2.0

57KB
426 lines

A HTML entity encoding library for Rust

crates.io version TravisCI build status Docs License

Example usage

All example assume a extern crate escaper; and use escaper::{relevant functions here}; is present.

Encoding

escaper::encode_minimal() encodes an input string using a minimal set of HTML entities.

let title = "Cats & dogs";
let tag = format!("<title>{}</title>", encode_minimal(title));
assert_eq!(tag.as_slice(), "<title>Cats &amp; dogs</title>");

There is also a escaper::encode_attribute() function for encoding strings that are to be used as html attribute values.

Decoding

escaper::decode_html() decodes an encoded string, replacing HTML entities with the

corresponding characters. Named, hex, and decimal entities are supported. A Result value is returned, with either the decoded string in Ok, or an error in Err.

let encoded = "Cats&#x20;&amp;&#32;dogs";
let decoded = match decode_html(encoded) {
  Err(reason) => panic!("Error {:?} at character {}", reason.kind, reason.position),
  Ok(s) => s
};
assert_eq!(decoded.as_slice(), "Cats & dogs");

Avoiding allocations

Both the encoding and decoding functions are available in forms that take a Writer for output rather than returning an String. These version can be used to avoid allocation and copying if the returned String was just going to be written to a Writer anyway.

LICENSE

MIT or Apache 2.0

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in pgp by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies