#codec #entity #html-escape #html-decode #html-encode #html-entities

htmlentity

A library for encoding and decoding HTML entities

14 releases (stable)

1.3.2 Jun 7, 2024
1.3.1 Apr 17, 2023
1.2.0 Mar 19, 2021
1.1.1 Feb 18, 2021
0.1.2 Nov 30, 2020

#124 in Encoding

Download history 1059/week @ 2024-09-24 648/week @ 2024-10-01 1162/week @ 2024-10-08 799/week @ 2024-10-15 869/week @ 2024-10-22 1143/week @ 2024-10-29 454/week @ 2024-11-05 733/week @ 2024-11-12 729/week @ 2024-11-19 689/week @ 2024-11-26 1325/week @ 2024-12-03 2392/week @ 2024-12-10 1790/week @ 2024-12-17 1129/week @ 2024-12-24 1745/week @ 2024-12-31 1621/week @ 2025-01-07

6,555 downloads per month
Used in 30 crates (10 directly)

MIT license

155KB
5.5K SLoC

htmlentity

A library for encoding and decoding HTML entities.

Docs Build Status codecov

How to use

use htmlentity::entity::{ encode, decode, EncodeType, CharacterSet, ICodedDataTrait };
use htmlentity::types::{ AnyhowResult, Byte };
fn main() -> AnyhowResult<()> {
  let html = "<div name='htmlentity'>Hello!世界!</div>";
  let html_after_encoded = "&lt;div name='htmlentity'&gt;Hello!&#x4e16;&#x754c;!&lt;/div&gt;";
  // encode
  let encoded_data = encode(
    html.as_bytes(), 
    // encode format
    &EncodeType::NamedOrHex,
    // charcters need to be encoded 
    &CharacterSet::HtmlAndNonASCII
  );
  assert_eq!(encoded_data.to_bytes(), html_after_encoded.as_bytes());
  assert_eq!(encoded_data.to_string()?, String::from(html_after_encoded));
  let char_list = String::from(html_after_encoded).chars().collect::<Vec<char>>();
  assert_eq!(encoded_data.to_chars()?, char_list);
  // decode
  let bytes = encoded_data.into_iter().map(|(byte, _)| *byte).collect::<Vec<Byte>>();
  let decoded_data = decode(&bytes);
  assert_eq!(decoded_data.to_bytes(), html.as_bytes());
  assert_eq!(decoded_data.to_string()?, String::from(html));
  let char_list = String::from(html).chars().collect::<Vec<char>>();
  assert_eq!(decoded_data.to_chars()?, char_list);
  // shortcut usage
  assert_eq!(
    encode(
      html.as_bytes(), 
      &EncodeType::NamedOrHex,
      &CharacterSet::HtmlAndNonASCII,
    ).to_string()?,
    String::from(html)
  );
  Ok(())
}

For more details, please see the document in Docs.rs

License

MIT License.

Dependencies

~0.4–0.8MB
~19K SLoC