|
1 | 1 | # htmlentity |
2 | 2 |
|
3 | | -html entity encode and decode. |
| 3 | +A library for encoding and decoding HTML entities. |
4 | 4 |
|
5 | | -[](https://docs.rs/htmlentity/badge.svg) |
6 | | -[](https://travis-ci.com/github/fefit/htmlentity) |
| 5 | +[](https://docs.rs/htmlentity) |
| 6 | +[](https://github.com/fefit/htmlentity/actions) |
7 | 7 | [](https://codecov.io/gh/fefit/htmlentity) |
8 | 8 |
|
9 | 9 | ## How to use |
10 | 10 |
|
11 | 11 | ```rust |
12 | | -use htmlentity::entity::*; |
13 | | - |
14 | | -let html = "<div class='header'></div>"; |
15 | | -let html_encoded: Vec<char> = encode(html, Entities::SpecialChars, EncodeType::Named); |
16 | | -assert_eq!(html_encoded.iter().collect::<String>(), "<div class='header'></div>"); |
17 | | - |
18 | | -let html_decoded: Vec<char> = decode_chars(&html_encoded); |
19 | | -assert_eq!(html, html_decoded.iter().collect::<String>()); |
| 12 | +use htmlentity::entity::{ encode, decode, EncodeType, CharacterSet, ICodedDataTrait }; |
| 13 | +use htmlentity::types::{ AnyhowResult, Byte }; |
| 14 | +fn main() -> AnyhowResult<()> { |
| 15 | + let html = "<div name='htmlentity'>Hello!世界!</div>"; |
| 16 | + let html_after_encoded = "<div name='htmlentity'>Hello!世界!</div>"; |
| 17 | + // encode |
| 18 | + let encoded_data = encode(html.as_bytes(), &EncodeType::NamedOrHex, &CharacterSet::HtmlAndNonASCII); |
| 19 | + assert_eq!(encoded_data.to_bytes(), html_after_encoded.as_bytes()); |
| 20 | + assert_eq!(encoded_data.to_string()?, String::from(html_after_encoded)); |
| 21 | + assert_eq!(encoded_data.to_chars()?, String::from(html_after_encoded).chars().collect::<Vec<char>>()); |
| 22 | + // decode |
| 23 | + let bytes = encoded_data.into_iter().map(|(byte, _)| *byte).collect::<Vec<Byte>>(); |
| 24 | + let decoded_data = decode(&bytes); |
| 25 | + assert_eq!(decoded_data.to_bytes(), html.as_bytes()); |
| 26 | + assert_eq!(decoded_data.to_string()?, String::from(html)); |
| 27 | + assert_eq!(decoded_data.to_chars()?, String::from(html).chars().collect::<Vec<char>>()); |
| 28 | + Ok(()) |
| 29 | +} |
20 | 30 | ``` |
21 | 31 |
|
22 | 32 | For more details, please see the document in [Docs.rs](https://docs.rs/htmlentity) |
|
0 commit comments