#codec #html #decoding #entity #string #error

htmlescape

A library for HTML entity encoding and decoding

1 unstable release

Uses old Rust 2015

0.3.1 Apr 2, 2017

#1441 in Encoding

Download history 117294/week @ 2023-12-12 75490/week @ 2023-12-19 44645/week @ 2023-12-26 106124/week @ 2024-01-02 101997/week @ 2024-01-09 58647/week @ 2024-01-16 45896/week @ 2024-01-23 47868/week @ 2024-01-30 43941/week @ 2024-02-06 53924/week @ 2024-02-13 47223/week @ 2024-02-20 54464/week @ 2024-02-27 56059/week @ 2024-03-05 53755/week @ 2024-03-12 56045/week @ 2024-03-19 44755/week @ 2024-03-26

218,446 downloads per month
Used in 203 crates (48 directly)

Apache-2.0 / MIT / MPL-2.0

54KB
606 lines

A HTML entity encoding library for Rust

Build Status

Example usage

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

###Encoding htmlescape::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 htmlescape::encode_attribute() function for encoding strings that are to be used as html attribute values.

###Decoding htmlescape::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.

No runtime deps