| Crates.io | escapade |
| lib.rs | escapade |
| version | 0.0.3 |
| created_at | 2016-11-05 19:19:20.46681+00 |
| updated_at | 2016-11-08 21:11:21.854002+00 |
| description | `escapade` provides String concatenation and writing, but automatically escapes any HTML in the data in the process. This prevents accidental unescaped writes to the output. |
| homepage | |
| repository | https://github.com/skade/escapade |
| max_upload_size | |
| id | 7139 |
| size | 13,166 |
escapade is inspired by ActiveSupports SafeBuffer.
escapade provides String concatenation and writing, but automatically escapes any HTML in the data in the process. This prevents accidental unescaped writes to the output.
The library provides both a String type for HTML-safe concatenation and a writer, wrapping types implementing Write.
The library works with any type that implements AsRef<str>.
You might want to use this library for your templating language ;).
Put the following in the dependencies section of your Cargo.toml:
escapade = "0.0.2"
Use the SafeWriter struct to make any type implementing Write html safe.
extern crate escapade;
use escapade::EscapedWriter;
use escapade::EscapedWrite;
fn main() {
let mut buffer = EscapedWriter::new(vec![]);
buffer.write_str("<hello>&world</hello>").expect("write should not fail");
assert_eq!("<hello>&world</hello>", String::from_utf8(buffer.into_inner()).unwrap());
}
Appending an unescaped string to any escaped string will escape the second string.
extern crate escapade;
use escapade::Append;
use escapade::Escapable;
fn main() {
let mut s = String::from("<hello>").escape();
s.append_str(String::from("&world</hello>"));
assert_eq!("<hello>&world</hello>", s.into_inner());
}
Escaped strings cannot be appended to normal strings.
Sometimes, you are sure that the string in question is safe (e.g., you painstakenly created it by hand). You can opt into safety in this case, to avoid escaping:
extern crate escapade;
use escapade::Escapable;
use escapade::EscapedWriter;
use escapade::EscapedWrite;
fn main() {
let mut buffer = EscapedWriter::new(vec![]);
buffer.write_str("<hello>&world</hello>".safe()).expect("write should not fail");
assert_eq!("<hello>&world</hello>", String::from_utf8(buffer.into_inner()).unwrap());
}
MIT
Encoding functionality taken from rust-htmlescape, by Viktor Dahl, licensed under MIT.