-
Notifications
You must be signed in to change notification settings - Fork 528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add filters for htmlEscape and htmlUnescape #1061
Conversation
Thanks for the PR @ad8lmondy
pub fn escape_html(input: &str) -> String {
let mut output = String::with_capacity(input.len() * 2);
for c in input.chars() {
match c {
'&' => output.push_str("&"),
'<' => output.push_str("<"),
'>' => output.push_str(">"),
'"' => output.push_str("""),
'\'' => output.push_str("'"),
'/' => output.push_str("/"),
_ => output.push(c),
}
}
// Not using shrink_to_fit() on purpose
output
} It's small code and easy to understand. I much prefer having this code in our own source code than using an (even small) dependency. Unescaping is a "bit more" difficult but it is also not a "hard" tasks: for instance, HTML escape and unescape in Python standard lib. That's say, we can merge this PR and implement escaping / unescaping latter,
|
Yes, I suppose you're right about the difficulty - not too bad, particularly for the encoding side. In saying that, abstracting any potential nuances to a seperate crate is appealing too 😄 In regard to the naming - I agree, it should I'll update the PR tomorrow with the new and better name. |
Great, just don't forget to rebase your commits to appeal our CI and everything should be ok! |
Filter names updated, all rebased, and quick test from my end looks to be working. |
These filters use the html_escape crate (inspired by the use of the percent_encoding crate) to escape and unescape html.
Oh yes, do let me know if there are any docs I should be updating. I wasn't sure if the grammar section on the website was generated or not - if not, I can update it 👍 |
/accept |
🕗 /accept is running, please wait for completion. |
✅ Pull request accepted and closed by |
Everything is good, once again thanks for the PR! |
This PR adds the filters
htmlEncode
andhtmlDecode
, inspired by theurlEncode
andurlDecode
filters.This allows content like:
"a > b && a < c"
to be encoded to"a > b && a < c"
and back again.I've added the filter to existing tests, and all are passing.
Regarding the info here: #1060 (comment) (thanks for that, btw!)
html-escape
. I took inspiration from the existing code for theurlEncode
filters, which uses thepercent-encoding
crate. I don't say this as justification for adding another crate, just that I'm not sure my skills in rust are up to task of implementing it myself 😅Thanks!