macro_rules! borrowed_safe_type {
(
$(#[$attr:meta])*
$Str:ident, $Owned:ident
) => {
$(#[$attr])*
#[must_use]
#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $Str(str);
impl $Str {
pub const EMPTY: &'static $Str = $Str::from_constant("");
pub const fn from_constant(val: &'static str) -> &'static $Str {
unsafe { $Str::from_str_unchecked(val) }
}
pub const unsafe fn from_str_unchecked(val: &str) -> &$Str {
unsafe { &*(val as *const str as *const $Str) }
}
pub const fn as_str(&self) -> &str {
&self.0
}
}
impl core::ops::Deref for $Str {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl AsRef<str> for $Str {
fn as_ref(&self) -> &str {
&self.0
}
}
impl core::fmt::Display for $Str {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.0)
}
}
impl<'a> Default for &'a $Str {
fn default() -> Self {
$Str::EMPTY
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl core::borrow::Borrow<$Str> for crate::types::$Owned {
fn borrow(&self) -> &$Str {
self
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl alloc::borrow::ToOwned for $Str {
type Owned = crate::types::$Owned;
fn to_owned(&self) -> crate::types::$Owned {
unsafe {
crate::types::$Owned::from_string_unchecked(alloc::string::String::from(
&self.0,
))
}
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl From<&$Str> for crate::types::$Owned {
fn from(value: &$Str) -> Self {
alloc::borrow::ToOwned::to_owned(value)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl PartialEq<$Str> for crate::types::$Owned {
fn eq(&self, other: &$Str) -> bool {
self.as_str() == other.as_str()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl PartialEq<crate::types::$Owned> for &$Str {
fn eq(&self, other: &crate::types::$Owned) -> bool {
self.as_str() == other.as_str()
}
}
};
}
borrowed_safe_type!(
SafeHtmlStr,
SafeHtml
);
borrowed_safe_type!(
SafeUrlStr,
SafeUrl
);
borrowed_safe_type!(
SafeScriptStr,
SafeScript
);
borrowed_safe_type!(
TrustedResourceUrlStr,
TrustedResourceUrl
);
borrowed_safe_type!(
SafeStyleStr,
SafeStyle
);
borrowed_safe_type!(
SafeStyleSheetStr,
SafeStyleSheet
);
impl SafeUrlStr {
pub const INNOCUOUS: &'static SafeUrlStr = SafeUrlStr::from_constant(crate::INNOCUOUS_URL);
}
use crate::writer::{CapacityError, SliceWriter};
impl SafeHtmlStr {
pub fn escape_into<'a>(
text: &str,
buf: &'a mut [u8],
) -> Result<&'a SafeHtmlStr, CapacityError> {
let mut w = SliceWriter::new(buf);
crate::escape_html_and_coerce_to(text, &mut w).map_err(|_| CapacityError)?;
Ok(unsafe { SafeHtmlStr::from_str_unchecked(w.finish()) })
}
pub fn escape_allow_character_references_into<'a>(
text: &str,
buf: &'a mut [u8],
) -> Result<&'a SafeHtmlStr, CapacityError> {
let mut w = SliceWriter::new(buf);
crate::escape_allow_character_references_to(text, &mut w).map_err(|_| CapacityError)?;
Ok(unsafe { SafeHtmlStr::from_str_unchecked(w.finish()) })
}
}
impl SafeScriptStr {
pub fn from_escaped_json_into<'a>(
json: &str,
buf: &'a mut [u8],
) -> Result<&'a SafeScriptStr, CapacityError> {
let mut w = SliceWriter::new(buf);
crate::escape_json_for_script_to(json, &mut w).map_err(|_| CapacityError)?;
Ok(unsafe { SafeScriptStr::from_str_unchecked(w.finish()) })
}
}
impl SafeUrlStr {
pub fn sanitize(url: &str) -> &SafeUrlStr {
if crate::is_safe_url_restrictive(url) {
unsafe { SafeUrlStr::from_str_unchecked(url) }
} else {
SafeUrlStr::INNOCUOUS
}
}
pub fn sanitize_block_only_javascript(url: &str) -> &SafeUrlStr {
if crate::is_safe_url_block_javascript(url) {
unsafe { SafeUrlStr::from_str_unchecked(url) }
} else {
SafeUrlStr::INNOCUOUS
}
}
pub fn sanitize_with_schemes<'a, S: AsRef<str>>(
url: &'a str,
extra_schemes: &[S],
) -> &'a SafeUrlStr {
if crate::is_safe_url_restrictive_with_schemes(url, extra_schemes) {
unsafe { SafeUrlStr::from_str_unchecked(url) }
} else {
SafeUrlStr::INNOCUOUS
}
}
pub fn sanitize_for_css_into<'a>(
url: &str,
buf: &'a mut [u8],
) -> Result<&'a SafeUrlStr, CapacityError> {
if !crate::is_safe_url_restrictive(url) {
return Ok(SafeUrlStr::INNOCUOUS);
}
let mut w = SliceWriter::new(buf);
crate::percent_escape_css_url_to(url, &mut w).map_err(|_| CapacityError)?;
Ok(unsafe { SafeUrlStr::from_str_unchecked(w.finish()) })
}
}