generated from tlibjs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
8 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/// [`html_escape::encode_safe`] | ||
fn char_encode_safe(c: char) -> Option<&'static str> { | ||
match c { | ||
'&' => Some("&"), | ||
'<' => Some("<"), | ||
'>' => Some(">"), | ||
'"' => Some("""), | ||
'\'' => Some("'"), | ||
'/' => Some("/"), | ||
_ => None, | ||
} | ||
} | ||
|
||
struct CharString { | ||
value: char, | ||
buf: [u8; 4], | ||
} | ||
|
||
impl CharString { | ||
fn new(value: char) -> Self { | ||
Self { value, buf: [0; 4] } | ||
} | ||
|
||
fn encode_safe(&mut self) -> &str { | ||
char_encode_safe(self.value).unwrap_or_else(|| self.value.encode_utf8(&mut self.buf)) | ||
} | ||
} | ||
|
||
pub struct IterCharStringEncodeSafe { | ||
taken: bool, | ||
char_string: CharString, | ||
} | ||
|
||
impl IterCharStringEncodeSafe { | ||
pub fn new(c: char) -> Self { | ||
Self { | ||
taken: false, | ||
char_string: CharString::new(c), | ||
} | ||
} | ||
} | ||
|
||
impl async_str_iter::AsyncStrIterator for IterCharStringEncodeSafe { | ||
fn poll_next_str( | ||
self: std::pin::Pin<&mut Self>, | ||
_: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Option<&str>> { | ||
let this = self.get_mut(); | ||
if this.taken { | ||
return std::task::Poll::Ready(None); | ||
} | ||
|
||
this.taken = true; | ||
let s = this.char_string.encode_safe(); | ||
std::task::Poll::Ready(Some(s)) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use frender_ssr_html::char::IterCharStringEncodeSafe; | ||
|
||
use crate::SsrElement; | ||
|
||
impl SsrElement for char { | ||
type HtmlChildren = IterCharStringEncodeSafe; | ||
|
||
fn into_html_children(self) -> Self::HtmlChildren { | ||
IterCharStringEncodeSafe::new(self) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ pub mod option; | |
pub mod str; | ||
pub mod tuple; | ||
|
||
mod char; | ||
mod scalar; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use frender::prelude::*; | ||
|
||
fn separated_html() -> impl Element { | ||
("<", "script>alert('DANGER')</", "script>") | ||
} | ||
|
||
#[test] | ||
fn separated_html_is_safe() { | ||
let out = futures_lite::future::block_on(separated_html().render_to_string()); | ||
|
||
assert_eq!( | ||
out, | ||
"<script>alert('DANGER')</script>" | ||
); | ||
} | ||
|
||
#[test] | ||
fn chars_are_safe() { | ||
let chars = [ | ||
'<', 's', 'c', 'r', 'i', 'p', 't', '>', 'a', 'l', 'e', 'r', 't', '(', '\'', 'D', 'A', 'N', | ||
'G', 'E', 'R', '\'', ')', '<', '/', 's', 'c', 'r', 'i', 'p', 't', '>', | ||
]; | ||
let out = futures_lite::future::block_on(chars.render_to_string()); | ||
|
||
assert_eq!( | ||
out, | ||
"<script>alert('DANGER')</script>" | ||
); | ||
} |