Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/magiclen/html-escape
This library is for encoding/escaping special characters in HTML and decoding/unescaping HTML entities as well.
https://github.com/magiclen/html-escape
escape html rust
Last synced: 3 months ago
JSON representation
This library is for encoding/escaping special characters in HTML and decoding/unescaping HTML entities as well.
- Host: GitHub
- URL: https://github.com/magiclen/html-escape
- Owner: magiclen
- License: mit
- Created: 2020-06-26T13:08:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T13:53:28.000Z (5 months ago)
- Last Synced: 2024-06-30T13:42:49.423Z (5 months ago)
- Topics: escape, html, rust
- Language: Rust
- Homepage:
- Size: 231 KB
- Stars: 39
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
HTML Escape
====================[![CI](https://github.com/magiclen/html-escape/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/html-escape/actions/workflows/ci.yml)
This library is for encoding/escaping special characters in HTML and decoding/unescaping HTML entities as well.
## Usage
### Encoding
This crate provides some `encode_*` functions to encode HTML text in different situations.
For example, to put a text between a start tag `` and an end tag ``, use the `encode_text` function to escape every `&`, `<`, and `>` in the text.
```rust
assert_eq!("a > b && a < c", html_escape::encode_text("a > b && a < c"));
```The functions suffixed with `_to_writer`, `_to_vec` or `_to_string` are useful to generate HTML.
```rust
let mut html = String::from("alert('");
assert_eq!(r"<script>\'s end tag is <\/script>", html_escape::encode_script_single_quoted_text_to_string("<script>'s end tag is ", &mut html));
html.push_str("');");assert_eq!("alert(\'<script>\\\'s end tag is <\\/script>\');", html);
```### Decoding
```rust
assert_eq!("Hello world!", html_escape::decode_html_entities("Hello world!"));
``````rust
assert_eq!("alert(');'", html_escape::decode_script(r"alert('<\/script>);'"));
```## No Std
Disable the default features to compile this crate without std.
```toml
[dependencies.html-escape]
version = "*"
default-features = false
```## Benchmark
```bash
cargo bench
```## Crates.io
https://crates.io/crates/html-escape
## Documentation
https://docs.rs/html-escape
## License
[MIT](LICENSE)