1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::fmt;

/// Formats the arguments while escaping `&<>"'` with their equivalent entities.

///

/// Accepts either an expression or a list of literals and formatting instructions.

///

/// # Examples

///

/// ```

/// use format_xml::escape;

/// assert_eq!(escape!({"Friday"} "'s the " {13} "th").to_string(), "Friday&apos;s the 13th");

/// assert_eq!(escape!("<script>alert(" {42;#x} ")</script>").to_string(), "&lt;script&gt;alert(0x2a)&lt;/script&gt;");

/// ```

#[macro_export]
macro_rules! escape {
	($e:expr) => {
		$crate::Escape($e)
	};
	({$e:expr}) => {
		$crate::Escape($e)
	};
	($($tt:tt)*) => {
		$crate::Escape($crate::FnFmt(|_f| { $crate::_template_!({_f} $($tt)*); Ok(()) }))
	};
}

/// Escapes `&<>"'` with their equivalent entities.

///

/// ```

/// use format_xml::Escape;

/// assert_eq!(Escape("&<>\"\'").to_string(), "&amp;&lt;&gt;&quot;&apos;");

/// ```

#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct Escape<T>(pub T);
impl<T: fmt::Display> fmt::Display for Escape<T> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		escape_w(&self.0.to_string(), f)
	}
}
fn escape_w<F: fmt::Write>(s: &str, result: &mut F) -> fmt::Result {
	for chr in s.chars() {
		match chr {
			'&' => result.write_str("&amp;"),
			'<' => result.write_str("&lt;"),
			'>' => result.write_str("&gt;"),
			'\'' => result.write_str("&apos;"),
			'\"' => result.write_str("&quot;"),
			chr => result.write_char(chr),
		}?;
	}
	Ok(())
}