Skip to content

Implement HtmlSafe for all reference wrappers #219

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

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions rinja/src/filters/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,6 @@ mark_html_safe! {
std::num::NonZeroU64, std::num::NonZeroU128, std::num::NonZeroUsize,
}

impl<T: HtmlSafe + ?Sized> HtmlSafe for &T {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for Box<T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::cell::Ref<'_, T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::cell::RefMut<'_, T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::rc::Rc<T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::pin::Pin<&T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::sync::Arc<T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::sync::MutexGuard<'_, T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::sync::RwLockReadGuard<'_, T> {}
impl<T: HtmlSafe + ?Sized> HtmlSafe for std::sync::RwLockWriteGuard<'_, T> {}
impl<T: HtmlSafe> HtmlSafe for std::num::Wrapping<T> {}
impl<T: fmt::Display> HtmlSafe for HtmlSafeOutput<T> {}

Expand All @@ -486,6 +476,12 @@ where
{
}

crate::impl_for_ref! {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests already for ensuring it's working for all references?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! No, there weren't any tests. Added.

impl HtmlSafe for T {}
}

impl<T: HtmlSafe> HtmlSafe for Pin<T> {}

/// Used internally by rinja to select the appropriate [`write!()`] mechanism
pub struct Writable<'a, S: ?Sized>(pub &'a S);

Expand Down
33 changes: 33 additions & 0 deletions testing/tests/simple.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#![allow(clippy::disallowed_names)] // For the use of `foo` in test cases

use std::cell::{RefCell, RefMut};
use std::collections::HashMap;
use std::fmt;
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard};

use rinja::Template;
use rinja::filters::HtmlSafe;

#[derive(Template)]
#[template(path = "simple.html")]
Expand Down Expand Up @@ -560,3 +565,31 @@ struct SplitTemplateDeclaration;
fn test_split_template_declaration() {
assert_eq!(SplitTemplateDeclaration.to_string(), "🙂");
}

#[test]
fn test_ref_custom_type() {
const TEXT: &str = "&this <is >not 'safe";

struct MySafeType;

impl HtmlSafe for MySafeType {}

impl fmt::Display for MySafeType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(TEXT)
}
}

#[derive(Template)]
#[template(ext = "html", source = "{{ data }}")]
struct MyTemplate<'a, 'b, 'c> {
data: &'a mut RefMut<'b, Pin<Arc<MutexGuard<'c, MySafeType>>>>,
}

let mutex = Mutex::new(MySafeType);
let cell = RefCell::new(Arc::pin(mutex.try_lock().unwrap()));
let tmpl = MyTemplate {
data: &mut cell.try_borrow_mut().unwrap(),
};
assert_eq!(tmpl.render().unwrap(), TEXT);
}