-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,58 @@ | ||
use crate::Renderable; | ||
use htmlescape::encode_minimal; | ||
|
||
/// Renders the string | ||
/// > TODO: this should escape HTML! | ||
/// Renders an escaped-html string | ||
impl Renderable for String { | ||
fn render(self) -> String { | ||
self | ||
encode_minimal(&self) | ||
} | ||
} | ||
|
||
/// Renders the string | ||
/// > TODO: this should escape HTML! | ||
/// Renders an escaped-html string | ||
impl Renderable for &str { | ||
fn render(self) -> String { | ||
self.to_string() | ||
encode_minimal(self) | ||
} | ||
} | ||
|
||
/// A raw (unencoded) html string | ||
#[derive(Debug)] | ||
pub struct Raw<'s>(&'s str); | ||
|
||
impl<'s> From<&'s str> for Raw<'s> { | ||
fn from(s: &'s str) -> Self { | ||
Raw(s) | ||
} | ||
} | ||
|
||
/// A raw (unencoded) html string | ||
impl<'s> Renderable for Raw<'s> { | ||
fn render(self) -> String { | ||
self.0.to_string() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn decodes_html() { | ||
let rendered = "<Hello />".render(); | ||
assert_eq!(rendered, "<Hello />"); | ||
} | ||
|
||
#[test] | ||
fn allows_raw_text() { | ||
let rendered = Raw::from("<Hello />").render(); | ||
assert_eq!(rendered, "<Hello />"); | ||
} | ||
} | ||
|
||
/// Creates a raw (unencoded) html string | ||
#[macro_export] | ||
macro_rules! raw { | ||
($text:expr) => { | ||
::render::Raw::from($text) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters