@@ -71,6 +71,16 @@ impl std::error::Error for EscapeError {}
7171/// | `&` | `&`
7272/// | `'` | `'`
7373/// | `"` | `"`
74+ ///
75+ /// This function performs following replacements:
76+ ///
77+ /// | Character | Replacement
78+ /// |-----------|------------
79+ /// | `<` | `<`
80+ /// | `>` | `>`
81+ /// | `&` | `&`
82+ /// | `'` | `'`
83+ /// | `"` | `"`
7484pub fn escape ( raw : & str ) -> Cow < str > {
7585 _escape ( raw, |ch| matches ! ( ch, b'<' | b'>' | b'&' | b'\'' | b'\"' ) )
7686}
@@ -88,10 +98,35 @@ pub fn escape(raw: &str) -> Cow<str> {
8898/// | `<` | `<`
8999/// | `>` | `>`
90100/// | `&` | `&`
101+ ///
102+ /// This function performs following replacements:
103+ ///
104+ /// | Character | Replacement
105+ /// |-----------|------------
106+ /// | `<` | `<`
107+ /// | `>` | `>`
108+ /// | `&` | `&`
91109pub fn partial_escape ( raw : & str ) -> Cow < str > {
92110 _escape ( raw, |ch| matches ! ( ch, b'<' | b'>' | b'&' ) )
93111}
94112
113+ /// XML standard [requires] that only `<` and `&` was escaped in text content or
114+ /// attribute value. All other characters not necessary to be escaped, although
115+ /// for compatibility with SGML they also should be escaped. Practically, escaping
116+ /// only those characters is enough.
117+ ///
118+ /// This function performs following replacements:
119+ ///
120+ /// | Character | Replacement
121+ /// |-----------|------------
122+ /// | `<` | `<`
123+ /// | `&` | `&`
124+ ///
125+ /// [requires]: https://www.w3.org/TR/xml11/#syntax
126+ pub fn minimal_escape ( raw : & str ) -> Cow < str > {
127+ _escape ( raw, |ch| matches ! ( ch, b'<' | b'&' ) )
128+ }
129+
95130/// Escapes an `&str` and replaces a subset of xml special characters (`<`, `>`,
96131/// `&`, `'`, `"`) with their corresponding xml escaped value.
97132pub ( crate ) fn _escape < F : Fn ( u8 ) -> bool > ( raw : & str , escape_chars : F ) -> Cow < str > {
@@ -1788,6 +1823,7 @@ fn test_escape() {
17881823 assert_eq ! ( unchanged, Cow :: Borrowed ( "test" ) ) ;
17891824 assert ! ( matches!( unchanged, Cow :: Borrowed ( _) ) ) ;
17901825
1826+ assert_eq ! ( escape( "<&\" '>" ) , "<&"'>" ) ;
17911827 assert_eq ! ( escape( "<test>" ) , "<test>" ) ;
17921828 assert_eq ! ( escape( "\" a\" bc" ) , ""a"bc" ) ;
17931829 assert_eq ! ( escape( "\" a\" b&c" ) , ""a"b&c" ) ;
@@ -1806,6 +1842,7 @@ fn test_partial_escape() {
18061842 assert_eq ! ( unchanged, Cow :: Borrowed ( "test" ) ) ;
18071843 assert ! ( matches!( unchanged, Cow :: Borrowed ( _) ) ) ;
18081844
1845+ assert_eq ! ( partial_escape( "<&\" '>" ) , "<&\" '>" ) ;
18091846 assert_eq ! ( partial_escape( "<test>" ) , "<test>" ) ;
18101847 assert_eq ! ( partial_escape( "\" a\" bc" ) , "\" a\" bc" ) ;
18111848 assert_eq ! ( partial_escape( "\" a\" b&c" ) , "\" a\" b&c" ) ;
@@ -1814,3 +1851,16 @@ fn test_partial_escape() {
18141851 "prefix_\" a\" b&<>c"
18151852 ) ;
18161853}
1854+
1855+ #[ test]
1856+ fn test_minimal_escape ( ) {
1857+ assert_eq ! ( minimal_escape( "test" ) , Cow :: Borrowed ( "test" ) ) ;
1858+ assert_eq ! ( minimal_escape( "<&\" '>" ) , "<&\" '>" ) ;
1859+ assert_eq ! ( minimal_escape( "<test>" ) , "<test>" ) ;
1860+ assert_eq ! ( minimal_escape( "\" a\" bc" ) , "\" a\" bc" ) ;
1861+ assert_eq ! ( minimal_escape( "\" a\" b&c" ) , "\" a\" b&c" ) ;
1862+ assert_eq ! (
1863+ minimal_escape( "prefix_\" a\" b&<>c" ) ,
1864+ "prefix_\" a\" b&<>c"
1865+ ) ;
1866+ }
0 commit comments